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