mirror of
https://github.com/apache/rocketmq.git
synced 2026-09-21 13:49:50 +08:00
[acl] Modify unit test to solve the problem of lack of license
Signed-off-by: zhangyang21 <zhangyang21@xiaomi.com>
This commit is contained in:
@@ -20,9 +20,11 @@ import com.alibaba.fastjson.JSONObject;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.util.Map;
|
||||
import java.util.SortedMap;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
@@ -274,6 +276,43 @@ public class AclUtils {
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean copyFile(String from, String to) {
|
||||
FileChannel input = null;
|
||||
FileChannel output = null;
|
||||
try {
|
||||
input = new FileInputStream(new File(from)).getChannel();
|
||||
output = new FileOutputStream(new File(to)).getChannel();
|
||||
output.transferFrom(input, 0, input.size());
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
log.error("file copy error. from={}, to={}", from, to, e);
|
||||
} finally {
|
||||
closeFileChannel(input);
|
||||
closeFileChannel(output);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean moveFile(String from, String to) {
|
||||
try {
|
||||
File file = new File(from);
|
||||
return file.renameTo(new File(to));
|
||||
} catch (Exception e) {
|
||||
log.error("file move error. from={}, to={}", from, to, e);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static void closeFileChannel(FileChannel fileChannel) {
|
||||
if (fileChannel != null) {
|
||||
try {
|
||||
fileChannel.close();
|
||||
} catch (IOException e) {
|
||||
log.error("Close file channel error.", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static RPCHook getAclRPCHook(String fileName) {
|
||||
JSONObject yamlDataObject = null;
|
||||
try {
|
||||
|
||||
@@ -316,4 +316,25 @@ public class AclUtilsTest {
|
||||
Assert.assertNull(incompleteContRPCHook);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCopyAndMoveFile() {
|
||||
String path = "src/test/resources/conf/plain_acl.yml";
|
||||
String backupPath = "src/test/resources/conf/plain_acl.yml_backup";
|
||||
Map<String, Object> aclYamlData = AclUtils.getYamlDataObject(path, Map.class);
|
||||
|
||||
AclUtils.copyFile(path, backupPath);
|
||||
Assert.assertEquals(aclYamlData, AclUtils.getYamlDataObject(backupPath, Map.class));
|
||||
|
||||
Map<String, Object> aclYamlMap = new HashMap<String, Object>();
|
||||
List<String> globalWhiteRemoteAddrs = new ArrayList<String>();
|
||||
globalWhiteRemoteAddrs.add("10.10.103.*");
|
||||
globalWhiteRemoteAddrs.add("192.168.0.*");
|
||||
aclYamlMap.put("globalWhiteRemoteAddrs", globalWhiteRemoteAddrs);
|
||||
AclUtils.writeDataObject(path, aclYamlMap);
|
||||
Assert.assertNotEquals(aclYamlData, AclUtils.getYamlDataObject(path, Map.class));
|
||||
|
||||
AclUtils.moveFile(backupPath, path);
|
||||
Assert.assertEquals(aclYamlData, AclUtils.getYamlDataObject(path, Map.class));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -328,7 +328,7 @@ public class PlainAccessValidatorTest {
|
||||
System.setProperty("rocketmq.acl.plain.file", "/conf/plain_acl_update_create.yml");
|
||||
|
||||
String targetFileName = "src/test/resources/conf/plain_acl_update_create.yml";
|
||||
Map<String, Object> backUpAclConfigMap = AclUtils.getYamlDataObject(targetFileName, Map.class);
|
||||
AclUtils.copyFile(targetFileName, buildBackupPath(targetFileName));
|
||||
|
||||
PlainAccessConfig plainAccessConfig = new PlainAccessConfig();
|
||||
plainAccessConfig.setAccessKey("RocketMQ");
|
||||
@@ -370,8 +370,8 @@ public class PlainAccessValidatorTest {
|
||||
List<Map<String, Object>> dataVersions = (List<Map<String, Object>>) readableMap.get("dataVersion");
|
||||
Assert.assertEquals(1,dataVersions.get(0).get("counter"));
|
||||
|
||||
// Restore the backup file and flush to yaml file
|
||||
AclUtils.writeDataObject(targetFileName, backUpAclConfigMap);
|
||||
// Restore the backup file
|
||||
AclUtils.moveFile(buildBackupPath(targetFileName), targetFileName);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -380,7 +380,7 @@ public class PlainAccessValidatorTest {
|
||||
System.setProperty("rocketmq.acl.plain.file", "/conf/plain_acl_update_create.yml");
|
||||
|
||||
String targetFileName = "src/test/resources/conf/plain_acl_update_create.yml";
|
||||
Map<String, Object> backUpAclConfigMap = AclUtils.getYamlDataObject(targetFileName, Map.class);
|
||||
AclUtils.copyFile(targetFileName, buildBackupPath(targetFileName));
|
||||
|
||||
PlainAccessConfig plainAccessConfig = new PlainAccessConfig();
|
||||
plainAccessConfig.setAccessKey("RocketMQ");
|
||||
@@ -401,18 +401,17 @@ public class PlainAccessValidatorTest {
|
||||
}
|
||||
Assert.assertEquals(verifyMap.get(AclConstants.CONFIG_SECRET_KEY),"123456789111");
|
||||
|
||||
// Restore the backup file and flush to yaml file
|
||||
AclUtils.writeDataObject(targetFileName, backUpAclConfigMap);
|
||||
// Restore the backup file
|
||||
AclUtils.moveFile(buildBackupPath(targetFileName), targetFileName);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void createAndUpdateAccessAclYamlConfigNormalTest() {
|
||||
System.setProperty("rocketmq.home.dir", "src/test/resources");
|
||||
System.setProperty("rocketmq.acl.plain.file", "/conf/plain_acl_update_create.yml");
|
||||
|
||||
String targetFileName = "src/test/resources/conf/plain_acl_update_create.yml";
|
||||
Map<String, Object> backUpAclConfigMap = AclUtils.getYamlDataObject(targetFileName, Map.class);
|
||||
AclUtils.copyFile(targetFileName, buildBackupPath(targetFileName));
|
||||
|
||||
PlainAccessConfig plainAccessConfig = new PlainAccessConfig();
|
||||
plainAccessConfig.setAccessKey("RocketMQ33");
|
||||
@@ -478,9 +477,8 @@ public class PlainAccessValidatorTest {
|
||||
Assert.assertEquals(2,dataVersions2.get(0).get(AclConstants.CONFIG_COUNTER));
|
||||
Assert.assertEquals(verifyMap2.get(AclConstants.CONFIG_SECRET_KEY),"1234567890123");
|
||||
|
||||
|
||||
// Restore the backup file and flush to yaml file
|
||||
AclUtils.writeDataObject(targetFileName, backUpAclConfigMap);
|
||||
// Restore the backup file
|
||||
AclUtils.moveFile(buildBackupPath(targetFileName), targetFileName);
|
||||
}
|
||||
|
||||
@Test(expected = AclException.class)
|
||||
@@ -503,15 +501,14 @@ public class PlainAccessValidatorTest {
|
||||
System.setProperty("rocketmq.acl.plain.file", "/conf/plain_acl_delete.yml");
|
||||
|
||||
String targetFileName = "src/test/resources/conf/plain_acl_delete.yml";
|
||||
Map<String, Object> backUpAclConfigMap = AclUtils.getYamlDataObject(targetFileName, Map.class);
|
||||
|
||||
AclUtils.copyFile(targetFileName, buildBackupPath(targetFileName));
|
||||
|
||||
String accessKey = "rocketmq2";
|
||||
PlainAccessValidator plainAccessValidator = new PlainAccessValidator();
|
||||
plainAccessValidator.deleteAccessConfig(accessKey);
|
||||
|
||||
Map<String, Object> readableMap = AclUtils.getYamlDataObject(targetFileName, Map.class);
|
||||
List<Map<String, Object>> accounts = (List<Map<String, Object>>)readableMap.get(AclConstants.CONFIG_ACCOUNTS);
|
||||
List<Map<String, Object>> accounts = (List<Map<String, Object>>) readableMap.get(AclConstants.CONFIG_ACCOUNTS);
|
||||
Map<String, Object> verifyMap = null;
|
||||
for (Map<String, Object> account : accounts) {
|
||||
if (account.get(AclConstants.CONFIG_ACCESS_KEY).equals(accessKey)) {
|
||||
@@ -526,8 +523,8 @@ public class PlainAccessValidatorTest {
|
||||
List<Map<String, Object>> dataVersions = (List<Map<String, Object>>) readableMap.get(AclConstants.CONFIG_DATA_VERSION);
|
||||
Assert.assertEquals(1,dataVersions.get(0).get(AclConstants.CONFIG_COUNTER));
|
||||
|
||||
// Restore the backup file and flush to yaml file
|
||||
AclUtils.writeDataObject(targetFileName, backUpAclConfigMap);
|
||||
// Restore the backup file
|
||||
AclUtils.moveFile(buildBackupPath(targetFileName), targetFileName);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -583,7 +580,7 @@ public class PlainAccessValidatorTest {
|
||||
System.setProperty("rocketmq.acl.plain.file", "/conf/plain_acl_global_white_addrs.yml");
|
||||
|
||||
String targetFileName = "src/test/resources/conf/plain_acl_global_white_addrs.yml";
|
||||
Map<String, Object> backUpAclConfigMap = AclUtils.getYamlDataObject(targetFileName, Map.class);
|
||||
AclUtils.copyFile(targetFileName, buildBackupPath(targetFileName));
|
||||
|
||||
PlainAccessValidator plainAccessValidator = new PlainAccessValidator();
|
||||
// Update global white remote addr value list in the acl access yaml config file
|
||||
@@ -605,8 +602,8 @@ public class PlainAccessValidatorTest {
|
||||
List<Map<String, Object>> dataVersions = (List<Map<String, Object>>) readableMap.get(AclConstants.CONFIG_DATA_VERSION);
|
||||
Assert.assertEquals(1,dataVersions.get(0).get(AclConstants.CONFIG_COUNTER));
|
||||
|
||||
// Restore the backup file and flush to yaml file
|
||||
AclUtils.writeDataObject(targetFileName, backUpAclConfigMap);
|
||||
// Restore the backup file
|
||||
AclUtils.moveFile(buildBackupPath(targetFileName), targetFileName);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -620,6 +617,9 @@ public class PlainAccessValidatorTest {
|
||||
|
||||
@Test
|
||||
public void updateAccessConfigEmptyPermListTest(){
|
||||
String targetFileName = "src/test/resources/conf/plain_acl.yml";
|
||||
AclUtils.copyFile(targetFileName, this.buildBackupPath(targetFileName));
|
||||
|
||||
PlainAccessValidator plainAccessValidator = new PlainAccessValidator();
|
||||
PlainAccessConfig plainAccessConfig = new PlainAccessConfig();
|
||||
String accessKey = "updateAccessConfigEmptyPerm";
|
||||
@@ -636,10 +636,16 @@ public class PlainAccessValidatorTest {
|
||||
Assert.assertEquals(0, result.getTopicPerms().size());
|
||||
|
||||
plainAccessValidator.deleteAccessConfig(accessKey);
|
||||
|
||||
// Restore the backup file
|
||||
AclUtils.moveFile(buildBackupPath(targetFileName), targetFileName);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateAccessConfigEmptyWhiteRemoteAddressTest(){
|
||||
String targetFileName = "src/test/resources/conf/plain_acl.yml";
|
||||
AclUtils.copyFile(targetFileName, this.buildBackupPath(targetFileName));
|
||||
|
||||
PlainAccessValidator plainAccessValidator = new PlainAccessValidator();
|
||||
PlainAccessConfig plainAccessConfig = new PlainAccessConfig();
|
||||
String accessKey = "updateAccessConfigEmptyWhiteRemoteAddress";
|
||||
@@ -656,5 +662,12 @@ public class PlainAccessValidatorTest {
|
||||
Assert.assertEquals("", result.getWhiteRemoteAddress());
|
||||
|
||||
plainAccessValidator.deleteAccessConfig(accessKey);
|
||||
|
||||
// Restore the backup file
|
||||
AclUtils.moveFile(buildBackupPath(targetFileName), targetFileName);
|
||||
}
|
||||
|
||||
private String buildBackupPath(String path) {
|
||||
return String.format("%s_backup", path);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user