修改测试代码
This commit is contained in:
parent
5264f97b34
commit
30087e7d53
116
src/test/java/test/LengthTest.java
Normal file
116
src/test/java/test/LengthTest.java
Normal file
@ -0,0 +1,116 @@
|
||||
package test;
|
||||
|
||||
import com.sunyard.proto.Util;
|
||||
import com.sunyard.sge.database.*;
|
||||
import com.sunyard.sge.database.Consts;
|
||||
import com.sunyard.sge.log.LogFactory;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class LengthTest
|
||||
{
|
||||
private SydApi api;
|
||||
@Before
|
||||
public void init(){
|
||||
SydApiBuilder builder = new SydApiBuilder(
|
||||
new String[]{ "192.168.0.200" },
|
||||
new int[]{ 8889 },
|
||||
10,
|
||||
15
|
||||
|
||||
);
|
||||
api = builder.build();
|
||||
}
|
||||
//
|
||||
@Test
|
||||
public void syd_SM4_ShortDataTest(){
|
||||
//解密 pcData长度为0
|
||||
byte[] bytes = api.SYD_SM4_ShortData(0, new byte[0], Consts.ECB_DEC);
|
||||
Arrays.toString(bytes);
|
||||
//解密 pcData长度不为0
|
||||
byte[] bytes2 = api.SYD_SM4_ShortData(0, new byte[0], Consts.ECB_DEC);
|
||||
Arrays.toString(bytes2);
|
||||
|
||||
}
|
||||
@Test
|
||||
public void syd_SM4_BatchDataTest(){
|
||||
// 密钥索引
|
||||
int[] keyIdx = new int[]{ 0, 1, 2 };
|
||||
// 测试数据
|
||||
List<byte[]> data = new ArrayList<>();
|
||||
data.add( new byte[0] );
|
||||
data.add( new byte[0] );
|
||||
data.add( new byte[0] );
|
||||
List<byte[]> data2 = new ArrayList<>();
|
||||
data.add( new byte[0] );
|
||||
data.add( new byte[0] );
|
||||
data.add( new byte[0] );
|
||||
//解密 pcData长度为0
|
||||
List<byte[]> orgDatas = api.SYD_SM4_BatchData( keyIdx , data, Consts.ECB_DEC );
|
||||
for (byte[] byte_1 :orgDatas) {
|
||||
System.out.printf("解密后数据", Arrays.toString(byte_1));
|
||||
}
|
||||
//解密 pcData长度不为0
|
||||
List<byte[]> orgDatas2 = api.SYD_SM4_BatchData( keyIdx , data2, Consts.ECB_DEC );
|
||||
for (byte[] byte_2 :orgDatas2) {
|
||||
System.out.printf("解密后数据", Arrays.toString(byte_2));
|
||||
}
|
||||
}
|
||||
@Test
|
||||
public void syd_SM4_LongDataTest(){
|
||||
// 密钥索引
|
||||
int keyIdx = 0;
|
||||
// 测试数据
|
||||
byte[] data = new byte[16 * 3 + 8];
|
||||
// 每包大小
|
||||
int pack = 16;
|
||||
//解密 pcData长度为0
|
||||
byte[] recv = api.SYD_SM4_LongData( keyIdx , new byte[0], Consts.PkgNumHead, Consts.ECB_DEC);
|
||||
System.out.println(Arrays.toString(recv));
|
||||
//解密 pcData长度不为0
|
||||
// 密文数据
|
||||
ByteBuffer enDataBuff = ByteBuffer.allocate( data.length + 16 );
|
||||
// 分包加密
|
||||
{
|
||||
int n = data.length / pack;
|
||||
int remainder = data.length % pack;
|
||||
if ( remainder > 0 ) {
|
||||
n += 1;
|
||||
} else {
|
||||
remainder = pack;
|
||||
}
|
||||
|
||||
for ( int i = 0 ; i < n ; i ++ ) {
|
||||
byte[] buff = new byte[ pack ];
|
||||
|
||||
// 首包
|
||||
if ( 0 == i ) {
|
||||
System.arraycopy( data, i * pack, buff, 0, pack );
|
||||
byte[] recv3 = api.SYD_SM4_LongData( keyIdx , buff, Consts.PkgNumHead, Consts.ECB_ENC);
|
||||
enDataBuff.put( recv3 );
|
||||
continue;
|
||||
}
|
||||
// 尾包
|
||||
if ( i == (n-1) ){
|
||||
buff = new byte[ remainder ];
|
||||
System.arraycopy( data, i * pack, buff, 0, remainder );
|
||||
byte[] recv3 = api.SYD_SM4_LongData( keyIdx , buff, Consts.PkgNumTail, Consts.ECB_ENC);
|
||||
enDataBuff.put( recv3);
|
||||
continue;
|
||||
}
|
||||
// 中间包
|
||||
System.arraycopy( data, i * pack, buff, 0, pack );
|
||||
byte[] recv3 = api.SYD_SM4_LongData( keyIdx , buff, Consts.PkgNumMiddle, Consts.ECB_ENC);
|
||||
enDataBuff.put( recv3 );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -10,13 +10,9 @@ import java.util.Arrays;
|
||||
|
||||
public class LogTest {
|
||||
public static void main(String[] args) {
|
||||
SydApi.SYD_SetLogConfig("./test-log/",11);
|
||||
// int[] a1 = {1,2,3};
|
||||
// Logger log = LogFactory.createLogger("./test-log/", 9);
|
||||
// log.error("error");
|
||||
// log.info(Arrays.toString(a1));
|
||||
// log.debug("debug");
|
||||
// System.out.println("发送数据");
|
||||
|
||||
Logger log = LogFactory.createLogger("./test-log/", 3);
|
||||
log.error("error");
|
||||
log.info("info");
|
||||
log.debug("debug");
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user