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 rdbStore 25var context 26const STORE_CONFIG_ENCRYPT = { 27 name: "Encrypt.db", 28 encrypt: true, 29} 30const STORE_CONFIG_UNENCRYPT = { 31 name: "Unencrypt.db", 32 encrypt: false, 33} 34const STORE_CONFIG_WRONG = { 35 name: "Encrypt.db", 36 encrypt: false, 37} 38 39async function CreatRdbStore(context, STORE_CONFIG) { 40 let rdbStore = await data_rdb.getRdbStore(context, STORE_CONFIG, 1) 41 await rdbStore.executeSql(CREATE_TABLE_TEST, null) 42 let u8 = new Uint8Array([1, 2, 3]) 43 { 44 const valueBucket = { 45 "name": "zhangsan", 46 "age": 18, 47 "salary": 100.5, 48 "blobType": u8, 49 } 50 await rdbStore.insert("test", valueBucket) 51 } 52 { 53 const valueBucket = { 54 "name": "lisi", 55 "age": 28, 56 "salary": 100.5, 57 "blobType": u8, 58 } 59 await rdbStore.insert("test", valueBucket) 60 } 61 { 62 const valueBucket = { 63 "name": "wangwu", 64 "age": 38, 65 "salary": 90.0, 66 "blobType": u8, 67 } 68 await rdbStore.insert("test", valueBucket) 69 } 70 return rdbStore 71} 72 73describe('rdbEncryptTest', function () { 74 beforeAll(async function () { 75 console.info(TAG + 'beforeAll') 76 77 }) 78 79 beforeEach(async function () { 80 console.info(TAG + 'beforeEach') 81 82 }) 83 84 afterEach(async function () { 85 console.info(TAG + 'afterEach') 86 await data_rdb.deleteRdbStore(context, STORE_CONFIG_ENCRYPT.name) 87 await data_rdb.deleteRdbStore(context, STORE_CONFIG_UNENCRYPT.name) 88 await data_rdb.deleteRdbStore(context, STORE_CONFIG_WRONG.name) 89 rdbStore = null 90 }) 91 92 afterAll(async function () { 93 console.info(TAG + 'afterAll') 94 }) 95 96 console.log(TAG + "*************Unit Test Begin*************") 97 98 /** 99 * @tc.name RDB encrypted test 100 * @tc.number SUB_DDM_RDB_JS_RdbEncryptTest_0010 101 * @tc.desc RDB create encrypt db test 102 */ 103 it('RdbEncryptTest_0010', 0, async function (done) { 104 await console.log(TAG + "************* RdbEncryptTest_0010 start *************") 105 context = ability_featureAbility.getContext() 106 let storePromise = data_rdb.getRdbStore(context, STORE_CONFIG_ENCRYPT, 1); 107 storePromise.then(async (store) => { 108 try { 109 await console.log(TAG + "getRdbStore done: " + store); 110 } catch (err) { 111 expect(null).assertFail(); 112 } 113 }).catch((err) => { 114 expect(null).assertFail(); 115 }) 116 await storePromise 117 storePromise = null 118 119 done() 120 await console.log(TAG + "************* RdbEncryptTest_0010 end *************") 121 }) 122 123 /** 124 * @tc.name RDB unencrypted test 125 * @tc.number SUB_DDM_RDB_JS_RdbEncryptTest_0020 126 * @tc.desc RDB create unencrypted db test 127 */ 128 it('RdbEncryptTest_0020', 0, async function (done) { 129 await console.log(TAG + "************* RdbEncryptTest_0020 start *************") 130 context = ability_featureAbility.getContext() 131 let storePromise = data_rdb.getRdbStore(context, STORE_CONFIG_UNENCRYPT, 1); 132 storePromise.then(async (store) => { 133 try { 134 await console.log(TAG + "getRdbStore done: " + store); 135 } catch (err) { 136 expect(null).assertFail(); 137 } 138 }).catch((err) => { 139 expect(null).assertFail(); 140 }) 141 await storePromise 142 storePromise = null 143 144 done() 145 await console.log(TAG + "************* RdbEncryptTest_0020 end *************") 146 }) 147 148 149 /** 150 * @tc.name RDB Encrypt test 151 * @tc.number SUB_DDM_RDB_JS_RdbEncryptTest_0030 152 * @tc.desc RDB Encrypt function test 153 */ 154 it('RdbEncryptTest_0030', 0, async function (done) { 155 await console.log(TAG + "************* RdbEncryptTest_0030 start *************") 156 context = ability_featureAbility.getContext() 157 rdbStore = await CreatRdbStore(context, STORE_CONFIG_ENCRYPT) 158 let predicates = new data_rdb.RdbPredicates("test") 159 predicates.equalTo("name", "zhangsan") 160 let resultSet = await rdbStore.query(predicates) 161 try { 162 console.log(TAG + "After restore resultSet query done") 163 expect(true).assertEqual(resultSet.goToFirstRow()) 164 const id = resultSet.getLong(resultSet.getColumnIndex("id")) 165 const name = resultSet.getString(resultSet.getColumnIndex("name")) 166 const blobType = resultSet.getBlob(resultSet.getColumnIndex("blobType")) 167 expect(1).assertEqual(id) 168 expect("zhangsan").assertEqual(name) 169 expect(1).assertEqual(blobType[0]) 170 } catch (err) { 171 expect(false).assertTrue() 172 } 173 resultSet = null 174 rdbStore = null 175 done() 176 await console.log(TAG + "************* RdbEncryptTest_0030 end *************") 177 }) 178 179 /** 180 * @tc.name RDB Encrypt test 181 * @tc.number SUB_DDM_RDB_JS_RdbEncryptTest_0040 182 * @tc.desc RDB Encrypt function test 183 */ 184 it('RdbEncryptTest_0040', 0, async function (done) { 185 await console.log(TAG + "************* RdbEncryptTest_0040 start *************") 186 context = ability_featureAbility.getContext() 187 rdbStore = await CreatRdbStore(context, STORE_CONFIG_ENCRYPT) 188 rdbStore = null 189 rdbStore = await CreatRdbStore(context, STORE_CONFIG_WRONG) 190 expect(rdbStore).assertNull 191 192 done() 193 await console.log(TAG + "************* RdbEncryptTest_0040 end *************") 194 }) 195 console.log(TAG + "*************Unit Test End*************") 196 } 197) 198