16
16
*/
17
17
package org .apache .rocketmq .acl .common ;
18
18
19
- import com .alibaba .fastjson .JSONObject ;
19
+ import com .alibaba .fastjson2 .JSONObject ;
20
20
import org .apache .commons .lang3 .StringUtils ;
21
21
import org .apache .rocketmq .acl .plain .PlainAccessData ;
22
22
import org .apache .rocketmq .common .PlainAccessConfig ;
32
32
import java .util .Collections ;
33
33
import java .util .List ;
34
34
import java .util .Map ;
35
+ import java .util .Objects ;
35
36
import java .util .UUID ;
36
37
38
+ import static org .junit .Assert .assertEquals ;
39
+ import static org .junit .Assert .assertFalse ;
40
+ import static org .junit .Assert .assertNotNull ;
41
+ import static org .junit .Assert .assertNull ;
42
+ import static org .junit .Assert .assertTrue ;
43
+
37
44
public class AclUtilsTest {
38
45
39
46
@ Test
@@ -47,7 +54,7 @@ public void testGetAddresses() {
47
54
addressList .add ("1.1.1.2" );
48
55
addressList .add ("1.1.1.3" );
49
56
addressList .add ("1.1.1.4" );
50
- Assert . assertEquals (newAddressList , addressList );
57
+ assertEquals (newAddressList , addressList );
51
58
52
59
// IPv6 test
53
60
String ipv6Address = "1:ac41:9987::bb22:666:{1,2,3,4}" ;
@@ -60,7 +67,7 @@ public void testGetAddresses() {
60
67
ipv6AddressList .add ("1:ac41:9987::bb22:666:2" );
61
68
ipv6AddressList .add ("1:ac41:9987::bb22:666:3" );
62
69
ipv6AddressList .add ("1:ac41:9987::bb22:666:4" );
63
- Assert . assertEquals (newIPv6AddressList , ipv6AddressList );
70
+ assertEquals (newIPv6AddressList , ipv6AddressList );
64
71
}
65
72
66
73
@ Test
@@ -70,9 +77,9 @@ public void testIsScope_StringArray() {
70
77
for (int i = 0 ; i < 6 ; i ++) {
71
78
boolean isScope = AclUtils .isScope (address , 4 );
72
79
if (i == 3 ) {
73
- Assert . assertTrue (isScope );
80
+ assertTrue (isScope );
74
81
} else {
75
- Assert . assertFalse (isScope );
82
+ assertFalse (isScope );
76
83
}
77
84
address = address + ".12" ;
78
85
}
@@ -82,46 +89,46 @@ public void testIsScope_StringArray() {
82
89
public void testIsScope_Array () {
83
90
String [] address = StringUtils .split ("12.12.12.12" , "." );
84
91
boolean isScope = AclUtils .isScope (address , 4 );
85
- Assert . assertTrue (isScope );
92
+ assertTrue (isScope );
86
93
isScope = AclUtils .isScope (address , 3 );
87
- Assert . assertTrue (isScope );
94
+ assertTrue (isScope );
88
95
89
96
address = StringUtils .split ("12.12.1222.1222" , "." );
90
97
isScope = AclUtils .isScope (address , 4 );
91
- Assert . assertFalse (isScope );
98
+ assertFalse (isScope );
92
99
isScope = AclUtils .isScope (address , 3 );
93
- Assert . assertFalse (isScope );
100
+ assertFalse (isScope );
94
101
95
102
// IPv6 test
96
103
address = StringUtils .split ("1050:0000:0000:0000:0005:0600:300c:326b" , ":" );
97
104
isScope = AclUtils .isIPv6Scope (address , 8 );
98
- Assert . assertTrue (isScope );
105
+ assertTrue (isScope );
99
106
isScope = AclUtils .isIPv6Scope (address , 4 );
100
- Assert . assertTrue (isScope );
107
+ assertTrue (isScope );
101
108
102
109
address = StringUtils .split ("1050:9876:0000:0000:0005:akkg:300c:326b" , ":" );
103
110
isScope = AclUtils .isIPv6Scope (address , 8 );
104
- Assert . assertFalse (isScope );
111
+ assertFalse (isScope );
105
112
isScope = AclUtils .isIPv6Scope (address , 4 );
106
- Assert . assertTrue (isScope );
113
+ assertTrue (isScope );
107
114
108
115
address = StringUtils .split (AclUtils .expandIP ("1050::0005:akkg:300c:326b" , 8 ), ":" );
109
116
isScope = AclUtils .isIPv6Scope (address , 8 );
110
- Assert . assertFalse (isScope );
117
+ assertFalse (isScope );
111
118
isScope = AclUtils .isIPv6Scope (address , 4 );
112
- Assert . assertTrue (isScope );
119
+ assertTrue (isScope );
113
120
}
114
121
115
122
@ Test
116
123
public void testIsScope_String () {
117
124
for (int i = 0 ; i < 256 ; i ++) {
118
125
boolean isScope = AclUtils .isScope (i + "" );
119
- Assert . assertTrue (isScope );
126
+ assertTrue (isScope );
120
127
}
121
128
boolean isScope = AclUtils .isScope ("-1" );
122
- Assert . assertFalse (isScope );
129
+ assertFalse (isScope );
123
130
isScope = AclUtils .isScope ("256" );
124
- Assert . assertFalse (isScope );
131
+ assertFalse (isScope );
125
132
}
126
133
127
134
@ Test
@@ -131,9 +138,9 @@ public void testIsScope_Integral() {
131
138
Assert .assertTrue (isScope );
132
139
}
133
140
boolean isScope = AclUtils .isScope (-1 );
134
- Assert . assertFalse (isScope );
141
+ assertFalse (isScope );
135
142
isScope = AclUtils .isScope (256 );
136
- Assert . assertFalse (isScope );
143
+ assertFalse (isScope );
137
144
138
145
// IPv6 test
139
146
int min = Integer .parseInt ("0" , 16 );
@@ -143,71 +150,71 @@ public void testIsScope_Integral() {
143
150
Assert .assertTrue (isScope );
144
151
}
145
152
isScope = AclUtils .isIPv6Scope (-1 );
146
- Assert . assertFalse (isScope );
153
+ assertFalse (isScope );
147
154
isScope = AclUtils .isIPv6Scope (max + 1 );
148
- Assert . assertFalse (isScope );
155
+ assertFalse (isScope );
149
156
}
150
157
151
158
@ Test
152
159
public void testIsAsterisk () {
153
160
boolean isAsterisk = AclUtils .isAsterisk ("*" );
154
- Assert . assertTrue (isAsterisk );
161
+ assertTrue (isAsterisk );
155
162
156
163
isAsterisk = AclUtils .isAsterisk ("," );
157
- Assert . assertFalse (isAsterisk );
164
+ assertFalse (isAsterisk );
158
165
}
159
166
160
167
@ Test
161
168
public void testIsComma () {
162
169
boolean isColon = AclUtils .isComma ("," );
163
- Assert . assertTrue (isColon );
170
+ assertTrue (isColon );
164
171
165
172
isColon = AclUtils .isComma ("-" );
166
- Assert . assertFalse (isColon );
173
+ assertFalse (isColon );
167
174
}
168
175
169
176
@ Test
170
177
public void testIsMinus () {
171
178
boolean isMinus = AclUtils .isMinus ("-" );
172
- Assert . assertTrue (isMinus );
179
+ assertTrue (isMinus );
173
180
174
181
isMinus = AclUtils .isMinus ("*" );
175
- Assert . assertFalse (isMinus );
182
+ assertFalse (isMinus );
176
183
}
177
184
178
185
@ Test
179
186
public void testV6ipProcess () {
180
187
String remoteAddr = "5::7:6:1-200:*" ;
181
- Assert . assertEquals (AclUtils .v6ipProcess (remoteAddr ), "0005:0000:0000:0000:0007:0006" );
188
+ assertEquals (AclUtils .v6ipProcess (remoteAddr ), "0005:0000:0000:0000:0007:0006" );
182
189
183
190
remoteAddr = "5::7:6:1-200" ;
184
- Assert . assertEquals (AclUtils .v6ipProcess (remoteAddr ), "0005:0000:0000:0000:0000:0007:0006" );
191
+ assertEquals (AclUtils .v6ipProcess (remoteAddr ), "0005:0000:0000:0000:0000:0007:0006" );
185
192
remoteAddr = "5::7:6:*" ;
186
- Assert . assertEquals (AclUtils .v6ipProcess (remoteAddr ), "0005:0000:0000:0000:0000:0007:0006" );
193
+ assertEquals (AclUtils .v6ipProcess (remoteAddr ), "0005:0000:0000:0000:0000:0007:0006" );
187
194
188
195
remoteAddr = "5:7:6:*" ;
189
- Assert . assertEquals (AclUtils .v6ipProcess (remoteAddr ), "0005:0007:0006" );
196
+ assertEquals (AclUtils .v6ipProcess (remoteAddr ), "0005:0007:0006" );
190
197
}
191
198
192
199
@ Test
193
200
public void testExpandIP () {
194
- Assert . assertEquals (AclUtils .expandIP ("::" , 8 ), "0000:0000:0000:0000:0000:0000:0000:0000" );
195
- Assert . assertEquals (AclUtils .expandIP ("::1" , 8 ), "0000:0000:0000:0000:0000:0000:0000:0001" );
196
- Assert . assertEquals (AclUtils .expandIP ("3::" , 8 ), "0003:0000:0000:0000:0000:0000:0000:0000" );
197
- Assert . assertEquals (AclUtils .expandIP ("2::2" , 8 ), "0002:0000:0000:0000:0000:0000:0000:0002" );
198
- Assert . assertEquals (AclUtils .expandIP ("4::aac4:92" , 8 ), "0004:0000:0000:0000:0000:0000:AAC4:0092" );
199
- Assert . assertEquals (AclUtils .expandIP ("ab23:56:901a::cc6:765:bb:9011" , 8 ), "AB23:0056:901A:0000:0CC6:0765:00BB:9011" );
200
- Assert . assertEquals (AclUtils .expandIP ("ab23:56:901a:1:cc6:765:bb:9011" , 8 ), "AB23:0056:901A:0001:0CC6:0765:00BB:9011" );
201
- Assert . assertEquals (AclUtils .expandIP ("5::7:6" , 6 ), "0005:0000:0000:0000:0007:0006" );
201
+ assertEquals (AclUtils .expandIP ("::" , 8 ), "0000:0000:0000:0000:0000:0000:0000:0000" );
202
+ assertEquals (AclUtils .expandIP ("::1" , 8 ), "0000:0000:0000:0000:0000:0000:0000:0001" );
203
+ assertEquals (AclUtils .expandIP ("3::" , 8 ), "0003:0000:0000:0000:0000:0000:0000:0000" );
204
+ assertEquals (AclUtils .expandIP ("2::2" , 8 ), "0002:0000:0000:0000:0000:0000:0000:0002" );
205
+ assertEquals (AclUtils .expandIP ("4::aac4:92" , 8 ), "0004:0000:0000:0000:0000:0000:AAC4:0092" );
206
+ assertEquals (AclUtils .expandIP ("ab23:56:901a::cc6:765:bb:9011" , 8 ), "AB23:0056:901A:0000:0CC6:0765:00BB:9011" );
207
+ assertEquals (AclUtils .expandIP ("ab23:56:901a:1:cc6:765:bb:9011" , 8 ), "AB23:0056:901A:0001:0CC6:0765:00BB:9011" );
208
+ assertEquals (AclUtils .expandIP ("5::7:6" , 6 ), "0005:0000:0000:0000:0007:0006" );
202
209
}
203
210
204
211
@ SuppressWarnings ("unchecked" )
205
212
@ Test
206
213
public void testGetYamlDataObject () throws IOException {
207
214
try (InputStream is = AclUtilsTest .class .getClassLoader ().getResourceAsStream ("conf/plain_acl_correct.yml" )) {
208
215
Map <String , Object > map = AclUtils .getYamlDataObject (is , Map .class );
209
- Assert . assertNotNull (map );
210
- Assert . assertFalse (map .isEmpty ());
216
+ assertNotNull (map );
217
+ assertFalse (map .isEmpty ());
211
218
}
212
219
}
213
220
@@ -225,7 +232,7 @@ private static String randomTmpFile() {
225
232
public void writeDataObject2YamlFileTest () throws IOException {
226
233
String targetFileName = randomTmpFile ();
227
234
File transport = new File (targetFileName );
228
- Assert . assertTrue (transport .createNewFile ());
235
+ assertTrue (transport .createNewFile ());
229
236
transport .deleteOnExit ();
230
237
231
238
PlainAccessData aclYamlMap = new PlainAccessData ();
@@ -248,14 +255,14 @@ public void writeDataObject2YamlFileTest() throws IOException {
248
255
};
249
256
accounts .add (accountsMap );
250
257
aclYamlMap .setAccounts (accounts );
251
- Assert . assertTrue (AclUtils .writeDataObject (targetFileName , aclYamlMap ));
258
+ assertTrue (AclUtils .writeDataObject (targetFileName , aclYamlMap ));
252
259
}
253
260
254
261
@ Test
255
262
public void updateExistedYamlFileTest () throws IOException {
256
263
String targetFileName = randomTmpFile ();
257
264
File transport = new File (targetFileName );
258
- Assert . assertTrue (transport .createNewFile ());
265
+ assertTrue (transport .createNewFile ());
259
266
transport .deleteOnExit ();
260
267
261
268
PlainAccessData aclYamlMap = new PlainAccessData ();
@@ -279,21 +286,42 @@ public void updateExistedYamlFileTest() throws IOException {
279
286
280
287
PlainAccessData readableMap = AclUtils .getYamlDataObject (targetFileName , PlainAccessData .class );
281
288
List <String > updatedGlobalWhiteRemoteAddrs = readableMap .getGlobalWhiteRemoteAddresses ();
282
- Assert . assertEquals ("192.168.1.2" , updatedGlobalWhiteRemoteAddrs .get (0 ));
289
+ assertEquals ("192.168.1.2" , updatedGlobalWhiteRemoteAddrs .get (0 ));
283
290
}
284
291
285
292
@ Test
286
293
public void getYamlDataIgnoreFileNotFoundExceptionTest () {
287
294
288
295
JSONObject yamlDataObject = AclUtils .getYamlDataObject ("plain_acl.yml" , JSONObject .class );
289
- Assert . assertNull (yamlDataObject );
296
+ assertNull (yamlDataObject );
290
297
}
291
298
292
299
@ Test
293
300
public void getAclRPCHookTest () throws IOException {
294
301
try (InputStream is = AclUtilsTest .class .getClassLoader ().getResourceAsStream ("conf/plain_acl_incomplete.yml" )) {
295
302
RPCHook incompleteContRPCHook = AclUtils .getAclRPCHook (is );
296
- Assert . assertNull (incompleteContRPCHook );
303
+ assertNull (incompleteContRPCHook );
297
304
}
298
305
}
306
+
307
+ @ Test
308
+ public void testGetAclRPCHookByFileName () {
309
+ RPCHook actual = AclUtils .getAclRPCHook (Objects .requireNonNull (AclUtilsTest .class .getResource ("/acl_hook/plain_acl.yml" )).getPath ());
310
+ assertNotNull (actual );
311
+ assertTrue (actual instanceof AclClientRPCHook );
312
+ assertAclClientRPCHook ((AclClientRPCHook ) actual );
313
+ }
314
+
315
+ @ Test
316
+ public void testGetAclRPCHookByInputStream () {
317
+ RPCHook actual = AclUtils .getAclRPCHook (Objects .requireNonNull (AclUtilsTest .class .getResourceAsStream ("/acl_hook/plain_acl.yml" )));
318
+ assertNotNull (actual );
319
+ assertTrue (actual instanceof AclClientRPCHook );
320
+ assertAclClientRPCHook ((AclClientRPCHook ) actual );
321
+ }
322
+
323
+ private void assertAclClientRPCHook (final AclClientRPCHook actual ) {
324
+ assertEquals ("rocketmq2" , actual .getSessionCredentials ().getAccessKey ());
325
+ assertEquals ("12345678" , actual .getSessionCredentials ().getSecretKey ());
326
+ }
299
327
}
0 commit comments