mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-10-04 19:42:01 +08:00
db unittest completed(annotations added to be compatible with go version's json)
This commit is contained in:
1 parent
f727a5c505
commit
c43a9a3a1c
7 files changed
+173
-27
No files matched your search
@@ -1,4 +1,109 @@
|
||||
package minsecurity.identity.sqlite.db;
|
||||
|
||||
import minsecurity.Common;
|
||||
import minsecurity.certificate.cert.CertUtils;
|
||||
import minsecurity.certificate.cert.Certificate;
|
||||
import minsecurity.crypto.PrivateKeyInterface;
|
||||
import minsecurity.crypto.sm2.SM2KeyPair;
|
||||
import minsecurity.identity.Identity;
|
||||
import minsecurity.identity.KeyParam;
|
||||
import minsecurity.identity.persist.sqlite.Sqlite;
|
||||
import minsecurity.identity.persist.sqlite.db.Db;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
|
||||
class OperationThread extends Thread{
|
||||
|
||||
private int threadNo = 0;
|
||||
|
||||
private CountDownLatch countDownLatch;
|
||||
|
||||
public OperationThread(CountDownLatch countDownLatch){
|
||||
this.countDownLatch = countDownLatch;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println(String.format("Operation %d starting", this.threadNo));
|
||||
try{
|
||||
// 测试PersistIdentity
|
||||
SM2KeyPair pair = SM2KeyPair.generateKeyPair();
|
||||
Identity identity = new Identity();
|
||||
identity.setName("wzq"+this.threadNo);
|
||||
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);
|
||||
Db.persistIdentity(identity);
|
||||
|
||||
Identity id = Db.getIdentityByNameFromStorage("wzq"+this.threadNo);
|
||||
|
||||
System.out.println("插入身份:" + id.getName());
|
||||
System.out.println(String.format("开始设置 wzq%d 为default 身份", this.threadNo));
|
||||
|
||||
Db.SetDefaultIdentityByNameInStorage("wzq"+this.threadNo);
|
||||
|
||||
Identity id2 = Db.getDefaultIdentityFromStorage();
|
||||
System.out.println("当前default身份:" + id2.getName());
|
||||
}catch (Exception ex){
|
||||
System.out.println(String.format("Thread %d: %s", this.threadNo, ex.getMessage()));
|
||||
}
|
||||
countDownLatch.countDown();
|
||||
}
|
||||
|
||||
public int getThreadNo() {
|
||||
return threadNo;
|
||||
}
|
||||
|
||||
public void setThreadNo(int threadNo) {
|
||||
this.threadNo = threadNo;
|
||||
}
|
||||
}
|
||||
public class DBTest {
|
||||
@Test
|
||||
public void testDatabaseWithHighConcurrency(){
|
||||
try {
|
||||
Sqlite.getInstance().openDefault();
|
||||
CountDownLatch countDownLatch = new CountDownLatch(1);
|
||||
for (int i = 1; i <= 1; i++){
|
||||
OperationThread t = new OperationThread(countDownLatch);
|
||||
t.setThreadNo(i);
|
||||
t.start();
|
||||
}
|
||||
|
||||
countDownLatch.await();
|
||||
List<Identity> identities = Db.getAllIdentityFromStorage();
|
||||
System.out.println(identities.get(0).getPrikeyRawByte());
|
||||
identities.get(0).unLock("0123456789abcdef", Common.SM4ECB);
|
||||
PrivateKeyInterface id = identities.get(0).getPrikey();
|
||||
System.out.println(identities.get(0).getPrikey().getBytes());
|
||||
}catch (Exception ex){
|
||||
System.out.println(ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user