Skip to content

Commit

Permalink
[ISSUE apache#9156] Refactor AclUtils#getAclRPCHook
Browse files Browse the repository at this point in the history
  • Loading branch information
yx9o committed Jan 23, 2025
1 parent de4e48d commit 69b5d62
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import java.util.Map;
import java.util.SortedMap;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson2.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.common.constant.LoggerName;
import org.apache.rocketmq.logging.org.slf4j.Logger;
Expand Down
124 changes: 76 additions & 48 deletions acl/src/test/java/org/apache/rocketmq/acl/common/AclUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
package org.apache.rocketmq.acl.common;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson2.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.acl.plain.PlainAccessData;
import org.apache.rocketmq.common.PlainAccessConfig;
Expand All @@ -32,8 +32,15 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

public class AclUtilsTest {

@Test
Expand All @@ -47,7 +54,7 @@ public void testGetAddresses() {
addressList.add("1.1.1.2");
addressList.add("1.1.1.3");
addressList.add("1.1.1.4");
Assert.assertEquals(newAddressList, addressList);
assertEquals(newAddressList, addressList);

// IPv6 test
String ipv6Address = "1:ac41:9987::bb22:666:{1,2,3,4}";
Expand All @@ -60,7 +67,7 @@ public void testGetAddresses() {
ipv6AddressList.add("1:ac41:9987::bb22:666:2");
ipv6AddressList.add("1:ac41:9987::bb22:666:3");
ipv6AddressList.add("1:ac41:9987::bb22:666:4");
Assert.assertEquals(newIPv6AddressList, ipv6AddressList);
assertEquals(newIPv6AddressList, ipv6AddressList);
}

@Test
Expand All @@ -70,9 +77,9 @@ public void testIsScope_StringArray() {
for (int i = 0; i < 6; i++) {
boolean isScope = AclUtils.isScope(address, 4);
if (i == 3) {
Assert.assertTrue(isScope);
assertTrue(isScope);
} else {
Assert.assertFalse(isScope);
assertFalse(isScope);
}
address = address + ".12";
}
Expand All @@ -82,46 +89,46 @@ public void testIsScope_StringArray() {
public void testIsScope_Array() {
String[] address = StringUtils.split("12.12.12.12", ".");
boolean isScope = AclUtils.isScope(address, 4);
Assert.assertTrue(isScope);
assertTrue(isScope);
isScope = AclUtils.isScope(address, 3);
Assert.assertTrue(isScope);
assertTrue(isScope);

address = StringUtils.split("12.12.1222.1222", ".");
isScope = AclUtils.isScope(address, 4);
Assert.assertFalse(isScope);
assertFalse(isScope);
isScope = AclUtils.isScope(address, 3);
Assert.assertFalse(isScope);
assertFalse(isScope);

// IPv6 test
address = StringUtils.split("1050:0000:0000:0000:0005:0600:300c:326b", ":");
isScope = AclUtils.isIPv6Scope(address, 8);
Assert.assertTrue(isScope);
assertTrue(isScope);
isScope = AclUtils.isIPv6Scope(address, 4);
Assert.assertTrue(isScope);
assertTrue(isScope);

address = StringUtils.split("1050:9876:0000:0000:0005:akkg:300c:326b", ":");
isScope = AclUtils.isIPv6Scope(address, 8);
Assert.assertFalse(isScope);
assertFalse(isScope);
isScope = AclUtils.isIPv6Scope(address, 4);
Assert.assertTrue(isScope);
assertTrue(isScope);

address = StringUtils.split(AclUtils.expandIP("1050::0005:akkg:300c:326b", 8), ":");
isScope = AclUtils.isIPv6Scope(address, 8);
Assert.assertFalse(isScope);
assertFalse(isScope);
isScope = AclUtils.isIPv6Scope(address, 4);
Assert.assertTrue(isScope);
assertTrue(isScope);
}

@Test
public void testIsScope_String() {
for (int i = 0; i < 256; i++) {
boolean isScope = AclUtils.isScope(i + "");
Assert.assertTrue(isScope);
assertTrue(isScope);
}
boolean isScope = AclUtils.isScope("-1");
Assert.assertFalse(isScope);
assertFalse(isScope);
isScope = AclUtils.isScope("256");
Assert.assertFalse(isScope);
assertFalse(isScope);
}

@Test
Expand All @@ -131,9 +138,9 @@ public void testIsScope_Integral() {
Assert.assertTrue(isScope);
}
boolean isScope = AclUtils.isScope(-1);
Assert.assertFalse(isScope);
assertFalse(isScope);
isScope = AclUtils.isScope(256);
Assert.assertFalse(isScope);
assertFalse(isScope);

// IPv6 test
int min = Integer.parseInt("0", 16);
Expand All @@ -143,71 +150,71 @@ public void testIsScope_Integral() {
Assert.assertTrue(isScope);
}
isScope = AclUtils.isIPv6Scope(-1);
Assert.assertFalse(isScope);
assertFalse(isScope);
isScope = AclUtils.isIPv6Scope(max + 1);
Assert.assertFalse(isScope);
assertFalse(isScope);
}

@Test
public void testIsAsterisk() {
boolean isAsterisk = AclUtils.isAsterisk("*");
Assert.assertTrue(isAsterisk);
assertTrue(isAsterisk);

isAsterisk = AclUtils.isAsterisk(",");
Assert.assertFalse(isAsterisk);
assertFalse(isAsterisk);
}

@Test
public void testIsComma() {
boolean isColon = AclUtils.isComma(",");
Assert.assertTrue(isColon);
assertTrue(isColon);

isColon = AclUtils.isComma("-");
Assert.assertFalse(isColon);
assertFalse(isColon);
}

@Test
public void testIsMinus() {
boolean isMinus = AclUtils.isMinus("-");
Assert.assertTrue(isMinus);
assertTrue(isMinus);

isMinus = AclUtils.isMinus("*");
Assert.assertFalse(isMinus);
assertFalse(isMinus);
}

@Test
public void testV6ipProcess() {
String remoteAddr = "5::7:6:1-200:*";
Assert.assertEquals(AclUtils.v6ipProcess(remoteAddr), "0005:0000:0000:0000:0007:0006");
assertEquals(AclUtils.v6ipProcess(remoteAddr), "0005:0000:0000:0000:0007:0006");

remoteAddr = "5::7:6:1-200";
Assert.assertEquals(AclUtils.v6ipProcess(remoteAddr), "0005:0000:0000:0000:0000:0007:0006");
assertEquals(AclUtils.v6ipProcess(remoteAddr), "0005:0000:0000:0000:0000:0007:0006");
remoteAddr = "5::7:6:*";
Assert.assertEquals(AclUtils.v6ipProcess(remoteAddr), "0005:0000:0000:0000:0000:0007:0006");
assertEquals(AclUtils.v6ipProcess(remoteAddr), "0005:0000:0000:0000:0000:0007:0006");

remoteAddr = "5:7:6:*";
Assert.assertEquals(AclUtils.v6ipProcess(remoteAddr), "0005:0007:0006");
assertEquals(AclUtils.v6ipProcess(remoteAddr), "0005:0007:0006");
}

@Test
public void testExpandIP() {
Assert.assertEquals(AclUtils.expandIP("::", 8), "0000:0000:0000:0000:0000:0000:0000:0000");
Assert.assertEquals(AclUtils.expandIP("::1", 8), "0000:0000:0000:0000:0000:0000:0000:0001");
Assert.assertEquals(AclUtils.expandIP("3::", 8), "0003:0000:0000:0000:0000:0000:0000:0000");
Assert.assertEquals(AclUtils.expandIP("2::2", 8), "0002:0000:0000:0000:0000:0000:0000:0002");
Assert.assertEquals(AclUtils.expandIP("4::aac4:92", 8), "0004:0000:0000:0000:0000:0000:AAC4:0092");
Assert.assertEquals(AclUtils.expandIP("ab23:56:901a::cc6:765:bb:9011", 8), "AB23:0056:901A:0000:0CC6:0765:00BB:9011");
Assert.assertEquals(AclUtils.expandIP("ab23:56:901a:1:cc6:765:bb:9011", 8), "AB23:0056:901A:0001:0CC6:0765:00BB:9011");
Assert.assertEquals(AclUtils.expandIP("5::7:6", 6), "0005:0000:0000:0000:0007:0006");
assertEquals(AclUtils.expandIP("::", 8), "0000:0000:0000:0000:0000:0000:0000:0000");
assertEquals(AclUtils.expandIP("::1", 8), "0000:0000:0000:0000:0000:0000:0000:0001");
assertEquals(AclUtils.expandIP("3::", 8), "0003:0000:0000:0000:0000:0000:0000:0000");
assertEquals(AclUtils.expandIP("2::2", 8), "0002:0000:0000:0000:0000:0000:0000:0002");
assertEquals(AclUtils.expandIP("4::aac4:92", 8), "0004:0000:0000:0000:0000:0000:AAC4:0092");
assertEquals(AclUtils.expandIP("ab23:56:901a::cc6:765:bb:9011", 8), "AB23:0056:901A:0000:0CC6:0765:00BB:9011");
assertEquals(AclUtils.expandIP("ab23:56:901a:1:cc6:765:bb:9011", 8), "AB23:0056:901A:0001:0CC6:0765:00BB:9011");
assertEquals(AclUtils.expandIP("5::7:6", 6), "0005:0000:0000:0000:0007:0006");
}

@SuppressWarnings("unchecked")
@Test
public void testGetYamlDataObject() throws IOException {
try (InputStream is = AclUtilsTest.class.getClassLoader().getResourceAsStream("conf/plain_acl_correct.yml")) {
Map<String, Object> map = AclUtils.getYamlDataObject(is, Map.class);
Assert.assertNotNull(map);
Assert.assertFalse(map.isEmpty());
assertNotNull(map);
assertFalse(map.isEmpty());
}
}

Expand All @@ -225,7 +232,7 @@ private static String randomTmpFile() {
public void writeDataObject2YamlFileTest() throws IOException {
String targetFileName = randomTmpFile();
File transport = new File(targetFileName);
Assert.assertTrue(transport.createNewFile());
assertTrue(transport.createNewFile());
transport.deleteOnExit();

PlainAccessData aclYamlMap = new PlainAccessData();
Expand All @@ -248,14 +255,14 @@ public void writeDataObject2YamlFileTest() throws IOException {
};
accounts.add(accountsMap);
aclYamlMap.setAccounts(accounts);
Assert.assertTrue(AclUtils.writeDataObject(targetFileName, aclYamlMap));
assertTrue(AclUtils.writeDataObject(targetFileName, aclYamlMap));
}

@Test
public void updateExistedYamlFileTest() throws IOException {
String targetFileName = randomTmpFile();
File transport = new File(targetFileName);
Assert.assertTrue(transport.createNewFile());
assertTrue(transport.createNewFile());
transport.deleteOnExit();

PlainAccessData aclYamlMap = new PlainAccessData();
Expand All @@ -279,21 +286,42 @@ public void updateExistedYamlFileTest() throws IOException {

PlainAccessData readableMap = AclUtils.getYamlDataObject(targetFileName, PlainAccessData.class);
List<String> updatedGlobalWhiteRemoteAddrs = readableMap.getGlobalWhiteRemoteAddresses();
Assert.assertEquals("192.168.1.2", updatedGlobalWhiteRemoteAddrs.get(0));
assertEquals("192.168.1.2", updatedGlobalWhiteRemoteAddrs.get(0));
}

@Test
public void getYamlDataIgnoreFileNotFoundExceptionTest() {

JSONObject yamlDataObject = AclUtils.getYamlDataObject("plain_acl.yml", JSONObject.class);
Assert.assertNull(yamlDataObject);
assertNull(yamlDataObject);
}

@Test
public void getAclRPCHookTest() throws IOException {
try (InputStream is = AclUtilsTest.class.getClassLoader().getResourceAsStream("conf/plain_acl_incomplete.yml")) {
RPCHook incompleteContRPCHook = AclUtils.getAclRPCHook(is);
Assert.assertNull(incompleteContRPCHook);
assertNull(incompleteContRPCHook);
}
}

@Test
public void testGetAclRPCHookByFileName() {
RPCHook actual = AclUtils.getAclRPCHook(Objects.requireNonNull(AclUtilsTest.class.getResource("/acl_hook/plain_acl.yml")).getPath());
assertNotNull(actual);
assertTrue(actual instanceof AclClientRPCHook);
assertAclClientRPCHook((AclClientRPCHook) actual);
}

@Test
public void testGetAclRPCHookByInputStream() {
RPCHook actual = AclUtils.getAclRPCHook(Objects.requireNonNull(AclUtilsTest.class.getResourceAsStream("/acl_hook/plain_acl.yml")));
assertNotNull(actual);
assertTrue(actual instanceof AclClientRPCHook);
assertAclClientRPCHook((AclClientRPCHook) actual);
}

private void assertAclClientRPCHook(final AclClientRPCHook actual) {
assertEquals("rocketmq2", actual.getSessionCredentials().getAccessKey());
assertEquals("12345678", actual.getSessionCredentials().getSecretKey());
}
}

0 comments on commit 69b5d62

Please sign in to comment.