• 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 
16 #include "asset_add_test.h"
17 
18 #include <gtest/gtest.h>
19 
20 #include "asset_api.h"
21 #include "asset_test_common.h"
22 
23 using namespace testing::ext;
24 namespace UnitTest::AssetAddTest {
25 class AssetAddTest : public testing::Test {
26 public:
27     static void SetUpTestCase(void);
28 
29     static void TearDownTestCase(void);
30 
31     void SetUp(void);
32 
33     void TearDown(void);
34 };
35 
SetUpTestCase(void)36 void AssetAddTest::SetUpTestCase(void)
37 {
38 }
39 
TearDownTestCase(void)40 void AssetAddTest::TearDownTestCase(void)
41 {
42 }
43 
SetUp(void)44 void AssetAddTest::SetUp(void)
45 {
46 }
47 
TearDown(void)48 void AssetAddTest::TearDown(void)
49 {
50 }
51 
checkMatchAttrResult(const Asset_Attr * attrs,uint32_t attrCnt,const Asset_Result * result)52 bool checkMatchAttrResult(const Asset_Attr *attrs, uint32_t attrCnt, const Asset_Result *result)
53 {
54     for (uint32_t i = 0; i < attrCnt; i++) {
55         if (attrs[i].tag == ASSET_TAG_CONFLICT_RESOLUTION) {
56             continue;
57         }
58         Asset_Attr *res = OH_Asset_ParseAttr(result, static_cast<Asset_Tag>(attrs[i].tag));
59         if (res == nullptr) {
60             return false;
61         }
62         switch (attrs[i].tag & ASSET_TAG_TYPE_MASK) {
63             case ASSET_TYPE_BOOL:
64                 if (attrs[i].value.boolean != res->value.boolean) {
65                     printf("tag is %x, %u vs %u", attrs[i].tag, attrs[i].value.boolean, res->value.boolean);
66                     return false;
67                 }
68                 break;
69             case ASSET_TYPE_NUMBER:
70                 if (attrs[i].value.u32 != res->value.u32) {
71                     printf("tag is %x, %u vs %u", attrs[i].tag, attrs[i].value.u32, res->value.u32);
72                     return false;
73                 }
74                 break;
75             case ASSET_TYPE_BYTES:
76                 if (!CompareBlob(&attrs[i].value.blob, &res->value.blob)) {
77                     printf("tag is %x, len %u vs len %u", attrs[i].tag, attrs[i].value.blob.size, res->value.blob.size);
78                     return false;
79                 }
80                 break;
81             default:
82                 return false;
83         };
84     }
85     return true;
86 }
87 
88 /**
89  * @tc.name: AssetAddTest.AssetAddTest001
90  * @tc.desc: Add asset with all attrs, then query, expect success and match
91  * @tc.type: FUNC
92  * @tc.result:0
93  */
94 HWTEST_F(AssetAddTest, AssetAddTest001, TestSize.Level0)
95 {
96     Asset_Blob funcName = { .size = strlen(__func__), .data = reinterpret_cast<uint8_t*>(const_cast<char*>(__func__)) };
97     Asset_Attr attr[] = {
98         { .tag = ASSET_TAG_ALIAS, .value.blob = funcName },
99         { .tag = ASSET_TAG_SECRET, .value.blob = funcName },
100         { .tag = ASSET_TAG_ACCESSIBILITY, .value.u32 = ASSET_ACCESSIBILITY_DEVICE_POWERED_ON },
101         { .tag = ASSET_TAG_REQUIRE_PASSWORD_SET, .value.boolean = false },
102         { .tag = ASSET_TAG_AUTH_TYPE, .value.u32 = ASSET_AUTH_TYPE_NONE },
103         { .tag = ASSET_TAG_SYNC_TYPE, .value.u32 = ASSET_SYNC_TYPE_NEVER },
104         { .tag = ASSET_TAG_CONFLICT_RESOLUTION, .value.u32 = ASSET_CONFLICT_OVERWRITE },
105         { .tag = ASSET_TAG_DATA_LABEL_NORMAL_1, .value.blob = funcName },
106         { .tag = ASSET_TAG_DATA_LABEL_NORMAL_2, .value.blob = funcName },
107         { .tag = ASSET_TAG_DATA_LABEL_NORMAL_3, .value.blob = funcName },
108         { .tag = ASSET_TAG_DATA_LABEL_NORMAL_4, .value.blob = funcName },
109         { .tag = ASSET_TAG_DATA_LABEL_CRITICAL_1, .value.blob = funcName },
110         { .tag = ASSET_TAG_DATA_LABEL_CRITICAL_2, .value.blob = funcName },
111         { .tag = ASSET_TAG_DATA_LABEL_CRITICAL_3, .value.blob = funcName },
112         { .tag = ASSET_TAG_DATA_LABEL_CRITICAL_4, .value.blob = funcName }
113     };
114     ASSERT_EQ(ASSET_SUCCESS, OH_Asset_Add(attr, ARRAY_SIZE(attr)));
115 
116     Asset_ResultSet resultSet = { 0 };
117     ASSERT_EQ(ASSET_SUCCESS, QueryByAlias(__func__, &resultSet));
118     ASSERT_EQ(1, resultSet.count);
119     Asset_Result result = resultSet.results[0];
120     ASSERT_EQ(true, checkMatchAttrResult(attr, ARRAY_SIZE(attr), &result));
121 
122     OH_Asset_FreeResultSet(&resultSet);
123     ASSERT_EQ(ASSET_SUCCESS, RemoveByAlias(__func__));
124 }
125 
126 /**
127  * @tc.name: AssetAddTest.AssetAddTest002
128  * @tc.desc: Add empty alias and secret, expect ASSET_INVALID_ARGUMENT
129  * @tc.type: FUNC
130  * @tc.result:0
131  */
132 HWTEST_F(AssetAddTest, AssetAddTest002, TestSize.Level0)
133 {
134     Asset_Blob alias = { .size = strlen(__func__), .data = nullptr };
135     Asset_Blob secret = { .size = 0, .data = reinterpret_cast<uint8_t*>(const_cast<char*>(__func__)) };
136     Asset_Attr attr[] = {
137         { .tag = ASSET_TAG_ALIAS, .value.blob = alias },
138         { .tag = ASSET_TAG_SECRET, .value.blob = secret },
139         { .tag = ASSET_TAG_ACCESSIBILITY, .value.u32 = ASSET_ACCESSIBILITY_DEVICE_POWERED_ON },
140     };
141     ASSERT_EQ(ASSET_INVALID_ARGUMENT, OH_Asset_Add(attr, ARRAY_SIZE(attr)));
142 }
143 
144 /**
145  * @tc.name: AssetAddTest.AssetAddTest003
146  * @tc.desc: Add alias and secret with wrong blob-u32/blob-boolean data type, expect ASSET_INVALID_ARGUMENT
147  * @tc.type: FUNC
148  * @tc.result:0
149  */
150 HWTEST_F(AssetAddTest, AssetAddTest003, TestSize.Level0)
151 {
152     Asset_Attr attr[] = {
153         { .tag = ASSET_TAG_ALIAS, .value.u32 = 1 },
154         { .tag = ASSET_TAG_SECRET, .value.boolean = true },
155         { .tag = ASSET_TAG_ACCESSIBILITY, .value.u32 = ASSET_ACCESSIBILITY_DEVICE_POWERED_ON },
156     };
157     ASSERT_EQ(ASSET_INVALID_ARGUMENT, OH_Asset_Add(attr, ARRAY_SIZE(attr)));
158 }
159 
160 /**
161  * @tc.name: AssetAddTest.AssetAddTest004
162  * @tc.desc: Add alias and secret with wrong u32-boolean data type, expect ASSET_INVALID_ARGUMENT
163  * @tc.type: FUNC
164  * @tc.result:0
165  */
166 HWTEST_F(AssetAddTest, AssetAddTest004, TestSize.Level0)
167 {
168     Asset_Blob alias = { .size = strlen(__func__), .data = nullptr };
169     Asset_Blob secret = { .size = 0, .data = reinterpret_cast<uint8_t*>(const_cast<char*>(__func__)) };
170     Asset_Attr attr[] = {
171         { .tag = ASSET_TAG_ALIAS, .value.blob = alias },
172         { .tag = ASSET_TAG_SECRET, .value.blob = secret },
173         { .tag = ASSET_TAG_AUTH_TYPE, .value.boolean = false },
174         { .tag = ASSET_TAG_ACCESSIBILITY, .value.u32 = ASSET_ACCESSIBILITY_DEVICE_POWERED_ON },
175     };
176     ASSERT_EQ(ASSET_INVALID_ARGUMENT, OH_Asset_Add(attr, ARRAY_SIZE(attr)));
177 }
178 
179 /**
180  * @tc.name: AssetAddTest.AssetAddTest005
181  * @tc.desc: Add alias and secret with wrong bool-blob data type, expect ASSET_INVALID_ARGUMENT
182  * @tc.type: FUNC
183  * @tc.result:0
184  */
185 HWTEST_F(AssetAddTest, AssetAddTest005, TestSize.Level0)
186 {
187     Asset_Blob alias = { .size = strlen(__func__), .data = nullptr };
188     Asset_Blob secret = { .size = 0, .data = reinterpret_cast<uint8_t*>(const_cast<char*>(__func__)) };
189     Asset_Attr attr[] = {
190         { .tag = ASSET_TAG_ALIAS, .value.blob = alias },
191         { .tag = ASSET_TAG_SECRET, .value.blob = secret },
192         { .tag = ASSET_TAG_REQUIRE_PASSWORD_SET, .value.blob = secret },
193         { .tag = ASSET_TAG_ACCESSIBILITY, .value.u32 = ASSET_ACCESSIBILITY_DEVICE_POWERED_ON },
194     };
195     ASSERT_EQ(ASSET_INVALID_ARGUMENT, OH_Asset_Add(attr, ARRAY_SIZE(attr)));
196 }
197 
198 /**
199  * @tc.name: AssetAddTest.AssetAddTest006
200  * @tc.desc: Add alias and secret, then add again, expect duplicate
201  * @tc.type: FUNC
202  * @tc.result:0
203  */
204 HWTEST_F(AssetAddTest, AssetAddTest006, TestSize.Level0)
205 {
206     Asset_Blob funcName = { .size = strlen(__func__), .data = reinterpret_cast<uint8_t*>(const_cast<char*>(__func__)) };
207     Asset_Attr attr[] = {
208         { .tag = ASSET_TAG_ALIAS, .value.blob = funcName },
209         { .tag = ASSET_TAG_SECRET, .value.blob = funcName },
210         { .tag = ASSET_TAG_ACCESSIBILITY, .value.u32 = ASSET_ACCESSIBILITY_DEVICE_POWERED_ON },
211     };
212     ASSERT_EQ(ASSET_SUCCESS, OH_Asset_Add(attr, ARRAY_SIZE(attr)));
213     ASSERT_EQ(ASSET_DUPLICATED, OH_Asset_Add(attr, ARRAY_SIZE(attr)));
214 
215     ASSERT_EQ(ASSET_SUCCESS, RemoveByAlias(__func__));
216 }
217 
218 /**
219  * @tc.name: AssetAddTest.AssetAddTest008
220  * @tc.desc: Add without attr, expect ASSET_INVALID_ARGUMENT
221  * @tc.type: FUNC
222  * @tc.result:0
223  */
224 HWTEST_F(AssetAddTest, AssetAddTest008, TestSize.Level0)
225 {
226     ASSERT_EQ(ASSET_INVALID_ARGUMENT, OH_Asset_Add(nullptr, 0));
227 }
228 
229 /**
230  * @tc.name: AssetAddTest.AssetAddTest009
231  * @tc.desc: Add without attr but count is wrong, expect ASSET_INVALID_ARGUMENT
232  * @tc.type: FUNC
233  * @tc.result:0
234  */
235 HWTEST_F(AssetAddTest, AssetAddTest009, TestSize.Level0)
236 {
237     ASSERT_EQ(ASSET_INVALID_ARGUMENT, OH_Asset_Add(nullptr, 1));
238 }
239 
240 /**
241  * @tc.name: AssetAddTest.AssetAddTest010
242  * @tc.desc: Add with attr but count is wrong, expect ASSET_INVALID_ARGUMENT
243  * @tc.type: FUNC
244  * @tc.result:0
245  */
246 HWTEST_F(AssetAddTest, AssetAddTest010, TestSize.Level0)
247 {
248     Asset_Blob funcName = { .size = strlen(__func__), .data = reinterpret_cast<uint8_t*>(const_cast<char*>(__func__)) };
249     Asset_Attr attr[] = {
250         { .tag = ASSET_TAG_ALIAS, .value.blob = funcName },
251         { .tag = ASSET_TAG_SECRET, .value.blob = funcName }
252     };
253     ASSERT_EQ(ASSET_INVALID_ARGUMENT, OH_Asset_Add(attr, 0));
254 }
255 }