1717
1818package src .com .cloud .server .auth .test ;
1919
20- import static org .junit .Assert .*;
20+ import static org .junit .Assert .assertEquals ;
21+ import static org .junit .Assert .fail ;
22+ import static org .mockito .Mockito .when ;
2123
2224import java .io .UnsupportedEncodingException ;
2325import java .security .NoSuchAlgorithmException ;
2426import java .util .Collections ;
27+ import java .util .HashMap ;
28+ import java .util .Map ;
2529
2630import javax .naming .ConfigurationException ;
2731
2832import org .bouncycastle .util .encoders .Base64 ;
33+ import org .junit .Assert ;
2934import org .junit .Before ;
3035import org .junit .Test ;
36+ import org .junit .runner .RunWith ;
37+ import org .mockito .InjectMocks ;
38+ import org .mockito .Mock ;
39+ import org .mockito .runners .MockitoJUnitRunner ;
3140
3241import com .cloud .server .auth .SHA256SaltedUserAuthenticator ;
42+ import com .cloud .user .UserAccount ;
43+ import com .cloud .user .dao .UserAccountDao ;
3344
45+ @ RunWith (MockitoJUnitRunner .class )
3446public class AuthenticatorTest {
3547
48+ @ Mock
49+ UserAccount adminAccount ;
50+
51+ @ Mock
52+ UserAccount adminAccount20Byte ;
53+
54+ @ Mock
55+ UserAccountDao _userAccountDao ;
56+
57+ @ InjectMocks
58+ SHA256SaltedUserAuthenticator authenticator ;
59+
3660 @ Before
3761 public void setUp () throws Exception {
62+ try {
63+ authenticator .configure ("SHA256" , Collections .<String , Object > emptyMap ());
64+ } catch (ConfigurationException e ) {
65+ fail (e .toString ());
66+ }
67+
68+ when (_userAccountDao .getUserAccount ("admin" , 0L )).thenReturn (adminAccount );
69+ when (_userAccountDao .getUserAccount ("admin20Byte" , 0L )).thenReturn (adminAccount20Byte );
70+ when (_userAccountDao .getUserAccount ("fake" , 0L )).thenReturn (null );
71+ //32 byte salt, and password="password"
72+ when (adminAccount .getPassword ()).thenReturn ("WS3UHhBPKHZeV+G3jnn7G2N3luXgLSfL+2ORDieXa1U=:VhuFOrOU2IpsjKYH8cH1VDaDBh/VivjMcuADjeEbIig=" );
73+ //20 byte salt, and password="password"
74+ when (adminAccount20Byte .getPassword ()).thenReturn ("QL2NsxVEmRuDaNRkvIyADny7C5w=:JoegiytiWnoBAxmSD/PwBZZYqkr746x2KzPrZNw4NgI=" );
75+
3876 }
3977
4078 @ Test
4179 public void testEncode () throws UnsupportedEncodingException , NoSuchAlgorithmException {
42- SHA256SaltedUserAuthenticator authenticator =
43- new SHA256SaltedUserAuthenticator ();
44-
45- try {
46- authenticator .configure ("SHA256" , Collections .<String ,Object >emptyMap ());
47- } catch (ConfigurationException e ) {
48- fail (e .toString ());
49- }
5080
5181 String encodedPassword = authenticator .encode ("password" );
5282
@@ -60,4 +90,35 @@ public void testEncode() throws UnsupportedEncodingException, NoSuchAlgorithmExc
6090
6191 }
6292
93+ @ Test
94+ public void testAuthentication () throws UnsupportedEncodingException , NoSuchAlgorithmException {
95+ Map <String , Object []> dummyMap = new HashMap <String , Object []>();
96+ assertEquals ("32 byte salt authenticated" , true , authenticator .authenticate ("admin" , "password" , 0L , dummyMap ));
97+ assertEquals ("20 byte salt authenticated" , true , authenticator .authenticate ("admin20Byte" , "password" , 0L , dummyMap ));
98+ assertEquals ("fake user not authenticated" , false , authenticator .authenticate ("fake" , "fake" , 0L , dummyMap ));
99+ assertEquals ("bad password not authenticated" , false , authenticator .authenticate ("admin" , "fake" , 0L , dummyMap ));
100+ assertEquals ("20 byte user bad password not authenticated" , false , authenticator .authenticate ("admin20Byte" , "fake" , 0L , dummyMap ));
101+ }
102+
103+ @ Test
104+ public void testTiming () throws UnsupportedEncodingException , NoSuchAlgorithmException {
105+ Map <String , Object []> dummyMap = new HashMap <String , Object []>();
106+ Double threshold = (double )500000 ; //half a millisecond
107+
108+ Long t1 = System .nanoTime ();
109+ authenticator .authenticate ("admin" , "password" , 0L , dummyMap );
110+ Long t2 = System .nanoTime ();
111+ authenticator .authenticate ("admin20Byte" , "password" , 0L , dummyMap );
112+ Long t3 = System .nanoTime ();
113+ authenticator .authenticate ("fake" , "fake" , 0L , dummyMap );
114+ Long t4 = System .nanoTime ();
115+ authenticator .authenticate ("admin" , "fake" , 0L , dummyMap );
116+ Long t5 = System .nanoTime ();
117+ Long diff1 = t2 - t1 ;
118+ Long diff2 = t3 - t2 ;
119+ Long diff3 = t4 - t3 ;
120+ Long diff4 = t5 - t4 ;
121+ Assert .assertTrue ("All computation times within " + threshold / 1000000 + " milisecond" ,
122+ (diff1 <= threshold ) && (diff2 <= threshold ) && (diff3 <= threshold ) && (diff4 <= threshold ));
123+ }
63124}
0 commit comments