|
|
|
@@ -0,0 +1,265 @@
|
|
|
|
|
package test;
|
|
|
|
|
|
|
|
|
|
import com.sunyard.sge.database.Consts;
|
|
|
|
|
import com.sunyard.sge.database.SydApi;
|
|
|
|
|
import com.sunyard.sge.database.SydApi4Database;
|
|
|
|
|
import com.sunyard.sge.database.SydApiBuilder;
|
|
|
|
|
import org.junit.After;
|
|
|
|
|
import org.junit.Assert;
|
|
|
|
|
import org.junit.Before;
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Random;
|
|
|
|
|
|
|
|
|
|
public class ShortBatchDataTest {
|
|
|
|
|
private SydApi api;
|
|
|
|
|
private static class RandomTestData {
|
|
|
|
|
private int size;
|
|
|
|
|
private int[] keyIdx;
|
|
|
|
|
private List<byte[]> data;
|
|
|
|
|
|
|
|
|
|
public int[] getKeyIdx() {
|
|
|
|
|
return keyIdx;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<byte[]> getData() {
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int getSize() {
|
|
|
|
|
return size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public RandomTestData(int lucky) {
|
|
|
|
|
this.size = lucky;
|
|
|
|
|
this.keyIdx = new int[lucky];
|
|
|
|
|
this.data = new ArrayList<>();
|
|
|
|
|
for ( int i = 0 ; i < lucky; i++ ) {
|
|
|
|
|
byte[] data = new byte[ new Random().nextInt(1024) ];
|
|
|
|
|
new Random().nextBytes( data );
|
|
|
|
|
this.data.add( data );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public RandomTestData() {
|
|
|
|
|
int lucky = new Random().nextInt(100);
|
|
|
|
|
this.size = lucky;
|
|
|
|
|
this.keyIdx = new int[lucky];
|
|
|
|
|
this.data = new ArrayList<>();
|
|
|
|
|
for ( int i = 0 ; i < lucky; i++ ) {
|
|
|
|
|
byte[] data = new byte[ new Random().nextInt(1024) ];
|
|
|
|
|
new Random().nextBytes( data );
|
|
|
|
|
this.data.add( data );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Before
|
|
|
|
|
public void init() {
|
|
|
|
|
// System.setProperty("com.sunyard.sydapi4j.debug", "true");
|
|
|
|
|
|
|
|
|
|
api =new SydApi4Database().SYD_Short_Connect_Ex(new String[]{"192.168.0.200"},
|
|
|
|
|
new int[]{8889},
|
|
|
|
|
20,
|
|
|
|
|
30);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@After
|
|
|
|
|
public void end() {
|
|
|
|
|
if (null != api) {
|
|
|
|
|
api.SYD_Disconnect_Ex();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@org.junit.Test
|
|
|
|
|
public void batchSM4Case1() {
|
|
|
|
|
// 密钥索引
|
|
|
|
|
int[] keyIdx = new int[]{0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2}; // 12 组
|
|
|
|
|
// 测试数据
|
|
|
|
|
List<byte[]> data = new ArrayList<>();
|
|
|
|
|
data.add(new byte[8]);
|
|
|
|
|
data.add(new byte[16]);
|
|
|
|
|
data.add(new byte[1024]);
|
|
|
|
|
data.add(new byte[8]);
|
|
|
|
|
data.add(new byte[16]);
|
|
|
|
|
data.add(new byte[1024]);
|
|
|
|
|
data.add(new byte[8]);
|
|
|
|
|
data.add(new byte[16]);
|
|
|
|
|
data.add(new byte[1024]);
|
|
|
|
|
data.add(new byte[8]);
|
|
|
|
|
data.add(new byte[16]);
|
|
|
|
|
data.add(new byte[1024]);
|
|
|
|
|
|
|
|
|
|
List<byte[]> encData = api.SYD_SM4_BatchData(keyIdx, data, Consts.ECB_ENC);
|
|
|
|
|
List<byte[]> orgData = api.SYD_SM4_BatchData(keyIdx, encData, Consts.ECB_DEC);
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < data.size(); i++) {
|
|
|
|
|
Assert.assertArrayEquals(orgData.get(i), data.get(i));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@org.junit.Test
|
|
|
|
|
public void batchSM4Case2() {
|
|
|
|
|
// 测试数据
|
|
|
|
|
ShortBatchDataTest.RandomTestData data = new ShortBatchDataTest.RandomTestData();
|
|
|
|
|
|
|
|
|
|
List<byte[]> encData = api.SYD_SM4_BatchData(data.getKeyIdx(), data.getData(), Consts.ECB_ENC);
|
|
|
|
|
List<byte[]> orgData = api.SYD_SM4_BatchData(data.getKeyIdx(), encData, Consts.ECB_DEC);
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < data.getSize(); i++) {
|
|
|
|
|
Assert.assertArrayEquals(orgData.get(i), data.getData().get(i));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@org.junit.Test
|
|
|
|
|
public void batchSM4Case3() {
|
|
|
|
|
// 密钥索引
|
|
|
|
|
int[] keyIdx = new int[]{0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2}; // 12 组
|
|
|
|
|
// 测试数据
|
|
|
|
|
List<byte[]> data = new ArrayList<>();
|
|
|
|
|
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]);
|
|
|
|
|
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]);
|
|
|
|
|
|
|
|
|
|
List<byte[]> encData = api.SYD_SM4_BatchData(keyIdx, data, Consts.ECB_ENC);
|
|
|
|
|
List<byte[]> orgData = api.SYD_SM4_BatchData(keyIdx, encData, Consts.ECB_DEC);
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < data.size(); i++) {
|
|
|
|
|
Assert.assertArrayEquals(orgData.get(i), data.get(i));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@org.junit.Test
|
|
|
|
|
public void batchSM4CaseZero() {
|
|
|
|
|
// 测试数据
|
|
|
|
|
ShortBatchDataTest.RandomTestData data = new ShortBatchDataTest.RandomTestData(0);
|
|
|
|
|
|
|
|
|
|
List<byte[]> encData = api.SYD_SM4_BatchData(data.getKeyIdx(), data.getData(), Consts.ECB_ENC);
|
|
|
|
|
List<byte[]> orgData = api.SYD_SM4_BatchData(data.getKeyIdx(), encData, Consts.ECB_DEC);
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < data.getSize(); i++) {
|
|
|
|
|
Assert.assertArrayEquals(orgData.get(i), data.getData().get(i));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@org.junit.Test
|
|
|
|
|
public void batchMACDataCase1() {
|
|
|
|
|
// 密钥索引
|
|
|
|
|
int[] keyIdx = new int[]{0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2}; // 12 组
|
|
|
|
|
// 测试数据
|
|
|
|
|
List<byte[]> data = new ArrayList<>();
|
|
|
|
|
data.add(new byte[8]);
|
|
|
|
|
data.add(new byte[16]);
|
|
|
|
|
data.add(new byte[1024]);
|
|
|
|
|
data.add(new byte[8]);
|
|
|
|
|
data.add(new byte[16]);
|
|
|
|
|
data.add(new byte[1024]);
|
|
|
|
|
data.add(new byte[8]);
|
|
|
|
|
data.add(new byte[16]);
|
|
|
|
|
data.add(new byte[1024]);
|
|
|
|
|
data.add(new byte[8]);
|
|
|
|
|
data.add(new byte[16]);
|
|
|
|
|
data.add(new byte[1024]);
|
|
|
|
|
|
|
|
|
|
String[] macs = api.SYD_SM4Mac_BatchData(keyIdx, data);
|
|
|
|
|
boolean[] checks = api.SYD_SM4Mac_BatchData(keyIdx, data, macs);
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < data.size(); i++) {
|
|
|
|
|
Assert.assertTrue(checks[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@org.junit.Test
|
|
|
|
|
public void batchMACDataCase2() {
|
|
|
|
|
// 密钥索引
|
|
|
|
|
int[] keyIdx = new int[]{0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2}; // 12 组
|
|
|
|
|
// 测试数据
|
|
|
|
|
List<byte[]> data = new ArrayList<>();
|
|
|
|
|
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]);
|
|
|
|
|
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]);
|
|
|
|
|
|
|
|
|
|
String[] macs = api.SYD_SM4Mac_BatchData(keyIdx, data);
|
|
|
|
|
boolean[] checks = api.SYD_SM4Mac_BatchData(keyIdx, data, macs);
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < data.size(); i++) {
|
|
|
|
|
Assert.assertTrue(checks[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@org.junit.Test
|
|
|
|
|
public void batchMACData3() {
|
|
|
|
|
// 密钥索引
|
|
|
|
|
int[] keyIdx = new int[]{0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2}; // 12 组
|
|
|
|
|
// 测试数据
|
|
|
|
|
List<byte[]> data = new ArrayList<>();
|
|
|
|
|
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]);
|
|
|
|
|
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]);
|
|
|
|
|
|
|
|
|
|
String[] macs = api.SYD_SM4Mac_BatchData(keyIdx, data);
|
|
|
|
|
boolean[] checks = api.SYD_SM4Mac_BatchData(keyIdx, data, macs);
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < data.size(); i++) {
|
|
|
|
|
Assert.assertTrue(checks[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@org.junit.Test
|
|
|
|
|
public void batchMACData() {
|
|
|
|
|
// 测试数据
|
|
|
|
|
ShortBatchDataTest.RandomTestData data = new ShortBatchDataTest.RandomTestData();
|
|
|
|
|
|
|
|
|
|
String[] macs = api.SYD_SM4Mac_BatchData(data.getKeyIdx(), data.getData());
|
|
|
|
|
boolean[] checks = api.SYD_SM4Mac_BatchData(data.getKeyIdx(), data.getData(), macs);
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < data.getSize(); i++) {
|
|
|
|
|
Assert.assertTrue(checks[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void batchMACDataZero() {
|
|
|
|
|
// 测试数据
|
|
|
|
|
ShortBatchDataTest.RandomTestData data = new ShortBatchDataTest.RandomTestData(0);
|
|
|
|
|
|
|
|
|
|
String[] macs = api.SYD_SM4Mac_BatchData(data.getKeyIdx(), data.getData());
|
|
|
|
|
boolean[] checks = api.SYD_SM4Mac_BatchData(data.getKeyIdx(), data.getData(), macs);
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < data.getSize(); i++) {
|
|
|
|
|
Assert.assertTrue(checks[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|