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 <gtest/gtest.h>
17 #include <memory>
18
19 #include "message_parcel.h"
20 #include "net_conn_constants.h"
21 #include "net_conn_security.h"
22 #include "net_datashare_utils.h"
23
24 namespace OHOS {
25 namespace NetManagerStandard {
26 namespace {
27 using namespace testing::ext;
28 using namespace Security::AccessToken;
29 using Security::AccessToken::AccessTokenID;
30
31 HapInfoParams testInfoParms = {.bundleName = "net_datashare_utils_test",
32 .userID = 100,
33 .instIndex = 0,
34 .appIDDesc = "test",
35 .isSystemApp = true};
36
37 PermissionDef testPermDef = {
38 .permissionName = "ohos.permission.MANAGE_SECURE_SETTINGS",
39 .bundleName = "net_datashare_utils_test",
40 .grantMode = 1,
41 .label = "label",
42 .labelId = 1,
43 .description = "Test net data share",
44 .descriptionId = 1,
45 .availableLevel = APL_SYSTEM_BASIC,
46 };
47
48 PermissionStateFull testState = {
49 .grantFlags = {2},
50 .grantStatus = {PermissionState::PERMISSION_GRANTED},
51 .isGeneral = true,
52 .permissionName = "ohos.permission.MANAGE_SECURE_SETTINGS",
53 .resDeviceID = {"local"},
54 };
55
56 HapPolicyParams testPolicyPrams = {
57 .apl = APL_SYSTEM_BASIC,
58 .domain = "test.domain",
59 .permList = {testPermDef},
60 .permStateList = {testState},
61 };
62 } // namespace
63
64 std::unique_ptr<NetDataShareHelperUtils> netDataShareHelperUtils_ = nullptr;
65 class NetDataShareHelperUtilsTest : public testing::Test {
66 public:
67 static void SetUpTestCase();
68 static void TearDownTestCase();
69 void SetUp();
70 void TearDown();
71 };
72
SetUpTestCase()73 void NetDataShareHelperUtilsTest::SetUpTestCase()
74 {
75 netDataShareHelperUtils_ = std::make_unique<NetDataShareHelperUtils>();
76 }
77
TearDownTestCase()78 void NetDataShareHelperUtilsTest::TearDownTestCase()
79 {
80 netDataShareHelperUtils_.reset();
81 }
82
SetUp()83 void NetDataShareHelperUtilsTest::SetUp() {}
84
TearDown()85 void NetDataShareHelperUtilsTest::TearDown() {}
86
87 /**
88 * @tc.name: InsertTest001
89 * @tc.desc: Test NetDataShareHelperUtils::Insert
90 * @tc.type: FUNC
91 */
92 HWTEST_F(NetDataShareHelperUtilsTest, InsertTest001, TestSize.Level1)
93 {
94 std::string airplaneMode = "1";
95 Uri uri(AIRPLANE_MODE_URI);
96 int32_t ret = netDataShareHelperUtils_->Insert(uri, KEY_AIRPLANE_MODE, airplaneMode);
97 ASSERT_TRUE(ret == NETMANAGER_ERROR);
98
99 airplaneMode = "0";
100 ret = netDataShareHelperUtils_->Insert(uri, KEY_AIRPLANE_MODE, airplaneMode);
101 ASSERT_TRUE(ret == NETMANAGER_ERROR);
102 }
103
104 /**
105 * @tc.name: InsertTest002
106 * @tc.desc: Test NetDataShareHelperUtils::Insert
107 * @tc.type: FUNC
108 */
109 HWTEST_F(NetDataShareHelperUtilsTest, InsertTest002, TestSize.Level1)
110 {
111 OHOS::NetManagerStandard::AccessToken token(testInfoParms, testPolicyPrams);
112 std::string airplaneMode = "1";
113 Uri uri(AIRPLANE_MODE_URI);
114 int32_t ret = netDataShareHelperUtils_->Insert(uri, KEY_AIRPLANE_MODE, airplaneMode);
115 ASSERT_TRUE(ret == NETMANAGER_SUCCESS);
116
117 airplaneMode = "0";
118 ret = netDataShareHelperUtils_->Insert(uri, KEY_AIRPLANE_MODE, airplaneMode);
119 ASSERT_TRUE(ret == NETMANAGER_SUCCESS);
120 }
121
122 /**
123 * @tc.name: UpdateTest001
124 * @tc.desc: Test NetDataShareHelperUtils::Update
125 * @tc.type: FUNC
126 */
127 HWTEST_F(NetDataShareHelperUtilsTest, UpdateTest001, TestSize.Level1)
128 {
129 std::string airplaneMode = "1";
130 Uri uri(AIRPLANE_MODE_URI);
131 int32_t ret = netDataShareHelperUtils_->Update(uri, KEY_AIRPLANE_MODE, airplaneMode);
132 ASSERT_TRUE(ret == NETMANAGER_ERROR);
133
134 airplaneMode = "0";
135 ret = netDataShareHelperUtils_->Update(uri, KEY_AIRPLANE_MODE, airplaneMode);
136 ASSERT_TRUE(ret == NETMANAGER_ERROR);
137 }
138
139 /**
140 * @tc.name: UpdateTest002
141 * @tc.desc: Test NetDataShareHelperUtils::Update
142 * @tc.type: FUNC
143 */
144 HWTEST_F(NetDataShareHelperUtilsTest, UpdateTest002, TestSize.Level1)
145 {
146 OHOS::NetManagerStandard::AccessToken token(testInfoParms, testPolicyPrams);
147 std::string airplaneMode = "1";
148 Uri uri(AIRPLANE_MODE_URI);
149 int32_t ret = netDataShareHelperUtils_->Update(uri, KEY_AIRPLANE_MODE, airplaneMode);
150 ASSERT_TRUE(ret == NETMANAGER_SUCCESS);
151
152 airplaneMode = "0";
153 ret = netDataShareHelperUtils_->Update(uri, KEY_AIRPLANE_MODE, airplaneMode);
154 ASSERT_TRUE(ret == NETMANAGER_SUCCESS);
155 }
156
157 /**
158 * @tc.name: QueryTest001
159 * @tc.desc: Test NetDataShareHelperUtils::Query
160 * @tc.type: FUNC
161 */
162 HWTEST_F(NetDataShareHelperUtilsTest, QueryTest001, TestSize.Level1)
163 {
164 std::string airplaneMode;
165 Uri uri(AIRPLANE_MODE_URI);
166 int32_t ret = netDataShareHelperUtils_->Query(uri, KEY_AIRPLANE_MODE, airplaneMode);
167 ASSERT_TRUE(ret == NETMANAGER_SUCCESS);
168 std::cout << "QueryTest result:" << airplaneMode << std::endl;
169 }
170
171 /**
172 * @tc.name: QueryTest002
173 * @tc.desc: Test NetDataShareHelperUtils::Query
174 * @tc.type: FUNC
175 */
176 HWTEST_F(NetDataShareHelperUtilsTest, QueryTest002, TestSize.Level1)
177 {
178 OHOS::NetManagerStandard::AccessToken token(testInfoParms, testPolicyPrams);
179 std::string airplaneMode;
180 Uri uri(AIRPLANE_MODE_URI);
181 int32_t ret = netDataShareHelperUtils_->Query(uri, KEY_AIRPLANE_MODE, airplaneMode);
182 ASSERT_TRUE(ret == NETMANAGER_SUCCESS);
183 std::cout << "QueryTest result:" << airplaneMode << std::endl;
184 }
185 } // namespace NetManagerStandard
186 } // namespace OHOS
187