mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-10-04 19:42:01 +08:00
Merge branch 'master' of gitee.com:willfree/min-dev-java
This commit is contained in:
commit
424fe59908
8 files changed
+866
-13
No files matched your search
@@ -41,7 +41,7 @@ public class TestIdentity {
|
||||
SM2PrivateKey sm2PrivateKey = new SM2PrivateKey(d);
|
||||
SM2PublicKey sm2PublicKey = new SM2PublicKey(x,y);
|
||||
KeyParam keyParam = new KeyParam(Common.SM2, Common.SM3withSM2);
|
||||
Identity identity = new Identity("root",keyParam,sm2PrivateKey,null,sm2PublicKey, "123456", null);
|
||||
Identity identity = new Identity("root",keyParam,sm2PrivateKey,null,sm2PublicKey, "123456", null, false);
|
||||
Certificate certificate = new Certificate(1, 1, sm2PublicKey, null,
|
||||
Common.SM3withSM2, Common.SM2, "root", "root",
|
||||
System.currentTimeMillis() - 1000, System.currentTimeMillis() + 5000,
|
||||
@@ -73,6 +73,8 @@ public class TestIdentity {
|
||||
idFromBytes = Identity.load(bytesOfDump, null);
|
||||
assertEquals(ByteUtils.toHexString(identity.getPrikey().getBytes()), ByteUtils.toHexString(idFromBytes.getPrikey().getBytes()));
|
||||
assertEquals(ByteUtils.toHexString(identity.getPubkey().getBytes()), ByteUtils.toHexString(idFromBytes.getPubkey().getBytes()));
|
||||
|
||||
logger.debug(identity.getPrikey().getBytes().length + " " + identity.getPubkey().getBytes().length);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,167 @@
|
||||
package minsecurity.identity;
|
||||
|
||||
|
||||
import minsecurity.Common;
|
||||
import minsecurity.certificate.cert.CertUtils;
|
||||
import minsecurity.certificate.cert.Certificate;
|
||||
import minsecurity.crypto.sm2.SM2Base;
|
||||
import minsecurity.crypto.sm2.SM2PrivateKey;
|
||||
import minsecurity.crypto.sm2.SM2PublicKey;
|
||||
import minsecurity.identity.persist.IdentitySerializer;
|
||||
import minsecurity.identity.persist.MapDB;
|
||||
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
|
||||
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
|
||||
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
|
||||
import org.junit.Test;
|
||||
import org.mapdb.BTreeMap;
|
||||
import org.mapdb.DB;
|
||||
import org.mapdb.DBMaker;
|
||||
import org.mapdb.Serializer;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
|
||||
/*
|
||||
* @Author: hongyu guo
|
||||
* @Description:
|
||||
* @Version: 1.0.0
|
||||
* @Date: 17:35 2021/03/09
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class TestPersist {
|
||||
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(TestIdentity.class);
|
||||
|
||||
@Test
|
||||
public void testMapDB() throws Exception {
|
||||
|
||||
AsymmetricCipherKeyPair keyPair = SM2Base.generateKeyPairParameter();
|
||||
ECPrivateKeyParameters priKey = (ECPrivateKeyParameters) keyPair.getPrivate();
|
||||
ECPublicKeyParameters pubKey = (ECPublicKeyParameters) keyPair.getPublic();
|
||||
byte[] d = priKey.getD().toByteArray();
|
||||
// d = Arrays.copyOf(d,32);
|
||||
byte[] x = pubKey.getQ().getAffineXCoord().getEncoded();
|
||||
byte[] y = pubKey.getQ().getAffineYCoord().getEncoded();
|
||||
// BigInteger bigInteger = priKey.getD();
|
||||
SM2PrivateKey sm2PrivateKey = new SM2PrivateKey(d);
|
||||
SM2PublicKey sm2PublicKey = new SM2PublicKey(x,y);
|
||||
KeyParam keyParam = new KeyParam(Common.SM2, Common.SM3withSM2);
|
||||
Identity identity = new Identity("root",keyParam,sm2PrivateKey,null,sm2PublicKey, "123456", null, false);
|
||||
Certificate certificate = new Certificate(1, 1, sm2PublicKey, null,
|
||||
Common.SM3withSM2, Common.SM2, "root", "root",
|
||||
System.currentTimeMillis() - 1000, System.currentTimeMillis() + 5000,
|
||||
Common.CertSign, true, System.currentTimeMillis());
|
||||
CertUtils.signCert(certificate, sm2PrivateKey);
|
||||
identity.setCert(certificate);
|
||||
MapDB mapDB = MapDB.getInstance("./target/test.db");
|
||||
int random = new Random().nextInt();
|
||||
mapDB.addIdentity("/test" + random, identity, false);
|
||||
mapDB.commit();
|
||||
mapDB.closeDB();
|
||||
mapDB = MapDB.getInstance("./target/test.db");
|
||||
ArrayList<Identity> arrayList = mapDB.getAllIdentity();
|
||||
logger.debug("db size: {}",arrayList.size());
|
||||
Identity id = mapDB.getIdentityByName("/test" + random);
|
||||
logger.debug(identity.toString());
|
||||
logger.debug(id.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMapDB2() throws Exception {
|
||||
MapDB mapDB = MapDB.getInstance("./target/test.db");
|
||||
for(int i = 0; i < 100; i++){
|
||||
AsymmetricCipherKeyPair keyPair = SM2Base.generateKeyPairParameter();
|
||||
ECPrivateKeyParameters priKey = (ECPrivateKeyParameters) keyPair.getPrivate();
|
||||
ECPublicKeyParameters pubKey = (ECPublicKeyParameters) keyPair.getPublic();
|
||||
byte[] d = priKey.getD().toByteArray();
|
||||
// d = Arrays.copyOf(d,32);
|
||||
byte[] x = pubKey.getQ().getAffineXCoord().getEncoded();
|
||||
byte[] y = pubKey.getQ().getAffineYCoord().getEncoded();
|
||||
// BigInteger bigInteger = priKey.getD();
|
||||
SM2PrivateKey sm2PrivateKey = new SM2PrivateKey(d);
|
||||
SM2PublicKey sm2PublicKey = new SM2PublicKey(x,y);
|
||||
KeyParam keyParam = new KeyParam(Common.SM2, Common.SM3withSM2);
|
||||
Identity identity = new Identity("root",keyParam,sm2PrivateKey,null,sm2PublicKey, "123456", null, false);
|
||||
Certificate certificate = new Certificate(1, 1, sm2PublicKey, null,
|
||||
Common.SM3withSM2, Common.SM2, "root", "root",
|
||||
System.currentTimeMillis() - 1000, System.currentTimeMillis() + 5000,
|
||||
Common.CertSign, true, System.currentTimeMillis());
|
||||
CertUtils.signCert(certificate, sm2PrivateKey);
|
||||
identity.setCert(certificate);
|
||||
int random = new Random().nextInt();
|
||||
mapDB.addIdentity("/test" + random, identity, false);
|
||||
}
|
||||
mapDB.commit();
|
||||
ArrayList<Identity> arrayList = mapDB.getAllIdentity();
|
||||
logger.debug("size = {}", arrayList.size());
|
||||
for(Identity id : arrayList){
|
||||
logger.debug(id.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHashmap(){
|
||||
HashMap<String, Integer> primeNumbers = new HashMap<>();
|
||||
|
||||
// 往HasMap中添加映射
|
||||
primeNumbers.put("Two", 2);
|
||||
primeNumbers.put("Three", 3);
|
||||
primeNumbers.put("Five", 5);
|
||||
System.out.println("HashMap: " + primeNumbers);
|
||||
|
||||
// 得到value
|
||||
Integer value = primeNumbers.get("Three");
|
||||
System.out.println("key Three 对应的 value: " + value);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefault() throws Exception {
|
||||
MapDB mapDB = MapDB.getInstance("./target/test.db");
|
||||
for(int i = 0; i < 20; i++){
|
||||
int random = new Random().nextInt();
|
||||
Identity identity = generateIdentity("/test/" + random);
|
||||
mapDB.addIdentity(identity.getName(), identity, false);
|
||||
}
|
||||
// mapDB.commit();
|
||||
ArrayList<Identity> arrayList = mapDB.getAllIdentity();
|
||||
logger.debug("size = {}", arrayList.size());
|
||||
for(Identity id : arrayList){
|
||||
logger.debug(id.getName() + " " + id.isDefault());
|
||||
}
|
||||
logger.debug("----setDefault----");
|
||||
Identity identity = generateIdentity("/abc");
|
||||
identity.setDefault(true);
|
||||
mapDB.addIdentity("/abc", identity, false);
|
||||
mapDB.setDefaultIdentity("/abc",false);
|
||||
mapDB.commit();
|
||||
|
||||
arrayList = mapDB.getAllIdentity();
|
||||
for(Identity id : arrayList){
|
||||
logger.debug(id.getName() + " " + id.isDefault());
|
||||
}
|
||||
logger.debug("get default");
|
||||
logger.debug(mapDB.getDefaultIdentity().toString());
|
||||
}
|
||||
|
||||
|
||||
private Identity generateIdentity(String name) throws Exception {
|
||||
AsymmetricCipherKeyPair keyPair = SM2Base.generateKeyPairParameter();
|
||||
ECPrivateKeyParameters priKey = (ECPrivateKeyParameters) keyPair.getPrivate();
|
||||
ECPublicKeyParameters pubKey = (ECPublicKeyParameters) keyPair.getPublic();
|
||||
byte[] d = priKey.getD().toByteArray();
|
||||
byte[] x = pubKey.getQ().getAffineXCoord().getEncoded();
|
||||
byte[] y = pubKey.getQ().getAffineYCoord().getEncoded();
|
||||
SM2PrivateKey sm2PrivateKey = new SM2PrivateKey(d);
|
||||
SM2PublicKey sm2PublicKey = new SM2PublicKey(x,y);
|
||||
KeyParam keyParam = new KeyParam(Common.SM2, Common.SM3withSM2);
|
||||
Identity identity = new Identity(name,keyParam,sm2PrivateKey,null,sm2PublicKey, "123456", null, false);
|
||||
Certificate certificate = new Certificate(1, 1, sm2PublicKey, null,
|
||||
Common.SM3withSM2, Common.SM2, "root", "root",
|
||||
System.currentTimeMillis() - 1000, System.currentTimeMillis() + 5000,
|
||||
Common.CertSign, true, System.currentTimeMillis());
|
||||
CertUtils.signCert(certificate, sm2PrivateKey);
|
||||
identity.setCert(certificate);
|
||||
return identity;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,420 @@
|
||||
package util;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/*
|
||||
* @Author: feng Zhao
|
||||
* @Description:
|
||||
* @Version: 1.0.0
|
||||
* @Date: 16:20 2021/3/9
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/public class ByteHelperTest {
|
||||
|
||||
// @Test
|
||||
// public void getFirstByte() {
|
||||
//
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// public void getLenBytes() {
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// public void testGetLenBytes() {
|
||||
// }
|
||||
|
||||
// 0 1 127 128 255
|
||||
@Test
|
||||
public void uint8ToInt() {
|
||||
byte b1 = 0x00;
|
||||
byte b2 = 0x01;
|
||||
byte b3 = 0x7f;
|
||||
byte b4 = (byte) 0x80;
|
||||
byte b5 = (byte) 0xff;
|
||||
ByteHelper byteHelper = new ByteHelper();
|
||||
int a1 = byteHelper.uint8ToInt(b1);
|
||||
int a2 = byteHelper.uint8ToInt(b2);
|
||||
int a3 = byteHelper.uint8ToInt(b3);
|
||||
int a4 = byteHelper.uint8ToInt(b4);
|
||||
int a5 = byteHelper.uint8ToInt(b5);
|
||||
|
||||
assertEquals(a1,0);
|
||||
assertEquals(a2,1);
|
||||
assertEquals(a3,127);
|
||||
assertEquals(a4,128);
|
||||
assertEquals(a5,255);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void uint8ToByte() {
|
||||
byte b1 = 0x00;
|
||||
byte b2 = 0x01;
|
||||
byte b3 = 0x7f;
|
||||
byte b4 = (byte) 0x80;
|
||||
byte b5 = (byte) 0xff;
|
||||
ByteHelper byteHelper = new ByteHelper();
|
||||
byte a1 = byteHelper.uint8ToByte(0);
|
||||
byte a2 = byteHelper.uint8ToByte(1);
|
||||
byte a3 = byteHelper.uint8ToByte(127);
|
||||
byte a4 = byteHelper.uint8ToByte(128);
|
||||
byte a5 = byteHelper.uint8ToByte(255);
|
||||
|
||||
assertEquals(a1,b1);
|
||||
assertEquals(a2,b2);
|
||||
assertEquals(a3,b3);
|
||||
assertEquals(a4,b4);
|
||||
assertEquals(a5,b5);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void uint8ToLong() {
|
||||
|
||||
byte b1 = 0x00;
|
||||
byte b2 = 0x01;
|
||||
byte b3 = 0x7f;
|
||||
byte b4 = (byte) 0x80;
|
||||
byte b5 = (byte) 0xff;
|
||||
ByteHelper byteHelper = new ByteHelper();
|
||||
long a1 = byteHelper.uint8ToLong(b1);
|
||||
long a2 = byteHelper.uint8ToLong(b2);
|
||||
long a3 = byteHelper.uint8ToLong(b3);
|
||||
long a4 = byteHelper.uint8ToLong(b4);
|
||||
long a5 = byteHelper.uint8ToLong(b5);
|
||||
|
||||
assertEquals(a1,0);
|
||||
assertEquals(a2,1);
|
||||
assertEquals(a3,127);
|
||||
assertEquals(a4,128);
|
||||
assertEquals(a5,255);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void uint16ToInt() {
|
||||
byte[] b1 = new byte[]{0x00,0x00}; //0
|
||||
byte[] b2 = new byte[]{0x00,0x01}; //1
|
||||
byte[] b3 = new byte[]{0x00,0x7f}; //127
|
||||
byte[] b4 = new byte[]{0x00, (byte) 0x80}; //128
|
||||
byte[] b5 = new byte[]{0x00, (byte) 0xff}; //255
|
||||
byte[] b6 = new byte[]{0x01, 0x00}; //256
|
||||
byte[] b7 = new byte[]{0x7f, (byte) 0xff}; //32767
|
||||
byte[] b8 = new byte[]{(byte) 0x80, 0x00}; //32768
|
||||
byte[] b9 = new byte[]{(byte) 0xff, (byte) 0xff};//65535
|
||||
ByteHelper byteHelper = new ByteHelper();
|
||||
int a1 = byteHelper.uint16ToInt(b1);
|
||||
int a2 = byteHelper.uint16ToInt(b2);
|
||||
int a3 = byteHelper.uint16ToInt(b3);
|
||||
int a4 = byteHelper.uint16ToInt(b4);
|
||||
int a5 = byteHelper.uint16ToInt(b5);
|
||||
int a6 = byteHelper.uint16ToInt(b6);
|
||||
int a7 = byteHelper.uint16ToInt(b7);
|
||||
int a8 = byteHelper.uint16ToInt(b8);
|
||||
int a9 = byteHelper.uint16ToInt(b9);
|
||||
assertEquals(a1,0);
|
||||
assertEquals(a2,1);
|
||||
assertEquals(a3,127);
|
||||
assertEquals(a4,128);
|
||||
assertEquals(a5,255);
|
||||
assertEquals(a6,256);
|
||||
assertEquals(a7,32767);
|
||||
assertEquals(a8,32768);
|
||||
assertEquals(a9,65535);
|
||||
}
|
||||
|
||||
//error
|
||||
@Test
|
||||
public void uint16ToByteArray() {
|
||||
ByteHelper byteHelper = new ByteHelper();
|
||||
byte[] b1 = new byte[]{0x00,0x00}; //0
|
||||
byte[] b2 = new byte[]{0x00,0x01}; //1
|
||||
byte[] b3 = new byte[]{0x00,0x7f}; //127
|
||||
byte[] b4 = new byte[]{0x00, (byte) 0x80}; //128
|
||||
byte[] b5 = new byte[]{0x00, (byte) 0xff}; //255
|
||||
byte[] b6 = new byte[]{0x01, 0x00}; //256
|
||||
byte[] b7 = new byte[]{0x7f, (byte) 0xff}; //32767
|
||||
byte[] b8 = new byte[]{(byte) 0x80, 0x00}; //32768
|
||||
byte[] b9 = new byte[]{(byte) 0xff, (byte) 0xff};//65535
|
||||
|
||||
byte [] a1 = byteHelper.uint16ToByteArray(0);
|
||||
byte [] a2 = byteHelper.uint16ToByteArray(1);
|
||||
byte [] a3 = byteHelper.uint16ToByteArray(127);
|
||||
byte [] a4 = byteHelper.uint16ToByteArray(128);
|
||||
byte [] a5 = byteHelper.uint16ToByteArray(255);
|
||||
byte [] a6 = byteHelper.uint16ToByteArray(256);
|
||||
byte [] a7 = byteHelper.uint16ToByteArray(32767);
|
||||
byte [] a8 = byteHelper.uint16ToByteArray(32768);
|
||||
byte [] a9 = byteHelper.uint16ToByteArray(65535);
|
||||
assertEquals(a1,b1);
|
||||
assertEquals(a2,b2);
|
||||
assertEquals(a3,b3);
|
||||
assertEquals(a4,b4);
|
||||
assertEquals(a5,b5);
|
||||
assertEquals(a6,b6);
|
||||
assertEquals(a7,b7);
|
||||
assertEquals(a8,b8);
|
||||
assertEquals(a9,b9);
|
||||
assertEquals(a6,b6);
|
||||
assertEquals(a7,b7);
|
||||
assertEquals(a8,b8);
|
||||
assertEquals(a9,b9);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void uint16ToLong() {
|
||||
byte[] b1 = new byte[]{0x00,0x00}; //0
|
||||
byte[] b2 = new byte[]{0x00,0x01}; //1
|
||||
byte[] b3 = new byte[]{0x00,0x7f}; //127
|
||||
byte[] b4 = new byte[]{0x00, (byte) 0x80}; //128
|
||||
byte[] b5 = new byte[]{0x00, (byte) 0xff}; //255
|
||||
byte[] b6 = new byte[]{0x01, 0x00}; //256
|
||||
byte[] b7 = new byte[]{0x7f, (byte) 0xff}; //32767
|
||||
byte[] b8 = new byte[]{(byte) 0x80, 0x00}; //32768
|
||||
byte[] b9 = new byte[]{(byte) 0xff, (byte) 0xff};//65535
|
||||
ByteHelper byteHelper = new ByteHelper();
|
||||
long a1 = byteHelper.uint16ToLong(b1);
|
||||
long a2 = byteHelper.uint16ToLong(b2);
|
||||
long a3 = byteHelper.uint16ToLong(b3);
|
||||
long a4 = byteHelper.uint16ToLong(b4);
|
||||
long a5 = byteHelper.uint16ToLong(b5);
|
||||
long a6 = byteHelper.uint16ToLong(b6);
|
||||
long a7 = byteHelper.uint16ToLong(b7);
|
||||
long a8 = byteHelper.uint16ToLong(b8);
|
||||
long a9 = byteHelper.uint16ToLong(b9);
|
||||
assertEquals(a1,0);
|
||||
assertEquals(a2,1);
|
||||
assertEquals(a3,127);
|
||||
assertEquals(a4,128);
|
||||
assertEquals(a5,255);
|
||||
assertEquals(a6,256);
|
||||
assertEquals(a7,32767);
|
||||
assertEquals(a8,32768);
|
||||
assertEquals(a9,65535);
|
||||
}
|
||||
|
||||
//error
|
||||
@Test
|
||||
public void uint32ToLong() {
|
||||
byte[] b1 = new byte[]{0x00,0x00,0x00,0x00}; //0
|
||||
byte[] b2 = new byte[]{0x00,0x00,0x00,0x01}; //1
|
||||
byte[] b3 = new byte[]{0x00,0x00,0x00,0x7f}; //127
|
||||
byte[] b4 = new byte[]{0x00,0x00,0x00, (byte) 0x80}; //128
|
||||
byte[] b5 = new byte[]{0x00,0x00,0x00, (byte) 0xff}; //255
|
||||
byte[] b6 = new byte[]{0x00,0x00,0x01, 0x00}; //256
|
||||
byte[] b7 = new byte[]{0x00,0x00,0x7f, (byte) 0xff}; //32767
|
||||
byte[] b8 = new byte[]{0x00,0x00,(byte) 0x80, 0x00}; //32768
|
||||
byte[] b9 = new byte[]{0x00,0x00,(byte) 0xff, (byte) 0xff}; //65535
|
||||
byte[] b10 = new byte[]{0x00,0x01,0x00,0x00}; //65536
|
||||
byte[] b11 = new byte[]{0x7f,(byte) 0xff,(byte) 0xff,(byte) 0xff};//2147483647
|
||||
byte[] b12 = new byte[]{(byte) 0x80,0x00,0x00,0x00}; //2147483648
|
||||
byte[] b13 = new byte[]{(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff};//4294967295
|
||||
ByteHelper byteHelper = new ByteHelper();
|
||||
long a1 = byteHelper.uint32ToLong(b1);
|
||||
long a2 = byteHelper.uint32ToLong(b2);
|
||||
long a3 = byteHelper.uint32ToLong(b3);
|
||||
long a4 = byteHelper.uint32ToLong(b4);
|
||||
long a5 = byteHelper.uint32ToLong(b5);
|
||||
long a6 = byteHelper.uint32ToLong(b6);
|
||||
long a7 = byteHelper.uint32ToLong(b7);
|
||||
long a8 = byteHelper.uint32ToLong(b8);
|
||||
long a9 = byteHelper.uint32ToLong(b9);
|
||||
long a10 = byteHelper.uint32ToLong(b10);
|
||||
long a11 = byteHelper.uint32ToLong(b11);
|
||||
long a12 = byteHelper.uint32ToLong(b12);
|
||||
long a13 = byteHelper.uint32ToLong(b13);
|
||||
|
||||
assertEquals(a1,0);
|
||||
assertEquals(a2,1);
|
||||
assertEquals(a3,127);
|
||||
assertEquals(a4,128);
|
||||
assertEquals(a5,255);
|
||||
assertEquals(a6,256);
|
||||
assertEquals(a7,32767);
|
||||
assertEquals(a8,32768);
|
||||
assertEquals(a9,65535);
|
||||
assertEquals(a10,65536);
|
||||
assertEquals(a11,2147483647);
|
||||
assertEquals(a12,2147483648L);
|
||||
assertEquals(a13,4294967295L);
|
||||
|
||||
}
|
||||
|
||||
//error
|
||||
@Test
|
||||
public void uint32ToByteArray() {
|
||||
byte[] b1 = new byte[]{0x00,0x00,0x00,0x00}; //0
|
||||
byte[] b2 = new byte[]{0x00,0x00,0x00,0x01}; //1
|
||||
byte[] b3 = new byte[]{0x00,0x00,0x00,0x7f}; //127
|
||||
byte[] b4 = new byte[]{0x00,0x00,0x00, (byte) 0x80}; //128
|
||||
byte[] b5 = new byte[]{0x00,0x00,0x00, (byte) 0xff}; //255
|
||||
byte[] b6 = new byte[]{0x00,0x00,0x01, 0x00}; //256
|
||||
byte[] b7 = new byte[]{0x00,0x00,0x7f, (byte) 0xff}; //32767
|
||||
byte[] b8 = new byte[]{0x00,0x00,(byte) 0x80, 0x00}; //32768
|
||||
byte[] b9 = new byte[]{0x00,0x00,(byte) 0xff, (byte) 0xff}; //65535
|
||||
byte[] b10 = new byte[]{0x00,0x01,0x00,0x00}; //65536
|
||||
byte[] b11 = new byte[]{0x7f,(byte) 0xff,(byte) 0xff,(byte) 0xff};//2147483647
|
||||
byte[] b12 = new byte[]{(byte) 0x80,0x00,0x00,0x00}; //2147483648
|
||||
byte[] b13 = new byte[]{(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff};//4294967295
|
||||
ByteHelper byteHelper = new ByteHelper();
|
||||
byte [] a1 = byteHelper.uint32ToByteArray(0);
|
||||
byte [] a2 = byteHelper.uint16ToByteArray(1);
|
||||
byte [] a3 = byteHelper.uint32ToByteArray(127);
|
||||
byte [] a4 = byteHelper.uint32ToByteArray(128);
|
||||
byte [] a5 = byteHelper.uint32ToByteArray(255);
|
||||
byte [] a6 = byteHelper.uint32ToByteArray(256);
|
||||
byte [] a7 = byteHelper.uint32ToByteArray(32767);
|
||||
byte [] a8 = byteHelper.uint32ToByteArray(32768);
|
||||
byte [] a9 = byteHelper.uint32ToByteArray(65535);
|
||||
byte [] a10 = byteHelper.uint32ToByteArray(65536);
|
||||
byte [] a11 = byteHelper.uint32ToByteArray(2147483647);
|
||||
byte [] a12 = byteHelper.uint32ToByteArray(2147483648l);
|
||||
byte [] a13 = byteHelper.uint32ToByteArray(4294967295l);
|
||||
assertEquals(a1,b1);
|
||||
assertEquals(a2,b2);
|
||||
assertEquals(a3,b3);
|
||||
assertEquals(a4,b4);
|
||||
assertEquals(a5,b5);
|
||||
assertEquals(a6,b6);
|
||||
assertEquals(a7,b7);
|
||||
assertEquals(a8,b8);
|
||||
assertEquals(a9,b9);
|
||||
assertEquals(a6,b6);
|
||||
assertEquals(a7,b7);
|
||||
assertEquals(a8,b8);
|
||||
assertEquals(a9,b9);
|
||||
assertEquals(a10,b10);
|
||||
assertEquals(a11,b11);
|
||||
assertEquals(a12,b12);
|
||||
assertEquals(a13,b13);
|
||||
}
|
||||
|
||||
//error
|
||||
//todo 大于2^64次方无法表示
|
||||
@Test
|
||||
public void uint64ToLong() {
|
||||
byte[] b1 = new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; //0
|
||||
byte[] b2 = new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01}; //1
|
||||
byte[] b3 = new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7f}; //127
|
||||
byte[] b4 = new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,0x00, (byte) 0x80}; //128
|
||||
byte[] b5 = new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,0x00, (byte) 0xff}; //255
|
||||
byte[] b6 = new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,0x01, 0x00}; //256
|
||||
byte[] b7 = new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,0x7f, (byte) 0xff}; //32767
|
||||
byte[] b8 = new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,(byte) 0x80, 0x00}; //32768
|
||||
byte[] b9 = new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,(byte) 0xff, (byte) 0xff}; //65535
|
||||
byte[] b10 = new byte[]{0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00}; //65536
|
||||
byte[] b11 = new byte[]{0x00,0x00,0x00,0x00,0x7f,(byte) 0xff,(byte) 0xff,(byte) 0xff};//2147483647
|
||||
byte[] b12 = new byte[]{0x00,0x00,0x00,0x00,(byte) 0x80,0x00,0x00,0x00}; //2147483648
|
||||
byte[] b13 = new byte[]{0x00,0x00,0x00,0x00,(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff};//4294967295
|
||||
byte[] b14 = new byte[]{0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00};//4294967296
|
||||
byte[] b15 = new byte[]{0x7f,(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff};//9223372036854775807
|
||||
byte[] b16 = new byte[]{(byte) 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00};//9223372036854775808
|
||||
byte[] b17 = new byte[]{(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff};//18446744073709551615
|
||||
ByteHelper byteHelper = new ByteHelper();
|
||||
|
||||
// byte[] b_test = new byte[]{0x00,0x00,0x00,0x00,(byte) 0x80,0x00,0x00,0x01};//2147483649
|
||||
// long a_test = byteHelper.uint64ToLong(b_test);
|
||||
// assertEquals(a_test,2147483649l);
|
||||
|
||||
long a1 = byteHelper.uint64ToLong(b1);
|
||||
long a2 = byteHelper.uint64ToLong(b2);
|
||||
long a3 = byteHelper.uint64ToLong(b3);
|
||||
long a4 = byteHelper.uint64ToLong(b4);
|
||||
long a5 = byteHelper.uint64ToLong(b5);
|
||||
long a6 = byteHelper.uint64ToLong(b6);
|
||||
long a7 = byteHelper.uint64ToLong(b7);
|
||||
long a8 = byteHelper.uint64ToLong(b8);
|
||||
long a9 = byteHelper.uint64ToLong(b9);
|
||||
long a10 = byteHelper.uint64ToLong(b10);
|
||||
long a11 = byteHelper.uint64ToLong(b11);
|
||||
long a12 = byteHelper.uint64ToLong(b12);
|
||||
long a13 = byteHelper.uint64ToLong(b13);
|
||||
long a14 = byteHelper.uint64ToLong(b14);
|
||||
long a15 = byteHelper.uint64ToLong(b15);
|
||||
long a16 = byteHelper.uint64ToLong(b16);
|
||||
long a17 = byteHelper.uint64ToLong(b17);
|
||||
assertEquals(a1,0);
|
||||
assertEquals(a2,1);
|
||||
assertEquals(a3,127);
|
||||
assertEquals(a4,128);
|
||||
assertEquals(a5,255);
|
||||
assertEquals(a6,256);
|
||||
assertEquals(a7,32767);
|
||||
assertEquals(a8,32768);
|
||||
assertEquals(a9,65535);
|
||||
assertEquals(a10,65536);
|
||||
assertEquals(a11,2147483647);
|
||||
|
||||
assertEquals(a12,2147483648L);
|
||||
assertEquals(a13,4294967295L);
|
||||
assertEquals(a14,4294967296L);
|
||||
assertEquals(a15,9223372036854775807L);
|
||||
//待解决:long number too large,暂无法表示
|
||||
// assertEquals(a16,9223372036854775808L);
|
||||
// assertEquals(a17,18446744073709551615L);
|
||||
|
||||
}
|
||||
|
||||
//error
|
||||
//todo 大于2^64次方无法表示
|
||||
@Test
|
||||
public void uint64ToByteArray() {
|
||||
byte[] b1 = new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; //0
|
||||
byte[] b2 = new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01}; //1
|
||||
byte[] b3 = new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7f}; //127
|
||||
byte[] b4 = new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,0x00, (byte) 0x80}; //128
|
||||
byte[] b5 = new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,0x00, (byte) 0xff}; //255
|
||||
byte[] b6 = new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,0x01, 0x00}; //256
|
||||
byte[] b7 = new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,0x7f, (byte) 0xff}; //32767
|
||||
byte[] b8 = new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,(byte) 0x80, 0x00}; //32768
|
||||
byte[] b9 = new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,(byte) 0xff, (byte) 0xff}; //65535
|
||||
byte[] b10 = new byte[]{0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00}; //65536
|
||||
byte[] b11 = new byte[]{0x00,0x00,0x00,0x00,0x7f,(byte) 0xff,(byte) 0xff,(byte) 0xff};//2147483647
|
||||
byte[] b12 = new byte[]{0x00,0x00,0x00,0x00,(byte) 0x80,0x00,0x00,0x00}; //2147483648
|
||||
byte[] b13 = new byte[]{0x00,0x00,0x00,0x00,(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff};//4294967295
|
||||
byte[] b14 = new byte[]{0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00};//4294967296
|
||||
byte[] b15 = new byte[]{0x7f,(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff};//9223372036854775807
|
||||
byte[] b16 = new byte[]{(byte) 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00};//9223372036854775808
|
||||
byte[] b17 = new byte[]{(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff};//18446744073709551615
|
||||
ByteHelper byteHelper = new ByteHelper();
|
||||
byte [] a1 = byteHelper.uint64ToByteArray(0);
|
||||
byte [] a2 = byteHelper.uint64ToByteArray(1);
|
||||
byte [] a3 = byteHelper.uint64ToByteArray(127);
|
||||
byte [] a4 = byteHelper.uint64ToByteArray(128);
|
||||
byte [] a5 = byteHelper.uint64ToByteArray(255);
|
||||
byte [] a6 = byteHelper.uint64ToByteArray(256);
|
||||
byte [] a7 = byteHelper.uint64ToByteArray(32767);
|
||||
byte [] a8 = byteHelper.uint64ToByteArray(32768);
|
||||
byte [] a9 = byteHelper.uint64ToByteArray(65535);
|
||||
byte [] a10 = byteHelper.uint64ToByteArray(65536);
|
||||
byte [] a11 = byteHelper.uint64ToByteArray(2147483647);
|
||||
byte [] a12 = byteHelper.uint64ToByteArray(2147483648l);
|
||||
byte [] a13 = byteHelper.uint64ToByteArray(4294967295l);
|
||||
byte [] a14 = byteHelper.uint64ToByteArray(4294967296L);
|
||||
byte [] a15 = byteHelper.uint64ToByteArray(9223372036854775807L);
|
||||
// byte [] a16 = byteHelper.uint64ToByteArray(9223372036854775808L);
|
||||
// byte [] a17 = byteHelper.uint64ToByteArray(18446744073709551615L);
|
||||
assertEquals(a1,b1);
|
||||
assertEquals(a2,b2);
|
||||
assertEquals(a3,b3);
|
||||
assertEquals(a4,b4);
|
||||
assertEquals(a5,b5);
|
||||
assertEquals(a6,b6);
|
||||
assertEquals(a7,b7);
|
||||
assertEquals(a8,b8);
|
||||
assertEquals(a9,b9);
|
||||
assertEquals(a6,b6);
|
||||
assertEquals(a7,b7);
|
||||
assertEquals(a8,b8);
|
||||
assertEquals(a9,b9);
|
||||
assertEquals(a10,b10);
|
||||
assertEquals(a11,b11);
|
||||
assertEquals(a12,b12);
|
||||
assertEquals(a13,b13);
|
||||
assertEquals(a14,b14);
|
||||
assertEquals(a15,b15);
|
||||
// assertEquals(a16,b16);
|
||||
// assertEquals(a17,b17);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user