2020
2121import org .junit .Test ;
2222
23+ import java .security .SecureRandom ;
24+
2325
2426/**
2527 * @author Dave Syer
@@ -44,17 +46,49 @@ public void unicode() {
4446 }
4547
4648 @ Test
47- public void matchesLengthChecked () {
49+ public void notMatches () {
4850 BCryptPasswordEncoder encoder = new BCryptPasswordEncoder ();
4951 String result = encoder .encode ("password" );
50- assertFalse (encoder .matches ("password " , result . substring ( 0 , result . length ()- 2 ) ));
52+ assertFalse (encoder .matches ("bogus " , result ));
5153 }
5254
5355 @ Test
54- public void notMatches () {
56+ public void customStrength () {
57+ BCryptPasswordEncoder encoder = new BCryptPasswordEncoder (8 );
58+ String result = encoder .encode ("password" );
59+ assertTrue (encoder .matches ("password" , result ));
60+ }
61+
62+ @ Test
63+ public void customRandom () {
64+ BCryptPasswordEncoder encoder = new BCryptPasswordEncoder (8 , new SecureRandom ());
65+ String result = encoder .encode ("password" );
66+ assertTrue (encoder .matches ("password" , result ));
67+ }
68+
69+ @ Test (expected = IllegalArgumentException .class )
70+ public void barfsOnNullEncodedValue () {
71+ BCryptPasswordEncoder encoder = new BCryptPasswordEncoder ();
72+ assertFalse (encoder .matches ("password" , null ));
73+ }
74+
75+ @ Test (expected = IllegalArgumentException .class )
76+ public void barfsOnEmptyEncodedValue () {
77+ BCryptPasswordEncoder encoder = new BCryptPasswordEncoder ();
78+ assertFalse (encoder .matches ("password" , "" ));
79+ }
80+
81+ @ Test (expected = IllegalArgumentException .class )
82+ public void barfsOnShortEncodedValue () {
5583 BCryptPasswordEncoder encoder = new BCryptPasswordEncoder ();
5684 String result = encoder .encode ("password" );
57- assertFalse (encoder .matches ("bogus" , result ));
85+ assertFalse (encoder .matches ("password" , result .substring (0 , 4 )));
86+ }
87+
88+ @ Test (expected = IllegalArgumentException .class )
89+ public void barfsOnBogusEncodedValue () {
90+ BCryptPasswordEncoder encoder = new BCryptPasswordEncoder ();
91+ assertFalse (encoder .matches ("password" , "012345678901234567890123456789" ));
5892 }
5993
6094}
0 commit comments