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