提交 f5cfa3d0 编写于 作者: Q qiaozzzh

add

Signed-off-by: Nqiaozzzh <qiaozhang@huawei.com>
上级 4440b413
......@@ -17,10 +17,18 @@ import { describe, beforeAll, afterEach, it, expect } from "@ohos/hypium";
import {
testHMACDigestPromise,
testMDDigestPromise,
testHMACErrorAlgorithm,
testHMACDigestPromiseErrorKey,
testHMACDigestPromiseDatablobNull,
testHMACDigestPromiseDatablobLong,
} from "./utils/digestalgorithm/publicDigestPromise";
import {
testHMACDigestCallback,
testMDDigestCallback,
testMDErrorAlgorithm,
testMDErrorAlgorithmNull,
testMDDigestCallbackLen,
testMDDigestCallbackLenNull,
} from "./utils/digestalgorithm/publicDigestCallback";
export default function DigestAlgorithmJsunit() {
......@@ -46,7 +54,56 @@ export default function DigestAlgorithmJsunit() {
});
/**
* @tc.number Security_crypto_framework_MD_0500
* @tc.number Security_crypto_framework_MD_0200
* @tc.name Test MD5 for MD digest algotithm
* @tc.desc Use the Promise Style of Interface
*/
it("Security_crypto_framework_MD_0200", 0, async function (done) {
await testMDDigestPromise("SHA1")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_MD_0300
* @tc.name Test MD5 for MD digest algotithm
* @tc.desc Use the Promise Style of Interface
*/
it("Security_crypto_framework_MD_0300", 0, async function (done) {
await testMDDigestPromise("SHA224")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_MD_0400
* @tc.name Test SHA384 for MD digest algotithm
* @tc.desc Use the Callback Style of Interface
*/
it("Security_crypto_framework_MD_0400", 0, async function (done) {
await testMDDigestCallback("SHA256")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_MD_
*
* @tc.name Test SHA384 for MD digest algotithm
* @tc.desc Use the Callback Style of Interface
*/
......@@ -61,6 +118,86 @@ export default function DigestAlgorithmJsunit() {
done();
});
/**
* @tc.number Security_crypto_framework_MD_0600
* @tc.name Test SHA384 for MD digest algotithm
* @tc.desc Use the Callback Style of Interface
*/
it("Security_crypto_framework_MD_0600", 0, async function (done) {
await testMDDigestCallback("SHA512")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_MD_0700
* @tc.name Test SHA384 for MD digest algotithm
* @tc.desc Use the Callback Style of Interface
*/
it("Security_crypto_framework_MD_0700", 0, async function (done) {
await testMDErrorAlgorithm("SHA5")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_MD_0800
* @tc.name Test SHA384 for MD digest algotithm
* @tc.desc Use the Callback Style of Interface
*/
it("Security_crypto_framework_MD_0800", 0, async function (done) {
await testMDErrorAlgorithmNull(null)
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_MD_0900
* @tc.name Test SHA384 for MD digest algotithm
* @tc.desc Use the Callback Style of Interface
*/
it("Security_crypto_framework_MD_0900", 0, async function (done) {
await testMDDigestCallbackLen("SHA224", 10000)
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_MD_1000
* @tc.name Test SHA384 for MD digest algotithm
* @tc.desc Use the Callback Style of Interface
*/
it("Security_crypto_framework_MD_1000", 0, async function (done) {
await testMDDigestCallbackLenNull("SHA224")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_HMAC_0100
* @tc.name Test SHA1 for HMAC digest algorithm
......@@ -92,5 +229,117 @@ export default function DigestAlgorithmJsunit() {
});
done();
});
/**
* @tc.number Security_crypto_framework_HMAC_0300
* @tc.name Test SHA1 for HMAC digest algorithm
* @tc.desc Use the Callback Style of Interface
*/
it("Security_crypto_framework_HMAC_0300", 0, async function (done) {
await testHMACDigestCallback("SHA256", "AES256")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_HMAC_0400
* @tc.name Test SHA1 for HMAC digest algorithm
* @tc.desc Use the Promise Style of Interface
*/
it("Security_crypto_framework_HMAC_0400", 0, async function (done) {
await testHMACDigestPromise("SHA384", "3DES192")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_HMAC_0500
* @tc.name Test SHA1 for HMAC digest algorithm
* @tc.desc Use the Promise Style of Interface
*/
it("Security_crypto_framework_HMAC_0500", 0, async function (done) {
await testHMACDigestPromise("SHA512", "3DES192")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_HMAC_0600
* @tc.name Test SHA1 for HMAC digest algorithm
* @tc.desc Use the Promise Style of Interface
*/
it("Security_crypto_framework_HMAC_0600", 0, async function (done) {
await testHMACErrorAlgorithm("SHA5", null)
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_HMAC_0700
* @tc.name Test SHA1 for HMAC digest algorithm
* @tc.desc Use the Promise Style of Interface
*/
it("Security_crypto_framework_HMAC_0700", 0, async function (done) {
await testHMACDigestPromiseErrorKey("SHA512", "RSA1024|PRIMES_2")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_HMAC_0800
* @tc.name Test SHA1 for HMAC digest algorithm
* @tc.desc Use the Promise Style of Interface
*/
it("Security_crypto_framework_HMAC_0800", 0, async function (done) {
await testHMACDigestPromiseDatablobNull("SHA512", "3DES192")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_HMAC_0900
* @tc.name Test SHA1 for HMAC digest algorithm
* @tc.desc Use the Promise Style of Interface
*/
it("Security_crypto_framework_HMAC_0900", 0, async function (done) {
await testHMACDigestPromiseDatablobLong("SHA512", "3DES192", 10000)
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
});
}
// @ts-nocheck
/*
* Copyright (C) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
......@@ -18,7 +19,11 @@ import {
testSecurityRandomPromise,
testSecurityRandomEnumPromise,
} from "./utils/securityrandom/publicSecurityRandomPromise";
import { testSecurityRandomCallback } from "./utils/securityrandom/publicSecurityRandomCallback";
import {
testSecurityRandomCallback,
testSecurityRandomLengthCallback,
testSecurityRandomCallbackSeed,
} from "./utils/securityrandom/publicSecurityRandomCallback";
export default function SecurityRandomJsunit() {
describe("SecurityRandomJsunit", function () {
......@@ -58,6 +63,73 @@ export default function SecurityRandomJsunit() {
done();
});
/**
* @tc.number Security_crypto_framework_Random_0300
* @tc.name support security random and set random seed
* @tc.desc the length of random is 32, use callback style of interface
*/
it("Security_crypto_framework_Random_0300", 0, async function (done) {
await testSecurityRandomLengthCallback(0)
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
await testSecurityRandomLengthCallback(null)
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
await testSecurityRandomLengthCallback(-1)
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
await testSecurityRandomLengthCallback(2147483647)
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_Random_0400
* @tc.name support security random and set random seed
* @tc.desc the length of random is 32, use callback style of interface
*/
it("Security_crypto_framework_Random_0400", 0, async function (done) {
await testSecurityRandomCallbackSeed(null)
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
await testSecurityRandomCallbackSeed(0)
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
await testSecurityRandomCallbackSeed(2147483647)
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_Random_0500
* @tc.name test enumerated values
......
......@@ -148,6 +148,113 @@ async function testMDDigestCallback(MDAlgoName) {
});
}
async function testMDDigestCallbackLen(MDAlgoName, DatablobLen) {
var globalMd;
var i;
var globalText;
var t = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefhijklmnopqrstuvwxyz",n = t.length,s="";
for (i = 0; i < DatablobLen; i++){
globalText += t.charAt(Math.floor(Math.random() * n));
}
console.warn("Datablob = " + globalText);
var ginBlob = {
data: stringTouInt8Array(globalText),
};
return new Promise((resolve, reject) => {
globalMd = cryptoFramework.createMd(MDAlgoName);
expect(globalMd != null).assertTrue();
console.warn("md= " + globalMd);
console.warn("MD algName is: " + globalMd.algName);
updateMd(globalMd, ginBlob)
.then((updateData) => {
expect(updateData === "update success").assertTrue();
return digestMd(globalMd);
})
.then((digestBlob) => {
console.warn(
"[callback]: digest result: " + uInt8ArrayToShowStr(digestBlob.data)
);
let mdLen = globalMd.getMdLength();
console.warn("Md len: " + mdLen);
expect(digestBlob != null && mdLen != 0 && mdLen != null).assertTrue();
resolve();
})
.catch((err) => {
console.error("testMDDigestCallback catch error: " + err);
reject(err);
});
});
}
async function testMDDigestCallbackLenNull(MDAlgoName) {
var globalMd;
var globalText1 = null;
var globalText2 = 0;
var inBlob = {
data: stringTouInt8Array(globalText2),
};
return new Promise((resolve, reject) => {
globalMd = cryptoFramework.createMd(MDAlgoName);
expect(globalMd != null).assertTrue();
console.warn("md= " + globalMd);
console.warn("MD algName is: " + globalMd.algName);
updateMd(globalMd, globalText1)
.then((updateData) => {
console.warn("updateData: " + updateData);
})
.catch((err) => {
console.error("testMDDigestCallback catch error1: " + err);
expect(err == "Error: inBlob is null").assertTrue();
});
updateMd(globalMd, inBlob)
.then((updateData) => {
console.warn("updateData: " + updateData);
reject();
})
.catch((err) => {
console.error("testMDDigestCallback catch error2: " + err);
expect(err == "Error: inBlob is null").assertTrue();
resolve();
});
});
}
async function testMDErrorAlgorithm(MDAlgoName) {
var globalMd;
return new Promise((resolve, reject) => {
console.warn("md= test begin");
try {
globalMd = cryptoFramework.createMd(MDAlgoName);
reject();
} catch (error) {
console.error("[Promise]: error code: " + error.code + ", message is: " + error.message);
expect(error.code == 801).assertTrue();
resolve();
}
});
}
async function testMDErrorAlgorithmNull(MDAlgoName) {
var globalMd;
return new Promise((resolve, reject) => {
try {
globalMd = cryptoFramework.createMd(MDAlgoName);
reject();
} catch (error) {
console.error("[Promise]: error code: " + error.code + ", message is: " + error.message);
expect(error.code == 401).assertTrue();
resolve();
}
});
}
async function testHMACDigestCallback(HMACAlgoName, keyAlgoName) {
var globalHMAC;
var globalText = "my test data";
......@@ -193,4 +300,5 @@ async function testHMACDigestCallback(HMACAlgoName, keyAlgoName) {
});
}
export { testMDDigestCallback, testHMACDigestCallback };
export { testMDDigestCallback, testHMACDigestCallback, testMDErrorAlgorithm,
testMDErrorAlgorithmNull, testMDDigestCallbackLen, testMDDigestCallbackLenNull };
......@@ -60,7 +60,7 @@ async function testHMACDigestPromise(HMACAlgoName, keyAlgoName) {
var globalHMAC;
var globalText = "my test data";
var globalsymKeyGenerator;
var ginBlob = {
var inBlob = {
data: stringTouInt8Array(globalText),
};
......@@ -71,6 +71,7 @@ async function testHMACDigestPromise(HMACAlgoName, keyAlgoName) {
console.warn("HMAC algName is: " + globalHMAC.algName);
console.log("start to call createSymKeyGenerator()");
globalsymKeyGenerator = cryptoFramework.createSymKeyGenerator(keyAlgoName);
console.warn("symKeyGenerator algName:" + globalsymKeyGenerator.algName);
expect(globalsymKeyGenerator != null).assertTrue();
console.log("createSymKeyGenerator ok");
console.warn("symKeyGenerator algName:" + globalsymKeyGenerator.algName);
......@@ -89,7 +90,7 @@ async function testHMACDigestPromise(HMACAlgoName, keyAlgoName) {
return promiseMacInit;
})
.then(() => {
var promiseMacUpdate = globalHMAC.update(ginBlob);
var promiseMacUpdate = globalHMAC.update(inBlob);
return promiseMacUpdate;
})
.then(() => {
......@@ -110,4 +111,187 @@ async function testHMACDigestPromise(HMACAlgoName, keyAlgoName) {
});
}
export { testMDDigestPromise, testHMACDigestPromise };
async function testHMACErrorAlgorithm(HMACAlgoName1, HMACAlgoName2) {
var globalHMAC;
var globalText = "my test data";
var inBlob = {
data: stringTouInt8Array(globalText),
};
return new Promise((resolve, reject) => {
try {
globalHMAC = cryptoFramework.createMac(HMACAlgoName1);
reject();
} catch (error) {
console.error("[Promise]: error code1: " + error.code + ", message is: " + error.message);
expect(error.code == 801).assertTrue();
}
try {
globalHMAC = cryptoFramework.createMac(HMACAlgoName2);
reject();
} catch (error) {
console.error("[Promise]: error code2: " + error.code + ", message is: " + error.message);
expect(error.code == 401).assertTrue();
resolve();
}
});
}
async function testHMACDigestPromiseErrorKey(HMACAlgoName, keyAlgoName) {
var globalHMAC;
var globalText = "my test data";
let globalsymKeyGenerator;
var inBlob = {
data: stringTouInt8Array(globalText),
};
return new Promise((resolve, reject) => {
globalHMAC = cryptoFramework.createMac(HMACAlgoName);
expect(globalHMAC != null).assertTrue();
console.warn("mac= " + globalHMAC);
console.warn("HMAC algName is: " + globalHMAC.algName);
console.log("start to call createSymKeyGenerator()");
try {
globalsymKeyGenerator = cryptoFramework.createAsyKeyGenerator(keyAlgoName);
} catch (error) {
console.error("[Promise]: error code1: " + error.code + ", message is: " + error.message);
}
console.warn("symKeyGenerator algName:" + globalsymKeyGenerator.algName);
expect(globalsymKeyGenerator != null).assertTrue();
console.log("createSymKeyGenerator ok");
let key = globalsymKeyGenerator.generateKeyPair();
try {
var promiseMacInit = globalHMAC.init(key);
expect(promiseMacInit == null).assertTrue();
reject();
} catch (error) {
console.error("[Promise]init(key): error code: " + error.code + ", message is: " + error.message);
expect(error.code == 401).assertTrue();
resolve();
};
try {
var promiseMacInit = globalHMAC.init(null);
expect(promiseMacInit == null).assertTrue();
reject();
} catch (error) {
console.error("[Promise]init(null): error code: " + error.code + ", message is: " + error.message);
expect(error.code == 401).assertTrue();
resolve();
};
});
}
async function testHMACDigestPromiseDatablobNull(HMACAlgoName, keyAlgoName) {
var globalHMAC;
var globalText = "my test data";
var globalsymKeyGenerator;
var inBlob = {
data: stringTouInt8Array(globalText),
};
return new Promise((resolve, reject) => {
globalHMAC = cryptoFramework.createMac(HMACAlgoName);
expect(globalHMAC != null).assertTrue();
console.warn("mac= " + globalHMAC);
console.warn("HMAC algName is: " + globalHMAC.algName);
console.log("start to call createSymKeyGenerator()");
globalsymKeyGenerator = cryptoFramework.createSymKeyGenerator(keyAlgoName);
console.warn("symKeyGenerator algName:" + globalsymKeyGenerator.algName);
expect(globalsymKeyGenerator != null).assertTrue();
console.log("createSymKeyGenerator ok");
console.warn("symKeyGenerator algName:" + globalsymKeyGenerator.algName);
globalsymKeyGenerator
.generateSymKey()
.then((key) => {
expect(key != null).assertTrue();
console.warn("generateSymKey ok");
console.warn("key algName:" + key.algName);
console.warn("key format:" + key.format);
var encodedKey = key.getEncoded();
console.warn(
"key getEncoded hex: " + uInt8ArrayToShowStr(encodedKey.data)
);
var promiseMacInit = globalHMAC.init(key);
return promiseMacInit;
})
.then(() => {
try {
var promiseMacUpdate = globalHMAC.update(null);
console.warn("promiseMacUpdate:" + promiseMacUpdate);
} catch (error) {
console.error("[Promise]: error code: " + error.code + ", message is: " + error.message);
expect(error.code == 401).assertTrue();
resolve();
}
})
.then(() => {
var promiseMacdoFinal = globalHMAC.doFinal();
return promiseMacdoFinal;
})
.catch((err) => {
console.error("[promise]catch err:" + err);
reject(err);
});
});
}
async function testHMACDigestPromiseDatablobLong(HMACAlgoName, keyAlgoName, DatablobLen) {
var globalHMAC;
var globalsymKeyGenerator;
var i;
var globalText;
var t = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefhijklmnopqrstuvwxyz",n = t.length,s="";
for (i = 0; i < DatablobLen; i++){
globalText += t.charAt(Math.floor(Math.random() * n));
}
console.warn("Datablob = " + globalText);
var inBlob = {
data: stringTouInt8Array(globalText),
};
return new Promise((resolve, reject) => {
globalHMAC = cryptoFramework.createMac(HMACAlgoName);
resolve();
expect(globalHMAC != null).assertTrue();
console.warn("mac= " + globalHMAC);
console.warn("HMAC algName is: " + globalHMAC.algName);
console.log("start to call createSymKeyGenerator()");
globalsymKeyGenerator = cryptoFramework.createSymKeyGenerator(keyAlgoName);
console.warn("symKeyGenerator algName:" + globalsymKeyGenerator.algName);
expect(globalsymKeyGenerator != null).assertTrue();
console.log("createSymKeyGenerator ok");
globalsymKeyGenerator
.generateSymKey()
.then((key) => {
expect(key != null).assertTrue();
console.warn("generateSymKey ok");
console.warn("key algName:" + key.algName);
console.warn("key format:" + key.format);
var encodedKey = key.getEncoded();
console.warn(
"key getEncoded hex: " + uInt8ArrayToShowStr(encodedKey.data)
);
var promiseMacInit = globalHMAC.init(key);
return promiseMacInit;
})
.then(() => {
try {
var promiseMacUpdate = globalHMAC.update(inBlob);
console.log("promiseMacUpdate = " + promiseMacUpdate);
} catch (error) {
console.error("[Promise]: error code: " + error.code + ", message is: " + error.message);
}
})
.then(() => {
var promiseMacdoFinal = globalHMAC.doFinal();
console.log("promiseMacdoFinal = " + promiseMacdoFinal);
return promiseMacdoFinal;
})
.catch((err) => {
console.error("[promise]catch err:" + err);
reject(err);
});
});
}
export { testMDDigestPromise, testHMACDigestPromise, testHMACErrorAlgorithm,
testHMACDigestPromiseErrorKey, testHMACDigestPromiseDatablobNull, testHMACDigestPromiseDatablobLong };
......@@ -59,4 +59,48 @@ async function testSecurityRandomCallback(length) {
});
}
export { testSecurityRandomCallback };
async function testSecurityRandomLengthCallback(length) {
var rand;
return new Promise((resolve, reject) => {
rand = cryptoFramework.createRandom();
expect(rand != null).assertTrue();
if(length != null) {
rand.generateRandom(length, (err, randData) => {
if (err) {
console.error("[callback]generateRandom catch error:" + err + "[callback]: error code: " + err.code);
expect(err.code == 401).assertTrue();
resolve();
} else {
expect(randData != null).assertTrue();
reject();
}
});
} else {
try{
rand.generateRandom(length);
reject();
} catch (error) {
console.error("[callback]generateRandom catch err1:" + error + "[callback]: error code: " + error.code);
resolve();
}
}
});
}
async function testSecurityRandomCallbackSeed(length) {
var rand;
return new Promise((resolve, reject) => {
rand = cryptoFramework.createRandom();
expect(rand != null).assertTrue();
try{
rand.setSeed(length);
reject();
} catch (error) {
console.error("[callback]setSeed catch err:" + error + "[callback]: error code: " + error.code);
expect(error.code == 401).assertTrue();
resolve();
}
});
}
export { testSecurityRandomCallback, testSecurityRandomLengthCallback, testSecurityRandomCallbackSeed };
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册