未验证 提交 bcd649c5 编写于 作者: O openharmony_ci 提交者: Gitee

!9397 【OpenHarmony 4.0.9.2】【xts_acts】【安全子系统】【ToD】加解密算法库框架更新摘要算法部分测试套的逻辑

Merge pull request !9397 from 黄坤/qwink230719
...@@ -17,30 +17,21 @@ import { describe, beforeAll, afterEach, it, expect } from "@ohos/hypium"; ...@@ -17,30 +17,21 @@ import { describe, beforeAll, afterEach, it, expect } from "@ohos/hypium";
import { import {
testHMACDigestPromise, testHMACDigestPromise,
testMDDigestPromise, testMDDigestPromise,
testHMACDigestPromiseDatablobLong,
} from "./utils/digestalgorithm/publicDigestPromise"; } from "./utils/digestalgorithm/publicDigestPromise";
import { import {
testHMACDigestCallback, testHMACDigestCallback,
testMDDigestCallback, testMDDigestCallback,
testMDErrorAlgorithm,
testMDErrorAlgorithmNull,
testMDDigestCallbackLen, testMDDigestCallbackLen,
} from "./utils/digestalgorithm/publicDigestCallback"; } from "./utils/digestalgorithm/publicDigestCallback";
import {
stringTouInt8Array,
uInt8ArrayToShowStr,
uInt8ArrayToString,
} from "./utils/common/publicDoString";
import cryptoFramework from "@ohos.security.cryptoFramework"; import cryptoFramework from "@ohos.security.cryptoFramework";
import { stringTouInt8Array } from "./utils/common/publicDoString";
export default function DigestAlgorithmJsunit() { export default function DigestAlgorithmJsunit() {
describe("DigestAlgorithmJsunit", function () { describe("DigestAlgorithmJsunit", function () {
console.info("##########start DigestAlgorithmJsunit##########"); console.info("##########start DigestAlgorithmJsunit##########");
beforeAll(function () { beforeAll(function () {});
}); afterEach(function () {});
afterEach(function () {
});
/** /**
* @tc.number Security_crypto_framework_MD_0100 * @tc.number Security_crypto_framework_MD_0100
...@@ -142,32 +133,30 @@ export default function DigestAlgorithmJsunit() { ...@@ -142,32 +133,30 @@ export default function DigestAlgorithmJsunit() {
/** /**
* @tc.number Security_crypto_framework_MD_0700 * @tc.number Security_crypto_framework_MD_0700
* @tc.name The encryption and decryption framework supports MD calculation, and the algorithm parameters are abnormal * @tc.name The encryption and decryption framework supports MD calculation, and the algorithm parameters are abnormal
* @tc.desc Use the Callback Style of Interface * @tc.desc Input SHA5
*/ */
it("Security_crypto_framework_MD_0700", 0, async function (done) { it("Security_crypto_framework_MD_0700", 0, async function (done) {
await testMDErrorAlgorithm("SHA5") try {
.then((data) => { cryptoFramework.createMd("SHA5");
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail(); expect(null).assertFail();
}); } catch (err) {
expect(err.code == 801).assertTrue();
}
done(); done();
}); });
/** /**
* @tc.number Security_crypto_framework_MD_0800 * @tc.number Security_crypto_framework_MD_0800
* @tc.name The encryption and decryption framework supports MD calculation, and the algorithm parameter is NULL * @tc.name The encryption and decryption framework supports MD calculation, and the algorithm parameter is NULL
* @tc.desc Use the Callback Style of Interface * @tc.desc Input null
*/ */
it("Security_crypto_framework_MD_0800", 0, async function (done) { it("Security_crypto_framework_MD_0800", 0, async function (done) {
await testMDErrorAlgorithmNull(null) try {
.then((data) => { cryptoFramework.createMd(null);
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail(); expect(null).assertFail();
}); } catch (err) {
expect(err.code == 401).assertTrue();
}
done(); done();
}); });
...@@ -196,14 +185,14 @@ export default function DigestAlgorithmJsunit() { ...@@ -196,14 +185,14 @@ export default function DigestAlgorithmJsunit() {
let mdGenerator = cryptoFramework.createMd("SHA224"); let mdGenerator = cryptoFramework.createMd("SHA224");
try { try {
await new Promise((resolve, reject) => { await new Promise((resolve, reject) => {
mdGenerator.update(null, (err,) => { mdGenerator.update(null, (err) => {
if (err) { if (err) {
reject(err); reject(err);
} else { } else {
resolve(); resolve();
} }
}) });
}) });
expect(null).assertFail(); expect(null).assertFail();
} catch (err) { } catch (err) {
expect(err.code).assertEqual(401); expect(err.code).assertEqual(401);
...@@ -218,7 +207,7 @@ export default function DigestAlgorithmJsunit() { ...@@ -218,7 +207,7 @@ export default function DigestAlgorithmJsunit() {
await mdGenerator.digest(); await mdGenerator.digest();
} catch (err) { } catch (err) {
console.error("err is " + err.code); console.error("err is " + err.code);
expect(err.code).assertEqual(401) expect(err.code).assertEqual(401);
} }
done(); done();
}); });
...@@ -313,14 +302,18 @@ export default function DigestAlgorithmJsunit() { ...@@ -313,14 +302,18 @@ export default function DigestAlgorithmJsunit() {
cryptoFramework.createMac("SHA5"); cryptoFramework.createMac("SHA5");
expect(null).assertFail(); expect(null).assertFail();
} catch (err) { } catch (err) {
console.error("[Promise]: error code: " + err.code + ", message is: " + err.message); console.error(
"[Promise]: error code: " + err.code + ", message is: " + err.message
);
expect(err.code).assertEqual(801); expect(err.code).assertEqual(801);
} }
try { try {
cryptoFramework.createMac(null); cryptoFramework.createMac(null);
expect(null).assertFail(); expect(null).assertFail();
} catch (err) { } catch (err) {
console.error("[Promise]: error code: " + err.code + ", message is: " + err.message); console.error(
"[Promise]: error code: " + err.code + ", message is: " + err.message
);
expect(err.code).assertEqual(401); expect(err.code).assertEqual(401);
} }
done(); done();
...@@ -333,22 +326,33 @@ export default function DigestAlgorithmJsunit() { ...@@ -333,22 +326,33 @@ export default function DigestAlgorithmJsunit() {
*/ */
it("Security_crypto_framework_HMAC_0700", 0, async function (done) { it("Security_crypto_framework_HMAC_0700", 0, async function (done) {
let globalHMAC = cryptoFramework.createMac("SHA512"); let globalHMAC = cryptoFramework.createMac("SHA512");
let globalsymKeyGenerator = cryptoFramework.createAsyKeyGenerator("RSA1024|PRIMES_2"); let globalsymKeyGenerator =
cryptoFramework.createAsyKeyGenerator("RSA1024|PRIMES_2");
let key = globalsymKeyGenerator.generateKeyPair(); let key = globalsymKeyGenerator.generateKeyPair();
try { try {
await globalHMAC.init(key); await globalHMAC.init(key);
expect(null).assertFail(); expect(null).assertFail();
} catch (err) { } catch (err) {
console.error("[Promise]init(key): error code: " + err.code + ", message is: " + err.message); console.error(
"[Promise]init(key): error code: " +
err.code +
", message is: " +
err.message
);
expect(err.code).assertEqual(401); expect(err.code).assertEqual(401);
}; }
try { try {
await globalHMAC.init(null); await globalHMAC.init(null);
expect(null).assertFail(); expect(null).assertFail();
} catch (err) { } catch (err) {
console.error("[Promise]init(null): error code: " + err.code + ", message is: " + err.message); console.error(
"[Promise]init(null): error code: " +
err.code +
", message is: " +
err.message
);
expect(err.code).assertEqual(401); expect(err.code).assertEqual(401);
}; }
done(); done();
}); });
...@@ -359,14 +363,20 @@ export default function DigestAlgorithmJsunit() { ...@@ -359,14 +363,20 @@ export default function DigestAlgorithmJsunit() {
*/ */
it("Security_crypto_framework_HMAC_0800", 0, async function (done) { it("Security_crypto_framework_HMAC_0800", 0, async function (done) {
let globalHMAC = cryptoFramework.createMac("SHA512"); let globalHMAC = cryptoFramework.createMac("SHA512");
let globalsymKeyGenerator = cryptoFramework.createSymKeyGenerator("3DES192"); let globalsymKeyGenerator =
cryptoFramework.createSymKeyGenerator("3DES192");
let key = await globalsymKeyGenerator.generateSymKey(); let key = await globalsymKeyGenerator.generateSymKey();
await globalHMAC.init(key); await globalHMAC.init(key);
try { try {
await globalHMAC.update(null); await globalHMAC.update(null);
expect(null).assertFail(); expect(null).assertFail();
} catch (err) { } catch (err) {
console.error("[Promise]init(null): error code: " + err.code + ", message is: " + err.message); console.error(
"[Promise]init(null): error code: " +
err.code +
", message is: " +
err.message
);
expect(err.code).assertEqual(401); expect(err.code).assertEqual(401);
} }
done(); done();
...@@ -378,13 +388,25 @@ export default function DigestAlgorithmJsunit() { ...@@ -378,13 +388,25 @@ export default function DigestAlgorithmJsunit() {
* @tc.desc Use the Promise Style of Interface * @tc.desc Use the Promise Style of Interface
*/ */
it("Security_crypto_framework_HMAC_0900", 0, async function (done) { it("Security_crypto_framework_HMAC_0900", 0, async function (done) {
await testHMACDigestPromiseDatablobLong("SHA512", "3DES192", 10000) let globalText = "";
.then((data) => { let t = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefhijklmnopqrstuvwxyz0123456789";
expect(data == null).assertTrue(); for (let i = 0; i < 10000; i++) {
}) globalText += t.charAt(Math.floor(Math.random() * t.length));
.catch((err) => { }
console.log("Datablob = " + globalText);
let inBlob = {
data: stringTouInt8Array(globalText),
};
try {
let macObj = cryptoFramework.createMac("SHA512");
let symKeyGenerator = cryptoFramework.createSymKeyGenerator("3DES192");
let symKey = await symKeyGenerator.generateSymKey();
await macObj.init(symKey);
await macObj.update(inBlob);
await macObj.doFinal();
} catch (err) {
expect(null).assertFail(); expect(null).assertFail();
}); }
done(); done();
}); });
}); });
......
...@@ -14,7 +14,11 @@ ...@@ -14,7 +14,11 @@
*/ */
import { expect } from "@ohos/hypium"; import { expect } from "@ohos/hypium";
import cryptoFramework from "@ohos.security.cryptoFramework"; import cryptoFramework from "@ohos.security.cryptoFramework";
import { stringTouInt8Array, uInt8ArrayToShowStr, uInt8ArrayToString, } from "../common/publicDoString"; import {
stringTouInt8Array,
uInt8ArrayToShowStr,
uInt8ArrayToString,
} from "../common/publicDoString";
async function generateSymKey(symKeyGenerator) { async function generateSymKey(symKeyGenerator) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
...@@ -145,74 +149,32 @@ async function testMDDigestCallback(MDAlgoName) { ...@@ -145,74 +149,32 @@ async function testMDDigestCallback(MDAlgoName) {
} }
async function testMDDigestCallbackLen(MDAlgoName, DatablobLen) { async function testMDDigestCallbackLen(MDAlgoName, DatablobLen) {
var globalMd; let globalText = "";
var i; let t = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefhijklmnopqrstuvwxyz0123456789";
var globalText; for (let i = 0; i < DatablobLen; i++) {
var t = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefhijklmnopqrstuvwxyz", n = t.length, s = ""; globalText += t.charAt(Math.floor(Math.random() * t.length));
for (i = 0; i < DatablobLen; i++) {
globalText += t.charAt(Math.floor(Math.random() * n));
} }
console.warn("Datablob = " + globalText); console.warn("Datablob = " + globalText);
var ginBlob = { var ginBlob = {
data: stringTouInt8Array(globalText), data: stringTouInt8Array(globalText),
}; };
return new Promise((resolve, reject) => { try {
globalMd = cryptoFramework.createMd(MDAlgoName); let mdObj = cryptoFramework.createMd(MDAlgoName);
expect(globalMd != null).assertTrue(); expect(mdObj != null).assertTrue();
console.warn("md= " + globalMd); let updateData = await updateMd(mdObj, ginBlob);
console.warn("MD algName is: " + globalMd.algName);
updateMd(globalMd, ginBlob)
.then((updateData) => {
expect(updateData === "update success").assertTrue(); expect(updateData === "update success").assertTrue();
return digestMd(globalMd); let digestBlob = await digestMd(mdObj);
})
.then((digestBlob) => {
console.warn( console.warn(
"[callback]: digest result: " + uInt8ArrayToShowStr(digestBlob.data) "[callback]: digest result: " + uInt8ArrayToShowStr(digestBlob.data)
); );
let mdLen = globalMd.getMdLength(); let mdLen = mdObj.getMdLength();
console.warn("Md len: " + mdLen); console.log("mdLen is: " + mdLen);
expect(digestBlob != null && mdLen != 0 && mdLen != null).assertTrue(); expect(digestBlob != null && mdLen != 0 && mdLen != null).assertTrue();
resolve(); } catch (err) {
})
.catch((err) => {
console.error("testMDDigestCallback catch error: " + err); console.error("testMDDigestCallback catch error: " + err);
reject(err); throw err;
});
});
}
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) { async function testHMACDigestCallback(HMACAlgoName, keyAlgoName) {
...@@ -263,7 +225,5 @@ async function testHMACDigestCallback(HMACAlgoName, keyAlgoName) { ...@@ -263,7 +225,5 @@ async function testHMACDigestCallback(HMACAlgoName, keyAlgoName) {
export { export {
testMDDigestCallback, testMDDigestCallback,
testHMACDigestCallback, testHMACDigestCallback,
testMDErrorAlgorithm, testMDDigestCallbackLen,
testMDErrorAlgorithmNull,
testMDDigestCallbackLen
}; };
...@@ -111,63 +111,4 @@ async function testHMACDigestPromise(HMACAlgoName, keyAlgoName) { ...@@ -111,63 +111,4 @@ async function testHMACDigestPromise(HMACAlgoName, keyAlgoName) {
}); });
} }
async function testHMACDigestPromiseDatablobLong(HMACAlgoName, keyAlgoName, DatablobLen) { export { testMDDigestPromise, testHMACDigestPromise };
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, testHMACDigestPromiseDatablobLong};
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册