KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > users > filesystem > StringEncrypterTest


1 package com.openedit.users.filesystem;
2
3 import junit.framework.TestCase;
4
5 import com.openedit.users.StringEncrypter;
6
7 public class StringEncrypterTest extends TestCase
8 {
9
10     public void testEncryptionKeyIsDefined()
11     {
12         assertEquals( StringEncrypter.DEFAULT_ENCRYPTION_KEY,
13                         "This is a fairly long phrase used to encrypt" );
14     }
15
16     public void testThrowsErrorOnInvalidKeySpec() throws Exception JavaDoc
17     {
18         String JavaDoc encryptionScheme = "asdf";
19         String JavaDoc encryptionKey = "123456789012345678901234567890";
20
21         try
22         {
23             StringEncrypter encrypter = new StringEncrypter( encryptionScheme,
24                     encryptionKey );
25         }
26         catch (IllegalArgumentException JavaDoc e)
27         {
28             assertEquals( "Encryption scheme not supported: asdf", e.getMessage() );
29         }
30     }
31
32     public void testEncryptsUsingDesEde() throws Exception JavaDoc
33     {
34         String JavaDoc stringToEncrypt = "test";
35         String JavaDoc encryptionKey = "123456789012345678901234567890";
36         String JavaDoc encryptionScheme = StringEncrypter.DESEDE_ENCRYPTION_SCHEME;
37
38         StringEncrypter encrypter = new StringEncrypter( encryptionScheme, encryptionKey );
39         String JavaDoc encryptedString = encrypter.encrypt( stringToEncrypt );
40
41         assertEquals( "Ni2Bih3nCUU=", encryptedString );
42     }
43
44     public void testEncryptsUsingDes() throws Exception JavaDoc
45     {
46         String JavaDoc stringToEncrypt = "test";
47         String JavaDoc encryptionKey = "123456789012345678901234567890";
48         String JavaDoc encryptionScheme = StringEncrypter.DES_ENCRYPTION_SCHEME;
49
50         StringEncrypter encrypter = new StringEncrypter( encryptionScheme, encryptionKey );
51         String JavaDoc encryptedString = encrypter.encrypt( stringToEncrypt );
52
53         assertEquals( "oEtoaxGK9ns=", encryptedString );
54     }
55
56     public void testEncryptionKeyCanContainLetters() throws Exception JavaDoc
57     {
58         String JavaDoc string = "test";
59         String JavaDoc encryptionKey = "ASDF asdf 1234 8983 jklasdf J2Jaf8";
60         String JavaDoc encryptionScheme = StringEncrypter.DESEDE_ENCRYPTION_SCHEME;
61
62         StringEncrypter encrypter = new StringEncrypter( encryptionScheme, encryptionKey );
63         String JavaDoc encryptedString = encrypter.encrypt( string );
64
65         assertEquals( "Q+UyPrxdge0=", encryptedString );
66     }
67
68     public void testDecryptsUsingDesEde() throws Exception JavaDoc
69     {
70         String JavaDoc string = "Ni2Bih3nCUU=";
71         String JavaDoc encryptionKey = "123456789012345678901234567890";
72         String JavaDoc encryptionScheme = StringEncrypter.DESEDE_ENCRYPTION_SCHEME;
73
74         StringEncrypter encrypter = new StringEncrypter( encryptionScheme, encryptionKey );
75         String JavaDoc decryptedString = encrypter.decrypt( string );
76
77         assertEquals( "test", decryptedString );
78     }
79
80     public void testDecryptsUsingDes() throws Exception JavaDoc
81     {
82         String JavaDoc string = "oEtoaxGK9ns=";
83         String JavaDoc encryptionKey = "123456789012345678901234567890";
84         String JavaDoc encryptionScheme = StringEncrypter.DES_ENCRYPTION_SCHEME;
85
86         StringEncrypter encrypter = new StringEncrypter( encryptionScheme, encryptionKey );
87         String JavaDoc decryptedString = encrypter.decrypt( string );
88
89         assertEquals( "test", decryptedString );
90     }
91
92     public void testCantInstantiateWithNullEncryptionKey() throws Exception JavaDoc
93     {
94         try
95         {
96             String JavaDoc encryptionScheme = StringEncrypter.DESEDE_ENCRYPTION_SCHEME;
97             String JavaDoc encryptionKey = null;
98
99             StringEncrypter encrypter = new StringEncrypter( encryptionScheme,
100                     encryptionKey );
101         }
102         catch (IllegalArgumentException JavaDoc e)
103         {
104             assertEquals( "encryption key was null", e.getMessage() );
105         }
106
107     }
108
109     public void testCantInstantiateWithEmptyEncryptionKey() throws Exception JavaDoc
110     {
111         try
112         {
113             String JavaDoc encryptionScheme = StringEncrypter.DESEDE_ENCRYPTION_SCHEME;
114             String JavaDoc encryptionKey = "";
115
116             StringEncrypter encrypter = new StringEncrypter( encryptionScheme,
117                     encryptionKey );
118         }
119         catch (IllegalArgumentException JavaDoc e)
120         {
121             assertEquals( "encryption key was less than 24 characters", e.getMessage() );
122         }
123
124     }
125
126     public void testCantDecryptWithNullString() throws Exception JavaDoc
127     {
128         try
129         {
130             String JavaDoc encryptionScheme = StringEncrypter.DESEDE_ENCRYPTION_SCHEME;
131             String JavaDoc encryptionKey = "123456789012345678901234";
132
133             StringEncrypter encrypter = new StringEncrypter( encryptionScheme,
134                     encryptionKey );
135             encrypter.decrypt( null );
136         }
137         catch (IllegalArgumentException JavaDoc e)
138         {
139             assertEquals( "encrypted string was null or empty", e.getMessage() );
140         }
141
142     }
143
144     public void testCantDecryptWithEmptyString() throws Exception JavaDoc
145     {
146         try
147         {
148             String JavaDoc encryptionScheme = StringEncrypter.DESEDE_ENCRYPTION_SCHEME;
149             String JavaDoc encryptionKey = "123456789012345678901234";
150
151             StringEncrypter encrypter = new StringEncrypter( encryptionScheme,
152                     encryptionKey );
153             encrypter.decrypt( "" );
154         }
155         catch (IllegalArgumentException JavaDoc e)
156         {
157             assertEquals( "encrypted string was null or empty", e.getMessage() );
158         }
159
160     }
161
162     public void testCantEncryptWithNullString() throws Exception JavaDoc
163     {
164         try
165         {
166             String JavaDoc encryptionScheme = StringEncrypter.DESEDE_ENCRYPTION_SCHEME;
167             String JavaDoc encryptionKey = "123456789012345678901234";
168
169             StringEncrypter encrypter = new StringEncrypter( encryptionScheme,
170                     encryptionKey );
171             encrypter.encrypt( null );
172         }
173         catch (IllegalArgumentException JavaDoc e)
174         {
175             assertEquals( "unencrypted string was null or empty", e.getMessage() );
176         }
177
178     }
179
180     public void testCantEncryptWithEmptyString() throws Exception JavaDoc
181     {
182         try
183         {
184             String JavaDoc encryptionScheme = StringEncrypter.DESEDE_ENCRYPTION_SCHEME;
185             String JavaDoc encryptionKey = "123456789012345678901234";
186
187             StringEncrypter encrypter = new StringEncrypter( encryptionScheme,
188                     encryptionKey );
189             encrypter.encrypt( "" );
190         }
191         catch (IllegalArgumentException JavaDoc e)
192         {
193             assertEquals( "unencrypted string was null or empty", e.getMessage() );
194         }
195
196     }
197 }
Popular Tags