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_update_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::AssetUpdateTest {
26 class AssetUpdateTest : 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 AssetUpdateTest::SetUpTestCase(void)
38 {
39 }
40 
TearDownTestCase(void)41 void AssetUpdateTest::TearDownTestCase(void)
42 {
43 }
44 
SetUp(void)45 void AssetUpdateTest::SetUp(void)
46 {
47 }
48 
TearDown(void)49 void AssetUpdateTest::TearDown(void)
50 {
51 }
52 
53 /**
54  * @tc.name: AssetUpdateTest.AssetUpdateTest001
55  * @tc.desc: Add asset, then update with new secret, expect success
56  * @tc.type: FUNC
57  * @tc.result:0
58  */
59 HWTEST_F(AssetUpdateTest, AssetUpdateTest001, 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_Attr queryAttr[] = {
70         { .tag = ASSET_TAG_ALIAS, .value.blob = funcName }
71     };
72     const char *secretNew = "secret_new";
73     Asset_Attr updateAttr[] = {
74         {
75             .tag = ASSET_TAG_SECRET,
76             .value.blob = {
77                 .size = strlen(secretNew), .data = reinterpret_cast<uint8_t*>(const_cast<char*>(secretNew))
78             }
79         }
80     };
81     ASSERT_EQ(ASSET_SUCCESS, OH_Asset_Update(queryAttr, ARRAY_SIZE(queryAttr), updateAttr, ARRAY_SIZE(updateAttr)));
82 
83     ASSERT_EQ(ASSET_SUCCESS, RemoveByAliasNdk(__func__));
84 }
85 
86 /**
87  * @tc.name: AssetUpdateTest.AssetUpdateTest002
88  * @tc.desc: Update with empty update attr, expect ASSET_INVALID_ARGUMENT
89  * @tc.type: FUNC
90  * @tc.result:0
91  */
92 HWTEST_F(AssetUpdateTest, AssetUpdateTest002, TestSize.Level0)
93 {
94     Asset_Blob funcName = { .size = strlen(__func__), .data = reinterpret_cast<uint8_t*>(const_cast<char*>(__func__)) };
95     Asset_Attr queryAttr[] = {
96         { .tag = ASSET_TAG_ALIAS, .value.blob = funcName }
97     };
98     ASSERT_EQ(ASSET_INVALID_ARGUMENT, OH_Asset_Update(queryAttr, ARRAY_SIZE(queryAttr), nullptr, 0));
99 }
100 
101 /**
102  * @tc.name: AssetUpdateTest.AssetUpdateTest003
103  * @tc.desc: Update with empty query attr, expect ASSET_INVALID_ARGUMENT
104  * @tc.type: FUNC
105  * @tc.result:0
106  */
107 HWTEST_F(AssetUpdateTest, AssetUpdateTest003, TestSize.Level0)
108 {
109     Asset_Blob funcName = { .size = strlen(__func__), .data = reinterpret_cast<uint8_t*>(const_cast<char*>(__func__)) };
110     Asset_Attr updateAttr[] = {
111         { .tag = ASSET_TAG_SECRET, .value.blob = funcName }
112     };
113     ASSERT_EQ(ASSET_INVALID_ARGUMENT, OH_Asset_Update(nullptr, 0, updateAttr, ARRAY_SIZE(updateAttr)));
114 }
115 
116 /**
117  * @tc.name: AssetUpdateTest.AssetUpdateTest004
118  * @tc.desc: Update non-exist asset, expect ASSET_NOT_FOUND
119  * @tc.type: FUNC
120  * @tc.result:0
121  */
122 HWTEST_F(AssetUpdateTest, AssetUpdateTest004, TestSize.Level0)
123 {
124     Asset_Blob funcName = { .size = strlen(__func__), .data = reinterpret_cast<uint8_t*>(const_cast<char*>(__func__)) };
125     Asset_Attr queryAttr[] = {
126         { .tag = ASSET_TAG_ALIAS, .value.blob = funcName }
127     };
128     const char *secretNew = "secret_new";
129     Asset_Attr updateAttr[] = {
130         {
131             .tag = ASSET_TAG_SECRET,
132             .value.blob = {
133                 .size = strlen(secretNew), .data = reinterpret_cast<uint8_t*>(const_cast<char*>(secretNew))
134             }
135         }
136     };
137     ASSERT_EQ(ASSET_NOT_FOUND, OH_Asset_Update(queryAttr, ARRAY_SIZE(queryAttr), updateAttr, ARRAY_SIZE(updateAttr)));
138 }
139 }