• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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  */
15 #include <iostream>
16 #include <stdio.h>
17 #include <unistd.h>
18 #include <string>
19 #include <string.h>
20 #include <cstdio>
21 #include <cstdlib>
22 #include <fcntl.h>
23 #include <securec.h>
24 #include <sys/stat.h>
25 #include <sys/types.h>
26 
27 #include "napi/native_api.h"
28 #include "common/common.h"
29 #include "relational_store.h"
30 #include "relational_store_error_code.h"
31 #include "oh_cursor.h"
32 #include "oh_predicates.h"
33 #include "oh_value_object.h"
34 #include "oh_values_bucket.h"
35 
36 char *RDB_TEST_PATH =  NULL;
37 char RDB_STORE_NAME[] =  "rdb_store_encrypt_test.db";
38 char RDB_STORE_NAME2[] =  "Encrypt.db";
39 char BUNDLE_NAME[] =  "com.acts.rdb.napitest";
40 char MODULE_NAME[] =  "com.acts.rdb.napitest";
41 
42 
43 OH_Rdb_Store *storeEncryptTestRdbStore_ = NULL;
44 OH_Rdb_Store *storeEncryptTestRdbStore2_ = NULL;
45 static OH_Rdb_Config config_;
46 static OH_Rdb_Config config1_;
47 static OH_Rdb_Config config2_;
48 static OH_Rdb_Config config3_;
InitRdbConfig()49 static void InitRdbConfig()
50 {
51     config_.dataBaseDir = RDB_TEST_PATH;
52     config_.storeName = RDB_STORE_NAME;
53     config_.bundleName = BUNDLE_NAME;
54     config_.moduleName = MODULE_NAME;
55     config_.securityLevel = OH_Rdb_SecurityLevel::S1;
56     config_.isEncrypt = false;
57     config_.selfSize = sizeof(OH_Rdb_Config);
58 }
59 
InitRdbConfig1()60 static void InitRdbConfig1()
61 {
62     config1_.dataBaseDir = RDB_TEST_PATH;
63     config1_.storeName = RDB_STORE_NAME;
64     config1_.bundleName = BUNDLE_NAME;
65     config1_.moduleName = MODULE_NAME;
66     config1_.securityLevel = OH_Rdb_SecurityLevel::S2;
67     config1_.isEncrypt = false;
68     config1_.selfSize = sizeof(OH_Rdb_Config);
69 }
70 
InitRdbConfig2()71 static void InitRdbConfig2()
72 {
73     config2_.dataBaseDir = RDB_TEST_PATH;
74     config2_.storeName = RDB_STORE_NAME2;
75     config2_.bundleName = BUNDLE_NAME;
76     config2_.moduleName = MODULE_NAME;
77     config2_.securityLevel = OH_Rdb_SecurityLevel::S1;
78     config2_.isEncrypt = true;
79     config2_.selfSize = sizeof(OH_Rdb_Config);
80 }
81 
InitRdbConfig3()82 static void InitRdbConfig3()
83 {
84     config3_.dataBaseDir = RDB_TEST_PATH;
85     config3_.storeName = RDB_STORE_NAME2;
86     config3_.bundleName = BUNDLE_NAME;
87     config3_.moduleName = MODULE_NAME;
88     config3_.securityLevel = OH_Rdb_SecurityLevel::S1;
89     config3_.isEncrypt = false;
90     config3_.selfSize = sizeof(OH_Rdb_Config);
91 }
92 
RdbFilePath(napi_env env,napi_callback_info info)93 static napi_value RdbFilePath(napi_env env, napi_callback_info info) {
94     int errCode = 0;
95     size_t argc = 1;
96     napi_value args[1] = {nullptr};
97     napi_get_cb_info(env, info, &argc, args , nullptr, nullptr);
98 
99     size_t bufferSize = 0;
100     napi_get_value_string_latin1(env, args[0], nullptr, 0, &bufferSize);
101 
102     char *buffer = (char*)malloc((bufferSize) + 1);
103     napi_get_value_string_utf8(env, args[0], buffer, bufferSize+1, &bufferSize);
104 
105     RDB_TEST_PATH = (char*)malloc((bufferSize) + 1);
106     sprintf(RDB_TEST_PATH, "%s", buffer);
107 
108     napi_value returnCode;
109     napi_create_double(env, errCode, &returnCode);
110     return returnCode;
111 }
112 
SUB_DDM_RDB_ENCRYPT_0100(napi_env env,napi_callback_info info)113 static napi_value SUB_DDM_RDB_ENCRYPT_0100(napi_env env, napi_callback_info info) {
114     InitRdbConfig2();
115     mkdir(config2_.dataBaseDir, 0770);
116 
117     InitRdbConfig3();
118     mkdir(config3_.dataBaseDir, 0770);
119 
120     int errCode = 0;
121     storeEncryptTestRdbStore_ = OH_Rdb_GetOrOpen(&config2_, &errCode);
122     NAPI_ASSERT(env, errCode == 0, "OH_Rdb_GetOrOpen is fail.");
123     NAPI_ASSERT(env, storeEncryptTestRdbStore_ != NULL, "OH_Rdb_GetOrOpen config2 is fail.");
124 
125     storeEncryptTestRdbStore2_ = OH_Rdb_GetOrOpen(&config3_, &errCode);
126     NAPI_ASSERT(env, storeEncryptTestRdbStore2_ != NULL, "OH_Rdb_GetOrOpen config3 is success.");
127 
128     errCode = OH_Rdb_CloseStore(storeEncryptTestRdbStore_);
129     NAPI_ASSERT(env, errCode == 0, "OH_Rdb_CloseStore is fail.");
130     storeEncryptTestRdbStore_ = NULL;
131     storeEncryptTestRdbStore2_ = NULL;
132 
133     errCode = OH_Rdb_DeleteStore(&config2_);
134     NAPI_ASSERT(env, errCode == 0, "OH_Rdb_DeleteStore is fail.");
135 
136     errCode = OH_Rdb_DeleteStore(&config3_);
137     NAPI_ASSERT(env, errCode == 0, "OH_Rdb_DeleteStore is fail.");
138 
139     napi_value returnCode;
140     napi_create_double(env, errCode, &returnCode);
141     return returnCode;
142 }
143 
144 
SUB_DDM_RDB_ENCRYPT_0200(napi_env env,napi_callback_info info)145 static napi_value SUB_DDM_RDB_ENCRYPT_0200(napi_env env, napi_callback_info info) {
146 
147     InitRdbConfig();
148     mkdir(config_.dataBaseDir, 0770);
149 
150     InitRdbConfig1();
151     mkdir(config1_.dataBaseDir, 0770);
152 
153     int errCode = 0;
154     storeEncryptTestRdbStore_ = OH_Rdb_GetOrOpen(&config_, &errCode);
155     NAPI_ASSERT(env, errCode == 0, "OH_Rdb_GetOrOpen is fail.");
156     NAPI_ASSERT(env, storeEncryptTestRdbStore_ != NULL, "OH_Rdb_GetOrOpen config is fail.");
157 
158     storeEncryptTestRdbStore2_ = OH_Rdb_GetOrOpen(&config1_, &errCode);
159     NAPI_ASSERT(env, storeEncryptTestRdbStore2_ != NULL, "OH_Rdb_GetOrOpen config1 is fail.");
160 
161     errCode = OH_Rdb_CloseStore(storeEncryptTestRdbStore_);
162     NAPI_ASSERT(env, errCode == 0, "OH_Rdb_CloseStore is fail.");
163     storeEncryptTestRdbStore_ = NULL;
164     storeEncryptTestRdbStore2_ = NULL;
165     errCode = OH_Rdb_DeleteStore(&config1_);
166     NAPI_ASSERT(env, errCode == 0, "OH_Rdb_DeleteStore is fail.");
167 
168     napi_value returnCode;
169     napi_create_double(env, errCode, &returnCode);
170     return returnCode;
171 }
172 
173 
174 EXTERN_C_START
Init(napi_env env,napi_value exports)175 static napi_value Init(napi_env env, napi_value exports) {
176     napi_property_descriptor desc[] = {
177     {"RdbFilePath", nullptr, RdbFilePath, nullptr, nullptr, nullptr, napi_default, nullptr},
178     {"SUB_DDM_RDB_ENCRYPT_0100", nullptr, SUB_DDM_RDB_ENCRYPT_0100, nullptr, nullptr, nullptr, napi_default, nullptr},
179     {"SUB_DDM_RDB_ENCRYPT_0200", nullptr, SUB_DDM_RDB_ENCRYPT_0200, nullptr, nullptr, nullptr, napi_default, nullptr}};
180     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
181     return exports;
182 }
183 EXTERN_C_END
184 
185 static napi_module demoModule = {
186     .nm_version = 1,
187     .nm_flags = 0,
188     .nm_filename = nullptr,
189     .nm_register_func = Init,
190     .nm_modname = "encrypt",
191     .nm_priv = ((void *)0),
192     .reserved = {0},
193 };
194 
RegisterEntryModule(void)195 extern "C" __attribute__((constructor)) void RegisterEntryModule(void) {
196     napi_module_register(&demoModule);
197 }
198