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

!5855 【OpenHarmony 3.2.8.1】【安全子系统】【cryptoFramework】新增ActsCryptoFrameworkJSNormalTest测试套

Merge pull request !5855 from Qwink2016/qwink20221011
......@@ -19,6 +19,7 @@ group("security") {
deps = [
"access_token/AccessTokenTest_Normal_js:ActsAccessTokenApiJSNormalTest",
"cipher/datacipher:datacipher",
"cryptoFramework:ActsCryptoFrameworkJSNormalTest",
"security_huks_basic",
]
}
......
# 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("//test/xts/tools/build/suite.gni")
ohos_js_hap_suite("ActsCryptoFrameworkJSNormalTest") {
hap_profile = "./src/main/config.json"
deps = [
":hjs_demo_js_assets",
":hjs_demo_resources",
]
certificate_profile = "./signature/openharmony_sx.p7b"
hap_name = "ActsCryptoFrameworkJSNormalTest"
subsystem_name = "security"
part_name = "crypto_framework"
}
ohos_js_assets("hjs_demo_js_assets") {
js2abc = true
hap_profile = "./src/main/config.json"
source_dir = "./src/main/js"
}
ohos_resources("hjs_demo_resources") {
sources = [ "./src/main/resources" ]
hap_profile = "./src/main/config.json"
}
{
"description": "Configuration for hjunit demo Tests",
"driver": {
"type": "OHJSUnitTest",
"test-timeout": "900000",
"shell-timeout": "900000",
"bundle-name": "com.openharmony.cryptoFramework",
"package-name": "com.openharmony.cryptoFramework"
},
"kits": [
{
"test-file-name": ["ActsCryptoFrameworkJSNormalTest.hap"],
"type": "AppInstallKit",
"cleanup-apps": true
},
{
"type": "ShellKit",
"run-command": ["remount", "mkdir /data/test"]
},
{
"type": "PushKit",
"push": [
"ActsCryptoFrameworkJSNormalTest.hap->/data/test/ActsCryptoFrameworkJSNormalTest.hap"
]
},
{
"type": "ShellKit",
"run-command": ["chmod 644 /data/test/*.hap", "chmod 777 -R data"]
}
]
}
{
"app": {
"bundleName": "com.openharmony.cryptoFramework",
"vendor": "acts",
"version": {
"code": 12,
"name": "1.0.0"
},
"apiVersion": {
"compatible": 4,
"target": 5,
"releaseType": "Beta1"
}
},
"deviceConfig": {},
"module": {
"package": "com.openharmony.cryptoFramework",
"name": ".entry",
"mainAbility": ".MainAbility",
"deviceType": ["phone", "tablet", "tv", "wearable"],
"distro": {
"deliveryWithInstall": true,
"moduleName": "entry",
"moduleType": "entry",
"installationFree": false
},
"abilities": [
{
"skills": [
{
"entities": ["entity.system.home"],
"actions": ["action.system.home"]
}
],
"orientation": "unspecified",
"formsEnabled": false,
"name": ".MainAbility",
"srcLanguage": "js",
"srcPath": "MainAbility",
"icon": "$media:icon",
"description": "$string:MainAbility_desc",
"label": "$string:MainAbility_label",
"type": "page",
"visible": true,
"launchType": "standard"
},
{
"orientation": "unspecified",
"formsEnabled": false,
"name": ".TestAbility",
"srcLanguage": "js",
"srcPath": "TestAbility",
"icon": "$media:icon",
"description": "$string:TestAbility_desc",
"label": "$string:TestAbility_label",
"type": "page",
"visible": true,
"launchType": "standard"
}
],
"js": [
{
"pages": ["pages/index/index"],
"name": "default",
"window": {
"designWidth": 720,
"autoDesignWidth": true
}
},
{
"pages": ["pages/index/index"],
"name": ".TestAbility",
"window": {
"designWidth": 720,
"autoDesignWidth": false
}
}
],
"testRunner": {
"name": "OpenHarmonyTestRunner",
"srcPath": "TestRunner"
},
"srcPath": ""
}
}
/*
* 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.
*/
export default {
onCreate() {
console.info('SecurityApplication onCreate');
},
onDestroy() {
console.info('SecurityApplication onDestroy');
}
};
{
"strings": {
"hello": "Hello",
"world": "World"
}
}
\ No newline at end of file
{
"strings": {
"hello": "您好",
"world": "世界"
}
}
\ No newline at end of file
.container {
flex-direction: column;
justify-content: center;
align-items: center;
width: 100%;
height: 100%;
}
.title {
font-size: 40px;
color: #000000;
opacity: 0.9;
}
@media screen and (device-type: tablet) and (orientation: landscape) {
.title {
font-size: 100px;
}
}
@media screen and (device-type: wearable) {
.title {
font-size: 28px;
color: #FFFFFF;
}
}
@media screen and (device-type: tv) {
.container {
background-image: url("/common/images/Wallpaper.png");
background-size: cover;
background-repeat: no-repeat;
background-position: center;
}
.title {
font-size: 100px;
color: #FFFFFF;
}
}
@media screen and (device-type: phone) and (orientation: landscape) {
.title {
font-size: 60px;
}
}
<div class="container">
<text class="title">
{{ $t('strings.hello') }} {{ title }}
</text>
</div>
/*
* 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 fileio from '@ohos.fileio'
export default {
data: {
title: ""
},
onInit() {
this.title = this.$t('strings.world');
},
onShow() {
console.info('onShow start')
},
onReady() {
},
}
/*
* 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 AbilityDelegatorRegistry from '@ohos.application.abilityDelegatorRegistry'
import { Hypium } from '@ohos/hypium'
import testsuite from '../test/List.test'
export default {
onCreate() {
console.info('TestApplication onCreate')
var abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator()
var abilityDelegatorArguments = AbilityDelegatorRegistry.getArguments()
console.info('start run testcase!!!')
Hypium.hypiumTest(abilityDelegator, abilityDelegatorArguments, testsuite)
},
onDestroy() {
console.info("TestApplication onDestroy");
}
};
{
"strings": {
"hello": "Hello",
"world": "World"
},
"Files": {
}
}
\ No newline at end of file
{
"strings": {
"hello": "您好",
"world": "世界"
},
"Files": {
}
}
\ No newline at end of file
.container {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
left: 0px;
top: 0px;
width: 100%;
height: 100%;
}
.title {
font-size: 60px;
text-align: center;
width: 100%;
height: 40%;
margin: 10px;
}
@media screen and (device-type: phone) and (orientation: landscape) {
.title {
font-size: 60px;
}
}
@media screen and (device-type: tablet) and (orientation: landscape) {
.title {
font-size: 100px;
}
}
\ No newline at end of file
<div class="container">
<text class="title">
{{ $t('strings.hello') }} {{ title }}
</text>
</div>
/*
* 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.
*/
export default {
data: {
title: ""
},
onInit() {
this.title = this.$t('strings.world');
}
}
/*
* 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 AbilityDelegatorRegistry from '@ohos.application.abilityDelegatorRegistry'
function translateParamsToString(parameters) {
const keySet = new Set([
'-s class', '-s notClass', '-s suite', '-s itName',
'-s level', '-s testType', '-s size', '-s timeout',
'-s package', '-s dryRun'
])
let targetParams = '';
for (const key in parameters) {
if (keySet.has(key)) {
targetParams += ' ' + key + ' ' + parameters[key]
}
}
return targetParams.trim()
}
export default {
onPrepare() {
console.info('OpenHarmonyTestRunner OnPrepare')
},
onRun() {
console.log('OpenHarmonyTestRunner onRun run')
var abilityDelegatorArguments = AbilityDelegatorRegistry.getArguments()
var abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator()
var testAbilityName = abilityDelegatorArguments.parameters['-p'] + '.TestAbility'
var cmd = 'aa start -d 0 -a ' + testAbilityName + ' -b ' + abilityDelegatorArguments.bundleName
cmd += ' ' + translateParamsToString(abilityDelegatorArguments.parameters)
var debug = abilityDelegatorArguments.parameters["-D"]
console.info('debug value : '+debug)
if (debug == 'true')
{
cmd += ' -D'
}
console.info('cmd : '+cmd)
abilityDelegator.executeShellCommand(cmd, (err, data) => {
console.info('executeShellCommand : err : ' + JSON.stringify(err));
console.info('executeShellCommand : data : ' + data.stdResult);
console.info('executeShellCommand : data : ' + data.exitCode);
})
}
};
/*
* 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 { describe, beforeAll, afterEach, it, expect } from "@ohos/hypium";
import * as asyPromise from "./utils/asymmetric/publicAsymmetricPromise";
import * as asyCallback from "./utils/asymmetric/publicAsymmetricCallback";
export default function AsymmetricCryptographyJsunit() {
describe("AsymmetricCryptographyJsunit", function () {
console.info("##########start AsymmetricCryptographyJsunit##########");
beforeAll(function () {});
afterEach(function () {});
/**
* @tc.number Security_crypto_framework_ASymmetric_Encryption_RSA_0100
* @tc.name Test RSA1024|PRIMES_2 normal encryption and decryption
* @tc.desc Use the Callback Style of Interface
*/
it(
"Security_crypto_framework_ASymmetric_Encryption_RSA_0100",
0,
async function (done) {
await asyCallback
.encryptAndDecryptNormalProcess(
"RSA1024|PRIMES_2",
"RSA1024|PKCS1|SHA256"
)
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
console.error(
"Security_crypto_framework_ASymmetric_Encryption_RSA_0100 catch error: " +
err
);
expect(null).assertFail();
});
done();
}
);
/**
* @tc.number Security_crypto_framework_ASymmetric_Encryption_RSA_0200
* @tc.name Test RSA2048|PRIMES_2 normal encryption and decryption
* @tc.desc Use the Promise Style of Interface
*/
it(
"Security_crypto_framework_ASymmetric_Encryption_RSA_0200",
0,
async function (done) {
await asyPromise
.encryptAndDecryptNormalProcess(
"RSA2048|PRIMES_2",
"RSA2048|PKCS1|MD5"
)
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
console.error(
"Security_crypto_framework_ASymmetric_Encryption_RSA_0200 catch error: " +
err
);
expect(null).assertFail();
});
done();
}
);
/**
* @tc.number Security_crypto_framework_ASymmetric_SignVerify_RSA_0100
* @tc.name Test RSA1024|PKCS1|MD5 normal sign and verify
* @tc.desc Use the Callback Style of Interface
*/
it(
"Security_crypto_framework_ASymmetric_SignVerify_RSA_0100",
0,
async function (done) {
await asyCallback
.signAndVerifyNormalProcess("RSA1024|PRIMES_2", "RSA1024|PKCS1|MD5")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
console.error(
"Security_crypto_framework_ASymmetric_SignVerify_RSA_0100 catch error: " +
err
);
expect(null).assertFail();
});
done();
}
);
/**
* @tc.number Security_crypto_framework_ASymmetric_SignVerify_RSA_0200
* @tc.name Test RSA2048|PKCS1|SHA1 normal sign and verify
* @tc.desc Use the Promise Style of Interface
*/
it(
"Security_crypto_framework_ASymmetric_SignVerify_RSA_0200",
0,
async function (done) {
await asyPromise
.signAndVerifyNormalProcess("RSA2048|PRIMES_2", "RSA2048|PKCS1|SHA1")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
console.error(
"Security_crypto_framework_ASymmetric_SignVerify_RSA_0200 catch error: " +
err
);
expect(null).assertFail();
});
done();
}
);
/**
* @tc.number Security_crypto_framework_ASymmetric_SignVerify_ECDH_0100
* @tc.name Test ECC224 SignVerify ECDH
* @tc.desc Use the Promise Style of Interface
*/
it(
"Security_crypto_framework_ASymmetric_SignVerify_ECDH_0100",
0,
async function (done) {
await asyPromise
.keyAgreementProcess("ECC224")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
console.error(
"Security_crypto_framework_ASymmetric_SignVerify_ECDH_0100 catch error: " +
err
);
expect(null).assertFail();
});
done();
}
);
/**
* @tc.number Security_crypto_framework_ASymmetric_SignVerify_ECDH_0200
* @tc.name Test ECC256 SignVerify ECDH
* @tc.desc Use the Callback Style of Interface
*/
it(
"Security_crypto_framework_ASymmetric_SignVerify_ECDH_0200",
0,
async function (done) {
await asyCallback
.keyAgreementProcess("ECC256")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
console.error(
"Security_crypto_framework_ASymmetric_SignVerify_ECDH_0200 catch error: " +
err
);
expect(null).assertFail();
});
done();
}
);
/**
* @tc.number Security_crypto_framework_ASymmetric_Encryption_RSA_3300
* @tc.name Test convertKey
* @tc.desc Use RSA3072|PRIMES_2 algorithm
*/
it(
"Security_crypto_framework_ASymmetric_Encryption_RSA_3300",
0,
async function (done) {
await asyPromise
.convertKeyEncryptAndDecryptProcess("RSA3072|PRIMES_2")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
console.error(
"Security_crypto_framework_ASymmetric_Encryption_RSA_3300 catch error: " +
err
);
expect(null).assertFail();
});
done();
}
);
/**
* @tc.number Security_crypto_framework_ASymmetric_Encryption_RSA_3100
* @tc.name Test convertKey
* @tc.desc Use RSA1024|PRIMES_2 algorithm
*/
it(
"Security_crypto_framework_ASymmetric_Encryption_RSA_3100",
0,
async function (done) {
await asyPromise
.AsyPriKeyClearProcess("RSA1024|PRIMES_2")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
console.error(
"Security_crypto_framework_ASymmetric_Encryption_RSA_3100 catch error: " +
err
);
expect(null).assertFail();
});
done();
}
);
});
}
/*
* 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 { describe, beforeAll, afterEach, it, expect } from "@ohos/hypium";
import * as certPromise from "./utils/certificate/publicCertificatePromise";
import * as certCallback from "./utils/certificate/publicCertificateCallback";
export default function CertificateJsunit() {
describe("CertificateJsunit", function () {
console.info("##########start CertificateJsunit##########");
beforeAll(function () {});
afterEach(function () {});
/**
* @tc.number Security_crypto_framework_X509Cert_0100
* @tc.name The X509 certificate list can be created for encryption and decryption.
* @tc.desc The X509 certificate FORMAT is FORMAT_DER
*/
it("Security_crypto_framework_X509Cert_0100", 0, async function (done) {
await certPromise
.createX509CertInstancePromise("der")
.then((data) => {
expect(data != null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
await certCallback
.createX509CertInstanceCallback("der")
.then((data) => {
expect(data != null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_X509Cert_0200
* @tc.name The X509 certificate list can be created for encryption and decryption.
* @tc.desc The X509 certificate FORMAT is FORMAT_PEM
*/
it("Security_crypto_framework_X509Cert_0200", 0, async function (done) {
await certPromise
.createX509CertInstancePromise("pem")
.then((data) => {
expect(data != null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
await certCallback
.createX509CertInstanceCallback("pem")
.then((data) => {
expect(data != null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_X509Cert_0700
* @tc.name check getEncoded interface
* @tc.desc The X509 certificate FORMAT is FORMAT_DER
*/
it("Security_crypto_framework_X509Cert_0700", 0, async function (done) {
await certPromise
.checkGetEncodedX509CertPromise("der")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
await certCallback
.checkGetEncodedX509CertCallback("der")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_X509Cert_0800
* @tc.name check getPublicKey interface
* @tc.desc The X509 certificate FORMAT is FORMAT_DER
*/
it("Security_crypto_framework_X509Cert_0800", 0, async function (done) {
await certPromise
.checkGetPublicKeyX509CertPromise("der")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
await certCallback
.checkGetPublicKeyX509CertCallback("der")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_X509Cert_0900
* @tc.name check validity of the date for X509 certificate
* @tc.desc The X509 certificate FORMAT is FORMAT_DER,date format:20220922145628+0800(YmdHMSz);
* @tc.desc Use the Promise Style of Interface
*/
it("Security_crypto_framework_X509Cert_0900", 0, async function (done) {
await certPromise
.checkValidityX509CertPromise("der", "20220830000000+0800")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_X509Cert_1200
* @tc.name getVersion for X509 certificate
* @tc.desc The X509 certificate FORMAT is FORMAT_DER
* @tc.desc Use the Callback Style of Interface
*/
it("Security_crypto_framework_X509Cert_1200", 0, async function (done) {
await certCallback
.getX509CertInfoCallback("der", "getVersion")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_X509Cert_1300
* @tc.name getSerialNumber for X509 certificate
* @tc.desc The X509 certificate FORMAT is FORMAT_DER
* @tc.desc Use the Callback Style of Interface
*/
it("Security_crypto_framework_X509Cert_1300", 0, async function (done) {
await certCallback
.getX509CertInfoCallback("der", "getSerialNumber")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_X509Cert_1400
* @tc.name getIssuerName for X509 certificate
* @tc.desc The X509 certificate FORMAT is FORMAT_DER
* @tc.desc Use the Callback Style of Interface
*/
it("Security_crypto_framework_X509Cert_1400", 0, async function (done) {
await certCallback
.getX509CertInfoCallback("der", "getIssuerName")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_X509Cert_1500
* @tc.name getSubjectName for X509 certificate
* @tc.desc The X509 certificate FORMAT is FORMAT_DER
* @tc.desc Use the Promise Style of Interface
*/
it("Security_crypto_framework_X509Cert_1500", 0, async function (done) {
await certPromise
.getX509CertInfoPromise("der", "getSubjectName")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_X509Cert_1600
* @tc.name getNotBeforeTime for X509 certificate
* @tc.desc The X509 certificate FORMAT is FORMAT_DER
* @tc.desc Use the Promise Style of Interface
*/
it("Security_crypto_framework_X509Cert_1600", 0, async function (done) {
await certPromise
.getX509CertInfoPromise("der", "getNotBeforeTime")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_X509Cert_1700
* @tc.name getNotAfterTime for X509 certificate
* @tc.desc The X509 certificate FORMAT is FORMAT_DER
* @tc.desc Use the Promise Style of Interface
*/
it("Security_crypto_framework_X509Cert_1700", 0, async function (done) {
await certPromise
.getX509CertInfoPromise("der", "getNotAfterTime")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_X509Cert_1800
* @tc.name getSignature for X509 certificate
* @tc.desc The X509 certificate FORMAT is FORMAT_DER
* @tc.desc Use the Promise Style of Interface
*/
it("Security_crypto_framework_X509Cert_1800", 0, async function (done) {
await certPromise
.getX509CertInfoPromise("der", "getSignature")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_X509Cert_1900
* @tc.name getSignatureAlgName for X509 certificate
* @tc.desc The X509 certificate FORMAT is FORMAT_DER
* @tc.desc Use the Promise Style of Interface
*/
it("Security_crypto_framework_X509Cert_1900", 0, async function (done) {
await certPromise
.getX509CertInfoPromise("der", "getSignatureAlgName")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_X509Cert_2000
* @tc.name getSignatureAlgOid for X509 certificate
* @tc.desc The X509 certificate FORMAT is FORMAT_DER
* @tc.desc Use the Callback Style of Interface
*/
it("Security_crypto_framework_X509Cert_2000", 0, async function (done) {
await certCallback
.getX509CertInfoCallback("der", "getSignatureAlgOid")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_X509Cert_2100
* @tc.name getSignatureAlgParams for X509 certificate
* @tc.desc The X509 certificate FORMAT is FORMAT_DER
* @tc.desc Use the Callback Style of Interface
*/
it("Security_crypto_framework_X509Cert_2100", 0, async function (done) {
await certCallback
.getX509CertInfoCallback("der", "getSignatureAlgParams")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_X509Cert_2400
* @tc.name getKeyUsage for X509 certificate
* @tc.desc The X509 certificate FORMAT is FORMAT_DER
* @tc.desc Use the Callback Style of Interface
*/
it("Security_crypto_framework_X509Cert_2400", 0, async function (done) {
await certCallback
.getX509CertInfoCallback("der", "getKeyUsage")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_X509Cert_2500
* @tc.name getExtKeyUsage for X509 certificate
* @tc.desc The X509 certificate FORMAT is FORMAT_DER
* @tc.desc Use the Callback Style of Interface
*/
it("Security_crypto_framework_X509Cert_2500", 0, async function (done) {
await certCallback
.getX509CertInfoCallback("der", "getExtKeyUsage")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_X509Cert_2600
* @tc.name getBasicConstraints for X509 certificate
* @tc.desc The X509 certificate FORMAT is FORMAT_DER
* @tc.desc Use the Callback Style of Interface
*/
it("Security_crypto_framework_X509Cert_2600", 0, async function (done) {
await certCallback
.getX509CertInfoCallback("der", "getBasicConstraints")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_X509Cert_2700
* @tc.name getSubjectAltNames for X509 certificate
* @tc.desc The X509 certificate FORMAT is FORMAT_DER
* @tc.desc Use the Promise Style of Interface
*/
it("Security_crypto_framework_X509Cert_2700", 0, async function (done) {
await certPromise
.getX509CertInfoPromise("der", "getSubjectAltNames")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_X509Cert_2800
* @tc.name getIssuerAltNames for X509 certificate
* @tc.desc The X509 certificate FORMAT is FORMAT_DER
* @tc.desc Use the Promise Style of Interface
*/
it("Security_crypto_framework_X509Cert_2800", 0, async function (done) {
await certPromise
.getX509CertInfoPromise("der", "getIssuerAltNames")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
});
}
/*
* 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 { describe, beforeAll, afterEach, it, expect } from "@ohos/hypium";
import * as certPromise from "./utils/certificate/publicCertificatePromise";
import * as certCallback from "./utils/certificate/publicCertificateCallback";
export default function CertificateCrlJsunit() {
describe("CertificateCrlJsunit", function () {
console.info("##########start CertificateCrlJsunit##########");
beforeAll(function () {});
afterEach(function () {});
/**
* @tc.number Security_crypto_framework_X509CRL_0100
* @tc.name The X509 certificate list can be revoked for encryption and decryption.
* @tc.desc The X509 certificate FORMAT is FORMAT_DER
*/
it("Security_crypto_framework_X509CRL_0100", 0, async function (done) {
await certPromise
.createX509CrlInstancePromise("der")
.then((data) => {
expect(data != null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
await certCallback
.createX509CrlInstanceCallback("der")
.then((data) => {
expect(data != null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_X509CRL_0200
* @tc.name The X509 certificate list can be revoked for encryption and decryption.
* @tc.desc The X509 certificate FORMAT is FORMAT_PEM
*/
it("Security_crypto_framework_X509CRL_0200", 0, async function (done) {
await certPromise
.createX509CrlInstancePromise("pem")
.then((data) => {
expect(data != null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
await certCallback
.createX509CrlInstanceCallback("pem")
.then((data) => {
expect(data != null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_X509CRL_0300
* @tc.name Check isRevoked for X509 Crl
* @tc.desc The X509 Crl FORMAT is FORMAT_PEM
*/
it("Security_crypto_framework_X509CRL_0300", 0, async function (done) {
await certPromise
.createX509CrlInstancePromise("pem")
.then((crlInstance) => {
return certPromise.checkIsRevokedX509CrlPromise(
crlInstance,
"normal"
);
})
.then((result) => {
expect(result == true).assertTrue();
})
.catch((err) => {
console.error("[promise] isRevoked failed! error is: " + err);
expect(null).assertFail();
});
await certPromise
.createX509CrlInstancePromise("pem")
.then((crlInstance) => {
return certCallback.checkIsRevokedX509CrlCallback(
crlInstance,
"normal"
);
})
.then((result) => {
expect(result == true).assertTrue();
})
.catch((err) => {
console.error("[promise] isRevoked failed! error is: " + err);
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_X509CRL_0600
* @tc.name Check getEncoded for X509 Crl
* @tc.desc The X509 Crl FORMAT is FORMAT_PEM
*/
it("Security_crypto_framework_X509CRL_0600", 0, async function (done) {
await certPromise
.getX509CrlInfoPromise("pem", "getType")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_X509CRL_0700
* @tc.name Check getEncoded for X509 Crl
* @tc.desc The X509 Crl FORMAT is FORMAT_PEM
*/
it("Security_crypto_framework_X509CRL_0700", 0, async function (done) {
await certPromise
.checkGetEncodedX509CrlPromise("pem")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
await certCallback
.checkGetEncodedX509CrlCallback("pem")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_X509CRL_1200
* @tc.name check getVersion for X509 Crl
* @tc.desc The X509 certificate FORMAT is FORMAT_PEM
* @tc.desc Use the Promise Style of Interface
*/
it("Security_crypto_framework_X509CRL_1200", 0, async function (done) {
await certPromise
.getX509CrlInfoPromise("pem", "getVersion")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_X509CRL_1300
* @tc.name check getIssuerName for X509 Crl
* @tc.desc The X509 certificate FORMAT is FORMAT_PEM
* @tc.desc Use the Promise Style of Interface
*/
it("Security_crypto_framework_X509CRL_1300", 0, async function (done) {
await certPromise
.getX509CrlInfoPromise("pem", "getIssuerName")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_X509CRL_1400
* @tc.name check getLastUpdate for X509 Crl
* @tc.desc The X509 certificate FORMAT is FORMAT_PEM
* @tc.desc Use the Promise Style of Interface
*/
it("Security_crypto_framework_X509CRL_1400", 0, async function (done) {
await certPromise
.getX509CrlInfoPromise("pem", "getLastUpdate")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_X509CRL_1500
* @tc.name check getNextUpdate for X509 Crl
* @tc.desc The X509 certificate FORMAT is FORMAT_PEM
* @tc.desc Use the Promise Style of Interface
*/
it("Security_crypto_framework_X509CRL_1500", 0, async function (done) {
await certPromise
.getX509CrlInfoPromise("pem", "getNextUpdate")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_X509CRL_1600
* @tc.name check getRevokedCert for X509 Crl
* @tc.desc The X509 certificate FORMAT is FORMAT_PEM
*/
it("Security_crypto_framework_X509CRL_1600", 0, async function (done) {
await certCallback
.checkGetRevokedCertX509CrlCallback("pem")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
await certPromise
.checkGetRevokedCertX509CrlPromise("pem")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_X509CRL_1700
* @tc.name check getRevokedCert for X509 Crl
* @tc.desc The X509 certificate FORMAT is FORMAT_PEM
* @tc.desc Use the Promise Style of Interface
*/
it("Security_crypto_framework_X509CRL_1700", 0, async function (done) {
await certPromise
.createX509CrlInstancePromise("pem")
.then((crlInstance) => {
return certPromise.checkGetRevokedCertWithCertX509CrlPromise(
crlInstance,
"normal"
);
})
.then((result) => {
expect(result != null).assertTrue();
})
.catch((err) => {
console.error(
"[promise] checkgetRevokedCertWithCertX509CrlPromis failed! error is: " +
err
);
expect(null).assertFail();
});
await certPromise
.createX509CrlInstancePromise("pem")
.then((crlInstance) => {
return certCallback.checkGetRevokedCertWithCertX509CrlCallback(
crlInstance,
"normal"
);
})
.then((result) => {
expect(result != null).assertTrue();
})
.catch((err) => {
console.error(
"[callback] checkgetRevokedCertWithCertX509CrlCallback failed! error is: " +
err
);
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_X509CRL_1800
* @tc.name check getRevokedCerts for X509 Crl
* @tc.desc The X509 certificate FORMAT is FORMAT_PEM
*/
it("Security_crypto_framework_X509CRL_1800", 0, async function (done) {
await certCallback
.checkGetRevokedCertsX509CrlCallback("pem")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
await certPromise
.checkGetRevokedCertsX509CrlPromise("pem")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_X509CRL_1900
* @tc.name check getTbsInfo for X509 Crl
* @tc.desc The X509 certificate FORMAT is FORMAT_PEM
*/
it("Security_crypto_framework_X509CRL_1900", 0, async function (done) {
await certPromise
.getX509CrlInfoPromise("pem", "getTbsInfo")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
await certCallback
.getX509CrlInfoCallback("pem", "getTbsInfo")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_X509CRL_2000
* @tc.name check getSignature for X509 Crl
* @tc.desc The X509 certificate FORMAT is FORMAT_PEM
*/
it("Security_crypto_framework_X509CRL_2000", 0, async function (done) {
await certPromise
.getX509CrlInfoPromise("pem", "getSignature")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_X509CRL_2100
* @tc.name check getSignatureAlgName for X509 Crl
* @tc.desc The X509 certificate FORMAT is FORMAT_PEM
*/
it("Security_crypto_framework_X509CRL_2100", 0, async function (done) {
await certPromise
.getX509CrlInfoPromise("pem", "getSignatureAlgName")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_X509CRL_2200
* @tc.name check getSignatureAlgOid for X509 Crl
* @tc.desc The X509 certificate FORMAT is FORMAT_PEM
*/
it("Security_crypto_framework_X509CRL_2200", 0, async function (done) {
await certPromise
.getX509CrlInfoPromise("pem", "getSignatureAlgOid")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_X509CRL_2300
* @tc.name check getSignatureAlgOid for X509 Crl
* @tc.desc The X509 certificate FORMAT is FORMAT_PEM
*/
it("Security_crypto_framework_X509CRL_2300", 0, async function (done) {
await certPromise
.getX509CrlInfoPromise("pem", "getSignatureAlgParams")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_X509CRL_2400
* @tc.name check getRevokedCerts for X509 Crl
* @tc.desc The X509 certificate FORMAT is FORMAT_PEM
*/
it("Security_crypto_framework_X509CRL_2400", 0, async function (done) {
await certCallback
.checkGetRevokedCertX509CrlCallback("pem")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
await certPromise
.checkGetRevokedCertX509CrlPromise("pem")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_X509CRL_2500
* @tc.name check getRevokedCerts for X509 Crl
* @tc.desc The X509 certificate FORMAT is FORMAT_PEM
*/
it("Security_crypto_framework_X509CRL_2500", 0, async function (done) {
await certCallback
.checkGetRevokedCertX509CrlCallback("pem")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
await certPromise
.checkGetRevokedCertX509CrlPromise("pem")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_X509CRL_2600
* @tc.name check getRevokedCerts for X509 Crl
* @tc.desc The X509 certificate FORMAT is FORMAT_PEM
*/
it("Security_crypto_framework_X509CRL_2600", 0, async function (done) {
await certCallback
.checkGetRevokedCertX509CrlCallback("pem")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
await certPromise
.checkGetRevokedCertX509CrlPromise("pem")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_X509CRL_2700
* @tc.name check getRevokedCerts for X509 Crl
* @tc.desc The X509 certificate FORMAT is FORMAT_PEM
*/
it("Security_crypto_framework_X509CRL_2700", 0, async function (done) {
await certCallback
.checkGetRevokedCertX509CrlCallback("pem")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
await certPromise
.checkGetRevokedCertX509CrlPromise("pem")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
});
}
/*
* 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 { describe, beforeAll, afterEach, it, expect } from "@ohos/hypium";
import {
testHMACDigestPromise,
testMDDigestPromise,
} from "./utils/digestalgorithm/publicDigestPromise";
import {
testHMACDigestCallback,
testMDDigestCallback,
} from "./utils/digestalgorithm/publicDigestCallback";
export default function DigestAlgorithmJsunit() {
describe("DigestAlgorithmJsunit", function () {
console.info("##########start DigestAlgorithmJsunit##########");
beforeAll(function () {});
afterEach(function () {});
/**
* @tc.number Security_crypto_framework_MD_0100
* @tc.name Test MD5 for MD digest algotithm
* @tc.desc Use the Promise Style of Interface
*/
it("Security_crypto_framework_MD_0100", 0, async function (done) {
await testMDDigestPromise("MD5")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_MD_0500
* @tc.name Test SHA384 for MD digest algotithm
* @tc.desc Use the Callback Style of Interface
*/
it("Security_crypto_framework_MD_0500", 0, async function (done) {
await testMDDigestCallback("SHA384")
.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
* @tc.desc Use the Callback Style of Interface
*/
it("Security_crypto_framework_HMAC_0100", 0, async function (done) {
await testHMACDigestCallback("SHA1", "AES128")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_HMAC_0200
* @tc.name Test SHA1 for HMAC digest algorithm
* @tc.desc Use the Promise Style of Interface
*/
it("Security_crypto_framework_HMAC_0200", 0, async function (done) {
await testHMACDigestPromise("SHA224", "AES192")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
});
}
/*
* 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 SymmetricCryptographyJsunit from "./SymmetricCryptography.test";
import AsymmetricCryptographyJsunit from "./AsymmetricCryptography.test";
import SecurityRandomJsunit from "./SecurityRandom.test";
import DigestAlgorithmJsunit from "./DigestAlgorithm.test";
import CertificateJsunit from "./Certificate.test";
import CertificateCrlJsunit from "./CertificateCrl.test";
export default function testsuite() {
SymmetricCryptographyJsunit();
AsymmetricCryptographyJsunit();
SecurityRandomJsunit();
CertificateJsunit();
CertificateCrlJsunit();
DigestAlgorithmJsunit();
}
/*
* 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 { describe, beforeAll, afterEach, it, expect } from "@ohos/hypium";
import {
testSecurityRandomPromise,
testSecurityRandomEnumPromise,
} from "./utils/securityrandom/publicSecurityRandomPromise";
import { testSecurityRandomCallback } from "./utils/securityrandom/publicSecurityRandomCallback";
export default function SecurityRandomJsunit() {
describe("SecurityRandomJsunit", function () {
console.info("##########start SecurityRandomJsunit##########");
beforeAll(function () {});
afterEach(function () {});
/**
* @tc.number Security_crypto_framework_Random_0100
* @tc.name support security random and set random seed
* @tc.desc the length of random is 32, use promise style of interface
*/
it("Security_crypto_framework_Random_0100", 0, async function (done) {
await testSecurityRandomPromise(32)
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_Random_0200
* @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_0200", 0, async function (done) {
await testSecurityRandomCallback(32)
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_Random_0500
* @tc.name test enumerated values
* @tc.desc cover 100% Enumerated values
*/
it("Security_crypto_framework_Random_0500", 0, async function (done) {
testSecurityRandomEnumPromise();
done();
});
});
}
/*
* 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 { describe, beforeAll, afterEach, it, expect } from "@ohos/hypium";
import * as symPromise from "./utils/symmetric/publicSymmetricPromise";
import * as symCallback from "./utils/symmetric/publicSymmetricCallback";
export default function SymmetricCryptographyJsunit() {
describe("SymmetricCryptographyJsunit", function () {
console.info("##########start SymmetricCryptographyJsunit##########");
beforeAll(function () {});
afterEach(function () {});
/**
* @tc.number Security_crypto_framework_Symmetric_Encryption_AES_0100
* @tc.name Test AES128|GCM|PKCS7 normal encryption and decryption
* @tc.desc Use the Callback Style of Interface
*/
it(
"Security_crypto_framework_Symmetric_Encryption_AES_0100",
0,
async function (done) {
await symCallback
.encryptAndDecryptNormalProcess(
"AES128",
"AES128|GCM|PKCS7",
"genGcmParamsSpec"
)
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
console.error(
"Security_crypto_framework_Symmetric_Encryption_AES_0100 catch error: " +
err
);
expect(null).assertFail();
});
done();
}
);
/**
* @tc.number Security_crypto_framework_Symmetric_Encryption_AES_0700
* @tc.name Test AES256|0FB|PKCS7 normal encryption and decryption
* @tc.desc Use the Promise Style of Interface
*/
it(
"Security_crypto_framework_Symmetric_Encryption_AES_0700",
0,
async function (done) {
await symPromise
.encryptAndDecryptNormalProcess(
"AES256",
"AES256|OFB|PKCS7",
"genIvParamsSpec"
)
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
console.error(
"Security_crypto_framework_Symmetric_Encryption_AES_0700 catch error: " +
err
);
expect(null).assertFail();
});
done();
}
);
/**
* @tc.number Security_crypto_framework_Symmetric_Encryption_AES_2100
* @tc.name Test AES128|GCM|PKCS7 convertKey encryption and decryption
* @tc.desc Use the Callback Style of Interface
*/
it(
"Security_crypto_framework_Symmetric_Encryption_AES_2100",
0,
async function (done) {
await symCallback
.convertKeyEncryptAndDecryptProcess(
"AES128",
"AES128|GCM|PKCS7",
"genGcmParamsSpec"
)
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
console.error(
"Security_crypto_framework_Symmetric_Encryption_AES_2100 catch error: " +
err
);
expect(null).assertFail();
});
done();
}
);
/**
* @tc.number Security_crypto_framework_Symmetric_Encryption_AES_2300
* @tc.name Test AES256|GCM|PKCS7 convertKey encryption and decryption
* @tc.desc Use the Promise Style of Interface
*/
it(
"Security_crypto_framework_Symmetric_Encryption_AES_2300",
0,
async function (done) {
await symPromise
.convertKeyEncryptAndDecryptProcess(
"AES256",
"AES256|GCM|PKCS7",
"genGcmParamsSpec"
)
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
console.error(
"Security_crypto_framework_Symmetric_Encryption_AES_2300 catch error: " +
err
);
expect(null).assertFail();
});
done();
}
);
/**
* @tc.number Security_crypto_framework_Symmetric_Encryption_AES_3700
* @tc.name Test clearMem interface
* @tc.desc Use the Promise Style of Interface
*/
it(
"Security_crypto_framework_Symmetric_Encryption_AES_3700",
0,
async function (done) {
await symPromise
.ClearMemProcess("AES256")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
console.error(
"Security_crypto_framework_Symmetric_Encryption_AES_3700 catch error: " +
err
);
expect(null).assertFail();
});
done();
}
);
});
}
/*
* 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 {
stringTouInt8Array,
uInt8ArrayToShowStr,
uInt8ArrayToString,
} from "../common/publicDoString";
import {
createAsyKeyGenerator,
createAsyCipher,
createAsySign,
createAsyVerify,
createAsyKeyAgreement,
} from "./publicAsymmetricCommon";
async function generateAsyKeyPair(rsaGenerator) {
var pubKey;
var priKey;
return new Promise((resolve, reject) => {
rsaGenerator.generateKeyPair((err, rsaKeyPair) => {
if (err) {
console.error("[Callback]generateSymKey failed. error is " + err);
reject(err);
} else {
pubKey = rsaKeyPair.pubKey;
let encodedPubKey = pubKey.getEncoded();
console.log("[Callback]: pubKey.getAlgorithm= " + pubKey.algName);
console.log("[Callback]: pubKey.getEncoded= " + encodedPubKey.data);
console.log("[Callback]: pubKey.getFormat= " + pubKey.format);
priKey = rsaKeyPair.priKey;
let encodedPriKey = priKey.getEncoded();
console.log("[Callback]: priKey.getAlgorithm= " + priKey.algName);
console.log("[Callback]: priKey.getEncoded= " + encodedPriKey.data);
console.log("[Callback]: priKey.getFormat= " + priKey.format);
resolve(rsaKeyPair);
}
});
});
}
async function convertAsyKey(rsaGenerator, pubKeyDataBlob, priKeyDataBlob) {
return new Promise((resolve, reject) => {
rsaGenerator.convertKey(
pubKeyDataBlob,
priKeyDataBlob,
(err, convertKeyPair) => {
if (err) {
console.error("[Callback]convertKey failed. error is " + err);
reject(err);
} else {
console.log(
"[Callback]convertKey success. convertKeyPair is " + convertKeyPair
);
resolve(convertKeyPair);
}
}
);
});
}
async function initCipher(cipherGenerator, mode, key, params) {
return new Promise((resolve, reject) => {
cipherGenerator.init(mode, key, params, (err) => {
if (err) {
console.error(
"[Callback]cipherGenerator init failed. error is " +
err +
"mode is " +
mode
);
reject(err);
} else {
console.log(
"[Callback]cipherGenerator init success! mode is : " + mode
);
resolve("init success");
}
});
});
}
async function doFinalCipher(cipherGenerator, mode, dataBlob) {
return new Promise((resolve, reject) => {
cipherGenerator.doFinal(dataBlob, (err, finalData) => {
if (err) {
console.error(
"[Callback]cipherGenerator doFinal failed. error is " +
err +
"mode is " +
mode
);
reject(err);
} else {
console.log(
"[Callback]cipherGenerator doFinal success! mode is : " + mode
);
resolve(finalData);
}
});
});
}
async function initSign(signGenerator, priKey) {
return new Promise((resolve, reject) => {
signGenerator.init(priKey, (err) => {
if (err) {
console.error("[Callback]signGenerator init failed. error is " + err);
reject(err);
} else {
console.log("[Callback]signGenerator init success!");
resolve("init success");
}
});
});
}
async function updateSign(signGenerator, dataBlob) {
return new Promise((resolve, reject) => {
signGenerator.update(dataBlob, (err) => {
if (err) {
console.error("[Callback]signGenerator update failed. error is " + err);
reject(err);
} else {
console.log("[Callback]signGenerator update success!");
resolve("update success");
}
});
});
}
async function signForSign(signGenerator, dataBlob) {
return new Promise((resolve, reject) => {
signGenerator.sign(dataBlob, (err, signOutput) => {
if (err) {
console.error("[Callback]signGenerator sign failed. error is " + err);
reject(err);
} else {
console.log("[Callback]signGenerator sign success!");
resolve(signOutput);
}
});
});
}
async function initVerify(verifyGenerator, pubKey) {
return new Promise((resolve, reject) => {
verifyGenerator.init(pubKey, (err) => {
if (err) {
console.error("[Callback]verifyGenerator init failed. error is " + err);
reject(err);
} else {
console.log("[Callback]verifyGenerator init success!");
resolve("init success");
}
});
});
}
async function updateVerify(verifyGenerator, dataBlob) {
return new Promise((resolve, reject) => {
verifyGenerator.update(dataBlob, (err) => {
if (err) {
console.error(
"[Callback]verifyGenerator update failed. error is " + err
);
reject(err);
} else {
console.log("[Callback]verifyGenerator update success!");
resolve("update success");
}
});
});
}
async function verifyForVerify(verifyGenerator, dataBlob, signDataBlob) {
return new Promise((resolve, reject) => {
verifyGenerator.verify(dataBlob, signDataBlob, (err, verifyOutput) => {
if (err) {
console.error("[Callback]signGenerator sign failed. error is " + err);
reject(err);
} else {
console.log("[Callback]signGenerator sign success!");
resolve(verifyOutput);
}
});
});
}
async function generateAsySecret(generator, priKey, pubKey) {
return new Promise((resolve, reject) => {
generator.generateSecret(priKey, pubKey, (err, output) => {
if (err) {
console.error("[Callback]generateSecret failed. error is " + err);
reject(err);
} else {
console.log("[Callback]generateSecret success!");
resolve(output);
}
});
});
}
async function encryptAndDecryptNormalProcess(asyAlgoName, cipherAlgoName) {
var globalCipherText;
var globalPubKey;
var globalPriKey;
var globalText = "This is a cipher test";
var input = { data: stringTouInt8Array(globalText) };
var encryptMode = cryptoFramework.CryptoMode.ENCRYPT_MODE;
var decryptMode = cryptoFramework.CryptoMode.DECRYPT_MODE;
return new Promise((resolve, reject) => {
var rsaGenerator = createAsyKeyGenerator(asyAlgoName);
expect(rsaGenerator != null).assertTrue();
var cipherGeneratorEncrypt = createAsyCipher(cipherAlgoName);
expect(cipherGeneratorEncrypt != null).assertTrue();
var cipherGeneratorDecrypt = createAsyCipher(cipherAlgoName);
expect(cipherGeneratorDecrypt != null).assertTrue();
generateAsyKeyPair(rsaGenerator)
.then((rsaKeyPair) => {
expect(rsaKeyPair != null).assertTrue();
globalPubKey = rsaKeyPair.pubKey;
globalPriKey = rsaKeyPair.priKey;
return initCipher(
cipherGeneratorEncrypt,
encryptMode,
globalPubKey,
null
);
})
.then((initData) => {
expect(initData === "init success").assertTrue();
return doFinalCipher(cipherGeneratorEncrypt, encryptMode, input);
})
.then((finalOutput) => {
expect(finalOutput != null).assertTrue();
globalCipherText = finalOutput;
console.log(
"cipherOutput: " + uInt8ArrayToShowStr(globalCipherText.data)
);
return initCipher(
cipherGeneratorDecrypt,
decryptMode,
globalPriKey,
null
);
})
.then((initData) => {
expect(initData === "init success").assertTrue();
return doFinalCipher(
cipherGeneratorDecrypt,
decryptMode,
globalCipherText
);
})
.then((finalOutput) => {
if (finalOutput == null) {
console.error("[Callback]decrypt doFinal out is null");
} else {
console.log(
"[Callback]decrypt doFinal out hex: " +
uInt8ArrayToShowStr(finalOutput.data)
);
}
let decryptData = uInt8ArrayToString(finalOutput.data);
expect(decryptData == globalText).assertTrue();
resolve();
})
.catch((err) => {
console.error(
"[Callback] encryptAndDecryptNormalProcess catch err:" + err
);
reject(err);
});
});
}
async function signAndVerifyNormalProcess(asyAlgoName, signVerifyAlgoName) {
var globalPubKey;
var globalPriKey;
var globalSignBlob;
var globalText = "This is a sign test";
var input = { data: stringTouInt8Array(globalText) };
return new Promise((resolve, reject) => {
var rsaGenerator = createAsyKeyGenerator(asyAlgoName);
expect(rsaGenerator != null).assertTrue();
var signGenerator = createAsySign(signVerifyAlgoName);
expect(signGenerator != null).assertTrue();
var verifyGenerator = createAsyVerify(signVerifyAlgoName);
expect(verifyGenerator != null).assertTrue();
generateAsyKeyPair(rsaGenerator)
.then((rsaKeyPair) => {
expect(rsaKeyPair != null).assertTrue();
globalPubKey = rsaKeyPair.pubKey;
globalPriKey = rsaKeyPair.priKey;
return initSign(signGenerator, globalPriKey);
})
.then((initData) => {
expect(initData === "init success").assertTrue();
return updateSign(signGenerator, input);
})
.then((updateData) => {
expect(updateData === "update success").assertTrue();
return signForSign(signGenerator, input);
})
.then((finalOutput) => {
expect(finalOutput != null).assertTrue();
globalSignBlob = finalOutput;
console.log("signOutput: " + uInt8ArrayToShowStr(globalSignBlob.data));
return initVerify(verifyGenerator, globalPubKey);
})
.then((initData) => {
expect(initData === "init success").assertTrue();
return updateVerify(verifyGenerator, input);
})
.then((updateData) => {
expect(updateData === "update success").assertTrue();
return verifyForVerify(verifyGenerator, input, globalSignBlob);
})
.then((finalStatus) => {
expect(finalStatus).assertTrue();
resolve();
})
.catch((err) => {
console.error("[Callback] signAndVerifyNormalProcess catch err:" + err);
reject(err);
});
});
}
async function convertKeyEncryptAndDecryptProcess(asyAlgoName) {
return new Promise((resolve, reject) => {
var rsaGenerator = createAsyKeyGenerator(asyAlgoName);
expect(rsaGenerator != null).assertTrue();
generateAsyKeyPair(rsaGenerator)
.then((rsaKeyPair) => {
expect(rsaKeyPair != null).assertTrue();
let encodedPubKey = rsaKeyPair.pubKey.getEncoded();
let encodedPriKey = rsaKeyPair.priKey.getEncoded();
return convertAsyKey(rsaGenerator, encodedPubKey, encodedPriKey);
})
.then((convertKeyPair) => {
expect(convertKeyPair != null).assertTrue();
let encodedConvertPubKey = convertKeyPair.pubKey.getEncoded();
console.warn(
"[Callback]: pubKey.getAlgorithm= " + convertKeyPair.pubKey.algName
);
console.warn(
"[Callback]: pubKey.getEncoded= " + encodedConvertPubKey.data
);
console.warn(
"[Callback]: pubKey.getFormat= " + convertKeyPair.pubKey.format
);
let encodedConvertPriKey = convertKeyPair.priKey.getEncoded();
console.warn(
"[Callback]: priKey.getAlgorithm= " + convertKeyPair.priKey.algName
);
console.warn(
"[Callback]: priKey.getEncoded= " + encodedConvertPriKey.data
);
console.warn(
"[Callback]: priKey.getFormat= " + convertKeyPair.priKey.format
);
resolve();
})
.catch((err) => {
console.error(
"[Callback] convertKeyEncryptAndDecryptProcess catch err:" + err
);
reject(err);
});
});
}
async function keyAgreementProcess(ECDHAlgoName) {
var globalPubKey;
var globalPriKey;
return new Promise((resolve, reject) => {
var rsaGenerator = createAsyKeyGenerator(ECDHAlgoName);
expect(rsaGenerator != null).assertTrue();
var globalECDHData = createAsyKeyAgreement(ECDHAlgoName);
expect(globalECDHData != null).assertTrue();
generateAsyKeyPair(rsaGenerator)
.then((rsaKeyPair) => {
expect(rsaKeyPair != null).assertTrue();
globalPubKey = rsaKeyPair.pubKey;
globalPriKey = rsaKeyPair.priKey;
return generateAsySecret(globalECDHData, globalPriKey, globalPubKey);
})
.then((result) => {
console.warn("result data is " + uInt8ArrayToShowStr(result.data));
expect(result != null).assertTrue();
resolve();
})
.catch((err) => {
console.error("[Callback] keyAgreementProcess catch err:" + err);
reject(err);
});
});
}
export {
encryptAndDecryptNormalProcess,
signAndVerifyNormalProcess,
convertKeyEncryptAndDecryptProcess,
keyAgreementProcess,
};
/*
* 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 cryptoFramework from "@ohos.security.cryptoFramework";
function createAsyKeyGenerator(asyAlgoName) {
try {
let rsaGenerator = cryptoFramework.createAsyKeyGenerator(asyAlgoName);
console.log("rsaGenerator algName: " + rsaGenerator.algName);
return rsaGenerator;
} catch (err) {
console.error("rsaGenerator error:" + err);
return err;
}
}
function createAsyCipher(cipherAlgoName) {
try {
let cipherGenerator = cryptoFramework.createCipher(cipherAlgoName);
console.log("cipherGenerator algName: " + cipherGenerator.algName);
return cipherGenerator;
} catch (err) {
console.error("cipherGenerator error:" + err);
return err;
}
}
function createAsySign(signAlgoName) {
try {
let signGenerator = cryptoFramework.createSign(signAlgoName);
console.log("signGenerator algName: " + signGenerator.algName);
return signGenerator;
} catch (err) {
console.error("signGenerator error:" + err);
return err;
}
}
function createAsyVerify(verifyAlgoName) {
try {
let verifyGenerator = cryptoFramework.createVerify(verifyAlgoName);
console.log("verifyGenerator algName: " + verifyGenerator.algName);
return verifyGenerator;
} catch (err) {
console.error("verifyGenerator error:" + err);
return err;
}
}
function createAsyKeyAgreement(ECDHAlgoName) {
try {
let generator = cryptoFramework.createKeyAgreement(ECDHAlgoName);
console.log("createKeyAgreement algName: " + generator.algName);
return generator;
} catch (err) {
console.error("createKeyAgreement error:" + err);
return err;
}
}
export {
createAsyKeyGenerator,
createAsyCipher,
createAsySign,
createAsyVerify,
createAsyKeyAgreement,
};
/*
* 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 {
stringTouInt8Array,
uInt8ArrayToShowStr,
uInt8ArrayToString,
} from "../common/publicDoString";
import {
createAsyKeyGenerator,
createAsyCipher,
createAsySign,
createAsyVerify,
createAsyKeyAgreement,
} from "./publicAsymmetricCommon";
async function generateAsyKeyPair(rsaGenerator) {
var pubKey;
var priKey;
return new Promise((resolve, reject) => {
rsaGenerator
.generateKeyPair()
.then((rsaKeyPair) => {
pubKey = rsaKeyPair.pubKey;
let encodedPubKey = pubKey.getEncoded();
console.log("[Promise]: pubKey.getAlgorithm= " + pubKey.algName);
console.log("[Promise]: pubKey.getEncoded= " + encodedPubKey.data);
console.log("[Promise]: pubKey.getFormat= " + pubKey.format);
priKey = rsaKeyPair.priKey;
let encodedPriKey = priKey.getEncoded();
console.log("[Promise]: priKey.getAlgorithm= " + priKey.algName);
console.log("[Promise]: priKey.getEncoded= " + encodedPriKey.data);
console.log("[Promise]: priKey.getFormat= " + priKey.format);
resolve(rsaKeyPair);
})
.catch((err) => {
console.error("[Promise]generateSymKey failed. error is " + err);
reject(err);
});
});
}
async function convertAsyKey(rsaGenerator, pubKeyDataBlob, priKeyDataBlob) {
return new Promise((resolve, reject) => {
rsaGenerator
.convertKey(pubKeyDataBlob, priKeyDataBlob)
.then((convertKeyPair) => {
console.log(
"[Promise]convertKey success. convertKeyPair is " + convertKeyPair
);
resolve(convertKeyPair);
})
.catch((err) => {
console.error("[Promise]convertKey failed. error is " + err);
reject(err);
});
});
}
async function initCipher(cipherGenerator, mode, key, params) {
return new Promise((resolve, reject) => {
cipherGenerator
.init(mode, key, params)
.then(() => {
console.log("[Promise]cipherGenerator init success! mode is : " + mode);
resolve("init success");
})
.catch((err) => {
console.error(
"[Promise]cipherGenerator init failed. error is " +
err +
"mode is " +
mode
);
reject(err);
});
});
}
async function doFinalCipher(cipherGenerator, mode, dataBlob) {
return new Promise((resolve, reject) => {
cipherGenerator
.doFinal(dataBlob)
.then((finalData) => {
console.log(
"[Promise]cipherGenerator doFinal success! mode is : " + mode
);
resolve(finalData);
})
.catch((err) => {
console.error(
"[Promise]cipherGenerator doFinal failed. error is " +
err +
"mode is " +
mode
);
reject(err);
});
});
}
async function initSign(signGenerator, priKey) {
return new Promise((resolve, reject) => {
signGenerator
.init(priKey)
.then(() => {
console.log("[Promise]signGenerator init success!");
resolve("init success");
})
.catch((err) => {
console.error("[Promise]signGenerator init failed. error is " + err);
reject(err);
});
});
}
async function updateSign(signGenerator, dataBlob) {
return new Promise((resolve, reject) => {
signGenerator
.update(dataBlob)
.then(() => {
console.log("[Promise]signGenerator update success!");
resolve("update success");
})
.catch((err) => {
console.error("[Promise]signGenerator update failed. error is " + err);
reject(err);
});
});
}
async function signForSign(signGenerator, dataBlob) {
return new Promise((resolve, reject) => {
signGenerator
.sign(dataBlob)
.then((signOutput) => {
console.log("[Promise]signGenerator sign success!");
resolve(signOutput);
})
.catch((err) => {
console.error("[Promise]signGenerator sign failed. error is " + err);
reject(err);
});
});
}
async function initVerify(verifyGenerator, pubKey) {
return new Promise((resolve, reject) => {
verifyGenerator
.init(pubKey)
.then(() => {
console.log("[Promise]verifyGenerator init success!");
resolve("init success");
})
.catch((err) => {
console.error("[Promise]verifyGenerator init failed. error is " + err);
reject(err);
});
});
}
async function updateVerify(verifyGenerator, dataBlob) {
return new Promise((resolve, reject) => {
verifyGenerator
.update(dataBlob)
.then(() => {
console.log("[Promise]verifyGenerator update success!");
resolve("update success");
})
.catch((err) => {
console.error(
"[Promise]verifyGenerator update failed. error is " + err
);
reject(err);
});
});
}
async function verifyForVerify(verifyGenerator, dataBlob, signDataBlob) {
return new Promise((resolve, reject) => {
verifyGenerator
.verify(dataBlob, signDataBlob)
.then((verifyOutput) => {
console.log("[Promise]signGenerator sign success!");
resolve(verifyOutput);
})
.catch((err) => {
console.error("[Promise]signGenerator sign failed. error is " + err);
reject(err);
});
});
}
async function generateAsySecret(generator, priKey, pubKey) {
return new Promise((resolve, reject) => {
generator
.generateSecret(priKey, pubKey)
.then((output) => {
console.log("[Promise]generateSecret success!");
resolve(output);
})
.catch((err) => {
console.error("[Promise]generateSecret failed. error is " + err);
reject(err);
});
});
}
async function encryptAndDecryptNormalProcess(asyAlgoName, cipherAlgoName) {
var globalCipherText;
var globalPubKey;
var globalPriKey;
var globalText = "This is a cipher test";
var input = { data: stringTouInt8Array(globalText) };
var encryptMode = cryptoFramework.CryptoMode.ENCRYPT_MODE;
var decryptMode = cryptoFramework.CryptoMode.DECRYPT_MODE;
return new Promise((resolve, reject) => {
var rsaGenerator = createAsyKeyGenerator(asyAlgoName);
expect(rsaGenerator != null).assertTrue();
var cipherGeneratorEncrypt = createAsyCipher(cipherAlgoName);
expect(cipherGeneratorEncrypt != null).assertTrue();
var cipherGeneratorDecrypt = createAsyCipher(cipherAlgoName);
expect(cipherGeneratorDecrypt != null).assertTrue();
generateAsyKeyPair(rsaGenerator)
.then((rsaKeyPair) => {
expect(rsaKeyPair != null).assertTrue();
globalPubKey = rsaKeyPair.pubKey;
globalPriKey = rsaKeyPair.priKey;
return initCipher(
cipherGeneratorEncrypt,
encryptMode,
globalPubKey,
null
);
})
.then((initData) => {
expect(initData === "init success").assertTrue();
return doFinalCipher(cipherGeneratorEncrypt, encryptMode, input);
})
.then((finalOutput) => {
expect(finalOutput != null).assertTrue();
globalCipherText = finalOutput;
console.log(
"cipherOutput: " + uInt8ArrayToShowStr(globalCipherText.data)
);
return initCipher(
cipherGeneratorDecrypt,
decryptMode,
globalPriKey,
null
);
})
.then((initData) => {
expect(initData === "init success").assertTrue();
return doFinalCipher(
cipherGeneratorDecrypt,
decryptMode,
globalCipherText
);
})
.then((finalOutput) => {
if (finalOutput == null) {
console.error("[promise]decrypt doFinal out is null");
} else {
console.log(
"[promise]decrypt doFinal out hex: " +
uInt8ArrayToShowStr(finalOutput.data)
);
}
let decryptData = uInt8ArrayToString(finalOutput.data);
expect(decryptData == globalText).assertTrue();
resolve();
})
.catch((err) => {
console.error(
"[promise] encryptAndDecryptNormalProcess catch err:" + err
);
reject(err);
});
});
}
async function signAndVerifyNormalProcess(asyAlgoName, signVerifyAlgoName) {
var globalPubKey;
var globalPriKey;
var globalSignBlob;
var globalText = "This is a sign test";
var input = { data: stringTouInt8Array(globalText) };
return new Promise((resolve, reject) => {
var rsaGenerator = createAsyKeyGenerator(asyAlgoName);
expect(rsaGenerator != null).assertTrue();
var signGenerator = createAsySign(signVerifyAlgoName);
expect(signGenerator != null).assertTrue();
var verifyGenerator = createAsyVerify(signVerifyAlgoName);
expect(verifyGenerator != null).assertTrue();
generateAsyKeyPair(rsaGenerator)
.then((rsaKeyPair) => {
expect(rsaKeyPair != null).assertTrue();
globalPubKey = rsaKeyPair.pubKey;
globalPriKey = rsaKeyPair.priKey;
return initSign(signGenerator, globalPriKey);
})
.then((initData) => {
expect(initData === "init success").assertTrue();
return updateSign(signGenerator, input);
})
.then((updateData) => {
expect(updateData === "update success").assertTrue();
return signForSign(signGenerator, input);
})
.then((finalOutput) => {
expect(finalOutput != null).assertTrue();
globalSignBlob = finalOutput;
console.log("signOutput: " + uInt8ArrayToShowStr(globalSignBlob.data));
return initVerify(verifyGenerator, globalPubKey);
})
.then((initData) => {
expect(initData === "init success").assertTrue();
return updateVerify(verifyGenerator, input);
})
.then((updateData) => {
expect(updateData === "update success").assertTrue();
return verifyForVerify(verifyGenerator, input, globalSignBlob);
})
.then((finalStatus) => {
expect(finalStatus).assertTrue();
resolve();
})
.catch((err) => {
console.error("[promise] signAndVerifyNormalProcess catch err:" + err);
reject(err);
});
});
}
async function convertKeyEncryptAndDecryptProcess(asyAlgoName) {
return new Promise((resolve, reject) => {
var rsaGenerator = createAsyKeyGenerator(asyAlgoName);
expect(rsaGenerator != null).assertTrue();
generateAsyKeyPair(rsaGenerator)
.then((rsaKeyPair) => {
expect(rsaKeyPair != null).assertTrue();
let encodedPubKey = rsaKeyPair.pubKey.getEncoded();
let encodedPriKey = rsaKeyPair.priKey.getEncoded();
return convertAsyKey(rsaGenerator, encodedPubKey, encodedPriKey);
})
.then((convertKeyPair) => {
expect(convertKeyPair != null).assertTrue();
let encodedConvertPubKey = convertKeyPair.pubKey.getEncoded();
console.warn(
"[Promise]: pubKey.getAlgorithm= " + convertKeyPair.pubKey.algName
);
console.warn(
"[Promise]: pubKey.getEncoded= " + encodedConvertPubKey.data
);
console.warn(
"[Promise]: pubKey.getFormat= " + convertKeyPair.pubKey.format
);
let encodedConvertPriKey = convertKeyPair.priKey.getEncoded();
console.warn(
"[Promise]: priKey.getAlgorithm= " + convertKeyPair.priKey.algName
);
console.warn(
"[Promise]: priKey.getEncoded= " + encodedConvertPriKey.data
);
console.warn(
"[Promise]: priKey.getFormat= " + convertKeyPair.priKey.format
);
resolve();
})
.catch((err) => {
console.error(
"[promise] convertKeyEncryptAndDecryptProcess catch err:" + err
);
reject(err);
});
});
}
async function keyAgreementProcess(ECDHAlgoName) {
var globalPubKey;
var globalPriKey;
return new Promise((resolve, reject) => {
var rsaGenerator = createAsyKeyGenerator(ECDHAlgoName);
expect(rsaGenerator != null).assertTrue();
var globalECDHData = createAsyKeyAgreement(ECDHAlgoName);
expect(globalECDHData != null).assertTrue();
generateAsyKeyPair(rsaGenerator)
.then((rsaKeyPair) => {
expect(rsaKeyPair != null).assertTrue();
globalPubKey = rsaKeyPair.pubKey;
globalPriKey = rsaKeyPair.priKey;
return generateAsySecret(globalECDHData, globalPriKey, globalPubKey);
})
.then((result) => {
console.warn("result data is " + uInt8ArrayToShowStr(result.data));
expect(result != null).assertTrue();
resolve();
})
.catch((err) => {
console.error("[promise] keyAgreementProcess catch err:" + err);
reject(err);
});
});
}
async function AsyPriKeyClearProcess(asyAlgoName) {
return new Promise((resolve, reject) => {
var rsaGenerator = createAsyKeyGenerator(asyAlgoName);
expect(rsaGenerator != null).assertTrue();
generateAsyKeyPair(rsaGenerator)
.then((rsaKeyPair) => {
expect(rsaKeyPair != null).assertTrue();
let result = rsaKeyPair.priKey.clearMem();
console.log("result is: " + result);
expect(result == undefined).assertTrue();
resolve();
})
.catch((err) => {
console.error("[promise] AsyPriKeyClearProcess catch err:" + err);
reject(err);
});
});
}
export {
encryptAndDecryptNormalProcess,
signAndVerifyNormalProcess,
convertKeyEncryptAndDecryptProcess,
keyAgreementProcess,
AsyPriKeyClearProcess,
};
/*
* 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.
*/
function stringToArray(str) {
var arr = [];
for (var i = 0, j = str.length; i < j; ++i) {
arr.push(str.charCodeAt(i));
}
return arr;
}
//字节流转成可理解的字符串
function uInt8ArrayToString(fileData) {
var dataString = "";
for (var i = 0; i < fileData.length; i++) {
dataString += String.fromCharCode(fileData[i]);
}
return dataString;
}
//可理解的字符串转成自字节流
function stringTouInt8Array(str) {
if (str.length == 0) {
console.error("stringTouInt8Array length is 0");
}
var arr = [];
for (var i = 0, j = str.length; i < j; ++i) {
arr.push(str.charCodeAt(i));
}
var tmpUint8Array = new Uint8Array(arr);
return tmpUint8Array;
}
//字节流以16进制形式输出
function uInt8ArrayToShowStr(uInt8Array) {
if (uInt8Array.length == 0) {
console.error("uInt8ArrayToShowStr length is 0");
}
return Array.prototype.map
.call(uInt8Array, (x) => ("00" + x.toString(16)).slice(-2))
.join("");
}
function genIvParamsSpec(n) {
var arr;
if (n == 8) {
arr = [0, 0, 0, 0, 0, 0, 0, 0];
} else if (n == 16) {
arr = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
} else {
console.error("genIvParamsSpec error: n = " + n);
return null;
}
var dataIv = new Uint8Array(arr);
var dataBlob = { data: dataIv };
var ivParamSpec = {
iv: dataBlob,
algoName: "IvParamsSpec",
};
return ivParamSpec;
}
function genGcmParamsSpec() {
var arr = [0, 0, 0, 0, 0, 0, 0, 0];
var dataAad = new Uint8Array(arr);
var aadBlob = { data: dataAad };
var arr = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
var dataIv = new Uint8Array(arr);
var ivBlob = { data: dataIv };
var arr = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
var dataTag = new Uint8Array(arr);
var tagBlob = { data: dataTag };
var gcmParamsSpec = {
iv: ivBlob,
aad: aadBlob,
authTag: tagBlob,
algoName: "GcmParamsSpec",
};
return gcmParamsSpec;
}
function genCcmParamsSpec() {
var arr = [0, 0, 0, 0, 0, 0, 0];
var dataIv = new Uint8Array(arr);
var ivBlob = { data: dataIv };
var arr = [0, 0, 0, 0, 0, 0, 0, 0];
var dataAad = new Uint8Array(arr);
var aadBlob = { data: dataAad };
var arr = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
var dataTag = new Uint8Array(arr);
var tagBlob = { data: dataTag };
var ccmParamsSpec = {
iv: ivBlob,
aad: aadBlob,
authTag: tagBlob,
algoName: "CcmParamsSpec",
};
return ccmParamsSpec;
}
function genKeyMaterialBlob(keyLen) {
var arr;
if (keyLen == 128) {
//16字节
arr = [
0xba, 0x3b, 0xc2, 0x71, 0x21, 0x1e, 0x30, 0x56, 0xad, 0x47, 0xfc, 0x5a,
0x46, 0x39, 0xee, 0x7c,
];
} else if (keyLen == 192) {
//24字节
arr = [
0xba, 0x3b, 0xc2, 0x71, 0x21, 0x1e, 0x30, 0x56, 0xad, 0x47, 0xfc, 0x5a,
0x46, 0x39, 0xee, 0x7c, 0xba, 0x3b, 0xc2, 0x71, 0x21, 0x1e, 0x30, 0x56,
];
} else if (keyLen == 256) {
//32字节
arr = [
0xba, 0x3b, 0xc2, 0x71, 0x21, 0x1e, 0x30, 0x56, 0xad, 0x47, 0xfc, 0x5a,
0x46, 0x39, 0xee, 0x7c, 0xba, 0x3b, 0xc2, 0x71, 0x21, 0x1e, 0x30, 0x56,
0xad, 0x47, 0xfc, 0x5a, 0x46, 0x39, 0xee, 0x7c,
];
} else {
console.error("genKeyMaterialBlob: KeyLen may be error");
arr = [];
for (let i = 0; i < keyLen; i++) {
arr[i] = 0xba;
}
}
var keyMaterial = new Uint8Array(arr);
var keyMaterialBlob = { data: keyMaterial };
return keyMaterialBlob;
}
export {
stringToArray,
uInt8ArrayToString,
stringTouInt8Array,
uInt8ArrayToShowStr,
genGcmParamsSpec,
genIvParamsSpec,
genCcmParamsSpec,
genKeyMaterialBlob,
};
/*
* 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 {
stringTouInt8Array,
uInt8ArrayToShowStr,
uInt8ArrayToString,
} from "../common/publicDoString";
async function generateSymKey(symKeyGenerator) {
return new Promise((resolve, reject) => {
symKeyGenerator.generateSymKey((err, symKey) => {
if (err) {
console.error("[Callback]generateSymKey failed. error is " + err);
reject(err);
} else {
console.log("[Callback]generateSymKey success. symKey is " + symKey);
console.warn("[Callback]key algName:" + symKey.algName);
console.warn("[Callback]key format:" + symKey.format);
var encodeKey = symKey.getEncoded();
console.warn(
"[Callback]key getEncoded hex: " + uInt8ArrayToShowStr(encodeKey.data)
);
resolve(symKey);
}
});
});
}
async function initMac(macGenerator, symKey) {
return new Promise((resolve, reject) => {
macGenerator.init(symKey, (err) => {
if (err) {
console.error("[Callback]macGenerator init failed. error is " + err);
reject(err);
} else {
console.log("[Callback]macGenerator init success!");
resolve("init success");
}
});
});
}
async function updateMac(macGenerator, dataBlob) {
return new Promise((resolve, reject) => {
macGenerator.update(dataBlob, (err) => {
if (err) {
console.error("[Callback]macGenerator update failed. error is " + err);
reject(err);
} else {
console.log("[Callback]macGenerator update success!");
resolve("update success");
}
});
});
}
async function doFinalMac(macGenerator) {
return new Promise((resolve, reject) => {
macGenerator.doFinal((err, output) => {
if (err) {
console.error("[Callback]macGenerator doFinal failed. error is " + err);
reject(err);
} else {
console.log(
"[Callback]macGenerator doFinal success! output is: " + output
);
resolve(output);
}
});
});
}
async function updateMd(mdGenerator, dataBlob) {
return new Promise((resolve, reject) => {
mdGenerator.update(dataBlob, (err) => {
if (err) {
console.error("[Callback]mdGenerator update failed. error is " + err);
reject(err);
} else {
console.log("[Callback]mdGenerator update success!");
resolve("update success");
}
});
});
}
async function digestMd(mdGenerator) {
return new Promise((resolve, reject) => {
mdGenerator.digest((err, output) => {
if (err) {
console.error("[Callback]mdGenerator digest failed. error is " + err);
reject(err);
} else {
console.log(
"[Callback]mdGenerator digest success! output is: " + output
);
resolve(output);
}
});
});
}
async function testMDDigestCallback(MDAlgoName) {
var globalMd;
var globalText = "my test data";
var inBlob = {
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, inBlob)
.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 testHMACDigestCallback(HMACAlgoName, keyAlgoName) {
var globalHMAC;
var globalText = "my test data";
var globalsymKeyGenerator;
var ginBlob = {
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);
expect(globalsymKeyGenerator != null).assertTrue();
console.warn("symKeyGenerator algName:" + globalsymKeyGenerator.algName);
generateSymKey(globalsymKeyGenerator)
.then((symKey) => {
expect(symKey != null).assertTrue();
return initMac(globalHMAC, symKey);
})
.then((initData) => {
expect(initData === "init success").assertTrue();
return updateMac(globalHMAC, ginBlob);
})
.then((updateData) => {
expect(updateData === "update success").assertTrue();
return doFinalMac(globalHMAC);
})
.then((macOutput) => {
console.warn("HMAC result:" + macOutput.data);
let macLen = globalHMAC.getMacLength();
console.warn("MAC len:" + macLen);
expect(macOutput != null && macLen != 0 && macLen != null).assertTrue();
resolve();
})
.catch((err) => {
console.error("testHMACDigestCallback catch error: " + err);
reject(err);
});
});
}
export { testMDDigestCallback, testHMACDigestCallback };
/*
* 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 {
stringTouInt8Array,
uInt8ArrayToShowStr,
uInt8ArrayToString,
} from "../common/publicDoString";
async function testMDDigestPromise(MDAlgoName) {
var globalMd;
var globalText = "my test data";
return new Promise((resolve, reject) => {
globalMd = cryptoFramework.createMd(MDAlgoName);
expect(globalMd != null).assertTrue();
console.warn("md= " + globalMd);
console.warn("MD algName is: " + globalMd.algName);
let inBlob = {
data: stringTouInt8Array(globalText),
};
globalMd
.update(inBlob)
.then(() => {
console.warn("[Promise]: update finished");
let digestBlob = globalMd.digest();
return digestBlob;
})
.then((mdOutput) => {
console.warn(
"[Promise]: digest result1: " + uInt8ArrayToShowStr(mdOutput.data)
);
console.warn("[Promise]: digest result2: " + mdOutput.data);
let mdLen = globalMd.getMdLength();
console.warn("Md len: " + mdLen);
expect(mdOutput != null && mdLen != 0 && mdLen != null).assertTrue();
resolve();
})
.catch((err) => {
console.error("[promise]catch err:" + err);
reject(err);
});
});
}
async function testHMACDigestPromise(HMACAlgoName, keyAlgoName) {
var globalHMAC;
var globalText = "my test data";
var globalsymKeyGenerator;
var ginBlob = {
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);
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(() => {
var promiseMacUpdate = globalHMAC.update(ginBlob);
return promiseMacUpdate;
})
.then(() => {
var promiseMacdoFinal = globalHMAC.doFinal();
return promiseMacdoFinal;
})
.then((macOutput) => {
console.warn("HMAC result:" + macOutput.data);
let macLen = globalHMAC.getMacLength();
console.warn("MAC len:" + macLen);
expect(macOutput != null && macLen != 0 && macLen != null).assertTrue();
resolve();
})
.catch((err) => {
console.error("[promise]catch err:" + err);
reject(err);
});
});
}
export { testMDDigestPromise, testHMACDigestPromise };
{
"string": [
{
"name": "entry_MainAbility",
"value": "entry_MainAbility"
},
{
"name": "mainability_description",
"value": "JS_Empty Ability"
},
{
"name": "MainAbility_desc",
"value": "description"
},
{
"name": "MainAbility_label",
"value": "label"
},
{
"name": "TestAbility_desc",
"value": "description"
},
{
"name": "TestAbility_label",
"value": "label"
}
]
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册