fix:IdentifyManager的小bug(异常值判断)

fix:引入mgmt.Exception(解决编译错误)
add:IdentifyManager单元测试
This commit is contained in:
ChessNineeee
2021-04-14 16:51:14 +08:00
parent 0dc6bb3165
commit bbee7f879e
13 changed files with 417 additions and 13 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
package component;
import com.sun.org.apache.regexp.internal.RE;
// import com.sun.org.apache.regexp.internal.RE;
import encoding.*;
import packet.LpPacket;
+3 -2
View File
@@ -2,6 +2,7 @@ package encoding;
import component.CanBePrefix;
import component.ComponentException;
import mgmt.MgmtException;
import packet.PacketException;
/*
@@ -46,7 +47,7 @@ public class SelfEncodingBase {
* @param iEncodingAble
* @return
*/
public Block selfWireEncode(IEncodingAble iEncodingAble) throws EncoderException, BlockException, ComponentException, PacketException {
public Block selfWireEncode(IEncodingAble iEncodingAble) throws EncoderException, BlockException, ComponentException, PacketException, MgmtException {
// this.setiEncodingAble(iEncodingAble);
if((this.rawBlock!=null)&&this.rawBlock.isValid()){
return this.rawBlock;
@@ -66,7 +67,7 @@ public class SelfEncodingBase {
* @param iEncodingAble
* @return
*/
private Block easyEncoder(IEncodingAble iEncodingAble) throws PacketException, ComponentException, EncoderException, BlockException {
private Block easyEncoder(IEncodingAble iEncodingAble) throws PacketException, ComponentException, EncoderException, BlockException, MgmtException {
Encoder encoder=new Encoder();
// 首先测量目标 TLV 的大小
@@ -246,6 +246,9 @@ public class CertUtils {
* @date 2021/3/7
**/
public static String toPem(Certificate certificate, byte[] passwd, int symAlgoMode) throws CertException, NoSuchPaddingException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, IllegalBlockSizeException, BadPaddingException, NoSuchProviderException, InvalidKeyException {
// 证书为空 返回空字符串
if (certificate == null)
return "";
if(certificate.getSignature() == null){
throw new CertException("该证书未签名");
}
@@ -134,6 +134,9 @@ public class Sqlite {
*/
public Connection getConn() throws Exception {
Connection c = null;
// 当未打开数据库获取连接时打开默认数据库获取连接
if ("".equals(db_path))
this.openDefault();
try{
String real_db_file = db_path + db_file;
// Class.forName("org.sqlite.JDBC");
+2 -1
View File
@@ -3,6 +3,7 @@ package packet;
import com.sun.xml.internal.ws.api.message.Packet;
import component.*;
import encoding.*;
import mgmt.MgmtException;
/*
* @Author: Wang Feng
@@ -177,7 +178,7 @@ public class CPacket implements InteractWithField, IEncodingAble {
minPacket.identifierField.addIdentifier(dstIdentifierWrapper);
return true;
} catch (EncoderException | BlockException | ComponentException e) {
} catch (EncoderException | BlockException | ComponentException | MgmtException e) {
throw new PacketException("CPacket.fillDataToFields: " + e.getMessage());
}
}
+2 -1
View File
@@ -2,6 +2,7 @@ package packet;
import component.*;
import encoding.*;
import mgmt.MgmtException;
/*
* @Author: Wang Feng
@@ -170,7 +171,7 @@ public class Data implements IEncodingAble, InteractWithField {
}
minPacket.identifierField.addIdentifier(identifierWrapper);
return true;
} catch (EncoderException | BlockException | ComponentException e) {
} catch (EncoderException | BlockException | ComponentException | MgmtException e) {
throw new PacketException("Data.fillDataToFields: " + e.getMessage());
}
}
+2 -1
View File
@@ -2,6 +2,7 @@ package packet;
import component.*;
import encoding.*;
import mgmt.MgmtException;
/*
* @Author: Wang Feng
@@ -254,7 +255,7 @@ public class Interest implements InteractWithField, IEncodingAble {
minPacket.identifierField.addIdentifier(identifierWrapper);
return true;
} catch (EncoderException | BlockException | ComponentException e) {
} catch (EncoderException | BlockException | ComponentException | MgmtException e) {
throw new PacketException("Interest.fillDataToFields: " + e.getMessage());
}
}
+26 -2
View File
@@ -77,6 +77,8 @@ public class IdentifyManager {
* @date 2021/3/11
**/
public Identity getIdentityByName(String name){
if (name == null)
return null;
return identifies.get(name);
}
@@ -90,6 +92,12 @@ public class IdentifyManager {
* @date 2021/3/11
**/
public boolean deleteIdentityByName(String name, boolean commit) throws Exception {
if (name == null)
return false;
// 内存中不存在该身份 不去数据库中操作
if (!this.identifies.containsKey(name))
return false;
// 从内存中删除掉它
this.identifies.remove(name);
// 从sqlite中删除掉它
@@ -110,7 +118,9 @@ public class IdentifyManager {
* @date 2021/3/11
**/
public boolean saveIdentity(Identity newIdentity, boolean force, boolean commit) throws Exception {
// identifies.con
if (newIdentity == null)
return false;
if(identifies.containsKey(newIdentity.getName()) && !force) {
return false;
}
@@ -140,6 +150,9 @@ public class IdentifyManager {
* @date 2021/3/11
**/
public Identity createIdentityByNameAndKeyParam(String name, KeyParam keyParam, String passwd, boolean commit) throws Exception {
if (name == null || keyParam == null)
return null;
if(identifies.containsKey(name)){
return null;
}
@@ -189,8 +202,17 @@ public class IdentifyManager {
public boolean setDefaultIdentity(Identity identity, boolean commit) throws Exception {
if (identity == null)
return false;
this.defaultIdentity = identity;
// 如果身份不存在,则先将其存储到内存与数据库再设置默认身份
if (!identifies.containsKey(identity.getName())){
saveIdentity(identity, true, true);
// 保存成功后缓存到内存中
identifies.put(identity.getName(), identity);
}
Persist.setDefaultIdentityByNameInStorage(identity.getName());
// 数据库设置成功后再设置内存中数据
this.defaultIdentity = identity;
return true;
}
@@ -202,6 +224,8 @@ public class IdentifyManager {
* @date 2021/3/11
**/
public boolean existIdentity(String name){
if (name == null)
return false;
return identifies.containsKey(name);
}
+2 -1
View File
@@ -1,6 +1,7 @@
package component;
import encoding.*;
import mgmt.MgmtException;
import org.checkerframework.checker.units.qual.K;
import org.junit.Test;
import packet.PacketException;
@@ -86,7 +87,7 @@ public class KeyLocatorTest {
}
@Test
public void TestKeyLocator_WireDecode() throws ComponentException, BlockException, EncoderException, PacketException {
public void TestKeyLocator_WireDecode() throws ComponentException, BlockException, EncoderException, PacketException, MgmtException {
Identifier gotIdentifier=new Identifier("/min/pku/111");
KeyLocator gotKey=new KeyLocator(gotIdentifier);
System.out.println(gotKey.getIdentifier().toUri());
@@ -1,6 +1,7 @@
package component;
import encoding.*;
import mgmt.MgmtException;
import org.junit.Test;
import packet.PacketException;
@@ -35,7 +36,7 @@ public class SignatureTypeTest {
}
@Test
public void TestCreateSignatureTypeByValue() throws PacketException, EncoderException, BlockException, ComponentException {
public void TestCreateSignatureTypeByValue() throws PacketException, EncoderException, BlockException, ComponentException, MgmtException {
SignatureAlgorithm value=new SignatureAlgorithm(0);
SignatureType got=new SignatureType(value);
System.out.println(got.getValue().getSignatureAlgorithm());
@@ -44,7 +45,7 @@ public class SignatureTypeTest {
}
@Test
public void TestSignatureType_WireEncode() throws PacketException, EncoderException, BlockException, ComponentException {
public void TestSignatureType_WireEncode() throws PacketException, EncoderException, BlockException, ComponentException, MgmtException {
SignatureAlgorithm value=new SignatureAlgorithm(0);
SignatureType got=new SignatureType(value);
System.out.println(got.getValue().getSignatureAlgorithm());
@@ -58,7 +59,7 @@ public class SignatureTypeTest {
}
@Test
public void TestCreateSignatureTypeByValue2() throws PacketException, EncoderException, BlockException, ComponentException {
public void TestCreateSignatureTypeByValue2() throws PacketException, EncoderException, BlockException, ComponentException, MgmtException {
SignatureAlgorithm value=new SignatureAlgorithm(0);
SignatureType got=new SignatureType(value);
System.out.println(got.getValue().getSignatureAlgorithm());
@@ -5,6 +5,7 @@ import minsecurity.certificate.cert.CertUtils;
import minsecurity.certificate.cert.Certificate;
import minsecurity.crypto.TestSM2;
import minsecurity.crypto.sm2.SM2Base;
import minsecurity.crypto.sm2.SM2KeyPair;
import minsecurity.crypto.sm2.SM2PrivateKey;
import minsecurity.crypto.sm2.SM2PublicKey;
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
@@ -25,6 +26,42 @@ import org.slf4j.LoggerFactory;
public class TestIdentity {
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(TestIdentity.class);
/**
* 随机生成身份数据
* @return Identity
*/
private Identity createRandomIdentity() throws Exception{
// 测试PersistIdentity
SM2KeyPair pair = SM2KeyPair.generateKeyPair();
Identity identity = new Identity();
identity.setName("wzq"+Math.random());
KeyParam keyParam = new KeyParam();
keyParam.PublicKeyAlgorithm = 0;
keyParam.SignatureAlgorithm = 0;
identity.setKeyParam(keyParam);
identity.setPrikey(pair.getSm2PrivateKey());
identity.setPubkey(pair.getSm2PublicKey());
identity.setPasswd("2DD29CA851E7B56E4697B0E1F08507293D761A05CE4D1B628663F411A8086D99");
identity.lock("0123456789abcdef", Common.SM4ECB);
Certificate cert = new Certificate();
cert.setVersion(1);
cert.setSerialNumber(1);
cert.setPublicKey(pair.getSm2PublicKey());
cert.setSignatureAlgorithm(Common.SM3withSM2); // TODO 名字有误? SM2withSM3?
cert.setPublicKeyAlgorithm(Common.SM2);
cert.setIssueTo("root");
cert.setIssuer("root");
long timestamp = System.currentTimeMillis() / 1000;
cert.setTimestamp(timestamp); // 10bit timestamp
cert.setNotAfter(timestamp); // 10bit timestamp
cert.setNotBefore(timestamp + 1000); // 10bit timestamp
cert.setKeyUsage(Common.CertSign);
cert.setCA(true);
CertUtils.signCert(cert, pair.getSm2PrivateKey());
identity.setCert(cert);
return identity;
}
@Test
public void testIdentity1() throws Exception {
+2 -1
View File
@@ -6,6 +6,7 @@ import encoding.Block;
import encoding.BlockException;
import encoding.EncoderException;
import encoding.SelfEncodingBase;
import mgmt.MgmtException;
import packet.PacketException;
/*
@@ -16,7 +17,7 @@ import packet.PacketException;
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class TestCallBack {
public static void main(String[] args) throws EncoderException, BlockException, PacketException, ComponentException {
public static void main(String[] args) throws EncoderException, BlockException, PacketException, ComponentException, MgmtException {
CanBePrefix canBePrefix=new CanBePrefix();
Block block=new SelfEncodingBase().selfWireEncode(canBePrefix);
}
@@ -0,0 +1,330 @@
package security;
import minsecurity.Common;
import minsecurity.certificate.cert.CertUtils;
import minsecurity.certificate.cert.Certificate;
import minsecurity.crypto.sm2.SM2KeyPair;
import minsecurity.identity.Identity;
import minsecurity.identity.KeyParam;
import minsecurity.identity.TestIdentity;
import minsecurity.identity.persist.sqlite.Sqlite;
import org.junit.Test;
import org.slf4j.LoggerFactory;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
public class IdentifyManagerTest {
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(TestIdentity.class);
/**
* 随机生成身份数据
* @return Identity
*/
private Identity createRandomIdentity() throws Exception{
// 测试PersistIdentity
SM2KeyPair pair = SM2KeyPair.generateKeyPair();
Identity identity = new Identity();
identity.setName("wzq"+Math.random());
KeyParam keyParam = new KeyParam();
keyParam.PublicKeyAlgorithm = 0;
keyParam.SignatureAlgorithm = 0;
identity.setKeyParam(keyParam);
identity.setPrikey(pair.getSm2PrivateKey());
identity.setPubkey(pair.getSm2PublicKey());
identity.setPasswd("2DD29CA851E7B56E4697B0E1F08507293D761A05CE4D1B628663F411A8086D99");
identity.lock("0123456789abcdef", Common.SM4ECB);
Certificate cert = new Certificate();
cert.setVersion(1);
cert.setSerialNumber(1);
cert.setPublicKey(pair.getSm2PublicKey());
cert.setSignatureAlgorithm(Common.SM3withSM2); // TODO 名字有误? SM2withSM3?
cert.setPublicKeyAlgorithm(Common.SM2);
cert.setIssueTo("root");
cert.setIssuer("root");
long timestamp = System.currentTimeMillis() / 1000;
cert.setTimestamp(timestamp); // 10bit timestamp
cert.setNotAfter(timestamp); // 10bit timestamp
cert.setNotBefore(timestamp + 1000); // 10bit timestamp
cert.setKeyUsage(Common.CertSign);
cert.setCA(true);
CertUtils.signCert(cert, pair.getSm2PrivateKey());
identity.setCert(cert);
return identity;
}
@Test
public void testIdentifyManagerInit(){
try{
// 打开数据库
// Sqlite.getInstance().openDefault();
IdentifyManager manager = new IdentifyManager();
logger.debug(String.format("默认身份:%s", manager.getDefaultIdentity().getName()));
logger.debug(String.format("身份数量:%d", manager.getIdentifies().size()));
logger.debug(String.format("私钥加密算法:%d", manager.getPrivateKeyEncryptionAlgorithm()));
}catch (Exception ex){
logger.debug(ex.getMessage());
}
}
@Test
public void testGetIdentityByName(){
try{
IdentifyManager manager = new IdentifyManager();
ConcurrentMap<String, Identity> hashMap = manager.getIdentifies();
Set<String> keySet = hashMap.keySet();
// 查询所有manager中的用户
for (String s:
keySet) {
logger.debug(String.format("查询身份:%s,结果身份:%s", s, manager.getIdentityByName(s).getName()));
}
}catch (Exception ex){
logger.debug(ex.getMessage());
}
}
@Test
public void testGetIdentityByName2(){
try{
IdentifyManager manager = new IdentifyManager();
logger.debug(String.format("身份数量:%d", manager.getIdentifies().size()));
ConcurrentMap<String, Identity> hashMap = manager.getIdentifies();
Set<String> keySet = hashMap.keySet();
for (String s:
keySet) {
// 查询不存在的名称 返回null
String sSet = s + "test";
logger.debug(String.format("查询身份:%s,结果身份:%s", sSet, manager.getIdentityByName(sSet)));
// 查询null 抛出异常 已改为返回null
sSet = null;
logger.debug(String.format("查询身份:%s,结果身份:%s", sSet, manager.getIdentityByName(sSet)));
}
logger.debug(String.format("身份数量:%d", manager.getIdentifies().size()));
}catch (Exception ex){
logger.debug(ex.getMessage());
}
}
@Test
public void testDeleteIdentityByName2(){
try{
IdentifyManager manager = new IdentifyManager();
logger.debug(String.format("身份数量:%d", manager.getIdentifies().size()));
ConcurrentMap<String, Identity> hashMap = manager.getIdentifies();
Set<String> keySet = hashMap.keySet();
for (String s:
keySet) {
// 删除不存在的名称 返回false
String sSet = s + "test";
logger.debug(String.format("删除身份:%s,结果:%s", sSet, manager.deleteIdentityByName(sSet, true)));
// 查询null 抛出异常 已改为返回false
sSet = null;
logger.debug(String.format("删除身份:%s,结果:%s", sSet, manager.deleteIdentityByName(sSet, true)));
}
logger.debug(String.format("身份数量:%d", manager.getIdentifies().size()));
}catch (Exception ex){
logger.debug(ex.getMessage());
}
}
@Test
public void testDeleteIdentityByName(){
try{
IdentifyManager manager = new IdentifyManager();
logger.debug(String.format("身份数量:%d", manager.getIdentifies().size()));
ConcurrentMap<String, Identity> hashMap = manager.getIdentifies();
Set<String> keySet = hashMap.keySet();
// 删除所有manager中的用户
for (String s:
keySet) {
logger.debug(String.format("删除身份:%s,结果:%s", s, manager.deleteIdentityByName(s, true)));
logger.debug(String.format("查询身份:%s,结果身份:%s", s, manager.getIdentityByName(s)));
}
logger.debug(String.format("身份数量:%d", manager.getIdentifies().size()));
}catch (Exception ex){
logger.debug(ex.getMessage());
}
}
@Test
public void testSaveIdentity(){
try{
IdentifyManager manager = new IdentifyManager();
logger.debug(String.format("身份数量:%d", manager.getIdentifies().size()));
// 随机生成身份并保存 重复五次
for (int i = 0; i < 5; i++) {
Identity id = createRandomIdentity();
// todo 确认是否需要强制覆盖?
logger.debug(String.format("保存身份:%s,结果:%s", id.getName(), manager.saveIdentity(id, true, true)));
}
logger.debug(String.format("身份数量:%d", manager.getIdentifies().size()));
}catch (Exception ex){
logger.debug(ex.getMessage());
}
}
@Test
public void testSaveIdentity2(){
try{
IdentifyManager manager = new IdentifyManager();
logger.debug(String.format("身份数量:%d", manager.getIdentifies().size()));
// 插入 null 重复五次
for (int i = 0; i < 5; i++) {
Identity id = null;
logger.debug(String.format("保存身份:%s,结果:%s", id, manager.saveIdentity(id, true, true)));
}
logger.debug(String.format("身份数量:%d", manager.getIdentifies().size()));
}catch (Exception ex){
logger.debug(ex.getMessage());
}
}
@Test
public void testCreateIdentityByNameAndKeyParam(){
try{
IdentifyManager manager = new IdentifyManager();
logger.debug(String.format("身份数量:%d", manager.getIdentifies().size()));
// 随机生成名称并保存 重复五次
for (int i = 0; i < 5; i++) {
String s = "wzq"+Math.random();
KeyParam keyParam = new KeyParam();
keyParam.PublicKeyAlgorithm = 0;
keyParam.SignatureAlgorithm = 0;
// todo 确认身份在没有证书的情况下能否存储
logger.debug(String.format("保存身份:%s,结果:%s", s, manager.createIdentityByNameAndKeyParam(s, keyParam, "1234", true)));
}
logger.debug(String.format("身份数量:%d", manager.getIdentifies().size()));
}catch (Exception ex){
logger.debug(ex.getMessage());
}
}
@Test
public void testCreateIdentityByName(){
try{
IdentifyManager manager = new IdentifyManager();
logger.debug(String.format("身份数量:%d", manager.getIdentifies().size()));
// 随机生成名称并保存 重复五次
for (int i = 0; i < 5; i++) {
String s = "wzq"+Math.random();
logger.debug(String.format("保存身份:%s,结果:%s", s, manager.createIdentityByName(s, "1234", true)));
}
logger.debug(String.format("身份数量:%d", manager.getIdentifies().size()));
}catch (Exception ex){
logger.debug(ex.getMessage());
}
}
@Test
public void testCreateIdentityByName2(){
try{
IdentifyManager manager = new IdentifyManager();
logger.debug(String.format("身份数量:%d", manager.getIdentifies().size()));
// 保存null 重复五次
for (int i = 0; i < 5; i++) {
String s = null;
// 保存失败 返回null
logger.debug(String.format("保存身份:%s,结果:%s", s, manager.createIdentityByName(s, "1234", true)));
}
logger.debug(String.format("身份数量:%d", manager.getIdentifies().size()));
}catch (Exception ex){
logger.debug(ex.getMessage());
}
}
@Test
public void testCreateIdentityByNameAndKeyParam2(){
try{
IdentifyManager manager = new IdentifyManager();
logger.debug(String.format("身份数量:%d", manager.getIdentifies().size()));
// 保存null 重复五次
for (int i = 0; i < 5; i++) {
String s = null;
// 不会插入 返回null
logger.debug(String.format("保存身份:%s,结果:%s", s, manager.createIdentityByName(s, "1234", true)));
}
logger.debug(String.format("身份数量:%d", manager.getIdentifies().size()));
}catch (Exception ex){
logger.debug(ex.getMessage());
}
}
@Test
public void testSetDefaultIdentity(){
try{
IdentifyManager manager = new IdentifyManager();
logger.debug(String.format("身份数量:%d", manager.getIdentifies().size()));
ConcurrentMap<String, Identity> hashMap = manager.getIdentifies();
Set<String> keySet = hashMap.keySet();
// 轮流设置默认身份
for (String s:
keySet) {
logger.debug(String.format("设置身份:%s,结果:%s", s, manager.setDefaultIdentity(manager.getIdentifies().get(s), true)));
logger.debug(String.format("查询默认身份:%s", manager.getDefaultIdentity()));
}
logger.debug(String.format("身份数量:%d", manager.getIdentifies().size()));
}catch (Exception ex){
logger.debug(ex.getMessage());
}
}
@Test
public void testSetDefaultIdentity2(){
try{
IdentifyManager manager = new IdentifyManager();
logger.debug(String.format("身份数量:%d", manager.getIdentifies().size()));
ConcurrentMap<String, Identity> hashMap = manager.getIdentifies();
Set<String> keySet = hashMap.keySet();
// 轮流设置默认身份 设置的身份原本不存在
for (String s:
keySet) {
Identity id = manager.getIdentifies().get(s);
String sNew = s + "test";
id.setName(sNew);
logger.debug(String.format("设置身份:%s,结果:%s", sNew, manager.setDefaultIdentity(id, true)));
logger.debug(String.format("查询默认身份:%s", manager.getDefaultIdentity()));
}
logger.debug(String.format("身份数量:%d", manager.getIdentifies().size()));
}catch (Exception ex){
logger.debug(ex.getMessage());
}
}
@Test
public void testExistIdentity(){
try{
IdentifyManager manager = new IdentifyManager();
logger.debug(String.format("身份数量:%d", manager.getIdentifies().size()));
ConcurrentMap<String, Identity> hashMap = manager.getIdentifies();
Set<String> keySet = hashMap.keySet();
// 正常查询身份是否存在
for (String s:
keySet) {
String sSet = "test" + s;
logger.debug(String.format("查询身份:%s 是否存在, 结果:%s", s, manager.existIdentity(s)));
logger.debug(String.format("查询身份:%s 是否存在, 结果:%s", sSet, manager.existIdentity(sSet)));
}
logger.debug(String.format("身份数量:%d", manager.getIdentifies().size()));
}catch (Exception ex){
logger.debug(ex.getMessage());
}
}
@Test
public void testExistIdentity2(){
try{
IdentifyManager manager = new IdentifyManager();
logger.debug(String.format("身份数量:%d", manager.getIdentifies().size()));
ConcurrentMap<String, Identity> hashMap = manager.getIdentifies();
Set<String> keySet = hashMap.keySet();
// 查询 null 是否存在
for (String s:
keySet) {
logger.debug(String.format("查询身份:%s 是否存在, 结果:%s", null, manager.existIdentity(null)));
}
logger.debug(String.format("身份数量:%d", manager.getIdentifies().size()));
}catch (Exception ex){
logger.debug(ex.getMessage());
}
}
}