1   package test.base.jdbs;
2   
3   import java.io.File;
4   import java.security.InvalidKeyException;
5   import java.security.NoSuchAlgorithmException;
6   import java.security.NoSuchProviderException;
7   import java.security.SignatureException;
8   import java.util.GregorianCalendar;
9   
10  import junit.framework.Test;
11  import junit.framework.TestSuite;
12  
13  import org.apache.log4j.Logger;
14  import org.apache.log4j.xml.DOMConfigurator;
15  
16  import base.jdbs.Repository;
17  import base.jdbs.cryptography.asymmetric.AsymmetricKeyRing;
18  import base.jdbs.cryptography.asymmetric.KeyPair;
19  import base.jdbs.cryptography.symmetric.SymmetricKey;
20  import base.user.Document;
21  import base.user.User;
22  import base.user.UserCredential;
23  import base.user.UserFactory;
24  import base.user.UserGender;
25  import base.util.FileUtil;
26  
27  public class AllTests {
28  
29  	private static final transient Logger logger = Logger.getLogger(AllTests.class.getName());
30  	
31  	public static final String TEST_DIRECTORY_PATH= "target"+File.separatorChar+"test";
32  	public static final String PASSWORD = "password";//It is assumed in that backup test that the keyStore and Symmectic Encryption passwords are the same.
33  	public static final String KEY_STORE_FILE_PATH = TEST_DIRECTORY_PATH+File.separatorChar+"keyStore.ks";
34  	public static final String REPOSITORY_PATH = TEST_DIRECTORY_PATH+File.separatorChar+"Repository";
35  	public static final int REPOSITORY_DECLARED_AVAILABLE_SPACE = 10000;//In Mb
36  	public static final boolean RUN_SILENTLY = true;
37  	public static final String BACKUP_DISASSEMBLY_OUTPUT_DIRECTORY_PATH = TEST_DIRECTORY_PATH+File.separatorChar+"Disassembly";
38  	
39  	private static AsymmetricKeyRing asymmetricKeyRing;
40  	private static SymmetricKey symmetricKey;
41  	private static Repository repository;
42  	private static User user;
43  	
44  	public AllTests(){
45  		initialize();
46  	}
47  	
48  	protected void initialize(){
49  		DOMConfigurator.configure("log4jConfiguration.xml");
50  		
51  		File testDir = new File(TEST_DIRECTORY_PATH);
52  		if(testDir.exists())FileUtil.deleteDirectory(testDir);
53  		
54  		testDir.mkdirs();
55  		File repositoryDir =  new File(REPOSITORY_PATH);
56  		repositoryDir.mkdirs();
57  		File disassemblyDir = new File(BACKUP_DISASSEMBLY_OUTPUT_DIRECTORY_PATH);
58  		disassemblyDir.mkdirs();
59  	}
60  	
61  	private static User testUser(){
62  		
63  		String name = "Guido Angelo";
64  		String surname = "Ingenito";
65  		String gender = UserGender.USER_GENDER[UserGender.MALE];
66  		GregorianCalendar birthday = new GregorianCalendar(1981,12,24);
67  		String nickname = "skunk";
68  		String password = "password";
69  		String credential = UserCredential.USER_CREDENTIAL[UserCredential.ADMINISTRATOR];
70  		Document document = null;
71  		String imagePath = "";
72  		
73  		return UserFactory.newUser(name, surname, gender, birthday.getTime(), nickname, password, credential, document, imagePath);
74  	}
75  	
76  	public static Test suite() {
77  		TestSuite suite = new TestSuite("Test for test.base.jdbs");
78  		//$JUnit-BEGIN$
79  		suite.addTestSuite(RepositoryTest.class);
80  		suite.addTestSuite(BackupArtifactDisassemblerTest.class);
81  		suite.addTestSuite(BackupArtifactAssemblerTest.class);
82  		//$JUnit-END$
83  		return suite;
84  	}
85  
86  	/***
87  	 * @return Returns the asymmetricKeyRing.
88  	 */
89  	public static AsymmetricKeyRing getAsymmetricKeyRing() {
90  		if(asymmetricKeyRing == null){
91  			asymmetricKeyRing = new AsymmetricKeyRing(testUser());
92  			
93  			File keyStoreFile = new File(KEY_STORE_FILE_PATH);
94  			keyStoreFile.getParentFile().mkdirs();
95  			
96  			if(!keyStoreFile.exists()){
97  				logger.debug("Key Store File doesn't exists, creating a new one...");
98  				KeyPair kp = null;
99  				try {
100 					kp = base.jdbs.cryptography.asymmetric.RSAKeyPairFactory.newKeyPair(asymmetricKeyRing,"Test Key Store");
101 				} catch (InvalidKeyException e) {
102 					// TODO Auto-generated catch block
103 					e.printStackTrace();
104 				} catch (NoSuchAlgorithmException e) {
105 					// TODO Auto-generated catch block
106 					e.printStackTrace();
107 				} catch (NoSuchProviderException e) {
108 					// TODO Auto-generated catch block
109 					e.printStackTrace();
110 				} catch (SecurityException e) {
111 					// TODO Auto-generated catch block
112 					e.printStackTrace();
113 				} catch (SignatureException e) {
114 					// TODO Auto-generated catch block
115 					e.printStackTrace();
116 				}
117 				asymmetricKeyRing.addKeyPair(kp);
118 				asymmetricKeyRing.store(keyStoreFile,AllTests.PASSWORD);
119 			}
120 			else{
121 				asymmetricKeyRing.retrieve(keyStoreFile,AllTests.PASSWORD);
122 			}
123 		}
124 		return asymmetricKeyRing;
125 	}
126 
127 	/***
128 	 * @return Returns the repository.
129 	 */
130 	public static Repository getRepository() {
131 		if(repository == null){
132 			repository = new Repository(new File(REPOSITORY_PATH),REPOSITORY_DECLARED_AVAILABLE_SPACE);
133 		}
134 		return repository;
135 	}
136 
137 	/***
138 	 * @return Returns the symmetricKey.
139 	 */
140 	public static SymmetricKey getSymmetricKey() {
141 		if(symmetricKey == null){
142 			symmetricKey = new SymmetricKey(PASSWORD);
143 		}
144 		return symmetricKey;
145 	}
146 
147 	/***
148 	 * @return Returns the user.
149 	 */
150 	public static User getUser() {
151 		if(user == null){
152 			user = testUser();
153 		}
154 		return user;
155 	}
156 
157 }