1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package waffle.util;
25
26 import org.assertj.core.api.BDDSoftAssertions;
27 import org.junit.jupiter.api.Assertions;
28 import org.junit.jupiter.api.Test;
29
30 import waffle.mock.http.SimpleHttpRequest;
31
32
33
34
35 class AuthorizationHeaderTest {
36
37
38 private static final String DIGEST_HEADER = "Digest username=\"admin\", realm=\"milton\", nonce=\"YjNjZDgxNDYtOGIwMS00NDk0LTlkMTItYzExMGJkNTcxZjli\", uri=\"/case-user-data/431b971d9e1441d381adb277de4f39f8/test\", response=\"30d2d15e89e0b7596325a12852ae6ca5\", qop=auth, nc=00000025, cnonce=\"fb2f97a275d3d9cb\"";
39
40
41
42
43 @Test
44 void testIsNull() {
45 final SimpleHttpRequest request = new SimpleHttpRequest();
46 final AuthorizationHeader header = new AuthorizationHeader(request);
47 Assertions.assertTrue(header.isNull());
48 request.addHeader("Authorization", "");
49 Assertions.assertTrue(header.isNull());
50 request.addHeader("Authorization", "12344234");
51 Assertions.assertFalse(header.isNull());
52 }
53
54
55
56
57 @Test
58 void testGetSecurityPackage() {
59 final SimpleHttpRequest request = new SimpleHttpRequest();
60 final AuthorizationHeader header = new AuthorizationHeader(request);
61 request.addHeader("Authorization", "NTLM TlRMTVNTUAABAAAABzIAAAYABgArAAAACwALACAAAABXT1JLU1RBVElPTkRPTUFJTg==");
62 Assertions.assertEquals("NTLM", header.getSecurityPackage());
63 request.addHeader("Authorization",
64 "Negotiate TlRMTVNTUAABAAAABzIAAAYABgArAAAACwALACAAAABXT1JLU1RBVElPTkRPTUFJTg==");
65 Assertions.assertEquals("Negotiate", header.getSecurityPackage());
66 }
67
68
69
70
71 @Test
72 void testIsNtlmType1Message() {
73 final SimpleHttpRequest request = new SimpleHttpRequest();
74 final AuthorizationHeader header = new AuthorizationHeader(request);
75 Assertions.assertFalse(header.isNtlmType1Message());
76 request.addHeader("Authorization", "");
77 Assertions.assertFalse(header.isNtlmType1Message());
78 request.addHeader("Authorization", "NTLM TlRMTVNTUAABAAAABzIAAAYABgArAAAACwALACAAAABXT1JLU1RBVElPTkRPTUFJTg==");
79 Assertions.assertTrue(header.isNtlmType1Message());
80 }
81
82
83
84
85 @Test
86 void testIsNtlmType1PostAuthorizationHeader() {
87 final SimpleHttpRequest request = new SimpleHttpRequest();
88 request.setContentLength(0);
89 request.addHeader("Authorization", "NTLM TlRMTVNTUAABAAAABzIAAAYABgArAAAACwALACAAAABXT1JLU1RBVElPTkRPTUFJTg==");
90
91 request.setMethod("GET");
92 final AuthorizationHeader header = new AuthorizationHeader(request);
93 Assertions.assertFalse(header.isNtlmType1PostAuthorizationHeader());
94
95 request.setMethod("POST");
96 Assertions.assertTrue(header.isNtlmType1PostAuthorizationHeader());
97
98 request.setMethod("PUT");
99 Assertions.assertTrue(header.isNtlmType1PostAuthorizationHeader());
100 }
101
102
103
104
105 @Test
106 void testIsSPNegTokenInitMessage() {
107 final SimpleHttpRequest request = new SimpleHttpRequest();
108 final AuthorizationHeader header = new AuthorizationHeader(request);
109 Assertions.assertFalse(header.isSPNegTokenInitMessage());
110 request.addHeader("Authorization", "");
111 Assertions.assertFalse(header.isSPNegTokenInitMessage());
112 request.addHeader("Authorization",
113 "Negotiate YHYGBisGAQUFAqBsMGqgMDAuBgorBgEEAYI3AgIKBgkqhkiC9xIBAgIGCSqGSIb3EgECAgYKKwYBBAGCNwICHqI2BDROVExNU1NQAAEAAACXsgjiAwADADEAAAAJAAkAKAAAAAYBsR0AAAAPR0xZQ0VSSU5FU0FE");
114 Assertions.assertTrue(header.isSPNegTokenInitMessage());
115 }
116
117
118
119
120 @Test
121 void testIsSPNegoPostAuthorizationHeader() {
122 final SimpleHttpRequest request = new SimpleHttpRequest();
123 request.setContentLength(0);
124 request.addHeader("Authorization",
125 "Negotiate YHYGBisGAQUFAqBsMGqgMDAuBgorBgEEAYI3AgIKBgkqhkiC9xIBAgIGCSqGSIb3EgECAgYKKwYBBAGCNwICHqI2BDROVExNU1NQAAEAAACXsgjiAwADADEAAAAJAAkAKAAAAAYBsR0AAAAPR0xZQ0VSSU5FU0FE");
126
127 request.setMethod("GET");
128 final AuthorizationHeader header = new AuthorizationHeader(request);
129 Assertions.assertFalse(header.isNtlmType1PostAuthorizationHeader());
130
131 request.setMethod("POST");
132 Assertions.assertTrue(header.isNtlmType1PostAuthorizationHeader());
133
134 request.setMethod("PUT");
135 Assertions.assertTrue(header.isNtlmType1PostAuthorizationHeader());
136 }
137
138
139
140
141
142
143 @Test
144 void testIsDigestAuthorizationHeaderFailure() {
145 final SimpleHttpRequest request = new SimpleHttpRequest();
146 final AuthorizationHeader header = new AuthorizationHeader(request);
147 request.addHeader("Authorization", AuthorizationHeaderTest.DIGEST_HEADER);
148
149 final BDDSoftAssertions softly = new BDDSoftAssertions();
150 softly.thenThrownBy(() -> header.getTokenBytes()).isInstanceOf(RuntimeException.class)
151 .hasMessageContaining("Invalid authorization header");
152 }
153 }