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
52 /**
53 * @tc.name: AssetAddTest.AssetAddTest001
54 * @tc.desc: Add asset with all attrs, then query, expect success and match
55 * @tc.type: FUNC
56 * @tc.result:0
57 */
58 HWTEST_F(AssetAddTest, AssetAddTest001, TestSize.Level0)
59 {
60 Asset_Blob funcName = { .size = strlen(__func__), .data = reinterpret_cast<uint8_t*>(const_cast<char*>(__func__)) };
61 Asset_Attr attr[] = {
62 { .tag = ASSET_TAG_ALIAS, .value.blob = funcName },
63 { .tag = ASSET_TAG_SECRET, .value.blob = funcName },
64 { .tag = ASSET_TAG_ACCESSIBILITY, .value.u32 = ASSET_ACCESSIBILITY_DEVICE_POWERED_ON },
65 { .tag = ASSET_TAG_REQUIRE_PASSWORD_SET, .value.boolean = false },
66 { .tag = ASSET_TAG_AUTH_TYPE, .value.u32 = ASSET_AUTH_TYPE_NONE },
67 { .tag = ASSET_TAG_SYNC_TYPE, .value.u32 = ASSET_SYNC_TYPE_NEVER },
68 { .tag = ASSET_TAG_CONFLICT_RESOLUTION, .value.u32 = ASSET_CONFLICT_OVERWRITE },
69 { .tag = ASSET_TAG_DATA_LABEL_NORMAL_1, .value.blob = funcName },
70 { .tag = ASSET_TAG_DATA_LABEL_NORMAL_2, .value.blob = funcName },
71 { .tag = ASSET_TAG_DATA_LABEL_NORMAL_3, .value.blob = funcName },
72 { .tag = ASSET_TAG_DATA_LABEL_NORMAL_4, .value.blob = funcName },
73 { .tag = ASSET_TAG_DATA_LABEL_NORMAL_LOCAL_1, .value.blob = funcName },
74 { .tag = ASSET_TAG_DATA_LABEL_NORMAL_LOCAL_2, .value.blob = funcName },
75 { .tag = ASSET_TAG_DATA_LABEL_NORMAL_LOCAL_3, .value.blob = funcName },
76 { .tag = ASSET_TAG_DATA_LABEL_NORMAL_LOCAL_4, .value.blob = funcName },
77 { .tag = ASSET_TAG_DATA_LABEL_CRITICAL_1, .value.blob = funcName },
78 { .tag = ASSET_TAG_DATA_LABEL_CRITICAL_2, .value.blob = funcName },
79 { .tag = ASSET_TAG_DATA_LABEL_CRITICAL_3, .value.blob = funcName },
80 { .tag = ASSET_TAG_DATA_LABEL_CRITICAL_4, .value.blob = funcName }
81 };
82 ASSERT_EQ(ASSET_SUCCESS, OH_Asset_Add(attr, ARRAY_SIZE(attr)));
83
84 Asset_ResultSet resultSet = { 0 };
85 ASSERT_EQ(ASSET_SUCCESS, QueryByAliasNdk(__func__, &resultSet));
86 ASSERT_EQ(1, resultSet.count);
87 Asset_Result result = resultSet.results[0];
88 ASSERT_EQ(true, CheckMatchAttrResultNdk(attr, ARRAY_SIZE(attr), &result));
89
90 OH_Asset_FreeResultSet(&resultSet);
91 ASSERT_EQ(ASSET_SUCCESS, RemoveByAliasNdk(__func__));
92 }
93
94 /**
95 * @tc.name: AssetAddTest.AssetAddTest002
96 * @tc.desc: Add empty alias and secret, expect ASSET_INVALID_ARGUMENT
97 * @tc.type: FUNC
98 * @tc.result:0
99 */
100 HWTEST_F(AssetAddTest, AssetAddTest002, TestSize.Level0)
101 {
102 Asset_Blob alias = { .size = strlen(__func__), .data = nullptr };
103 Asset_Blob secret = { .size = 0, .data = reinterpret_cast<uint8_t*>(const_cast<char*>(__func__)) };
104 Asset_Attr attr[] = {
105 { .tag = ASSET_TAG_ALIAS, .value.blob = alias },
106 { .tag = ASSET_TAG_SECRET, .value.blob = secret },
107 { .tag = ASSET_TAG_ACCESSIBILITY, .value.u32 = ASSET_ACCESSIBILITY_DEVICE_POWERED_ON },
108 };
109 ASSERT_EQ(ASSET_INVALID_ARGUMENT, OH_Asset_Add(attr, ARRAY_SIZE(attr)));
110 }
111
112 /**
113 * @tc.name: AssetAddTest.AssetAddTest003
114 * @tc.desc: Add alias and secret with wrong blob-u32/blob-boolean data type, expect ASSET_INVALID_ARGUMENT
115 * @tc.type: FUNC
116 * @tc.result:0
117 */
118 HWTEST_F(AssetAddTest, AssetAddTest003, TestSize.Level0)
119 {
120 Asset_Attr attr[] = {
121 { .tag = ASSET_TAG_ALIAS, .value.u32 = 1 },
122 { .tag = ASSET_TAG_SECRET, .value.boolean = true },
123 { .tag = ASSET_TAG_ACCESSIBILITY, .value.u32 = ASSET_ACCESSIBILITY_DEVICE_POWERED_ON },
124 };
125 ASSERT_EQ(ASSET_INVALID_ARGUMENT, OH_Asset_Add(attr, ARRAY_SIZE(attr)));
126 }
127
128 /**
129 * @tc.name: AssetAddTest.AssetAddTest004
130 * @tc.desc: Add alias and secret with wrong u32-boolean data type, expect ASSET_INVALID_ARGUMENT
131 * @tc.type: FUNC
132 * @tc.result:0
133 */
134 HWTEST_F(AssetAddTest, AssetAddTest004, TestSize.Level0)
135 {
136 Asset_Blob alias = { .size = strlen(__func__), .data = nullptr };
137 Asset_Blob secret = { .size = 0, .data = reinterpret_cast<uint8_t*>(const_cast<char*>(__func__)) };
138 Asset_Attr attr[] = {
139 { .tag = ASSET_TAG_ALIAS, .value.blob = alias },
140 { .tag = ASSET_TAG_SECRET, .value.blob = secret },
141 { .tag = ASSET_TAG_AUTH_TYPE, .value.boolean = false },
142 { .tag = ASSET_TAG_ACCESSIBILITY, .value.u32 = ASSET_ACCESSIBILITY_DEVICE_POWERED_ON },
143 };
144 ASSERT_EQ(ASSET_INVALID_ARGUMENT, OH_Asset_Add(attr, ARRAY_SIZE(attr)));
145 }
146
147 /**
148 * @tc.name: AssetAddTest.AssetAddTest005
149 * @tc.desc: Add alias and secret with wrong bool-blob data type, expect ASSET_INVALID_ARGUMENT
150 * @tc.type: FUNC
151 * @tc.result:0
152 */
153 HWTEST_F(AssetAddTest, AssetAddTest005, TestSize.Level0)
154 {
155 Asset_Blob alias = { .size = strlen(__func__), .data = nullptr };
156 Asset_Blob secret = { .size = 0, .data = reinterpret_cast<uint8_t*>(const_cast<char*>(__func__)) };
157 Asset_Attr attr[] = {
158 { .tag = ASSET_TAG_ALIAS, .value.blob = alias },
159 { .tag = ASSET_TAG_SECRET, .value.blob = secret },
160 { .tag = ASSET_TAG_REQUIRE_PASSWORD_SET, .value.blob = secret },
161 { .tag = ASSET_TAG_ACCESSIBILITY, .value.u32 = ASSET_ACCESSIBILITY_DEVICE_POWERED_ON },
162 };
163 ASSERT_EQ(ASSET_INVALID_ARGUMENT, OH_Asset_Add(attr, ARRAY_SIZE(attr)));
164 }
165
166 /**
167 * @tc.name: AssetAddTest.AssetAddTest006
168 * @tc.desc: Add alias and secret, then add again, expect duplicate
169 * @tc.type: FUNC
170 * @tc.result:0
171 */
172 HWTEST_F(AssetAddTest, AssetAddTest006, TestSize.Level0)
173 {
174 Asset_Blob funcName = { .size = strlen(__func__), .data = reinterpret_cast<uint8_t*>(const_cast<char*>(__func__)) };
175 Asset_Attr attr[] = {
176 { .tag = ASSET_TAG_ALIAS, .value.blob = funcName },
177 { .tag = ASSET_TAG_SECRET, .value.blob = funcName },
178 { .tag = ASSET_TAG_ACCESSIBILITY, .value.u32 = ASSET_ACCESSIBILITY_DEVICE_POWERED_ON },
179 };
180 ASSERT_EQ(ASSET_SUCCESS, OH_Asset_Add(attr, ARRAY_SIZE(attr)));
181 ASSERT_EQ(ASSET_DUPLICATED, OH_Asset_Add(attr, ARRAY_SIZE(attr)));
182
183 ASSERT_EQ(ASSET_SUCCESS, RemoveByAliasNdk(__func__));
184 }
185
186 /**
187 * @tc.name: AssetAddTest.AssetAddTest008
188 * @tc.desc: Add without attr, expect ASSET_INVALID_ARGUMENT
189 * @tc.type: FUNC
190 * @tc.result:0
191 */
192 HWTEST_F(AssetAddTest, AssetAddTest008, TestSize.Level0)
193 {
194 ASSERT_EQ(ASSET_INVALID_ARGUMENT, OH_Asset_Add(nullptr, 0));
195 }
196
197 /**
198 * @tc.name: AssetAddTest.AssetAddTest009
199 * @tc.desc: Add without attr but count is wrong, expect ASSET_INVALID_ARGUMENT
200 * @tc.type: FUNC
201 * @tc.result:0
202 */
203 HWTEST_F(AssetAddTest, AssetAddTest009, TestSize.Level0)
204 {
205 ASSERT_EQ(ASSET_INVALID_ARGUMENT, OH_Asset_Add(nullptr, 1));
206 }
207
208 /**
209 * @tc.name: AssetAddTest.AssetAddTest010
210 * @tc.desc: Add with attr but count is wrong, expect ASSET_INVALID_ARGUMENT
211 * @tc.type: FUNC
212 * @tc.result:0
213 */
214 HWTEST_F(AssetAddTest, AssetAddTest010, TestSize.Level0)
215 {
216 Asset_Blob funcName = { .size = strlen(__func__), .data = reinterpret_cast<uint8_t*>(const_cast<char*>(__func__)) };
217 Asset_Attr attr[] = {
218 { .tag = ASSET_TAG_ALIAS, .value.blob = funcName },
219 { .tag = ASSET_TAG_SECRET, .value.blob = funcName }
220 };
221 ASSERT_EQ(ASSET_INVALID_ARGUMENT, OH_Asset_Add(attr, 0));
222 }
223 }