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 <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::AssetQueryTest {
26 class AssetQueryTest : 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 AssetQueryTest::SetUpTestCase(void)
38 {
39 }
40 
TearDownTestCase(void)41 void AssetQueryTest::TearDownTestCase(void)
42 {
43 }
44 
SetUp(void)45 void AssetQueryTest::SetUp(void)
46 {
47 }
48 
TearDown(void)49 void AssetQueryTest::TearDown(void)
50 {
51 }
52 
53 /**
54  * @tc.name: AssetQueryTest.AssetQueryTest001
55  * @tc.desc: Add asset, then query with correct attr, expect success
56  * @tc.type: FUNC
57  * @tc.result:0
58  */
59 HWTEST_F(AssetQueryTest, AssetQueryTest001, TestSize.Level0)
60 {
61     Asset_Blob funcName = { .size = strlen(__func__), .data = reinterpret_cast<uint8_t*>(const_cast<char*>(__func__)) };
62     Asset_Attr addAttr[] = {
63         { .tag = ASSET_TAG_ALIAS, .value.blob = funcName },
64         { .tag = ASSET_TAG_SECRET, .value.blob = funcName },
65         { .tag = ASSET_TAG_ACCESSIBILITY, .value.u32 = ASSET_ACCESSIBILITY_DEVICE_POWERED_ON },
66     };
67     ASSERT_EQ(ASSET_SUCCESS, OH_Asset_Add(addAttr, ARRAY_SIZE(addAttr)));
68 
69     Asset_ResultSet resultSet = { 0 };
70     Asset_Attr queryAttr[] = {
71         { .tag = ASSET_TAG_ALIAS, .value.blob = funcName },
72         { .tag = ASSET_TAG_RETURN_TYPE, .value.u32 = ASSET_RETURN_ALL }
73     };
74     ASSERT_EQ(ASSET_SUCCESS, OH_Asset_Query(queryAttr, ARRAY_SIZE(queryAttr), &resultSet));
75     ASSERT_EQ(1, resultSet.count);
76 
77     OH_Asset_FreeResultSet(&resultSet);
78     ASSERT_EQ(ASSET_SUCCESS, RemoveByAliasNdk(__func__));
79 }
80 
81 /**
82  * @tc.name: AssetQueryTest.AssetQueryTest002
83  * @tc.desc: Query non-exist asset, expect ASSET_NOT_FOUND
84  * @tc.type: FUNC
85  * @tc.result:0
86  */
87 HWTEST_F(AssetQueryTest, AssetQueryTest002, TestSize.Level0)
88 {
89     Asset_Blob funcName = { .size = strlen(__func__), .data = reinterpret_cast<uint8_t*>(const_cast<char*>(__func__)) };
90     Asset_ResultSet resultSet = { 0 };
91     Asset_Attr attr[] = {
92         { .tag = ASSET_TAG_ALIAS, .value.blob = funcName },
93         { .tag = ASSET_TAG_RETURN_TYPE, .value.u32 = ASSET_RETURN_ALL }
94     };
95     ASSERT_EQ(ASSET_NOT_FOUND, OH_Asset_Query(attr, ARRAY_SIZE(attr), &resultSet));
96 
97     OH_Asset_FreeResultSet(&resultSet);
98 }
99 
100 /**
101  * @tc.name: AssetQueryTest.AssetQueryTest003
102  * @tc.desc: Query non-exist asset, expect ASSET_NOT_FOUND
103  * @tc.type: FUNC
104  * @tc.result:0
105  */
106 HWTEST_F(AssetQueryTest, AssetQueryTest003, TestSize.Level0)
107 {
108     Asset_Blob funcName = { .size = strlen(__func__), .data = reinterpret_cast<uint8_t*>(const_cast<char*>(__func__)) };
109     Asset_ResultSet resultSet = { 0 };
110     Asset_Attr attr[] = {
111         { .tag = ASSET_TAG_ALIAS, .value.blob = funcName },
112         { .tag = ASSET_TAG_RETURN_TYPE, .value.u32 = ASSET_RETURN_ALL }
113     };
114     ASSERT_EQ(ASSET_NOT_FOUND, OH_Asset_Query(attr, ARRAY_SIZE(attr), &resultSet));
115 
116     OH_Asset_FreeResultSet(&resultSet);
117 }
118 
119 /**
120  * @tc.name: AssetQueryTest.AssetQueryTest004
121  * @tc.desc: Query without attr, expect ASSET_INVALID_ARGUMENT
122  * @tc.type: FUNC
123  * @tc.result:0
124  */
125 HWTEST_F(AssetQueryTest, AssetQueryTest004, TestSize.Level0)
126 {
127     Asset_ResultSet resultSet = { 0 };
128     ASSERT_EQ(ASSET_INVALID_ARGUMENT, OH_Asset_Query(nullptr, 1, &resultSet));
129 }
130 
131 /**
132  * @tc.name: AssetQueryTest.AssetQueryTest005
133  * @tc.desc: Query with attr but count is wrong, expect ASSET_INVALID_ARGUMENT
134  * @tc.type: FUNC
135  * @tc.result:0
136  */
137 HWTEST_F(AssetQueryTest, AssetQueryTest005, TestSize.Level0)
138 {
139     Asset_Blob funcName = { .size = strlen(__func__), .data = reinterpret_cast<uint8_t*>(const_cast<char*>(__func__)) };
140     Asset_ResultSet resultSet = { 0 };
141     Asset_Attr attr[] = {
142         { .tag = ASSET_TAG_ALIAS, .value.blob = funcName },
143         { .tag = ASSET_TAG_RETURN_TYPE, .value.u32 = ASSET_RETURN_ALL }
144     };
145     ASSERT_EQ(ASSET_NOT_FOUND, OH_Asset_Query(attr, 0, &resultSet));
146 }
147 
148 /**
149  * @tc.name: AssetQueryTest.AssetQueryTest006
150  * @tc.desc: Query without attr but count is wrong, expect ASSET_INVALID_ARGUMENT
151  * @tc.type: FUNC
152  * @tc.result:0
153  */
154 HWTEST_F(AssetQueryTest, AssetQueryTest006, TestSize.Level0)
155 {
156     Asset_ResultSet resultSet = { 0 };
157     ASSERT_EQ(ASSET_INVALID_ARGUMENT, OH_Asset_Query(nullptr, 1, &resultSet));
158 }
159 
160 /**
161  * @tc.name: AssetQueryTest.AssetQueryTest007
162  * @tc.desc: Query without resultSet, expect ASSET_INVALID_ARGUMENT
163  * @tc.type: FUNC
164  * @tc.result:0
165  */
166 HWTEST_F(AssetQueryTest, AssetQueryTest007, TestSize.Level0)
167 {
168     Asset_Blob funcName = { .size = strlen(__func__), .data = reinterpret_cast<uint8_t*>(const_cast<char*>(__func__)) };
169     Asset_Attr attr[] = {
170         { .tag = ASSET_TAG_ALIAS, .value.blob = funcName },
171         { .tag = ASSET_TAG_RETURN_TYPE, .value.u32 = ASSET_RETURN_ALL }
172     };
173     ASSERT_EQ(ASSET_INVALID_ARGUMENT, OH_Asset_Query(attr, ARRAY_SIZE(attr), nullptr));
174 }
175 }