1 /*
2  * Copyright (c) 2024 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 #include <gtest/gtest.h>
16 
17 #include "array_usb_device_id_serializer_test.h"
18 #include "edm_constants.h"
19 #include "utils.h"
20 
21 using namespace testing::ext;
22 
23 namespace OHOS {
24 namespace EDM {
25 namespace TEST {
SetUpTestSuite(void)26 void ArrayUsbDeviceIdSerializerTest::SetUpTestSuite(void)
27 {
28     Utils::SetEdmInitialEnv();
29 }
30 
TearDownTestSuite(void)31 void ArrayUsbDeviceIdSerializerTest::TearDownTestSuite(void)
32 {
33     Utils::ResetTokenTypeAndUid();
34     ASSERT_TRUE(Utils::IsOriginalUTEnv());
35     std::cout << "now ut process is orignal ut env : " << Utils::IsOriginalUTEnv() << std::endl;
36 }
37 
38 /**
39  * @tc.name: TestSerializeEmpty
40  * @tc.desc: Test ClipboardSerializer::Serialize when jsonString is empty
41  * @tc.type: FUNC
42  */
43 HWTEST_F(ArrayUsbDeviceIdSerializerTest, TestSerializeEmpty, TestSize.Level1)
44 {
45     auto serializer = ArrayUsbDeviceIdSerializer::GetInstance();
46     std::string jsonString;
47     std::vector<UsbDeviceId> dataObj;
48     bool ret = serializer->Serialize(dataObj, jsonString);
49     ASSERT_TRUE(ret);
50     ASSERT_TRUE(jsonString.empty());
51 }
52 
53 /**
54  * @tc.name: TestSerializeSuc
55  * @tc.desc: Test ClipboardSerializer::Serialize
56  * @tc.type: FUNC
57  */
58 HWTEST_F(ArrayUsbDeviceIdSerializerTest, TestSerializeSuc, TestSize.Level1)
59 {
60     auto serializer = ArrayUsbDeviceIdSerializer::GetInstance();
61     std::string jsonString;
62     std::vector<UsbDeviceId> dataObj;
63     UsbDeviceId id1;
64     id1.SetVendorId(111);
65     id1.SetProductId(222);
66     dataObj.push_back(id1);
67     bool ret = serializer->Serialize(dataObj, jsonString);
68     ASSERT_TRUE(ret);
69     ASSERT_FALSE(jsonString.empty());
70 }
71 
72 /**
73  * @tc.name: TestGetPolicyOutOfRange
74  * @tc.desc: Test ClipboardSerializer::GetPolicy out of range
75  * @tc.type: FUNC
76  */
77 HWTEST_F(ArrayUsbDeviceIdSerializerTest, TestGetPolicyOutOfRange, TestSize.Level1)
78 {
79     auto serializer = ArrayUsbDeviceIdSerializer::GetInstance();
80     MessageParcel data;
81     data.WriteInt32(EdmConstants::ALLOWED_USB_DEVICES_MAX_SIZE + 1);
82     std::vector<UsbDeviceId> dataObj;
83     bool ret = serializer->GetPolicy(data, dataObj);
84     ASSERT_FALSE(ret);
85 }
86 
87 /**
88  * @tc.name: TestGetPolicy
89  * @tc.desc: Test ClipboardSerializer::GetPolicy
90  * @tc.type: FUNC
91  */
92 HWTEST_F(ArrayUsbDeviceIdSerializerTest, TestGetPolicy, TestSize.Level1)
93 {
94     auto serializer = ArrayUsbDeviceIdSerializer::GetInstance();
95     MessageParcel data;
96     std::vector<UsbDeviceId> dataObj;
97     data.WriteInt32(1);
98     data.WriteInt32(111);
99     data.WriteInt32(222);
100     bool ret = serializer->GetPolicy(data, dataObj);
101     ASSERT_TRUE(ret);
102     ASSERT_TRUE(dataObj[0].GetVendorId() == 111);
103     ASSERT_TRUE(dataObj[0].GetProductId() == 222);
104 }
105 
106 /**
107  * @tc.name: TestWritePolicy
108  * @tc.desc: Test ClipboardSerializer::WritePolicy
109  * @tc.type: FUNC
110  */
111 HWTEST_F(ArrayUsbDeviceIdSerializerTest, TestWritePolicy, TestSize.Level1)
112 {
113     auto serializer = ArrayUsbDeviceIdSerializer::GetInstance();
114     MessageParcel reply;
115     std::vector<UsbDeviceId> dataObj;
116     UsbDeviceId id1;
117     id1.SetVendorId(111);
118     id1.SetProductId(222);
119     dataObj.push_back(id1);
120     bool ret = serializer->WritePolicy(reply, dataObj);
121     ASSERT_TRUE(ret);
122     ASSERT_TRUE(reply.ReadInt32() == 111);
123     ASSERT_TRUE(reply.ReadInt32() == 222);
124 }
125 
126 /**
127  * @tc.name: TestMergePolicy
128  * @tc.desc: Test ClipboardSerializer::MergePolicy
129  * @tc.type: FUNC
130  */
131 HWTEST_F(ArrayUsbDeviceIdSerializerTest, TestMergePolicy, TestSize.Level1)
132 {
133     auto serializer = ArrayUsbDeviceIdSerializer::GetInstance();
134     std::vector<std::vector<UsbDeviceId>> dataObj;
135     std::vector<UsbDeviceId> data;
136     UsbDeviceId id1;
137     id1.SetVendorId(111);
138     id1.SetProductId(222);
139     data.push_back(id1);
140     dataObj.push_back(data);
141     std::vector<UsbDeviceId> result;
142     bool ret = serializer->MergePolicy(dataObj, result);
143     ASSERT_TRUE(ret);
144     ASSERT_TRUE(result.size() == 1);
145 }
146 
147 } // namespace TEST
148 } // namespace EDM
149 } // namespace OHOS