1 /*
2 * Copyright (c) 2024 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 #include "asset_auth_query_test.h"
18
19 #include <string>
20 #include <gtest/gtest.h>
21
22 #include "asset_api.h"
23 #include "asset_test_common.h"
24
25 using namespace testing::ext;
26 namespace UnitTest::AssetAuthQueryTest {
27 class AssetAuthQueryTest : public testing::Test {
28 public:
29 static void SetUpTestCase(void);
30
31 static void TearDownTestCase(void);
32
33 void SetUp(void);
34
35 void TearDown(void);
36 };
37
SetUpTestCase(void)38 void AssetAuthQueryTest::SetUpTestCase(void)
39 {
40 }
41
TearDownTestCase(void)42 void AssetAuthQueryTest::TearDownTestCase(void)
43 {
44 }
45
SetUp(void)46 void AssetAuthQueryTest::SetUp(void)
47 {
48 }
49
TearDown(void)50 void AssetAuthQueryTest::TearDown(void)
51 {
52 }
53
54 /**
55 * @tc.name: AssetAuthQueryTest.AssetAuthQueryTest001
56 * @tc.desc: Add auth asset, pre-query, query added auth asset with wrong auth token, post-query,
57 * expect ASSET_INVALID_ARGUMENT.
58 * @tc.type: FUNC
59 * @tc.result:0
60 */
61 HWTEST_F(AssetAuthQueryTest, AssetAuthQueryTest001, TestSize.Level0)
62 {
63 Asset_Blob funcName = { .size = strlen(__func__), .data = reinterpret_cast<uint8_t*>(const_cast<char*>(__func__)) };
64 Asset_Attr addAttr[] = {
65 { .tag = ASSET_TAG_ALIAS, .value.blob = funcName },
66 { .tag = ASSET_TAG_SECRET, .value.blob = funcName },
67 { .tag = ASSET_TAG_ACCESSIBILITY, .value.u32 = ASSET_ACCESSIBILITY_DEVICE_POWERED_ON },
68 { .tag = ASSET_TAG_AUTH_TYPE, .value.u32 = ASSET_AUTH_TYPE_ANY },
69 };
70 ASSERT_EQ(ASSET_SUCCESS, OH_Asset_Add(addAttr, ARRAY_SIZE(addAttr)));
71
72 Asset_Attr preQueryAttr[] = {};
73 Asset_Blob challenge = { 0 };
74 ASSERT_EQ(ASSET_SUCCESS, OH_Asset_PreQuery(preQueryAttr, ARRAY_SIZE(preQueryAttr), &challenge));
75
76 Asset_ResultSet queryResultSet = { 0 };
77 Asset_Attr queryAttr[] = {
78 { .tag = ASSET_TAG_ALIAS, .value.blob = funcName },
79 { .tag = ASSET_TAG_RETURN_TYPE, .value.u32 = ASSET_RETURN_ALL },
80 { .tag = ASSET_TAG_AUTH_CHALLENGE, .value.blob = challenge },
81 { .tag = ASSET_TAG_AUTH_TOKEN, .value.blob = funcName },
82 };
83 ASSERT_EQ(ASSET_INVALID_ARGUMENT, OH_Asset_Query(queryAttr, ARRAY_SIZE(queryAttr), &queryResultSet));
84
85 Asset_Attr postQueryAttr[] = {
86 { .tag = ASSET_TAG_AUTH_CHALLENGE, .value.blob = challenge },
87 };
88 ASSERT_EQ(ASSET_SUCCESS, OH_Asset_PostQuery(postQueryAttr, ARRAY_SIZE(postQueryAttr)));
89
90 Asset_Blob blob = { .size = 0, .data = nullptr };
91 OH_Asset_FreeBlob(&blob);
92 OH_Asset_FreeBlob(nullptr);
93 Asset_ResultSet resultSet = { .count = 0, .results = nullptr };
94 OH_Asset_FreeResultSet(&queryResultSet);
95 OH_Asset_FreeResultSet(&resultSet);
96 OH_Asset_FreeResultSet(nullptr);
97 ASSERT_EQ(ASSET_SUCCESS, RemoveByAliasNdk(__func__));
98 }
99
100 /**
101 * @tc.name: AssetAuthQueryTest.AssetAuthQueryTest002
102 * @tc.desc: Add auth asset, pre-query, query added auth asset without auth token, post-query,
103 * expect ASSET_INVALID_ARGUMENT.
104 * @tc.type: FUNC
105 * @tc.result:0
106 */
107 HWTEST_F(AssetAuthQueryTest, AssetAuthQueryTest002, TestSize.Level0)
108 {
109 Asset_Blob funcName = { .size = strlen(__func__), .data = reinterpret_cast<uint8_t*>(const_cast<char*>(__func__)) };
110 Asset_Attr addAttr[] = {
111 { .tag = ASSET_TAG_ALIAS, .value.blob = funcName },
112 { .tag = ASSET_TAG_SECRET, .value.blob = funcName },
113 { .tag = ASSET_TAG_ACCESSIBILITY, .value.u32 = ASSET_ACCESSIBILITY_DEVICE_POWERED_ON },
114 { .tag = ASSET_TAG_AUTH_TYPE, .value.u32 = ASSET_AUTH_TYPE_ANY },
115 };
116 ASSERT_EQ(ASSET_SUCCESS, OH_Asset_Add(addAttr, ARRAY_SIZE(addAttr)));
117
118 Asset_Attr preQueryAttr[] = {};
119 Asset_Blob challenge = { 0 };
120 ASSERT_EQ(ASSET_SUCCESS, OH_Asset_PreQuery(preQueryAttr, ARRAY_SIZE(preQueryAttr), &challenge));
121
122 Asset_ResultSet queryResultSet = { 0 };
123 Asset_Attr queryAttr[] = {
124 { .tag = ASSET_TAG_ALIAS, .value.blob = funcName },
125 { .tag = ASSET_TAG_RETURN_TYPE, .value.u32 = ASSET_RETURN_ALL },
126 { .tag = ASSET_TAG_AUTH_CHALLENGE, .value.blob = challenge },
127 };
128 ASSERT_EQ(ASSET_INVALID_ARGUMENT, OH_Asset_Query(queryAttr, ARRAY_SIZE(queryAttr), &queryResultSet));
129
130 Asset_Attr postQueryAttr[] = {
131 { .tag = ASSET_TAG_AUTH_CHALLENGE, .value.blob = challenge },
132 };
133 ASSERT_EQ(ASSET_SUCCESS, OH_Asset_PostQuery(postQueryAttr, ARRAY_SIZE(postQueryAttr)));
134
135 Asset_Blob blob = { .size = 0, .data = nullptr };
136 OH_Asset_FreeBlob(&blob);
137 OH_Asset_FreeBlob(nullptr);
138 Asset_ResultSet resultSet = { .count = 0, .results = nullptr };
139 OH_Asset_FreeResultSet(&queryResultSet);
140 OH_Asset_FreeResultSet(&resultSet);
141 OH_Asset_FreeResultSet(nullptr);
142 ASSERT_EQ(ASSET_SUCCESS, RemoveByAliasNdk(__func__));
143 }
144
145 /**
146 * @tc.name: AssetAuthQueryTest.AssetAuthQueryTest003
147 * @tc.desc: Pre-query, expect ASSET_NOT_FOUND.
148 * @tc.type: FUNC
149 * @tc.result:0
150 */
151 HWTEST_F(AssetAuthQueryTest, AssetAuthQueryTest003, TestSize.Level0)
152 {
153 Asset_Attr preQueryAttr[] = {};
154 Asset_Blob challenge = { 0 };
155 ASSERT_EQ(ASSET_NOT_FOUND, OH_Asset_PreQuery(preQueryAttr, ARRAY_SIZE(preQueryAttr), &challenge));
156
157 Asset_Blob blob = { .size = 0, .data = nullptr };
158 OH_Asset_FreeBlob(&blob);
159 OH_Asset_FreeBlob(nullptr);
160 Asset_ResultSet resultSet = { .count = 0, .results = nullptr };
161 OH_Asset_FreeResultSet(&resultSet);
162 OH_Asset_FreeResultSet(nullptr);
163 }
164
165 /**
166 * @tc.name: AssetAuthQueryTest.AssetAuthQueryTest004
167 * @tc.desc: Add auth asset, pre-query, post-query, expect ASSET_SUCCESS.
168 * @tc.type: FUNC
169 * @tc.result:0
170 */
171 HWTEST_F(AssetAuthQueryTest, AssetAuthQueryTest004, TestSize.Level0)
172 {
173 Asset_Blob funcName = { .size = strlen(__func__), .data = reinterpret_cast<uint8_t*>(const_cast<char*>(__func__)) };
174 Asset_Attr addAttr[] = {
175 { .tag = ASSET_TAG_ALIAS, .value.blob = funcName },
176 { .tag = ASSET_TAG_SECRET, .value.blob = funcName },
177 { .tag = ASSET_TAG_ACCESSIBILITY, .value.u32 = ASSET_ACCESSIBILITY_DEVICE_POWERED_ON },
178 { .tag = ASSET_TAG_AUTH_TYPE, .value.u32 = ASSET_AUTH_TYPE_ANY },
179 };
180 ASSERT_EQ(ASSET_SUCCESS, OH_Asset_Add(addAttr, ARRAY_SIZE(addAttr)));
181
182 Asset_Attr preQueryAttr[] = {};
183 Asset_Blob challenge = { 0 };
184 ASSERT_EQ(ASSET_SUCCESS, OH_Asset_PreQuery(preQueryAttr, ARRAY_SIZE(preQueryAttr), &challenge));
185
186 Asset_Attr postQueryAttr[] = {
187 { .tag = ASSET_TAG_AUTH_CHALLENGE, .value.blob = challenge },
188 };
189 ASSERT_EQ(ASSET_SUCCESS, OH_Asset_PostQuery(postQueryAttr, ARRAY_SIZE(postQueryAttr)));
190
191
192 Asset_Blob blob = { .size = 0, .data = nullptr };
193 OH_Asset_FreeBlob(&blob);
194 OH_Asset_FreeBlob(nullptr);
195 Asset_ResultSet resultSet = { .count = 0, .results = nullptr };
196 OH_Asset_FreeResultSet(&resultSet);
197 OH_Asset_FreeResultSet(nullptr);
198 ASSERT_EQ(ASSET_SUCCESS, RemoveByAliasNdk(__func__));
199 }
200 }