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_remove_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::AssetRemoveTest {
25 class AssetRemoveTest : 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 AssetRemoveTest::SetUpTestCase(void)
37 {
38 }
39
TearDownTestCase(void)40 void AssetRemoveTest::TearDownTestCase(void)
41 {
42 }
43
SetUp(void)44 void AssetRemoveTest::SetUp(void)
45 {
46 }
47
TearDown(void)48 void AssetRemoveTest::TearDown(void)
49 {
50 }
51
52 /**
53 * @tc.name: AssetRemoveTest.AssetRemoveTest001
54 * @tc.desc: Add alias and secret, then remove it, expect success
55 * @tc.type: FUNC
56 * @tc.result:0
57 */
58 HWTEST_F(AssetRemoveTest, AssetRemoveTest001, 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_DATA_LABEL_NORMAL_1, .value.blob = funcName },
69 { .tag = ASSET_TAG_DATA_LABEL_NORMAL_2, .value.blob = funcName },
70 { .tag = ASSET_TAG_DATA_LABEL_NORMAL_3, .value.blob = funcName },
71 { .tag = ASSET_TAG_DATA_LABEL_NORMAL_4, .value.blob = funcName },
72 { .tag = ASSET_TAG_DATA_LABEL_CRITICAL_1, .value.blob = funcName },
73 { .tag = ASSET_TAG_DATA_LABEL_CRITICAL_2, .value.blob = funcName },
74 { .tag = ASSET_TAG_DATA_LABEL_CRITICAL_3, .value.blob = funcName },
75 { .tag = ASSET_TAG_DATA_LABEL_CRITICAL_4, .value.blob = funcName }
76 };
77 ASSERT_EQ(ASSET_SUCCESS, OH_Asset_Add(attr, ARRAY_SIZE(attr)));
78 Asset_Attr attr2[] = {
79 { .tag = ASSET_TAG_ALIAS, .value.blob = funcName },
80 { .tag = ASSET_TAG_ACCESSIBILITY, .value.u32 = ASSET_ACCESSIBILITY_DEVICE_POWERED_ON },
81 { .tag = ASSET_TAG_REQUIRE_PASSWORD_SET, .value.boolean = false },
82 { .tag = ASSET_TAG_AUTH_TYPE, .value.u32 = ASSET_AUTH_TYPE_NONE },
83 { .tag = ASSET_TAG_SYNC_TYPE, .value.u32 = ASSET_SYNC_TYPE_NEVER },
84 { .tag = ASSET_TAG_IS_PERSISTENT, .value.boolean = false },
85 { .tag = ASSET_TAG_DATA_LABEL_NORMAL_1, .value.blob = funcName },
86 { .tag = ASSET_TAG_DATA_LABEL_NORMAL_2, .value.blob = funcName },
87 { .tag = ASSET_TAG_DATA_LABEL_NORMAL_3, .value.blob = funcName },
88 { .tag = ASSET_TAG_DATA_LABEL_NORMAL_4, .value.blob = funcName },
89 { .tag = ASSET_TAG_DATA_LABEL_CRITICAL_1, .value.blob = funcName },
90 { .tag = ASSET_TAG_DATA_LABEL_CRITICAL_2, .value.blob = funcName },
91 { .tag = ASSET_TAG_DATA_LABEL_CRITICAL_3, .value.blob = funcName },
92 { .tag = ASSET_TAG_DATA_LABEL_CRITICAL_4, .value.blob = funcName }
93 };
94 ASSERT_EQ(ASSET_SUCCESS, OH_Asset_Remove(attr2, ARRAY_SIZE(attr2)));
95 }
96
97 /**
98 * @tc.name: AssetRemoveTest.AssetRemoveTest002
99 * @tc.desc: remove wrong blob type, expect fail
100 * @tc.type: FUNC
101 * @tc.result:0
102 */
103 HWTEST_F(AssetRemoveTest, AssetRemoveTest002, TestSize.Level0)
104 {
105 int arr[] = {
106 ASSET_TAG_ALIAS,
107 ASSET_TAG_DATA_LABEL_NORMAL_1, ASSET_TAG_DATA_LABEL_NORMAL_2,
108 ASSET_TAG_DATA_LABEL_NORMAL_3, ASSET_TAG_DATA_LABEL_NORMAL_4,
109 ASSET_TAG_DATA_LABEL_CRITICAL_1, ASSET_TAG_DATA_LABEL_CRITICAL_2,
110 ASSET_TAG_DATA_LABEL_CRITICAL_3, ASSET_TAG_DATA_LABEL_CRITICAL_4
111 };
112 for (int i = 0; i < ARRAY_SIZE(arr); i++) {
113 Asset_Blob tmpBlob = { .size = strlen(__func__), .data = nullptr };
114 Asset_Attr attr[] = {
115 { .tag = arr[i], .value.blob = tmpBlob }
116 };
117 ASSERT_EQ(ASSET_INVALID_ARGUMENT, OH_Asset_Remove(attr, ARRAY_SIZE(attr)));
118
119 attr[0].value.u32 = 0;
120 ASSERT_EQ(ASSET_INVALID_ARGUMENT, OH_Asset_Remove(attr, ARRAY_SIZE(attr)));
121
122 attr[0].value.boolean = true;
123 ASSERT_EQ(ASSET_INVALID_ARGUMENT, OH_Asset_Remove(attr, ARRAY_SIZE(attr)));
124 }
125 }
126
127 /**
128 * @tc.name: AssetRemoveTest.AssetRemoveTest003
129 * @tc.desc: remove wrong u32 type, expect fail
130 * @tc.type: FUNC
131 * @tc.result:0
132 */
133 HWTEST_F(AssetRemoveTest, AssetRemoveTest003, TestSize.Level0)
134 {
135 int arr[] = { ASSET_TAG_ACCESSIBILITY, ASSET_TAG_AUTH_TYPE, ASSET_TAG_SYNC_TYPE };
136 for (int i = 0; i < ARRAY_SIZE(arr); i++) {
137 Asset_Blob tmpBlob = { .size = strlen(__func__), .data = nullptr };
138 Asset_Attr attr[] = {
139 { .tag = arr[i], .value.blob = tmpBlob }
140 };
141 ASSERT_EQ(ASSET_INVALID_ARGUMENT, OH_Asset_Remove(attr, ARRAY_SIZE(attr)));
142
143 tmpBlob = { .size = strlen(__func__), .data = reinterpret_cast<uint8_t*>(const_cast<char*>(__func__)) };
144 attr[0].value.blob = tmpBlob;
145 ASSERT_EQ(ASSET_INVALID_ARGUMENT, OH_Asset_Remove(attr, ARRAY_SIZE(attr)));
146 }
147 }
148 }