This commit is contained in:
Cheney Wong
2025-07-02 15:46:50 +08:00
parent f905cd8608
commit ffae50c347
3 changed files with 311 additions and 152 deletions
+52
View File
@@ -0,0 +1,52 @@
package test;
import java.nio.ByteBuffer;
public class ByteBufferUtil {
public static boolean hasFlipped(ByteBuffer buffer) {
return buffer.position() == 0 && buffer.limit() < buffer.capacity();
}
/**
* 获取 ByteBuffer 中实际有效数据的 byte 数组
*
* @param buffer ByteBuffer 对象
* @return 包含有效数据的 byte 数组
*/
public static byte[] getValidArray(ByteBuffer buffer) {
if (buffer == null) {
return new byte[0];
}
if ( ! hasFlipped(buffer) ) {
buffer.flip();
}
// 保存当前 position 位置
int originalPosition = buffer.position();
// 获取有效数据长度
int remaining = buffer.remaining();
if (remaining == 0) {
return new byte[0];
}
// 创建结果数组
byte[] result = new byte[remaining];
// 如果 buffer 有 backing array
if (buffer.hasArray()) {
// 计算实际数据在 backing array 中的起始位置
int arrayOffset = buffer.arrayOffset();
System.arraycopy(buffer.array(), arrayOffset + originalPosition, result, 0, remaining);
} else {
// 对于直接缓冲区,需要手动复制数据
buffer.get(result);
// 恢复原来的 position
buffer.position(originalPosition);
}
return result;
}
}
+22 -22
View File
@@ -10,6 +10,9 @@ import java.nio.ByteBuffer;
import java.util.Arrays; import java.util.Arrays;
import java.util.Random; import java.util.Random;
import static test.TestAll.ECB_DEC;
import static test.TestAll.ECB_ENC;
public class LongDataTest { public class LongDataTest {
private SydApi api; private SydApi api;
@@ -135,8 +138,22 @@ public class LongDataTest {
} }
/**
* 解密纯 0
*/
@Test @Test
public void sm4() { public void case7() {
byte[] data = new byte[4064];
ISM4 sydApiSM4 = api.initSM4(0, 0);
for (int i = 0; i < 10; i++) {
sydApiSM4.update(data);
}
sydApiSM4.finish();
}
public static boolean case1(SydApi api) {
int max = 1000; int max = 1000;
int size = 4065; int size = 4065;
@@ -156,26 +173,6 @@ public class LongDataTest {
} }
byte[] v = en.finish(); byte[] v = en.finish();
enData.put(v); enData.put(v);
if (1 == max) {
byte[] in = new byte[inData.position()];
System.arraycopy(inData.array(), 0, in, 0, in.length);
byte[] bytes = api.SYD_SM4_ShortData(1, in, Consts.ECB_ENC);
byte[] look = new byte[enData.position()];
System.arraycopy(enData.array(), 0, look, 0, look.length);
byte[] deBytes = api.SYD_SM4_ShortData(1, bytes, Consts.ECB_DEC);
}
enData.flip(); enData.flip();
@@ -199,8 +196,11 @@ public class LongDataTest {
} }
} }
byte[] deArray = ByteBufferUtil.getValidArray( deData );
byte[] inArray = ByteBufferUtil.getValidArray( inData );
Assert.assertArrayEquals(deData.array(), inData.array()); Assert.assertArrayEquals(deArray, inArray);
return Arrays.equals(deArray, inArray);
} }
} }
+229 -122
View File
@@ -4,13 +4,18 @@ import com.sunyard.SydApiException;
import com.sunyard.proto.Util; import com.sunyard.proto.Util;
import com.sunyard.sge.database.*; import com.sunyard.sge.database.*;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Date;
import java.util.List; import java.util.List;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.io.FileWriter;
import java.text.SimpleDateFormat;
public class TestAll { public class TestAll {
private static final String[] ipListElec = new String[]{"127.0.0.1", "172.1.41.129"}; private static final String[] ipListElec = new String[]{"127.0.0.1", "172.1.41.129"};
@@ -28,6 +33,58 @@ public class TestAll {
System.setProperty("com.sunyard.sydapi4j.debug", "false"); System.setProperty("com.sunyard.sydapi4j.debug", "false");
} }
private static void startPerformanceFile() {
try (FileWriter fw = new FileWriter(PERF_RESULT_FILE, true);
BufferedWriter bw = new BufferedWriter(fw)) {
Date now = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String timeStr = sdf.format(now);
bw.write("==================== Performance Test Start: " + timeStr + " ====================");
bw.newLine();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void stopPerformanceFile() {
try (FileWriter fw = new FileWriter(PERF_RESULT_FILE, true);
BufferedWriter bw = new BufferedWriter(fw)) {
Date now = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String timeStr = sdf.format(now);
bw.write("==================== Performance Test End: " + timeStr + " ====================");
bw.newLine();
} catch (IOException e) {
e.printStackTrace();
}
}
private void writeToPerformanceFile(String interfaceName, int dataLen, int machineCount, int threadNum, int totalRequests, double tps, double avgLatency) {
try (FileWriter fw = new FileWriter(PERF_RESULT_FILE, true);
BufferedWriter bw = new BufferedWriter(fw)) {
String line = String.format("%s\t%d\t%d\t%d\t%d\t%.2f\t%.2f",
interfaceName, dataLen, machineCount, threadNum, totalRequests, tps, avgLatency);
bw.write(line);
bw.newLine();
} catch (IOException e) {
e.printStackTrace();
}
}
private void writeToPerformanceFile2(String interfaceName, String dataLen, int machineCount, int threadNum, double totalRequests) {
try (FileWriter fw = new FileWriter(PERF_RESULT_FILE, true);
BufferedWriter bw = new BufferedWriter(fw)) {
String line = String.format("%s\t%s\t%d\t%d\t%.2f",
interfaceName, dataLen, machineCount, threadNum, totalRequests);
bw.write(line);
bw.newLine();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void menu1(Config config, Choise choise, TestAll test) throws Exception { public static void menu1(Config config, Choise choise, TestAll test) throws Exception {
@@ -43,13 +100,18 @@ public class TestAll {
switch (choise.getAsInt()) { switch (choise.getAsInt()) {
case 1: case 1:
funcMenu(config, choise, test);
break;
case 2:
test.funcAll(); test.funcAll();
break; break;
case 3: case 3:
perfMenu(config, choise, test); perfMenu(config, choise, test);
break; break;
case 4: case 4:
startPerformanceFile();
test.perfAll(); test.perfAll();
stopPerformanceFile();
break; break;
default: default:
System.exit(0); System.exit(0);
@@ -65,6 +127,10 @@ public class TestAll {
System.out.println("-------- SM4短数据加解密性能测试结束 --------\n"); System.out.println("-------- SM4短数据加解密性能测试结束 --------\n");
} }
private static final String PERF_RESULT_FILE = "performance_results.txt";
public void sm4ShortEnde(int numberOfThreads, int totalRequest, int plainLen) throws InterruptedException { public void sm4ShortEnde(int numberOfThreads, int totalRequest, int plainLen) throws InterruptedException {
byte[] data = new byte[plainLen]; byte[] data = new byte[plainLen];
SydApiBuilder builder = new SydApiBuilder( SydApiBuilder builder = new SydApiBuilder(
@@ -100,7 +166,11 @@ public class TestAll {
System.out.println("并发数: " + numberOfThreads); System.out.println("并发数: " + numberOfThreads);
System.out.println("加密共花费 " + String.format("%.2f", duration / 1000.0) + " 秒"); System.out.println("加密共花费 " + String.format("%.2f", duration / 1000.0) + " 秒");
System.out.println("TPS: " + String.format("%.2f", tps)); System.out.println("TPS: " + String.format("%.2f", tps));
System.out.println("吞吐量: " + String.format("%.2f", throughput) + " MB/S\n"); System.out.println("吞吐量: " + String.format("%.2f", throughput) + " MB/S");
double avgLatencyEnc = duration * 1.0 / totalRequest;
System.out.println("平均延时(加密): " + String.format("%.2f", avgLatencyEnc) + " ms");
writeToPerformanceFile("SM4短数据加密", 1024, 1, numberOfThreads, totalRequest, tps, avgLatencyEnc);
SydApi apiTest = Api.get(); SydApi apiTest = Api.get();
byte[] res = apiTest.SYD_SM4_ShortData(0, data, ECB_ENC); byte[] res = apiTest.SYD_SM4_ShortData(0, data, ECB_ENC);
@@ -124,7 +194,11 @@ public class TestAll {
System.out.println("并发数: " + numberOfThreads); System.out.println("并发数: " + numberOfThreads);
System.out.println("解密共花费 " + String.format("%.2f", duration / 1000.0) + " 秒"); System.out.println("解密共花费 " + String.format("%.2f", duration / 1000.0) + " 秒");
System.out.println("TPS: " + String.format("%.2f", tps)); System.out.println("TPS: " + String.format("%.2f", tps));
System.out.println("吞吐量: " + String.format("%.2f", throughput) + " MB/S\n"); System.out.println("吞吐量: " + String.format("%.2f", throughput) + " MB/S");
double avgLatencyDec = duration * 1.0 / totalRequest;
System.out.println("平均延时(解密): " + String.format("%.2f", avgLatencyDec) + " ms");
writeToPerformanceFile("SM4短数据解密", 1024, 1, numberOfThreads, totalRequest, tps, avgLatencyDec);
} }
public void sm4BatchEndes() throws InterruptedException { public void sm4BatchEndes() throws InterruptedException {
@@ -135,12 +209,12 @@ public class TestAll {
} }
public void sm4BatchEnde(int numberOfThreads, int totalRequest, int plainLen) throws InterruptedException { public void sm4BatchEnde(int numberOfThreads, int totalRequest, int plainLen) throws InterruptedException {
int[] keyIdx = new int[]{0, 0, 1, 1}; int[] keyIdx = new int[]{0, 0, 0, 0};
List<byte[]> data = new ArrayList<>(); List<byte[]> data = new ArrayList<>();
data.add(new byte[1000]); data.add(new byte[1024]);
data.add(new byte[1000]); data.add(new byte[1024]);
data.add(new byte[1000]); data.add(new byte[1024]);
data.add(new byte[1000]); data.add(new byte[1024]);
SydApiBuilder builder = new SydApiBuilder( SydApiBuilder builder = new SydApiBuilder(
config.getIps(), config.getIps(),
@@ -175,7 +249,9 @@ public class TestAll {
System.out.println("并发数: " + numberOfThreads); System.out.println("并发数: " + numberOfThreads);
System.out.println("加密共花费 " + String.format("%.2f", duration / 1000.0) + " 秒"); System.out.println("加密共花费 " + String.format("%.2f", duration / 1000.0) + " 秒");
System.out.println("TPS: " + String.format("%.2f", tps)); System.out.println("TPS: " + String.format("%.2f", tps));
System.out.println("吞吐量: " + String.format("%.2f", throughput) + " MB/S\n"); System.out.println("吞吐量: " + String.format("%.2f", throughput) + " MB/S");
double avgLatency = duration * 1.0 / totalRequest;
writeToPerformanceFile("SM4批量数据加密", 1024, 1, numberOfThreads, totalRequest, tps, avgLatency);
SydApi apiTest = Api.get(); SydApi apiTest = Api.get();
List<byte[]> res = apiTest.SYD_SM4_BatchData(keyIdx, data, ECB_ENC); List<byte[]> res = apiTest.SYD_SM4_BatchData(keyIdx, data, ECB_ENC);
@@ -199,9 +275,12 @@ public class TestAll {
System.out.println("并发数: " + numberOfThreads); System.out.println("并发数: " + numberOfThreads);
System.out.println("解密共花费 " + String.format("%.2f", duration / 1000.0) + " 秒"); System.out.println("解密共花费 " + String.format("%.2f", duration / 1000.0) + " 秒");
System.out.println("TPS: " + String.format("%.2f", tps)); System.out.println("TPS: " + String.format("%.2f", tps));
System.out.println("吞吐量: " + String.format("%.2f", throughput) + " MB/S\n"); System.out.println("吞吐量: " + String.format("%.2f", throughput) + " MB/S");
avgLatency = duration * 1.0 / totalRequest;
writeToPerformanceFile("SM4批量数据解密", 1024, 1, numberOfThreads, totalRequest, tps, avgLatency);
} }
public void sm4macShortEndes() throws InterruptedException { public void sm4macShortEndes() throws InterruptedException {
System.out.println("-------- SM4MAC短数据性能测试开始 --------"); System.out.println("-------- SM4MAC短数据性能测试开始 --------");
sm4macShortEnde(1, config.getTotal(), 1024); sm4macShortEnde(1, config.getTotal(), 1024);
@@ -209,6 +288,7 @@ public class TestAll {
System.out.println("-------- SM4MAC短数据性能测试结束 --------\n"); System.out.println("-------- SM4MAC短数据性能测试结束 --------\n");
} }
public void sm4macShortEnde(int numberOfThreads, int totalRequest, int plainLen) throws InterruptedException { public void sm4macShortEnde(int numberOfThreads, int totalRequest, int plainLen) throws InterruptedException {
byte[] data = new byte[plainLen]; byte[] data = new byte[plainLen];
SydApiBuilder builder = new SydApiBuilder( SydApiBuilder builder = new SydApiBuilder(
@@ -244,18 +324,20 @@ public class TestAll {
System.out.println("并发数: " + numberOfThreads); System.out.println("并发数: " + numberOfThreads);
System.out.println("共花费 " + String.format("%.2f", duration / 1000.0) + " 秒"); System.out.println("共花费 " + String.format("%.2f", duration / 1000.0) + " 秒");
System.out.println("TPS: " + String.format("%.2f", tps)); System.out.println("TPS: " + String.format("%.2f", tps));
System.out.println("吞吐量: " + String.format("%.2f", throughput) + " MB/S\n"); System.out.println("吞吐量: " + String.format("%.2f", throughput) + " MB/S");
double avgLatency = duration * 1.0 / totalRequest;
writeToPerformanceFile("SM4-MAC短数据", 1024, 1, numberOfThreads, totalRequest, tps, avgLatency);
} }
public void sm4macBatchEndes() throws InterruptedException { public void sm4macBatchEndes() throws InterruptedException {
System.out.println("-------- SM4MAC批量数据性能测试开始 --------"); System.out.println("-------- SM4MAC批量数据性能测试开始 --------");
sm4macBatchEnde(1, 100, 1024); sm4macBatchEnde(1, config.getTotal(), 1024);
// sm4macBatchEnde(40, config.getTotal(), 1024); sm4macBatchEnde(40, config.getTotal(), 1024);
System.out.println("-------- SM4MAC批量数据性能测试结束 --------\n"); System.out.println("-------- SM4MAC批量数据性能测试结束 --------\n");
} }
public void sm4macBatchEnde(int numberOfThreads, int totalRequest, int plainLen) throws InterruptedException { public void sm4macBatchEnde(int numberOfThreads, int totalRequest, int plainLen) throws InterruptedException {
int[] keyIdx = new int[]{0, 1, 0, 1}; int[] keyIdx = new int[]{0, 0, 0, 0};
SydApiBuilder builder = new SydApiBuilder( SydApiBuilder builder = new SydApiBuilder(
config.getIps(), config.getIps(),
config.getPorts(), config.getPorts(),
@@ -281,7 +363,6 @@ public class TestAll {
poolEnc.submit(() -> { poolEnc.submit(() -> {
SydApi api = Api.get(); SydApi api = Api.get();
String[] s = api.SYD_SM4Mac_BatchData(keyIdx, data); String[] s = api.SYD_SM4Mac_BatchData(keyIdx, data);
System.out.println(s);
}); });
} }
poolEnc.shutdown(); poolEnc.shutdown();
@@ -295,7 +376,9 @@ public class TestAll {
System.out.println("并发数: " + numberOfThreads); System.out.println("并发数: " + numberOfThreads);
System.out.println("共花费 " + String.format("%.2f", duration / 1000.0) + " 秒"); System.out.println("共花费 " + String.format("%.2f", duration / 1000.0) + " 秒");
System.out.println("TPS: " + String.format("%.2f", tps)); System.out.println("TPS: " + String.format("%.2f", tps));
System.out.println("吞吐量: " + String.format("%.2f", throughput) + " MB/S\n"); System.out.println("吞吐量: " + String.format("%.2f", throughput) + " MB/S");
double avgLatency = duration * 1.0 / totalRequest;
writeToPerformanceFile("SM4MAC短数据", 1024, 1, numberOfThreads, totalRequest, tps, avgLatency);
} }
public void sm3ShortEndes() throws InterruptedException { public void sm3ShortEndes() throws InterruptedException {
@@ -340,7 +423,9 @@ public class TestAll {
System.out.println("并发数: " + numberOfThreads); System.out.println("并发数: " + numberOfThreads);
System.out.println("共花费 " + String.format("%.2f", duration / 1000.0) + " 秒"); System.out.println("共花费 " + String.format("%.2f", duration / 1000.0) + " 秒");
System.out.println("TPS: " + String.format("%.2f", tps)); System.out.println("TPS: " + String.format("%.2f", tps));
System.out.println("吞吐量: " + String.format("%.2f", throughput) + " MB/S\n"); System.out.println("吞吐量: " + String.format("%.2f", throughput) + " MB/S");
double avgLatency = duration * 1.0 / totalRequest;
writeToPerformanceFile("SM3", 1024, 1, numberOfThreads, totalRequest, tps, avgLatency);
} }
public void sm4ShortMacEndes() throws InterruptedException { public void sm4ShortMacEndes() throws InterruptedException {
@@ -395,7 +480,9 @@ public class TestAll {
System.out.println("并发数: " + numberOfThreads); System.out.println("并发数: " + numberOfThreads);
System.out.println("加密共花费 " + String.format("%.2f", duration / 1000.0) + " 秒"); System.out.println("加密共花费 " + String.format("%.2f", duration / 1000.0) + " 秒");
System.out.println("TPS: " + String.format("%.2f", tps)); System.out.println("TPS: " + String.format("%.2f", tps));
System.out.println("吞吐量: " + String.format("%.2f", throughput) + " MB/S\n"); System.out.println("吞吐量: " + String.format("%.2f", throughput) + " MB/S");
double avgLatency = duration * 1.0 / totalRequest;
writeToPerformanceFile("SM4短数据加密并生成MAC", 1024, 1, numberOfThreads, totalRequest, tps, avgLatency);
@@ -418,7 +505,9 @@ public class TestAll {
System.out.println("并发数: " + numberOfThreads); System.out.println("并发数: " + numberOfThreads);
System.out.println("解密共花费 " + String.format("%.2f", duration / 1000.0) + " 秒"); System.out.println("解密共花费 " + String.format("%.2f", duration / 1000.0) + " 秒");
System.out.println("TPS: " + String.format("%.2f", tps)); System.out.println("TPS: " + String.format("%.2f", tps));
System.out.println("吞吐量: " + String.format("%.2f", throughput) + " MB/S\n"); System.out.println("吞吐量: " + String.format("%.2f", throughput) + " MB/S");
avgLatency = duration * 1.0 / totalRequest;
writeToPerformanceFile("SM4短数据解密并校验MAC", 1024, 1, numberOfThreads, totalRequest, tps, avgLatency);
} }
public void sm4BatchMacEndes() throws InterruptedException { public void sm4BatchMacEndes() throws InterruptedException {
@@ -476,7 +565,9 @@ public class TestAll {
System.out.println("并发数: " + numberOfThreads); System.out.println("并发数: " + numberOfThreads);
System.out.println("加密共花费 " + String.format("%.2f", duration / 1000.0) + " 秒"); System.out.println("加密共花费 " + String.format("%.2f", duration / 1000.0) + " 秒");
System.out.println("TPS: " + String.format("%.2f", tps)); System.out.println("TPS: " + String.format("%.2f", tps));
System.out.println("吞吐量: " + String.format("%.2f", throughput) + " MB/S\n"); System.out.println("吞吐量: " + String.format("%.2f", throughput) + " MB/S");
double avgLatency = duration * 1.0 / totalRequest;
writeToPerformanceFile("SM4批量数据加密并生成MAC", 1024, 1, numberOfThreads, totalRequest, tps, avgLatency);
ExecutorService poolDec = Executors.newFixedThreadPool(numberOfThreads); ExecutorService poolDec = Executors.newFixedThreadPool(numberOfThreads);
start = System.currentTimeMillis(); start = System.currentTimeMillis();
@@ -497,13 +588,17 @@ public class TestAll {
System.out.println("并发数: " + numberOfThreads); System.out.println("并发数: " + numberOfThreads);
System.out.println("解密共花费 " + String.format("%.2f", duration / 1000.0) + " 秒"); System.out.println("解密共花费 " + String.format("%.2f", duration / 1000.0) + " 秒");
System.out.println("TPS: " + String.format("%.2f", tps)); System.out.println("TPS: " + String.format("%.2f", tps));
System.out.println("吞吐量: " + String.format("%.2f", throughput) + " MB/S\n"); System.out.println("吞吐量: " + String.format("%.2f", throughput) + " MB/S");
avgLatency = duration * 1.0 / totalRequest;
writeToPerformanceFile("SM4批量数据解密并校验MAC", 1024, 1, numberOfThreads, totalRequest, tps, avgLatency);
} }
public void sm4LongEnde() { public void sm4LongEnde() {
System.out.println("-------- SM4长数据加解密性能测试开始 --------"); System.out.println("-------- SM4长数据加解密性能测试开始 --------");
byte[] data = new byte[1024]; byte[] data = new byte[1024 * 4];
int key = 0; int key = 0;
ByteBuffer enDataBuff = ByteBuffer.allocate(100 * 1024 * 1024 + 16); ByteBuffer enDataBuff = ByteBuffer.allocate(100 * 1024 * 1024 + 16);
@@ -528,7 +623,8 @@ public class TestAll {
double throughput = 100.0 * 1000 / duration; double throughput = 100.0 * 1000 / duration;
System.out.println("加密共花费 " + String.format("%.2f", duration / 1000.0) + " 秒"); System.out.println("加密共花费 " + String.format("%.2f", duration / 1000.0) + " 秒");
System.out.println("吞吐量: " + String.format("%.2f", throughput) + " MB/S\n"); System.out.println("吞吐量: " + String.format("%.2f", throughput) + " MB/S");
writeToPerformanceFile2("SM4长数据加密", "100M", 1, 1, throughput);
start = System.currentTimeMillis(); start = System.currentTimeMillis();
@@ -544,7 +640,8 @@ public class TestAll {
throughput = 100.0 * 1000 / duration; throughput = 100.0 * 1000 / duration;
System.out.println("解密共花费 " + String.format("%.2f", duration / 1000.0) + " 秒"); System.out.println("解密共花费 " + String.format("%.2f", duration / 1000.0) + " 秒");
System.out.println("吞吐量: " + String.format("%.2f", throughput) + " MB/S\n"); System.out.println("吞吐量: " + String.format("%.2f", throughput) + " MB/S");
writeToPerformanceFile2("SM4长数据解密", "100M", 1, 1, throughput);
System.out.println("-------- SM4长数据加解密性能测试结束 --------\\n"); System.out.println("-------- SM4长数据加解密性能测试结束 --------\\n");
@@ -576,7 +673,8 @@ public class TestAll {
double throughput = 100.0 * 1000 / duration; double throughput = 100.0 * 1000 / duration;
System.out.println("共花费 " + String.format("%.2f", duration / 1000.0) + " 秒"); System.out.println("共花费 " + String.format("%.2f", duration / 1000.0) + " 秒");
System.out.println("吞吐量: " + String.format("%.2f", throughput) + " MB/S\n"); System.out.println("吞吐量: " + String.format("%.2f", throughput) + " MB/S");
writeToPerformanceFile2("SM4长数据MAC", "100M", 1, 1, throughput);
System.out.println("-------- SM4MAC长数据性能测试结束 --------"); System.out.println("-------- SM4MAC长数据性能测试结束 --------");
} }
@@ -607,7 +705,8 @@ public class TestAll {
double throughput = 100.0 * 1000 / duration; double throughput = 100.0 * 1000 / duration;
System.out.println("共花费 " + String.format("%.2f", duration / 1000.0) + " 秒"); System.out.println("共花费 " + String.format("%.2f", duration / 1000.0) + " 秒");
System.out.println("吞吐量: " + String.format("%.2f", throughput) + " MB/S\n"); System.out.println("吞吐量: " + String.format("%.2f", throughput) + " MB/S");
writeToPerformanceFile2("SM3长数据", "100MB", 1, 1, throughput);
System.out.println("-------- SM3长数据性能测试结束 --------"); System.out.println("-------- SM3长数据性能测试结束 --------");
} }
@@ -722,18 +821,6 @@ public class TestAll {
System.out.println("预期结果:" + CalcRes.SM3_LONG_RESULT2); System.out.println("预期结果:" + CalcRes.SM3_LONG_RESULT2);
} }
try {
sm3 = api.initSM3();
data = new byte[0];
for (int i = 0; i < 10; i++) {
sm3.update(data);
}
sm3.digest();
System.out.println("SM3长数据长度超出测试失败");
} catch (SydApiException e) {
System.out.println("SM3长数据长度超出测试成功");
}
System.out.println("-------- SM3长数据功能测试结束 --------\n"); System.out.println("-------- SM3长数据功能测试结束 --------\n");
} }
@@ -792,82 +879,15 @@ public class TestAll {
2000 2000
); );
SydApi api = builder.build(); SydApi api = builder.build();
boolean ret = LongDataTest.case1(api);
int key = 0; if ( ! ret ){
byte[] data1 = new byte[4064]; System.out.println("-------- SM4长数据加解密功能测试失败 --------\n");
byte[] data2 = new byte[16];
ISM4 sm4 = api.initSM4(key, ECB_ENC);
for (int i = 0; i < 10; i++) {
sm4.update(data1);
}
byte[] enc1 = sm4.finish();
String res1 = Util.bytes2HexString(enc1);
sm4 = api.initSM4(key, ECB_ENC);
for (int i = 0; i < 10; i++) {
sm4.update(data2);
}
byte[] enc2 = sm4.finish();
String res2 = Util.bytes2HexString(enc2);
sm4 = api.initSM4(key, ECB_DEC);
for (int i = 0; i < 10; i++) {
sm4.update(Arrays.copyOfRange(enc1, i * 4064, (i + 1) * 4064));
}
sm4.update(Arrays.copyOfRange(enc1, 10 * 4064, 10 * 4064 + 16));
byte[] dec1 = sm4.finish();
sm4 = api.initSM4(key, ECB_DEC);
for (int i = 0; i < 10; i++) {
sm4.update(Arrays.copyOfRange(enc1, i * 16, (i + 1) * 16));
}
sm4.update(Arrays.copyOfRange(enc1, 10 * 16, 10 * 16 + 16));
byte[] dec2 = sm4.finish();
if (res2.equals(CalcRes.SM4_LONG_RESULT2)
&& Arrays.equals(dec1, new byte[40640]) && Arrays.equals(dec2, new byte[160])) {
System.out.println("SM4长数据加解密测试成功");
} else { } else {
System.out.println("SM4长数据加解密测试失败");
System.out.println("加密计算结果:" + res1);
// System.out.println("加密预期结果:" + CalcRes.SM4_LONG_RESULT1);
System.out.println("解密计算结果:" + Util.bytes2HexString(dec1));
System.out.println("解密预期结果:" + Util.bytes2HexString(new byte[40640]));
System.out.println("加密计算结果:" + res2);
System.out.println("加密预期结果:" + CalcRes.SM4_LONG_RESULT2);
System.out.println("解密计算结果:" + Util.bytes2HexString(dec2));
System.out.println("解密预期结果:" + Util.bytes2HexString(new byte[160]));
}
try {
sm4 = api.initSM4(key, ECB_ENC);
data1 = new byte[4080];
for (int i = 0; i < 10; i++) {
sm4.update(data1);
}
sm4.finish();
System.out.println("SM4长数据超长度检测失败");
} catch (SydApiException e) {
System.out.println("SM4长数据超长度检测成功");
}
try {
sm4 = api.initSM4(key, ECB_ENC);
data1 = new byte[17];
for (int i = 0; i < 10; i++) {
sm4.update(data1);
}
sm4.finish();
System.out.println("SM4长数据16字节未对齐检测失败");
} catch (SydApiException e) {
System.out.println("'SM4长数据16字节未对齐检测成功'");
}
System.out.println("-------- SM4长数据加解密功能测试结束 --------\n"); System.out.println("-------- SM4长数据加解密功能测试结束 --------\n");
} }
}
public void sm4macShortFunc() { public void sm4macShortFunc() {
System.out.println("-------- SM4MAC短数据功能测试开始 --------"); System.out.println("-------- SM4MAC短数据功能测试开始 --------");
SydApiBuilder builder = new SydApiBuilder( SydApiBuilder builder = new SydApiBuilder(
@@ -1046,10 +1066,7 @@ public class TestAll {
data.add(new byte[1024]); data.add(new byte[1024]);
data.add(new byte[1024]); data.add(new byte[1024]);
data.add(new byte[1024]); data.add(new byte[1024]);
data.add(new byte[1024]); int[] keyIdx = new int[]{0, 0, 0, 0, 0};
data.add(new byte[1024]);
data.add(new byte[1024]);
int[] keyIdx = new int[]{0, 0, 0, 0, 0, 0, 0};
String[] s = api.SYD_SM4Mac_BatchData(keyIdx, data); String[] s = api.SYD_SM4Mac_BatchData(keyIdx, data);
@@ -1082,11 +1099,7 @@ public class TestAll {
data.add(new byte[1024]); data.add(new byte[1024]);
data.add(new byte[1024]); data.add(new byte[1024]);
data.add(new byte[1024]); data.add(new byte[1024]);
data.add(new byte[1024]); keyIdx = new int[]{0, 0, 0, 0};
data.add(new byte[1024]);
data.add(new byte[1024]);
data.add(new byte[1024]);
keyIdx = new int[]{0, 0, 0, 0, 0, 0, 0, 0};
api.SYD_SM4Mac_BatchData(keyIdx, data); api.SYD_SM4Mac_BatchData(keyIdx, data);
System.out.println("SM4MAC批量数据长度超出测试失败"); System.out.println("SM4MAC批量数据长度超出测试失败");
@@ -1214,18 +1227,111 @@ public class TestAll {
this.loggerFunc(); this.loggerFunc();
this.connectFunc(); this.connectFunc();
this.longConnectFunc(); this.longConnectFunc();
this.sm3ShortFunc(); this.sm3ShortFunc();
this.sm3LongFunc();
this.sm4ShortFunc(); this.sm4ShortFunc();
this.sm4LongFunc();
this.sm4macShortFunc();
this.sm4macLongFunc();
this.sm4BatchFunc(); this.sm4BatchFunc();
this.sm4macShortFunc();
this.sm4macBatchFunc(); this.sm4macBatchFunc();
this.sm4ShortMacFunc(); this.sm4ShortMacFunc();
this.sm4BatchMacFunc(); this.sm4BatchMacFunc();
this.sm4LongFunc();
this.sm4macLongFunc();
this.sm3LongFunc();
} }
public static void funcMenu(Config config, Choise choise, TestAll test) throws Exception{
boolean flag = true;
while (flag) {
System.out.println("请选择测试项:");
System.out.println("1. 日志测试");
System.out.println("2. 短链接测试");
System.out.println("3. 长链接测试");
System.out.println("4. SM3短数据");
System.out.println("5. SM4 短数据加解密");
System.out.println("6. SM4 批量数据加解密");
System.out.println("7. SM4-MAC 短数据");
System.out.println("8. SM4-MAC 批量数据");
System.out.println("9. SM4短数据加密后生成MAC,解密验证MAC");
System.out.println("10. SM4批量数据加密后生成MAC,解密验证MAC");
System.out.println("11. SM4长数据加解密");
System.out.println("12. SM4长数据MAC");
System.out.println("13. SM3长数据");
System.out.println("-1.退出");
switch (choise.getAsInt()) {
case 1:
test.loggerFunc();
break;
case 2:
test.connectFunc();
break;
case 3:
test.longConnectFunc();
break;
case 4:
test.sm3ShortFunc();
break;
case 5:
test.sm4ShortFunc();
break;
case 6:
test.sm4BatchFunc();
break;
case 7:
test.sm4macShortFunc();
break;
case 8: {
test.sm4macBatchFunc();
break;
}
case 9: {
test.sm4ShortMacFunc();
break;
}
case 10: {
test.sm4BatchMacFunc();
break;
}
case 11:
test.sm4macLongFunc();
break;
case 12:
test.sm4LongFunc();
break;
case 13:
test.sm3LongFunc();
break;
case -1:
flag = false;
break;
}
}
}
public void perfAll() throws InterruptedException { public void perfAll() throws InterruptedException {
this.sm4ShortEndes(); this.sm4ShortEndes();
this.sm4BatchEndes(); this.sm4BatchEndes();
@@ -1234,6 +1340,7 @@ public class TestAll {
this.sm3ShortEndes(); this.sm3ShortEndes();
this.sm4ShortMacEndes(); this.sm4ShortMacEndes();
this.sm4BatchMacEndes(); this.sm4BatchMacEndes();
this.sm4LongEnde(); this.sm4LongEnde();
this.sm4MacLongEnde(); this.sm4MacLongEnde();
this.sm3LongEnde(); this.sm3LongEnde();