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 "UTTest_dm_import_auth_code.h"
17 #include "dm_device_info.h"
18
19 #include <unistd.h>
20 #include "accesstoken_kit.h"
21 #include "device_manager_notify.h"
22 #include "dm_constants.h"
23 #include "dm_log.h"
24 #include "ipc_skeleton.h"
25 #include "nativetoken_kit.h"
26 #include "securec.h"
27 #include "token_setproc.h"
28
29 namespace OHOS {
30 namespace DistributedHardware {
SetUp()31 void DMImportAuthCodeTest::SetUp()
32 {
33 const int32_t permsNum = 2;
34 const int32_t indexZero = 0;
35 const int32_t indexOne = 1;
36 uint64_t tokenId;
37 const char *perms[permsNum];
38 perms[indexZero] = "ohos.permission.ACCESS_SERVICE_DM";
39 perms[indexOne] = "ohos.permission.DISTRIBUTED_DATASYNC";
40 NativeTokenInfoParams infoInstance = {
41 .dcapsNum = 0,
42 .permsNum = permsNum,
43 .aclsNum = 0,
44 .dcaps = NULL,
45 .perms = perms,
46 .acls = NULL,
47 .processName = "com.huawei.msdp.hmringgenerator",
48 .aplStr = "system_core",
49 };
50 tokenId = GetAccessTokenId(&infoInstance);
51 SetSelfTokenID(tokenId);
52 OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
53 }
54
TearDown()55 void DMImportAuthCodeTest::TearDown()
56 {
57 }
58
SetUpTestCase()59 void DMImportAuthCodeTest::SetUpTestCase()
60 {
61 }
62
TearDownTestCase()63 void DMImportAuthCodeTest::TearDownTestCase()
64 {
65 }
66
67 namespace {
68 HWTEST_F(DMImportAuthCodeTest, ImportAuthCode_001, testing::ext::TestSize.Level0)
69 {
70 std::string pkgName = "com.samples.devicemanager";
71 std::string authCode = "123456";
72 std::shared_ptr<DmInitCallback> initcallback = std::make_shared<DmInitCallbackTest>();
73 int32_t ret = DeviceManager::GetInstance().InitDeviceManager(pkgName, initcallback);
74 ret = DeviceManager::GetInstance().ImportAuthCode(pkgName, authCode);
75 ASSERT_EQ(ret, DM_OK);
76 DeviceManager::GetInstance().UnInitDeviceManager(pkgName);
77 }
78
79 HWTEST_F(DMImportAuthCodeTest, ExportAuthCode_001, testing::ext::TestSize.Level0)
80 {
81 std::string pkgName = "com.samples.devicemanager";
82 std::string authCode = "";
83 std::shared_ptr<DmInitCallback> initcallback = std::make_shared<DmInitCallbackTest>();
84 int32_t ret = DeviceManager::GetInstance().InitDeviceManager(pkgName, initcallback);
85 ret = DeviceManager::GetInstance().ExportAuthCode(authCode);
86 ASSERT_EQ(ret, DM_OK);
87 DeviceManager::GetInstance().UnInitDeviceManager(pkgName);
88 }
89
90 HWTEST_F(DMImportAuthCodeTest, PeerTargetId_001, testing::ext::TestSize.Level0)
91 {
92 PeerTargetId peerTargetId = {
93 .deviceId = "pkgName",
94 .brMac = "brMac",
95 .bleMac = "bleMac",
96 .wifiIp = "wifiIp",
97 .wifiPort = 0,
98 };
99
100 PeerTargetId other;
101 ASSERT_NE(other == peerTargetId, true);
102 ASSERT_EQ(other < peerTargetId, true);
103 }
104 } // namespace
105 } // namespace DistributedHardware
106 } // namespace OHOS