Skip to content

Commit b450e7e

Browse files
committed
react randombytes 鸿蒙化
Signed-off-by: wangmaolin <wangmaolin12@h-partners.com>
1 parent 0b596c4 commit b450e7e

31 files changed

+515
-5
lines changed

harmony/random_bytes.har

2.52 KB
Binary file not shown.

harmony/random_bytes/BuildProfile.ets

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export default class BuildProfile {
2+
static readonly HAR_VERSION = '3.6.1-0.0.1';
3+
static readonly BUILD_MODE_NAME = 'debug';
4+
static readonly DEBUG = true;
5+
static readonly TARGET_NAME = 'default';
6+
}

harmony/random_bytes/Index.ets

Whitespace-only changes.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"apiType": "stageMode",
3+
"buildOption": {
4+
},
5+
"buildOptionSet": [
6+
{
7+
"name": "release",
8+
"arkOptions": {
9+
"obfuscation": {
10+
"ruleOptions": {
11+
"enable": true,
12+
"files": [
13+
"./obfuscation-rules.txt"
14+
]
15+
},
16+
"consumerFiles": [
17+
"./consumer-rules.txt"
18+
]
19+
}
20+
},
21+
},
22+
],
23+
"targets": [
24+
{
25+
"name": "default"
26+
},
27+
{
28+
"name": "ohosTest"
29+
}
30+
]
31+
}

harmony/random_bytes/consumer-rules.txt

Whitespace-only changes.

