From 5bd4d87ea21f4560244aebafefbc26fed925266c Mon Sep 17 00:00:00 2001 From: Qwink2016 <714794360@qq.com> Date: Fri, 16 Dec 2022 14:14:54 +0800 Subject: [PATCH] =?UTF-8?q?fixed=20539e8dd=20from=20https://gitee.com/qwin?= =?UTF-8?q?k2016/xts=5Facts/pulls/6942=20=E5=8A=A0=E8=A7=A3=E5=AF=86?= =?UTF-8?q?=E7=AE=97=E6=B3=95=E5=BA=93=E8=AF=81=E4=B9=A6=E7=AE=A1=E7=90=86?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Qwink2016 <714794360@qq.com> Change-Id: I27c5a51a4806918f9233576c2b1b0d26f295f6dc --- .../cryptoFramework/js_api_test_two/Test.json | 1 + .../src/main/js/test/Certificate.test.js | 6 +- .../src/main/js/test/SecurityRandom.test.js | 8 +- .../certificate/publicCertificateCallback.js | 204 ++++++------------ .../certificate/publicCertificatePromise.js | 142 +++++------- .../publicSecurityRandomCallback.js | 46 ++-- .../publicSecurityRandomCommon.js | 46 ++++ .../publicSecurityRandomPromise.js | 26 +-- 8 files changed, 198 insertions(+), 281 deletions(-) create mode 100644 security/cryptoFramework/js_api_test_two/src/main/js/test/utils/securityrandom/publicSecurityRandomCommon.js diff --git a/security/cryptoFramework/js_api_test_two/Test.json b/security/cryptoFramework/js_api_test_two/Test.json index 101daabda..f6d21ed1a 100644 --- a/security/cryptoFramework/js_api_test_two/Test.json +++ b/security/cryptoFramework/js_api_test_two/Test.json @@ -4,6 +4,7 @@ "type": "OHJSUnitTest", "test-timeout": "900000", "shell-timeout": "900000", + "testcase-timeout": "60000", "bundle-name": "com.openharmony.cryptoFramework2", "package-name": "com.openharmony.cryptoFramework2" }, diff --git a/security/cryptoFramework/js_api_test_two/src/main/js/test/Certificate.test.js b/security/cryptoFramework/js_api_test_two/src/main/js/test/Certificate.test.js index c9d6ec7f8..825b19cd3 100644 --- a/security/cryptoFramework/js_api_test_two/src/main/js/test/Certificate.test.js +++ b/security/cryptoFramework/js_api_test_two/src/main/js/test/Certificate.test.js @@ -246,7 +246,7 @@ export default function CertificateJsunit() { await certCallback .checkValidityX509CertCallback("der", "20330830000000+0800") .then((data) => { - expect(data == 17630004).assertTrue(); + expect(data == 19030004).assertTrue(); }) .catch((err) => { expect(null).assertFail(); @@ -264,7 +264,7 @@ export default function CertificateJsunit() { await certCallback .checkValidityX509CertCallback("der", "NULL") .then((data) => { - expect(data == 17630001).assertTrue(); + expect(data == 19030001).assertTrue(); }) .catch((err) => { expect(null).assertFail(); @@ -272,7 +272,7 @@ export default function CertificateJsunit() { await certCallback .checkValidityX509CertCallback("der", "string") .then((data) => { - expect(data == 17630001).assertTrue(); + expect(data == 19030001).assertTrue(); }) .catch((err) => { expect(null).assertFail(); diff --git a/security/cryptoFramework/js_api_test_two/src/main/js/test/SecurityRandom.test.js b/security/cryptoFramework/js_api_test_two/src/main/js/test/SecurityRandom.test.js index b8790d689..336b36698 100644 --- a/security/cryptoFramework/js_api_test_two/src/main/js/test/SecurityRandom.test.js +++ b/security/cryptoFramework/js_api_test_two/src/main/js/test/SecurityRandom.test.js @@ -15,15 +15,13 @@ */ import { describe, beforeAll, afterEach, it, expect } from "@ohos/hypium"; -import { - testSecurityRandomPromise, - testSecurityRandomEnumPromise, -} from "./utils/securityrandom/publicSecurityRandomPromise"; +import { testSecurityRandomPromise } from "./utils/securityrandom/publicSecurityRandomPromise"; import { testSecurityRandomCallback, testSecurityRandomLengthCallback, testSecurityRandomCallbackSeed, } from "./utils/securityrandom/publicSecurityRandomCallback"; +import { testSecurityRandomEnumCommon } from "./utils/securityrandom/publicSecurityRandomCommon"; export default function SecurityRandomJsunit() { describe("SecurityRandomJsunit", function () { @@ -136,7 +134,7 @@ export default function SecurityRandomJsunit() { * @tc.desc cover 100% Enumerated values */ it("Security_crypto_framework_Random_0500", 0, async function (done) { - testSecurityRandomEnumPromise(); + testSecurityRandomEnumCommon(); done(); }); }); diff --git a/security/cryptoFramework/js_api_test_two/src/main/js/test/utils/certificate/publicCertificateCallback.js b/security/cryptoFramework/js_api_test_two/src/main/js/test/utils/certificate/publicCertificateCallback.js index 4dc802749..786fc50a0 100644 --- a/security/cryptoFramework/js_api_test_two/src/main/js/test/utils/certificate/publicCertificateCallback.js +++ b/security/cryptoFramework/js_api_test_two/src/main/js/test/utils/certificate/publicCertificateCallback.js @@ -15,6 +15,7 @@ import { expect } from "@ohos/hypium"; import cryptoFramework from "@ohos.security.cryptoFramework"; +import cert from "@ohos.security.cert"; import { stringTouInt8Array, uInt8ArrayToString, @@ -38,14 +39,14 @@ function createX509CertInstanceCallback(certType) { var encodingData; if (certType == "der") { - certformat = cryptoFramework.EncodingFormat.FORMAT_DER; + certformat = cert.EncodingFormat.FORMAT_DER; encodingData = new Uint8Array( selfSignedCaCertDer.match(/[\da-f]{2}/gi).map(function (h) { return parseInt(h, 16); }) ); } else { - certformat = cryptoFramework.EncodingFormat.FORMAT_PEM; + certformat = cert.EncodingFormat.FORMAT_PEM; encodingData = stringTouInt8Array(selfSignedCaCertPem); } @@ -55,7 +56,7 @@ function createX509CertInstanceCallback(certType) { }; return new Promise((resolve, reject) => { - cryptoFramework.createX509Cert(encodingBlob, (err, data) => { + cert.createX509Cert(encodingBlob, (err, data) => { if (data == null) { console.error( "[callback] create X509 Cert failed! err code: " + err.code @@ -77,10 +78,10 @@ function createX509CertInstanceforCrlTestCallback(flag) { var invalidParams = false; if (flag == "normal") { - certformat = cryptoFramework.EncodingFormat.FORMAT_PEM; + certformat = cert.EncodingFormat.FORMAT_PEM; encodingData = stringTouInt8Array(testCert); } else if (flag == "error") { - certformat = cryptoFramework.EncodingFormat.FORMAT_PEM; + certformat = cert.EncodingFormat.FORMAT_PEM; encodingData = stringTouInt8Array(testErrorCert); } else { invalidParams = true; @@ -92,7 +93,7 @@ function createX509CertInstanceforCrlTestCallback(flag) { }; return new Promise((resolve, reject) => { - cryptoFramework.createX509Cert(encodingBlob, (err, data) => { + cert.createX509Cert(encodingBlob, (err, data) => { if (data == null) { console.error( "[callback] create X509 Cert failed! err code: " + err.code @@ -112,22 +113,6 @@ function createX509CertInstanceforCrlTestCallback(flag) { }); } -async function getX509CertPublicKey(certInstance) { - return new Promise((resolve, reject) => { - certInstance.getPublicKey((err, publicKey) => { - if (err) { - console.error("[Callback]getPublicKey failed. error is " + err); - reject(err); - } else { - console.log( - "[Callback]getPublicKey success. publicKey is " + publicKey - ); - resolve(publicKey); - } - }); - }); -} - async function verifyX509Cert(certInstance, pubKey) { return new Promise((resolve, reject) => { certInstance.verify(pubKey, (err) => { @@ -144,13 +129,12 @@ async function verifyX509Cert(certInstance, pubKey) { async function verifyX509CertCallback(certType, flag) { var gInstance; + var pubKey; return new Promise((resolve, reject) => { createX509CertInstanceCallback(certType) .then((instance) => { gInstance = instance; - return getX509CertPublicKey(gInstance); - }) - .then((pubKey) => { + pubKey = gInstance.getPublicKey(); expect(pubKey != null).assertTrue(); if (flag != undefined) { if (flag == "wrong") { @@ -177,31 +161,12 @@ async function verifyX509CertCallback(certType, flag) { }); } -async function checkValidityWithDate(certInstance, date) { - return new Promise((resolve, reject) => { - certInstance.checkValidityWithDate(date, (err) => { - if (err) { - console.error( - "[Callback]checkValidityWithDate failed. error is " + err - ); - reject(err); - } else { - console.log("[Callback]checkValidityWithDate success!"); - resolve(); - } - }); - }); -} - async function checkValidityX509CertCallback(certType, date) { return new Promise((resolve, reject) => { createX509CertInstanceCallback(certType) .then((instance) => { - return checkValidityWithDate(instance, date); - }) - .then((result) => { - console.warn("[callback] checkValidityX509Cert success!"); - resolve(result); + instance.checkValidityWithDate(date); + resolve(null); }) .catch((err) => { console.error( @@ -353,23 +318,14 @@ async function checkGetEncodedX509CertCallback(certType) { } async function checkGetPublicKeyX509CertCallback(certType) { - var gInstance; + var publicKey; return new Promise((resolve, reject) => { createX509CertInstanceCallback(certType) .then((instance) => { - gInstance = instance; - gInstance.getPublicKey((err, publicKey) => { - if (err) { - console.error("[callback] getPublicKey failed, err is:" + err); - reject(err); - } else { - console.warn( - "[callback] getPublicKey success, publicKey is:" + publicKey - ); - expect(publicKey != null).assertTrue(); - resolve(); - } - }); + publicKey = instance.getPublicKey(); + console.warn("[callback] publicKey success, publicKey is:" + publicKey); + expect(publicKey != null).assertTrue(); + resolve(); }) .catch((err) => { console.error("[callback] getPublickey failed! error is: " + err); @@ -383,14 +339,14 @@ function createX509CrlInstanceCallback(certType) { var encodingData; if (certType == "der") { - certformat = cryptoFramework.EncodingFormat.FORMAT_DER; + certformat = cert.EncodingFormat.FORMAT_DER; encodingData = new Uint8Array( testCrlDer.match(/[\da-f]{2}/gi).map(function (h) { return parseInt(h, 16); }) ); } else { - certformat = cryptoFramework.EncodingFormat.FORMAT_PEM; + certformat = cert.EncodingFormat.FORMAT_PEM; encodingData = stringTouInt8Array(testCrlPem); } @@ -400,7 +356,7 @@ function createX509CrlInstanceCallback(certType) { }; return new Promise((resolve, reject) => { - cryptoFramework.createX509Crl(encodingBlob, (err, data) => { + cert.createX509Crl(encodingBlob, (err, data) => { if (err) { console.error( "[callback] createX509Crl failed! error code is: " + err.code @@ -417,20 +373,13 @@ function createX509CrlInstanceCallback(certType) { } function checkIsRevokedX509CrlCallback(crlInstance, flag) { + var status; return new Promise((resolve, reject) => { createX509CertInstanceforCrlTestCallback(flag) .then((certInstance) => { - crlInstance.isRevoked(certInstance, (err, status) => { - if (err) { - console.error( - "[callback] isRevoked failed! err code is:" + err.code - ); - reject(err); - } else { - console.warn("[callback] isRevoked status is:" + status); - resolve(status); - } - }); + status = crlInstance.isRevoked(certInstance); + console.warn("[callback] isRevoked status is:" + status); + resolve(status); }) .catch((err) => { console.error("[callback] isRevoked failed! err is: " + err); @@ -512,7 +461,7 @@ function verifyX509CrlCallback(crlType, flag) { var globalCrlInstance; if (crlType == "pem" || crlType == "der") { - certformat = cryptoFramework.EncodingFormat.FORMAT_DER; + certformat = cert.EncodingFormat.FORMAT_DER; priKeyEncodingData = new Uint8Array( crlVerifyPriKeyHex.match(/[\da-f]{2}/gi).map(function (h) { return parseInt(h, 16); @@ -544,7 +493,7 @@ function verifyX509CrlCallback(crlType, flag) { var asyKeyGenerator = cryptoFramework.createAsyKeyGenerator("RSA1024|PRIMES_3"); expect(asyKeyGenerator != null).assertTrue(); - cryptoFramework + cert .createX509Crl(crlEncodingBlob) .then((crlInstance) => { expect(crlInstance != null).assertTrue(); @@ -640,16 +589,11 @@ async function getX509CrlInfoCallback(certType, processType) { expect(signatureAlgParams != null).assertTrue(); break; case "getTbsInfo": - gInstance.getTbsInfo((err, tbsInfo) => { - if (err) { - console.error("[callback] getTbsInfo failed! error is: " + err); - reject(err); - } - expect(tbsInfo != null).assertTrue(); - console.log( - processType + " is : " + uInt8ArrayToString(tbsInfo.data) - ); - }); + let tbsInfo = gInstance.getTbsInfo(); + console.log( + processType + " is : " + uInt8ArrayToString(tbsInfo.data) + ); + expect(tbsInfo != null).assertTrue(); break; default: console.log("Invalid parameter !"); @@ -664,32 +608,12 @@ async function getX509CrlInfoCallback(certType, processType) { }); } -async function getRevokedCertWithCert(crlInstance, certInstance) { - return new Promise((resolve, reject) => { - crlInstance.getRevokedCertWithCert(certInstance, (err, x509CrlEntry) => { - if (err) { - console.error( - "[Callback]getRevokedCertWithCert failed. error is " + err - ); - reject(err); - } else { - console.log( - "[Callback]getRevokedCertWithCert success. x509CrlEntry is " + - x509CrlEntry - ); - resolve(x509CrlEntry); - } - }); - }); -} - function checkGetRevokedCertWithCertX509CrlCallback(crlInstance, flag) { + var x509CrlEntry; return new Promise((resolve, reject) => { createX509CertInstanceforCrlTestCallback(flag) .then((certInstance) => { - return getRevokedCertWithCert(crlInstance, certInstance); - }) - .then((x509CrlEntry) => { + x509CrlEntry = crlInstance.getRevokedCertWithCert(certInstance); expect(x509CrlEntry != null).assertTrue(); let num = x509CrlEntry.getSerialNumber(); console.warn("[num] getRevokedCertWithCert num is:" + num); @@ -705,13 +629,12 @@ function checkGetRevokedCertWithCertX509CrlCallback(crlInstance, flag) { } async function checkGetRevokedCertsX509CrlCallback(certType) { - var gInstance; var gIndex; + var revocation; return new Promise((resolve, reject) => { createX509CrlInstanceCallback(certType) .then((instance) => { - gInstance = instance; - gInstance.getRevokedCerts((err, certs) => { + instance.getRevokedCerts((err, certs) => { expect(certs != null && certs.length != 0).assertTrue(); console.warn("[callback] Crl get gevoked certs success"); for (var i = 0; i < certs.length; i++) { @@ -722,20 +645,19 @@ async function checkGetRevokedCertsX509CrlCallback(certType) { " serialNumber is: " + certs[gIndex].getSerialNumber() ); - certs[gIndex].getRevocationDate((err1, revocation) => { - expect(revocation != null).assertTrue(); + revocation = certs[gIndex].getRevocationDate(); + expect(revocation != null).assertTrue(); + console.log( + "[callback] certs i: " + + gIndex + + " revocation date is: " + + revocation + ); + certs[gIndex].getEncoded((err2, eData) => { + expect(eData != null).assertTrue(); console.log( - "[callback] certs i: " + - gIndex + - " revocation date is: " + - revocation + "[callback] certs i: " + gIndex + " getEncoded is: " + eData ); - certs[gIndex].getEncoded((err2, eData) => { - expect(eData != null).assertTrue(); - console.log( - "[callback] certs i: " + gIndex + " getEncoded is: " + eData - ); - }); }); } }); @@ -749,26 +671,24 @@ async function checkGetRevokedCertsX509CrlCallback(certType) { } async function checkGetRevokedCertX509CrlCallback(certType) { - var gInstance; + var certIssuer; + var cert1; + var revocation; return new Promise((resolve, reject) => { createX509CrlInstanceCallback(certType) .then((instance) => { - gInstance = instance; - gInstance.getRevokedCert(1, (err, cert) => { - expect(cert != null).assertTrue(); - console.warn("[callback] Crl get gevoked cert success"); - cert.getRevocationDate((err1, revocation) => { - expect(revocation != null).assertTrue(); - console.log("[callback] cert revocation date is: " + revocation); - }); - cert.getCertIssuer((err2, certIssuer) => { - expect(certIssuer != null).assertTrue(); - console.log( - "[callback] cert certIssuer is: " + - uInt8ArrayToString(certIssuer.data) - ); - }); - }); + cert1 = instance.getRevokedCert(1); + expect(cert1 != null).assertTrue(); + console.warn("[callback] Crl get gevoked cert success"); + revocation = cert1.getRevocationDate(); + expect(revocation != null).assertTrue(); + console.log("[callback] cert revocation date is: " + revocation); + certIssuer = cert1.getCertIssuer(); + expect(certIssuer != null).assertTrue(); + console.log( + "[Callback] cert certIssuer is: " + + +uInt8ArrayToString(certIssuer.data) + ); resolve(); }) .catch((err) => { @@ -781,7 +701,7 @@ async function checkGetRevokedCertX509CrlCallback(certType) { function checkValidateOfCertChainValidatorCallback(algName, flag) { return new Promise((resolve, reject) => { try { - var validator = cryptoFramework.createCertChainValidator(algName); + var validator = cert.createCertChainValidator(algName); var algorithm = validator.algorithm; console.log( "createCertChainValidator success! algorithm is: " + algorithm @@ -835,7 +755,7 @@ function checkValidateOfCertChainValidatorCallback(algName, flag) { var certChainData = { data: dataArray, count: 2, - encodingFormat: cryptoFramework.EncodingFormat.FORMAT_PEM, + encodingFormat: cert.EncodingFormat.FORMAT_PEM, }; console.log("certChainData.data is: " + certChainData.data); validator.validate(certChainData, (err) => { diff --git a/security/cryptoFramework/js_api_test_two/src/main/js/test/utils/certificate/publicCertificatePromise.js b/security/cryptoFramework/js_api_test_two/src/main/js/test/utils/certificate/publicCertificatePromise.js index a92c8414b..2304f6c54 100644 --- a/security/cryptoFramework/js_api_test_two/src/main/js/test/utils/certificate/publicCertificatePromise.js +++ b/security/cryptoFramework/js_api_test_two/src/main/js/test/utils/certificate/publicCertificatePromise.js @@ -15,6 +15,7 @@ import { expect } from "@ohos/hypium"; import cryptoFramework from "@ohos.security.cryptoFramework"; +import cert from "@ohos.security.cert"; import { stringTouInt8Array, uInt8ArrayToString, @@ -38,14 +39,14 @@ function createX509CertInstancePromise(certType) { var encodingData; if (certType == "der") { - certformat = cryptoFramework.EncodingFormat.FORMAT_DER; + certformat = cert.EncodingFormat.FORMAT_DER; encodingData = new Uint8Array( selfSignedCaCertDer.match(/[\da-f]{2}/gi).map(function (h) { return parseInt(h, 16); }) ); } else { - certformat = cryptoFramework.EncodingFormat.FORMAT_PEM; + certformat = cert.EncodingFormat.FORMAT_PEM; encodingData = stringTouInt8Array(selfSignedCaCertPem); } @@ -55,7 +56,7 @@ function createX509CertInstancePromise(certType) { }; return new Promise((resolve, reject) => { - cryptoFramework + cert .createX509Cert(encodingBlob) .then((data) => { console.warn( @@ -78,10 +79,10 @@ function createX509CertInstanceforCrlTestPromise(flag) { var invalidParams = false; if (flag == "normal") { - certformat = cryptoFramework.EncodingFormat.FORMAT_PEM; + certformat = cert.EncodingFormat.FORMAT_PEM; encodingData = stringTouInt8Array(testCert); } else if (flag == "error") { - certformat = cryptoFramework.EncodingFormat.FORMAT_PEM; + certformat = cert.EncodingFormat.FORMAT_PEM; encodingData = stringTouInt8Array(testErrorCert); } else { invalidParams = true; @@ -93,7 +94,7 @@ function createX509CertInstanceforCrlTestPromise(flag) { }; return new Promise((resolve, reject) => { - cryptoFramework + cert .createX509Cert(encodingBlob) .then((data) => { console.warn( @@ -116,13 +117,12 @@ function createX509CertInstanceforCrlTestPromise(flag) { async function verifyX509CertPromise(certType, flag) { var gInstance; + var publicKey; return new Promise((resolve, reject) => { createX509CertInstancePromise(certType) .then((instance) => { gInstance = instance; - return gInstance.getPublicKey(); - }) - .then((publicKey) => { + publicKey = gInstance.getPublicKey(); expect(publicKey != null).assertTrue(); console.log("[promise] getPublickey success!"); if (flag != undefined) { @@ -149,15 +149,11 @@ async function verifyX509CertPromise(certType, flag) { } async function checkValidityX509CertPromise(certType, date) { - var gInstance; + var result; return new Promise((resolve, reject) => { createX509CertInstancePromise(certType) .then((instance) => { - gInstance = instance; - return gInstance.checkValidityWithDate(date); - }) - .then((result) => { - console.warn("[promise] checkValidityX509Cert success!"); + result = instance.checkValidityWithDate(date); resolve(result); }) .catch((err) => { @@ -304,14 +300,11 @@ async function checkGetEncodedX509CertPromise(certType) { } async function checkGetPublicKeyX509CertPromise(certType) { - var gInstance; + var publicKey; return new Promise((resolve, reject) => { createX509CertInstancePromise(certType) .then((instance) => { - gInstance = instance; - return gInstance.getPublicKey(); - }) - .then((publicKey) => { + publicKey = instance.getPublicKey(); console.warn("[promise] publicKey success, publicKey is:" + publicKey); expect(publicKey != null).assertTrue(); resolve(); @@ -328,14 +321,14 @@ function createX509CrlInstancePromise(certType) { var encodingData; if (certType == "der") { - certformat = cryptoFramework.EncodingFormat.FORMAT_DER; + certformat = cert.EncodingFormat.FORMAT_DER; encodingData = new Uint8Array( testCrlDer.match(/[\da-f]{2}/gi).map(function (h) { return parseInt(h, 16); }) ); } else { - certformat = cryptoFramework.EncodingFormat.FORMAT_PEM; + certformat = cert.EncodingFormat.FORMAT_PEM; encodingData = stringTouInt8Array(testCrlPem); } @@ -345,7 +338,7 @@ function createX509CrlInstancePromise(certType) { }; return new Promise((resolve, reject) => { - cryptoFramework + cert .createX509Crl(encodingBlob) .then((data) => { console.warn( @@ -363,13 +356,11 @@ function createX509CrlInstancePromise(certType) { } function checkIsRevokedX509CrlPromise(crlInstance, flag) { + var status; return new Promise((resolve, reject) => { createX509CertInstanceforCrlTestPromise(flag) .then((certInstance) => { - let status = crlInstance.isRevoked(certInstance); - return status; - }) - .then((status) => { + status = crlInstance.isRevoked(certInstance); console.warn("[promise] isRevoked status is:" + status); resolve(status); }) @@ -413,7 +404,7 @@ function verifyX509CrlPromise(crlType, flag) { var crlEncodingData; var globalCrlInstance; if (crlType == "pem" || crlType == "der") { - certFormat = cryptoFramework.EncodingFormat.FORMAT_DER; + certFormat = cert.EncodingFormat.FORMAT_DER; priKeyEncodingData = new Uint8Array( crlVerifyPriKeyHex.match(/[\da-f]{2}/gi).map(function (h) { return parseInt(h, 16); @@ -446,7 +437,7 @@ function verifyX509CrlPromise(crlType, flag) { var asyKeyGenerator = cryptoFramework.createAsyKeyGenerator("RSA1024|PRIMES_3"); expect(asyKeyGenerator != null); - cryptoFramework + cert .createX509Crl(crlEncodingBlob) .then((crlInstance) => { expect(crlInstance != null).assertTrue(); @@ -542,18 +533,11 @@ async function getX509CrlInfoPromise(certType, processType) { expect(signatureAlgParams != null).assertTrue(); break; case "getTbsInfo": - gInstance - .getTbsInfo() - .then((tbsInfo) => { - expect(tbsInfo != null).assertTrue(); - console.log( - processType + " is : " + uInt8ArrayToString(tbsInfo.data) - ); - }) - .catch((err) => { - console.error("[promise] getTbsInfo failed! error is: " + err); - reject(err); - }); + let tbsInfo = gInstance.getTbsInfo(); + console.log( + processType + " is : " + uInt8ArrayToString(tbsInfo.data) + ); + expect(tbsInfo != null).assertTrue(); break; default: console.log("Invalid parameter !"); @@ -573,9 +557,6 @@ function checkGetRevokedCertWithCertX509CrlPromise(crlInstance, flag) { createX509CertInstanceforCrlTestPromise(flag) .then((certInstance) => { let crlEntry = crlInstance.getRevokedCertWithCert(certInstance); - return crlEntry; - }) - .then((crlEntry) => { console.warn("[promise] getRevokedCertWithCert is:" + crlEntry); let num = crlEntry.getSerialNumber(); console.warn("[promise] getRevokedCertWithCert num is:" + num); @@ -589,13 +570,12 @@ function checkGetRevokedCertWithCertX509CrlPromise(crlInstance, flag) { } async function checkGetRevokedCertsX509CrlPromise(certType) { - var gInstance; var gIndex; + var revocation; return new Promise((resolve, reject) => { createX509CrlInstancePromise(certType) .then((instance) => { - gInstance = instance; - return gInstance.getRevokedCerts(); + return instance.getRevokedCerts(); }) .then((certs) => { expect(certs != null && certs.length != 0).assertTrue(); @@ -608,20 +588,14 @@ async function checkGetRevokedCertsX509CrlPromise(certType) { " serialNumber is: " + certs[gIndex].getSerialNumber() ); - certs[gIndex] - .getRevocationDate() - .then((revocation) => { - expect(revocation != null).assertTrue(); - console.log( - "[Promise] certs i: " + - gIndex + - " revocation date is: " + - revocation - ); - }) - .catch((err) => { - reject(err); - }); + revocation = certs[gIndex].getRevocationDate(); + expect(revocation != null).assertTrue(); + console.log( + "[Promise] certs i: " + + gIndex + + " revocation date is: " + + revocation + ); certs[gIndex] .getEncoded() .then((eData) => { @@ -647,39 +621,23 @@ async function checkGetRevokedCertsX509CrlPromise(certType) { } async function checkGetRevokedCertX509CrlPromise(certType) { - var gInstance; + var certIssuer; + var revocation; return new Promise((resolve, reject) => { createX509CrlInstancePromise(certType) .then((instance) => { - gInstance = instance; - return gInstance.getRevokedCert(1); - }) - .then((cert) => { - expect(cert != null).assertTrue(); + let cert1 = instance.getRevokedCert(1); + expect(cert1 != null).assertTrue(); console.warn("[Promise] Crl get gevoked certs success"); - cert - .getRevocationDate() - .then((revocation) => { - expect(revocation != null).assertTrue(); - console.log("[Promise] cert revocation date is: " + revocation); - }) - .catch((err) => { - console.error("[Promise] cert revocation date is: " + err); - reject(err); - }); - cert - .getCertIssuer() - .then((certIssuer) => { - expect(certIssuer != null).assertTrue(); - console.log( - "[Promise] cert certIssuer is: " + - +uInt8ArrayToString(certIssuer.data) - ); - }) - .catch((err) => { - console.error("[Promise] cert certIssuer is: " + err); - reject(err); - }); + revocation = cert1.getRevocationDate(); + expect(revocation != null).assertTrue(); + console.log("[Promise] cert revocation date is: " + revocation); + certIssuer = cert1.getCertIssuer(); + expect(certIssuer != null).assertTrue(); + console.log( + "[Promise] cert certIssuer is: " + + +uInt8ArrayToString(certIssuer.data) + ); resolve(); }) .catch((err) => { @@ -692,7 +650,7 @@ async function checkGetRevokedCertX509CrlPromise(certType) { function checkValidateOfCertChainValidatorPromise(algName, flag) { return new Promise((resolve, reject) => { try { - var validator = cryptoFramework.createCertChainValidator(algName); + var validator = cert.createCertChainValidator(algName); var algorithm = validator.algorithm; console.log( "createCertChainValidator success! algorithm is: " + algorithm @@ -746,7 +704,7 @@ function checkValidateOfCertChainValidatorPromise(algName, flag) { var certChainData = { data: dataArray, count: 2, - encodingFormat: cryptoFramework.EncodingFormat.FORMAT_PEM, + encodingFormat: cert.EncodingFormat.FORMAT_PEM, }; console.log("certChainData.data is: " + certChainData.data); validator diff --git a/security/cryptoFramework/js_api_test_two/src/main/js/test/utils/securityrandom/publicSecurityRandomCallback.js b/security/cryptoFramework/js_api_test_two/src/main/js/test/utils/securityrandom/publicSecurityRandomCallback.js index 6d6589d0a..315f6bc1f 100644 --- a/security/cryptoFramework/js_api_test_two/src/main/js/test/utils/securityrandom/publicSecurityRandomCallback.js +++ b/security/cryptoFramework/js_api_test_two/src/main/js/test/utils/securityrandom/publicSecurityRandomCallback.js @@ -35,13 +35,12 @@ async function testSecurityRandomCallback(length) { console.log( "[callback] rand result first:" + uInt8ArrayToShowStr(randData.data) ); - } - rand.setSeed(randData, (err1) => { - if (err1) { - console.error("[callback]setSeed catch err:" + err1); + try { + rand.setSeed(randData); + } catch (err1) { reject(err1); } - }); + } }); rand.generateRandom(length, (err, randData) => { @@ -64,10 +63,15 @@ async function testSecurityRandomLengthCallback(length) { return new Promise((resolve, reject) => { rand = cryptoFramework.createRandom(); expect(rand != null).assertTrue(); - if(length != null) { + if (length != null) { rand.generateRandom(length, (err, randData) => { if (err) { - console.error("[callback]generateRandom catch error:" + err + "[callback]: error code: " + err.code); + console.error( + "[callback]generateRandom catch error:" + + err + + "[callback]: error code: " + + err.code + ); expect(err.code == 401).assertTrue(); resolve(); } else { @@ -76,31 +80,45 @@ async function testSecurityRandomLengthCallback(length) { } }); } else { - try{ + try { rand.generateRandom(length); reject(); } catch (error) { - console.error("[callback]generateRandom catch err1:" + error + "[callback]: error code: " + error.code); + console.error( + "[callback]generateRandom catch err1:" + + error + + "[callback]: error code: " + + error.code + ); resolve(); } } }); } -async function testSecurityRandomCallbackSeed(length) { +async function testSecurityRandomCallbackSeed(data) { var rand; return new Promise((resolve, reject) => { rand = cryptoFramework.createRandom(); expect(rand != null).assertTrue(); - try{ - rand.setSeed(length); + try { + rand.setSeed(data); reject(); } catch (error) { - console.error("[callback]setSeed catch err:" + error + "[callback]: error code: " + error.code); + console.error( + "[callback]setSeed catch err:" + + error + + "[callback]: error code: " + + error.code + ); expect(error.code == 401).assertTrue(); resolve(); } }); } -export { testSecurityRandomCallback, testSecurityRandomLengthCallback, testSecurityRandomCallbackSeed }; +export { + testSecurityRandomCallback, + testSecurityRandomLengthCallback, + testSecurityRandomCallbackSeed, +}; diff --git a/security/cryptoFramework/js_api_test_two/src/main/js/test/utils/securityrandom/publicSecurityRandomCommon.js b/security/cryptoFramework/js_api_test_two/src/main/js/test/utils/securityrandom/publicSecurityRandomCommon.js new file mode 100644 index 000000000..037128767 --- /dev/null +++ b/security/cryptoFramework/js_api_test_two/src/main/js/test/utils/securityrandom/publicSecurityRandomCommon.js @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import { expect } from "@ohos/hypium"; +import cryptoFramework from "@ohos.security.cryptoFramework"; +import cert from "@ohos.security.cert"; + +function testSecurityRandomEnumCommon() { + expect(401).assertEqual(cryptoFramework.Result.INVALID_PARAMS); + expect(401).assertEqual(cert.CertResult.INVALID_PARAMS); + expect(801).assertEqual(cryptoFramework.Result.NOT_SUPPORT); + expect(801).assertEqual(cert.CertResult.NOT_SUPPORT); + expect(17620001).assertEqual(cryptoFramework.Result.ERR_OUT_OF_MEMORY); + expect(19020001).assertEqual(cert.CertResult.ERR_OUT_OF_MEMORY); + expect(17620002).assertEqual(cryptoFramework.Result.ERR_RUNTIME_ERROR); + expect(19020002).assertEqual(cert.CertResult.ERR_RUNTIME_ERROR); + expect(17630001).assertEqual(cryptoFramework.Result.ERR_CRYPTO_OPERATION); + expect(19030001).assertEqual(cert.CertResult.ERR_CRYPTO_OPERATION); + expect(19030002).assertEqual(cert.CertResult.ERR_CERT_SIGNATURE_FAILURE); + expect(19030003).assertEqual(cert.CertResult.ERR_CERT_NOT_YET_VALID); + expect(19030004).assertEqual(cert.CertResult.ERR_CERT_HAS_EXPIRED); + expect(19030005).assertEqual( + cert.CertResult.ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY + ); + expect(19030006).assertEqual(cert.CertResult.ERR_KEYUSAGE_NO_CERTSIGN); + expect(19030007).assertEqual( + cert.CertResult.ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE + ); + expect(0).assertEqual(cert.EncodingFormat.FORMAT_DER); + expect(1).assertEqual(cert.EncodingFormat.FORMAT_PEM); + expect(0).assertEqual(cryptoFramework.CryptoMode.ENCRYPT_MODE); + expect(1).assertEqual(cryptoFramework.CryptoMode.DECRYPT_MODE); +} + +export { testSecurityRandomEnumCommon }; diff --git a/security/cryptoFramework/js_api_test_two/src/main/js/test/utils/securityrandom/publicSecurityRandomPromise.js b/security/cryptoFramework/js_api_test_two/src/main/js/test/utils/securityrandom/publicSecurityRandomPromise.js index bb1eeba3d..e96bc900c 100644 --- a/security/cryptoFramework/js_api_test_two/src/main/js/test/utils/securityrandom/publicSecurityRandomPromise.js +++ b/security/cryptoFramework/js_api_test_two/src/main/js/test/utils/securityrandom/publicSecurityRandomPromise.js @@ -60,28 +60,4 @@ async function testSecurityRandomPromise(length) { }); } -function testSecurityRandomEnumPromise() { - expect(401).assertEqual(cryptoFramework.Result.INVALID_PARAMS); - expect(801).assertEqual(cryptoFramework.Result.NOT_SUPPORT); - expect(17620001).assertEqual(cryptoFramework.Result.ERR_OUT_OF_MEMORY); - expect(17620002).assertEqual(cryptoFramework.Result.ERR_RUNTIME_ERROR); - expect(17630001).assertEqual(cryptoFramework.Result.ERR_CRYPTO_OPERATION); - expect(17630002).assertEqual( - cryptoFramework.Result.ERR_CERT_SIGNATURE_FAILURE - ); - expect(17630003).assertEqual(cryptoFramework.Result.ERR_CERT_NOT_YET_VALID); - expect(17630004).assertEqual(cryptoFramework.Result.ERR_CERT_HAS_EXPIRED); - expect(17630005).assertEqual( - cryptoFramework.Result.ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY - ); - expect(17630006).assertEqual(cryptoFramework.Result.ERR_KEYUSAGE_NO_CERTSIGN); - expect(17630007).assertEqual( - cryptoFramework.Result.ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE - ); - expect(0).assertEqual(cryptoFramework.EncodingFormat.FORMAT_DER); - expect(1).assertEqual(cryptoFramework.EncodingFormat.FORMAT_PEM); - expect(0).assertEqual(cryptoFramework.CryptoMode.ENCRYPT_MODE); - expect(1).assertEqual(cryptoFramework.CryptoMode.DECRYPT_MODE); -} - -export { testSecurityRandomPromise, testSecurityRandomEnumPromise }; +export { testSecurityRandomPromise }; -- GitLab