• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (C) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from 'deccjsunit/index'
16import data_rdb from '@ohos.data.rdb'
17import ability_featureAbility from '@ohos.ability.featureAbility'
18
19
20const TAG = "[RDB_JSKITS_TEST]"
21const CREATE_TABLE_TEST = "CREATE TABLE IF NOT EXISTS test (" + "id INTEGER PRIMARY KEY AUTOINCREMENT, "
22    + "name TEXT NOT NULL, " + "age INTEGER, " + "salary REAL, " + "blobType BLOB)"
23
24var context
25const STORE_CONFIG_ENCRYPT = {
26    name: "Encrypt.db",
27    encrypt: true,
28}
29const STORE_CONFIG_UNENCRYPT = {
30    name: "Unencrypt.db",
31    encrypt: false,
32}
33const STORE_CONFIG_WRONG = {
34    name: "Encrypt.db",
35    encrypt: false,
36}
37
38async function CreateRdbStore(context, STORE_CONFIG) {
39    let rdbStore = await data_rdb.getRdbStore(context, STORE_CONFIG, 1)
40    await rdbStore.executeSql(CREATE_TABLE_TEST, null)
41    let u8 = new Uint8Array([1, 2, 3])
42    {
43        const valueBucket = {
44            "name": "zhangsan",
45            "age": 18,
46            "salary": 100.5,
47            "blobType": u8,
48        }
49        await rdbStore.insert("test", valueBucket)
50    }
51    {
52        const valueBucket = {
53            "name": "lisi",
54            "age": 28,
55            "salary": 100.5,
56            "blobType": u8,
57        }
58        await rdbStore.insert("test", valueBucket)
59    }
60    {
61        const valueBucket = {
62            "name": "wangwu",
63            "age": 38,
64            "salary": 90.0,
65            "blobType": u8,
66        }
67        await rdbStore.insert("test", valueBucket)
68    }
69    return rdbStore
70}
71
72describe('rdbEncryptTest', function () {
73    beforeAll(async function () {
74        console.info(TAG + 'beforeAll')
75    })
76
77    beforeEach(async function () {
78        console.info(TAG + 'beforeEach')
79    })
80
81    afterEach(async function () {
82        console.info(TAG + 'afterEach')
83        await data_rdb.deleteRdbStore(context, STORE_CONFIG_ENCRYPT.name)
84        await data_rdb.deleteRdbStore(context, STORE_CONFIG_UNENCRYPT.name)
85        await data_rdb.deleteRdbStore(context, STORE_CONFIG_WRONG.name)
86    })
87
88    afterAll(async function () {
89        console.info(TAG + 'afterAll')
90    })
91
92    console.log(TAG + "*************Unit Test Begin*************")
93
94    /**
95     * @tc.name RDB encrypted test
96     * @tc.number SUB_DDM_RDB_JS_RdbEncryptTest_0010
97     * @tc.desc RDB create encrypt db test
98     */
99    it('RdbEncryptTest_0010', 0, async function () {
100        await console.log(TAG + "************* RdbEncryptTest_0010 start *************")
101        try {
102            context = ability_featureAbility.getContext()
103            await data_rdb.getRdbStore(context, STORE_CONFIG_ENCRYPT, 1);
104        } catch (err) {
105            console.log(TAG + `failed, errcode:${JSON.stringify(err)}.`);
106            expect(null).assertFail();
107        }
108        await console.log(TAG + "************* RdbEncryptTest_0010 end *************")
109    })
110
111    /**
112     * @tc.name RDB unencrypted test
113     * @tc.number SUB_DDM_RDB_JS_RdbEncryptTest_0020
114     * @tc.desc RDB create unencrypted db test
115     */
116    it('RdbEncryptTest_0020', 0, async function () {
117        await console.log(TAG + "************* RdbEncryptTest_0020 start *************")
118        context = ability_featureAbility.getContext()
119        try {
120            await data_rdb.getRdbStore(context, STORE_CONFIG_UNENCRYPT, 1);
121        } catch (err) {
122            console.log(TAG + `failed, errcode:${JSON.stringify(err)}.`);
123            expect(null).assertFail();
124        }
125        await console.log(TAG + "************* RdbEncryptTest_0020 end *************")
126    })
127
128
129    /**
130     * @tc.name RDB Encrypt test
131     * @tc.number SUB_DDM_RDB_JS_RdbEncryptTest_0030
132     * @tc.desc RDB Encrypt function test
133     */
134    it('RdbEncryptTest_0030', 0, async function () {
135        await console.log(TAG + "************* RdbEncryptTest_0030 start *************")
136        context = ability_featureAbility.getContext()
137        let rdbStore = await CreateRdbStore(context, STORE_CONFIG_ENCRYPT)
138        let predicates = new data_rdb.RdbPredicates("test")
139        predicates.equalTo("name", "zhangsan")
140        let resultSet = await rdbStore.query(predicates)
141        try {
142            console.log(TAG + "After restore resultSet query done")
143            expect(true).assertEqual(resultSet.goToFirstRow())
144            const id = resultSet.getLong(resultSet.getColumnIndex("id"))
145            const name = resultSet.getString(resultSet.getColumnIndex("name"))
146            const blobType = resultSet.getBlob(resultSet.getColumnIndex("blobType"))
147            expect(1).assertEqual(id)
148            expect("zhangsan").assertEqual(name)
149            expect(1).assertEqual(blobType[0])
150        } catch (err) {
151            console.log(TAG + `failed, errcode:${JSON.stringify(err)}.`);
152            expect().assertFail()
153        } finally {
154            resultSet.close()
155            resultSet = null
156            rdbStore = null
157        }
158        await console.log(TAG + "************* RdbEncryptTest_0030 end *************")
159    })
160
161    /**
162     * @tc.name RDB Encrypt test
163     * @tc.number SUB_DDM_RDB_JS_RdbEncryptTest_0040
164     * @tc.desc RDB Encrypt function test
165     */
166    it('RdbEncryptTest_0040', 0, async function () {
167        await console.log(TAG + "************* RdbEncryptTest_0040 start *************")
168        context = ability_featureAbility.getContext()
169        await CreateRdbStore(context, STORE_CONFIG_ENCRYPT)
170        try {
171            await CreateRdbStore(context, STORE_CONFIG_WRONG)
172            expect().assertFail()
173        } catch (err) {
174            console.log(TAG + `failed, errcode:${JSON.stringify(err)}.`);
175        }
176
177        await console.log(TAG + "************* RdbEncryptTest_0040 end *************")
178    })
179    console.log(TAG + "*************Unit Test End*************")
180})