harmony/random_bytes/hvigorfile.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { harTasks } from '@ohos/hvigor-ohos-plugin';
2+
3+
export default {
4+
system: harTasks, /* Built-in plugin of Hvigor. It cannot be modified. */
5+
plugins:[] /* Custom plugin to extend the functionality of Hvigor. */
6+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Define project specific obfuscation rules here.
2+
# You can include the obfuscation configuration files in the current module's build-profile.json5.
3+
#
4+
# For more details, see
5+
# https://gitee.com/openharmony/arkcompiler_ets_frontend/blob/master/arkguard/README.md
6+
7+
# Obfuscation options:
8+
# -disable-obfuscation: disable all obfuscations
9+
# -enable-property-obfuscation: obfuscate the property names
10+
# -enable-toplevel-obfuscation: obfuscate the names in the global scope
11+
# -compact: remove unnecessary blank spaces and all line feeds
12+
# -remove-log: remove all console.* statements
13+
# -print-namecache: print the name cache that contains the mapping from the old names to new names
14+
# -apply-namecache: reuse the given cache file
15+
16+
# Keep options:
17+
# -keep-property-name: specifies property names that you want to keep
18+
# -keep-global-name: specifies names that you want to keep in the global scope

harmony/random_bytes/oh-package.json5

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "react-native-randombytes",
3+
"version": "3.6.1-0.0.1",
4+
"description": "randomBytes for react-native",
5+
"main": "Index.ets",
6+
"author": "",
7+
"license": "Apache-2.0",
8+
"dependencies": {
9+
"@rnoh/react-native-openharmony": 'file:../react_native_openharmony'
10+
}
11+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { RNPackage, TurboModulesFactory } from '@rnoh/react-native-openharmony/ts';
2+
import type {
3+
TurboModule,
4+
TurboModuleContext,
5+
} from '@rnoh/react-native-openharmony/ts';
6+
import { TM, RNC } from "@rnoh/react-native-openharmony/generated/ts"
7+
import { RandomBytesTurboModule } from './randomBytesTurboModule';
8+
9+
10+
class RandomBytesTurboModuleFactory extends TurboModulesFactory {
11+
createTurboModule(name: string): TurboModule | null {
12+
if (this.hasTurboModule(name)) {
13+
return new RandomBytesTurboModule(this.ctx);
14+
}
15+
return null;
16+
}
17+
18+
hasTurboModule(name: string): boolean {
19+
return name === TM.RandomBytesNativeModule.NAME;
20+
}
21+
}
22+
23+
export class RandomBytesPackage extends RNPackage {
24+
createTurboModulesFactory(ctx: TurboModuleContext): TurboModulesFactory {
25+
return new RandomBytesTurboModuleFactory(ctx);
26+
}
27+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { TurboModule, RNOHError, Tag } from '@rnoh/react-native-openharmony/ts';
2+
import { TM } from "@rnoh/react-native-openharmony/generated/ts"
3+
import cryptoFramework from '@ohos.security.cryptoFramework';
4+
import util from '@ohos.util';
5+
import { BusinessError } from '@kit.BasicServicesKit';
6+
7+
8+
export class RandomBytesTurboModule extends TurboModule implements TM.RandomBytesNativeModule.Spec {
9+
10+
private static readonly seed: string = 'seed';
11+
private static base64Helper = new util.Base64Helper();
12+
13+
public getName(): string {
14+
return "RNRandomBytes";
15+
}
16+
17+
public getConstants() {
18+
return {
19+
seed: this.getRandomBytes(4096)
20+
};
21+
}
22+
23+
public randomBytes(size: number, cb: (err: string, base64String: string) => void): void {
24+
try {
25+
cb(null, this.getRandomBytes(size))
26+
} catch (err) {
27+
let e: BusinessError = err;
28+
cb(`randomBytes error, ${e.code}, ${e.message}`, '')
29+
}
30+
}
31+
32+
private getRandomBytes(size: number) {
33+
const { base64Helper } = RandomBytesTurboModule;
34+
let rand = cryptoFramework.createRandom();
35+
try {
36+
const randomData = rand.generateRandomSync(size);
37+
const base64String = base64Helper.encodeToStringSync(randomData.data);
38+
return base64String;
39+
} catch (err) {
40+
throw err;
41+
}
42+
}
43+
44+
}
45+
46+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"module": {
3+
"name": "random_bytes",
4+
"type": "har",
5+
"deviceTypes": [
6+
"default"
7+
]
8+
}
9+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"string": [
3+
{
4+
"name": "page_show",
5+
"value": "page from package"
6+
}
7+
]
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"string": [
3+
{
4+
"name": "page_show",
5+
"value": "page from package"
6+
}
7+
]
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"string": [
3+
{
4+
"name": "page_show",
5+
"value": "page from package"
6+
}
7+
]
8+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{
2+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { hilog } from '@kit.PerformanceAnalysisKit';
2+
import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium';
3+
4+
export default function abilityTest() {
5+
describe('ActsAbilityTest', () => {
6+
// Defines a test suite. Two parameters are supported: test suite name and test suite function.
7+
beforeAll(() => {
8+
// Presets an action, which is performed only once before all test cases of the test suite start.
9+
// This API supports only one parameter: preset action function.
10+
})
11+
beforeEach(() => {
12+
// Presets an action, which is performed before each unit test case starts.
13+
// The number of execution times is the same as the number of test cases defined by **it**.
14+
// This API supports only one parameter: preset action function.
15+
})
16+
afterEach(() => {
17+
// Presets a clear action, which is performed after each unit test case ends.
18+
// The number of execution times is the same as the number of test cases defined by **it**.
19+
// This API supports only one parameter: clear action function.
20+
})
21+
afterAll(() => {
22+
// Presets a clear action, which is performed after all test cases of the test suite end.
23+
// This API supports only one parameter: clear action function.
24+
})
25+
it('assertContain', 0, () => {
26+
// Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function.
27+
hilog.info(0x0000, 'testTag', '%{public}s', 'it begin');
28+
let a = 'abc';
29+
let b = 'b';
30+
// Defines a variety of assertion methods, which are used to declare expected boolean conditions.
31+
expect(a).assertContain(b);
32+
expect(a).assertEqual(a);
33+
})
34+
})
35+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import abilityTest from './Ability.test';
2+
3+
export default function testsuite() {
4+
abilityTest();
5+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit';
2+
import { abilityDelegatorRegistry } from '@kit.TestKit';
3+
import { hilog } from '@kit.PerformanceAnalysisKit';
4+
import { window } from '@kit.ArkUI';
5+
import { Hypium } from '@ohos/hypium';
6+
import testsuite from '../test/List.test';
7+
8+
export default class TestAbility extends UIAbility {
9+
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
10+
hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility onCreate');
11+
hilog.info(0x0000, 'testTag', '%{public}s', 'want param:' + JSON.stringify(want) ?? '');
12+
hilog.info(0x0000, 'testTag', '%{public}s', 'launchParam:' + JSON.stringify(launchParam) ?? '');
13+
let abilityDelegator: abilityDelegatorRegistry.AbilityDelegator;
14+
abilityDelegator = abilityDelegatorRegistry.getAbilityDelegator();
15+
let abilityDelegatorArguments: abilityDelegatorRegistry.AbilityDelegatorArgs;
16+
abilityDelegatorArguments = abilityDelegatorRegistry.getArguments();
17+
hilog.info(0x0000, 'testTag', '%{public}s', 'start run testcase!!!');
18+
Hypium.hypiumTest(abilityDelegator, abilityDelegatorArguments, testsuite);
19+
}
20+
21+
onDestroy() {
22+
hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility onDestroy');
23+
}
24+
25+
onWindowStageCreate(windowStage: window.WindowStage) {
26+
hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility onWindowStageCreate');
27+
windowStage.loadContent('testability/pages/Index', (err) => {
28+
if (err.code) {
29+
hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
30+
return;
31+
}
32+
hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.');
33+
});
34+
}
35+
36+
onWindowStageDestroy() {
37+
hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility onWindowStageDestroy');
38+
}
39+
40+
onForeground() {
41+
hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility onForeground');
42+
}
43+
44+
onBackground() {
45+
hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility onBackground');
46+
}
47+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
@Entry
2+
@Component
3+
struct Index {
4+
@State message: string = 'Hello World';
5+
6+
build() {
7+
Row() {
8+
Column() {
9+
Text(this.message)
10+
.fontSize(50)
11+
.fontWeight(FontWeight.Bold)
12+
}
13+
.width('100%')
14+
}
15+
.height('100%')
16+
}
17+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import { abilityDelegatorRegistry, TestRunner } from '@kit.TestKit';
2+
import { UIAbility, Want } from '@kit.AbilityKit';
3+
import { BusinessError } from '@kit.BasicServicesKit';
4+
import { hilog } from '@kit.PerformanceAnalysisKit';
5+
import { resourceManager } from '@kit.LocalizationKit';
6+
import { util } from '@kit.ArkTS';
7+
8+
let abilityDelegator: abilityDelegatorRegistry.AbilityDelegator;
9+
let abilityDelegatorArguments: abilityDelegatorRegistry.AbilityDelegatorArgs;
10+
let jsonPath: string = 'mock/mock-config.json';
11+
let tag: string = 'testTag';
12+
13+
async function onAbilityCreateCallback(data: UIAbility) {
14+
hilog.info(0x0000, 'testTag', 'onAbilityCreateCallback, data: ${}', JSON.stringify(data));
15+
}
16+
17+
async function addAbilityMonitorCallback(err: BusinessError) {
18+
hilog.info(0x0000, 'testTag', 'addAbilityMonitorCallback : %{public}s', JSON.stringify(err) ?? '');
19+
}
20+
21+
export default class OpenHarmonyTestRunner implements TestRunner {
22+
constructor() {
23+
}
24+
25+
onPrepare() {
26+
hilog.info(0x0000, 'testTag', '%{public}s', 'OpenHarmonyTestRunner OnPrepare');
27+
}
28+
29+
async onRun() {
30+
let tag = 'testTag';
31+
hilog.info(0x0000, tag, '%{public}s', 'OpenHarmonyTestRunner onRun run');
32+
abilityDelegatorArguments = abilityDelegatorRegistry.getArguments()
33+
abilityDelegator = abilityDelegatorRegistry.getAbilityDelegator()
34+
let moduleName = abilityDelegatorArguments.parameters['-m'];
35+
let context = abilityDelegator.getAppContext().getApplicationContext().createModuleContext(moduleName);
36+
let mResourceManager = context.resourceManager;
37+
await checkMock(abilityDelegator, mResourceManager);
38+
const bundleName = abilityDelegatorArguments.bundleName;
39+
const testAbilityName: string = 'TestAbility';
40+
let lMonitor: abilityDelegatorRegistry.AbilityMonitor = {
41+
abilityName: testAbilityName,
42+
onAbilityCreate: onAbilityCreateCallback,
43+
moduleName: moduleName
44+
};
45+
abilityDelegator.addAbilityMonitor(lMonitor, addAbilityMonitorCallback)
46+
const want: Want = {
47+
bundleName: bundleName,
48+
abilityName: testAbilityName,
49+
moduleName: moduleName
50+
};
51+
abilityDelegator.startAbility(want, (err: BusinessError, data: void) => {
52+
hilog.info(0x0000, tag, 'startAbility : err : %{public}s', JSON.stringify(err) ?? '');
53+
hilog.info(0x0000, tag, 'startAbility : data : %{public}s', JSON.stringify(data) ?? '');
54+
})
55+
hilog.info(0x0000, tag, '%{public}s', 'OpenHarmonyTestRunner onRun end');
56+
}
57+
}
58+
59+
async function checkMock(abilityDelegator: abilityDelegatorRegistry.AbilityDelegator, resourceManager: resourceManager.ResourceManager) {
60+
let rawFile: Uint8Array;
61+
try {
62+
rawFile = resourceManager.getRawFileContentSync(jsonPath);
63+
hilog.info(0x0000, tag, 'MockList file exists');
64+
let mockStr: string = util.TextDecoder.create('utf-8', { ignoreBOM: true }).decodeWithStream(rawFile);
65+
let mockMap: Record<string, string> = getMockList(mockStr);
66+
try {
67+
abilityDelegator.setMockList(mockMap)
68+
} catch (error) {
69+
let code = (error as BusinessError).code;
70+
let message = (error as BusinessError).message;
71+
hilog.error(0x0000, tag, `abilityDelegator.setMockList failed, error code: ${code}, message: ${message}.`);
72+
}
73+
} catch (error) {
74+
let code = (error as BusinessError).code;
75+
let message = (error as BusinessError).message;
76+
hilog.error(0x0000, tag, `ResourceManager:callback getRawFileContent failed, error code: ${code}, message: ${message}.`);
77+
}
78+
}
79+
80+
function getMockList(jsonStr: string) {
81+
let jsonObj: Record<string, Object> = JSON.parse(jsonStr);
82+
let map: Map<string, object> = new Map<string, object>(Object.entries(jsonObj));
83+
let mockList: Record<string, string> = {};
84+
map.forEach((value: object, key: string) => {
85+
let realValue: string = value['source'].toString();
86+
mockList[key] = realValue;
87+
});
88+
hilog.info(0x0000, tag, '%{public}s', 'mock-json value:' + JSON.stringify(mockList) ?? '');
89+
return mockList;
90+
}

0 commit comments

Comments
 (0)