提交 69c24993 编写于 作者: Q qiaozzzh

ActsCryptoFrameworkJSNormalTest测试套提交

Signed-off-by: Nqiaozzzh <qmfzzz@163.com>
Change-Id: I45ed1c1e30d9a7b4d1af29972397d1a3c9d43b39
上级 5fdaba60
......@@ -17,9 +17,10 @@ group("security") {
testonly = true
if (is_standard_system) {
deps = [
"access_token/AccessTokenTest_Normal_js:ActsAccessTokenApiJSNormalTest",
"cipher/datacipher:datacipher",
"security_huks_basic",
#"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.example.myapplication101",
"package-name": "com.example.myapplication101"
},
"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"
]
}
]
}
\ No newline at end of file
{
"app": {
"bundleName": "com.example.myapplication101",
"vendor": "acts",
"version": {
"code": 12,
"name": "1.0.0"
},
"apiVersion": {
"compatible": 4,
"target": 5,
"releaseType": "Beta1"
}
},
"deviceConfig": {},
"module": {
"package": "com.example.myapplication101",
"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": ""
}
}
\ No newline at end of file
/*
* 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('AceApplication onCreate');
},
onDestroy() {
console.info('AceApplication 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 {
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");
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");
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");
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");
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 SecurityRandomJsunit from "./SecurityRandom.test.js";
import DigestAlgorithmJsunit from "./DigestAlgorithm.test.js";
import CertificateJsunit from "./Certificate.test.js";
export default function testsuite() {
// SecurityRandomJsunit();
// DigestAlgorithmJsunit();
CertificateJsunit();
DigestAlgorithmJsunit();
SecurityRandomJsunit();
}
\ No newline at end of file
/*
* 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);
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);
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) {
await 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.
*/
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
};
\ No newline at end of file
/*
* 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 testMDDigestCallback(MDAlgoName) {
var globalMd;
var globalText = "my test data";
globalMd = cryptoFramework.createMd(MDAlgoName);
expect(globalMd != null).assertTrue();
console.warn("md= " + globalMd);
console.warn("MD algName is: " + globalMd.algName);
// test input data
let inBlob = {
data: stringTouInt8Array(globalText)
}
globalMd.update(inBlob, (err,) => {
if (err) {
console.error("[callback]catch err:" + err);
expect(err == null).assertFail();
}
console.warn("[callback]: update finished");
globalMd.digest((err1, digestBlob) => {
if (err1) {
console.error("[callback]catch err:" + err1);
expect(err == null).assertFail();
}
console.warn("[callback]: digest result1: " + uInt8ArrayToShowStr(digestBlob.data));
console.warn("[callback]: digest result2: " + digestBlob.data);
let mdLen = globalMd.getMdLength();
console.warn("Md len: " + mdLen);
expect(digestBlob != null && mdLen == 16).assertTrue();
});
})
}
async function testHMACDigestCallback(HMACAlgoName, keyAlgoName) {
var globalHMAC;
var globalText = "my test data";
var globalsymKeyGenerator;
var ginBlob = {
data: stringTouInt8Array(globalText)
};
globalHMAC = cryptoFramework.createMac(HMACAlgoName);
expect(globalHMAC != null).assertTrue();
console.warn("mac= " + globalHMAC);
console.warn("HMAC algName is: " + globalHMAC.algName);
// create sym key generator
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((err, key) => {
if (err) {
console.error("[callback]catch err:" + err.code);
expect(err == null).assertFail();
}
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));
globalHMAC.init(key, (err1,) => {
if (err1) {
console.error("[callback]catch err:" + err1.code);
expect(err1 == null).assertFail();
}
globalHMAC.update(ginBlob, (err2,) => {
if (err2) {
console.error("[callback]catch err:" + err2.code);
expect(err2 == null).assertFail();
}
globalHMAC.doFinal((err3, macOutput) => {
if (err3) {
console.error("[callback]catch err:" + err3.code);
expect(err3 == null).assertFail();
}
console.warn("HMAC result:" + macOutput.data);
let macLen = globalHMAC.getMacLength();
console.warn("MAC len:" + macLen);
expect(macOutput != null && macLen == 20).assertTrue();
})
})
})
})
}
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";
globalMd = cryptoFramework.createMd(MDAlgoName);
expect(globalMd != null).assertTrue();
console.warn("md= " + globalMd);
console.warn("MD algName is: " + globalMd.algName);
// test input data
let inBlob = {
data: stringTouInt8Array(globalText)
}
//test functionalities by promise async
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 == 16).assertTrue();
}).catch(err => {
console.error("[promise]catch err:" + err);
expect(err == null).assertFail();
})
}
async function testHMACDigestPromise(HMACAlgoName, keyAlgoName) {
var globalHMAC;
var globalText = "my test data";
var globalsymKeyGenerator;
var ginBlob = {
data: stringTouInt8Array(globalText)
};
globalHMAC = cryptoFramework.createMac(HMACAlgoName);
expect(globalHMAC != null).assertTrue();
console.warn("mac= " + globalHMAC);
console.warn("HMAC algName is: " + globalHMAC.algName);
// create sym key generator
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();
expect(macOutput != null && macLen == 20).assertTrue();
console.warn("MAC len:" + macLen);
}).catch(err => {
console.error("[promise]catch err:" + err);
expect(err == null).assertFail();
})
}
export { testMDDigestPromise, testHMACDigestPromise };
/*
* 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 testSecurityRandomCallback(length) {
var rand;
var callbackGenerateRand;
rand = cryptoFramework.createRandom();
expect(rand != null).assertTrue();
rand.generateRandom(length, (err, randData)=>{
if(err) {
console.error("[callback]catch err:" + err);
expect(null).assertFail();
} else {
expect(randData != null).assertTrue();
console.log("[callback] rand result first:" + uInt8ArrayToShowStr(randData.data));
}
rand.setSeed(randData, (err1, )=>{
if(err1) {
console.error("[callback]catch err1:" + err1);
expect(null).assertFail();
} else {
expect(null).assertTrue();
}
})
})
rand.generateRandom(length, (err, randData)=>{
if(err) {
console.error("[callback]catch err:" + err);
expect(null).assertFail();
} else {
expect(randData != null).assertTrue();
console.log("[callback] rand result second:" + uInt8ArrayToShowStr(randData.data));
}
})
}
export { testSecurityRandomCallback };
/*
* 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 testSecurityRandomPromise(length) {
var rand;
rand = cryptoFramework.createRandom();
expect(rand != null).assertTrue();
rand.generateRandom(length).then(randData => {
expect(randData != null).assertTrue();
console.log("[Promise] rand result first:" + uInt8ArrayToShowStr(randData.data));
let result = rand.setSeed(randData);
return result;
}).then(() => {
console.log("[Promise] rand set Seed is success!");
expect(null).assertTrue();
}).catch(err => {
console.error("[promise]catch err:" + err);
expect(null).assertFail();
})
rand.generateRandom(length).then(randData => {
expect(randData != null).assertTrue();
console.log("[Promise] rand result second: " + uInt8ArrayToShowStr(randData.data));
}).catch(err => {
console.error("[promise]catch err:" + err);
expect(null).assertFail();
})
}
async 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_INTERNAL_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 };
{
"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.
先完成此消息的编辑!
想要评论请 注册