15
15
import com .ning .http .client .Realm .AuthScheme ;
16
16
import com .ning .http .client .Realm .RealmBuilder ;
17
17
import org .testng .Assert ;
18
+ import java .math .BigInteger ;
19
+ import java .security .MessageDigest ;
20
+ import java .security .NoSuchAlgorithmException ;
18
21
import org .testng .annotations .Test ;
19
22
20
23
public class RealmTest {
@@ -36,4 +39,82 @@ public void testClone() {
36
39
Assert .assertEquals ( clone .getAlgorithm (), orig .getAlgorithm () );
37
40
Assert .assertEquals ( clone .getAuthScheme (), orig .getAuthScheme () );
38
41
}
42
+ @ Test (groups = "fast" )
43
+ public void testOldDigestEmptyString () {
44
+ String qop ="" ;
45
+ testOldDigest (qop );
46
+ }
47
+ @ Test (groups = "fast" )
48
+ public void testOldDigestNull () {
49
+ String qop =null ;
50
+ testOldDigest (qop );
51
+ }
52
+
53
+ private void testOldDigest (String qop ){
54
+ String user ="user" ;
55
+ String pass ="pass" ;
56
+ String realm ="realm" ;
57
+ String nonce ="nonce" ;
58
+ String method ="GET" ;
59
+ String uri ="/foo" ;
60
+ RealmBuilder builder = new RealmBuilder ();
61
+ builder .setPrincipal ( user ).setPassword ( pass );
62
+ builder .setNonce ( nonce );
63
+ builder .setUri ( uri );
64
+ builder .setMethodName (method );
65
+ builder .setRealmName ( realm );
66
+ builder .setQop (qop );
67
+ builder .setScheme ( AuthScheme .DIGEST );
68
+ Realm orig = builder .build ();
69
+
70
+ String ha1 =getMd5 (user +":" + realm +":" +pass );
71
+ String ha2 =getMd5 (method +":" + uri );
72
+ String expectedResponse =getMd5 (ha1 +":" + nonce +":" + ha2 );
73
+
74
+ Assert .assertEquals (expectedResponse ,orig .getResponse ());
75
+ }
76
+
77
+ @ Test (groups = "fast" )
78
+ public void testStrongDigest () {
79
+ String user ="user" ;
80
+ String pass ="pass" ;
81
+ String realm ="realm" ;
82
+ String nonce ="nonce" ;
83
+ String method ="GET" ;
84
+ String uri ="/foo" ;
85
+ String qop ="auth" ;
86
+ RealmBuilder builder = new RealmBuilder ();
87
+ builder .setPrincipal ( user ).setPassword ( pass );
88
+ builder .setNonce ( nonce );
89
+ builder .setUri ( uri );
90
+ builder .setMethodName (method );
91
+ builder .setRealmName ( realm );
92
+ builder .setQop (qop );
93
+ builder .setScheme ( AuthScheme .DIGEST );
94
+ Realm orig = builder .build ();
95
+
96
+ String nc = orig .getNc ();
97
+ String cnonce = orig .getCnonce ();
98
+ String ha1 =getMd5 (user +":" + realm +":" +pass );
99
+ String ha2 =getMd5 (method +":" + uri );
100
+ String expectedResponse =getMd5 (ha1 +":" + nonce +":" + nc + ":" + cnonce +":" + qop + ":" + ha2 );
101
+
102
+ Assert .assertEquals (expectedResponse ,orig .getResponse ());
103
+ }
104
+
105
+ private String getMd5 (String what ){
106
+ try {
107
+ MessageDigest md = MessageDigest .getInstance ("MD5" );
108
+ md .update (what .getBytes ("ISO-8859-1" ));
109
+ byte [] hash = md .digest ();
110
+ BigInteger bi = new BigInteger (1 , hash );
111
+ String result = bi .toString (16 );
112
+ if (result .length () % 2 != 0 ) {
113
+ return "0" + result ;
114
+ }
115
+ return result ;
116
+ } catch (Exception e ) {
117
+ throw new RuntimeException (e );
118
+ }
119
+ }
39
120
}
0 commit comments