1 /*
2 * Copyright (c) 2021 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 #ifdef IPPOVERUSB_ENABLE
17 #include <gtest/gtest.h>
18 #define private public
19 #include "print_http_server_manager.h"
20 #undef private
21 #include "print_log.h"
22
23 using namespace testing::ext;
24
25 namespace OHOS {
26 namespace Print {
27 using namespace httplib;
28 static const int64_t WAIT_TIME_IN_MS = 2000;
29 static const std::string IPP_PRINTER =
30 R"({"busNum":2,"clazz":0,"configs":[{"attributes":192,"id":1,"interfaces":[{"alternateSetting":0,"clazz":7,)"\
31 R"("endpoints":[{"address":1,"attributes":2,"direction":0,"interfaceId":0,"interval":0,"maxPacketSize":512,)"\
32 R"("number":1,"type":2},{"address":129,"attributes":2,"direction":128,"interfaceId":0,"interval":0,)"\
33 R"("maxPacketSize":512,"number":1,"type":2}],"id":0,"name":"","protocol":2,"subClass":1},{"alternateSetting":0,)"\
34 R"("clazz":255,"endpoints":[{"address":2,"attributes":2,"direction":0,"interfaceId":1,"interval":0,)"\
35 R"("maxPacketSize":512,"number":2,"type":2},{"address":130,"attributes":2,"direction":128,"interfaceId":1,)"\
36 R"("interval":0,"maxPacketSize":512,"number":2,"type":2}],"id":1,"name":"","protocol":255,"subClass":255},)"\
37 R"({"alternateSetting":0,"clazz":7,"endpoints":[{"address":3,"attributes":2,"direction":0,"interfaceId":2,)"\
38 R"("interval":0,"maxPacketSize":512,"number":3,"type":2},{"address":131,"attributes":2,"direction":128,)"\
39 R"("interfaceId":2,"interval":0,"maxPacketSize":512,"number":3,"type":2}],"id":2,"name":"","protocol":4,)"\
40 R"("subClass":1},{"alternateSetting":0,"clazz":7,"endpoints":[{"address":4,"attributes":2,"direction":0,)"\
41 R"("interfaceId":3,"interval":0,"maxPacketSize":512,"number":4,"type":2},{"address":132,"attributes":2,)"\
42 R"("direction":128,"interfaceId":3,"interval":0,"maxPacketSize":512,"number":4,"type":2}],"id":3,"name":"",)"\
43 R"("protocol":4,"subClass":1},{"alternateSetting":0,"clazz":7,"endpoints":[{"address":5,"attributes":2,)"\
44 R"("direction":0,"interfaceId":4,"interval":0,"maxPacketSize":512,"number":5,"type":2},{"address":133,)"\
45 R"("attributes":2,"direction":128,"interfaceId":4,"interval":0,"maxPacketSize":512,"number":5,"type":2}],)"\
46 R"("id":4,"name":"","protocol":4,"subClass":1},{"alternateSetting":0,"clazz":7,"endpoints":[{"address":6,)"\
47 R"("attributes":2,"direction":0,"interfaceId":5,"interval":0,"maxPacketSize":512,"number":6,"type":2},)"\
48 R"({"address":134,"attributes":2,"direction":128,"interfaceId":5,"interval":0,"maxPacketSize":512,)"\
49 R"("number":6,"type":2}],"id":5,"name":" ","protocol":4,"subClass":1}],"isRemoteWakeup":false,)"\
50 R"("isSelfPowered":true,"maxPower":1,"name":" "}],"devAddress":5,"manufacturerName":" ","name":"2-5",)"\
51 R"("productId":4293,"productName":" ","protocol":0,"serial":"","subClass":0,"vendorId":4817,"version":"0404"})";
52
53 class PrintHttpServerManagerTest : public testing::Test {
54 public:
55 static void SetUpTestCase(void);
56 static void TearDownTestCase(void);
57 void SetUp();
58 void TearDown();
59 };
60
SetUpTestCase(void)61 void PrintHttpServerManagerTest::SetUpTestCase(void)
62 {}
63
TearDownTestCase(void)64 void PrintHttpServerManagerTest::TearDownTestCase(void)
65 {}
66
SetUp(void)67 void PrintHttpServerManagerTest::SetUp(void)
68 {
69 static int32_t testNo = 0;
70 PRINT_HILOGI("PrintHttpServerManagerTest_%{public}d", ++testNo);
71 }
72
TearDown(void)73 void PrintHttpServerManagerTest::TearDown(void)
74 {}
75
76 /**
77 * @tc.name: PrintHttpServerManagerTest_001
78 * @tc.desc: Verify the CreateServer function.
79 * @tc.type: FUNC
80 * @tc.require:
81 */
82 HWTEST_F(PrintHttpServerManagerTest, PrintHttpServerManagerTest_001, TestSize.Level1)
83 {
84 auto service = std::make_shared<PrintHttpServerManager>();
85 std::string printerName = "HUAWEI PixLab V1-0105";
86 int32_t port = HTTP_MIN_PORT;
87 EXPECT_TRUE(service->CreateServer(printerName, port));
88 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_TIME_IN_MS));
89 service->StopServer(printerName);
90 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_TIME_IN_MS));
91 }
92
93 /**
94 * @tc.name: PrintHttpServerManagerTest_002
95 * @tc.desc: Verify the CreateServer function.
96 * @tc.type: FUNC
97 * @tc.require:
98 */
99 HWTEST_F(PrintHttpServerManagerTest, PrintHttpServerManagerTest_002, TestSize.Level1)
100 {
101 auto service = std::make_shared<PrintHttpServerManager>();
102 std::string printerName = "HUAWEI PixLab V1-0105";
103 int32_t port;
104 std::shared_ptr<httplib::Server> newServer = std::make_shared<httplib::Server>();
105 EXPECT_NE(newServer, nullptr);
106 service->printHttpServerMap[printerName] = newServer;
107 service->CreateServer(printerName, port);
108 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_TIME_IN_MS));
109 service->StopServer(printerName);
110 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_TIME_IN_MS));
111 }
112
113 /**
114 * @tc.name: PrintHttpServerManagerTest_003
115 * @tc.desc: Verify the CreateServer function.
116 * @tc.type: FUNC
117 * @tc.require:
118 */
119 HWTEST_F(PrintHttpServerManagerTest, PrintHttpServerManagerTest_003, TestSize.Level1)
120 {
121 auto service = std::make_shared<PrintHttpServerManager>();
122 std::string printerName = "HUAWEI PixLab V1-0105";
123 int32_t port;
124 std::shared_ptr<httplib::Server> newServer = std::make_shared<httplib::Server>();
125 EXPECT_NE(newServer, nullptr);
126 service->printHttpServerMap[printerName] = newServer;
127 service->printHttpPortMap[printerName] = 60000;
128 service->CreateServer(printerName, port);
129 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_TIME_IN_MS));
130 service->StopServer(printerName);
131 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_TIME_IN_MS));
132 }
133
134
135 /**
136 * @tc.name: PrintHttpServerManagerTest_004
137 * @tc.desc: Verify the StopServer function.
138 * @tc.type: FUNC
139 * @tc.require:
140 */
141 HWTEST_F(PrintHttpServerManagerTest, PrintHttpServerManagerTest_004, TestSize.Level1)
142 {
143 OHOS::Print::PrintHttpServerManager printHttpServerManager;
144 std::string printerName = "HUAWEI PixLab V1-0105";
145 std::shared_ptr<httplib::Server> newServer = std::make_shared<httplib::Server>();
146 EXPECT_NE(newServer, nullptr);
147 std::shared_ptr<PrintHttpRequestProcess> newProcess = std::make_shared<PrintHttpRequestProcess>();
148 EXPECT_NE(newProcess, nullptr);
149 printHttpServerManager.printHttpServerMap[printerName] = newServer;
150 printHttpServerManager.printHttpPortMap[printerName] = 60000;
151 printHttpServerManager.printHttpProcessMap[printerName] = newProcess;
152 printHttpServerManager.StopServer(printerName);
153 }
154
155 /**
156 * @tc.name: PrintHttpServerManagerTest_005
157 * @tc.desc: Verify the AllocatePort function.
158 * @tc.type: FUNC
159 * @tc.require:
160 */
161 HWTEST_F(PrintHttpServerManagerTest, PrintHttpServerManagerTest_005, TestSize.Level1)
162 {
163 OHOS::Print::PrintHttpServerManager printHttpServerManager;
164 std::string printerName = "HUAWEI PixLab V1-0105";
165 std::shared_ptr<httplib::Server> newServer = std::make_shared<httplib::Server>();
166 EXPECT_NE(newServer, nullptr);
167 std::shared_ptr<PrintHttpRequestProcess> newProcess = std::make_shared<PrintHttpRequestProcess>();
168 EXPECT_NE(newProcess, nullptr);
169 int32_t port = 0;
170 printHttpServerManager.printHttpServerMap[printerName] = newServer;
171 EXPECT_EQ(printHttpServerManager.AllocatePort(newServer, port), true);
172 printHttpServerManager.printHttpPortMap[printerName] = 60000;
173 printHttpServerManager.printHttpProcessMap[printerName] = newProcess;
174 EXPECT_EQ(printHttpServerManager.AllocatePort(newServer, port), true);
175 }
176
177 /**
178 * @tc.name: PrintHttpServerManagerTest_006
179 * @tc.desc: Verify the DealUsbDevDetch function.
180 * @tc.type: FUNC
181 * @tc.require:
182 */
183 HWTEST_F(PrintHttpServerManagerTest, PrintHttpServerManagerTest_006, TestSize.Level1)
184 {
185 OHOS::Print::PrintHttpServerManager printHttpServerManager;
186 std::string printerName = "HUAWEI PixLab V1-0105";
187 std::shared_ptr<httplib::Server> newServer = std::make_shared<httplib::Server>();
188 EXPECT_NE(newServer, nullptr);
189 std::shared_ptr<PrintHttpRequestProcess> newProcess = std::make_shared<PrintHttpRequestProcess>();
190 EXPECT_NE(newProcess, nullptr);
191 printHttpServerManager.printHttpServerMap[printerName] = newServer;
192 printHttpServerManager.printHttpPortMap[printerName] = 60000;
193 printHttpServerManager.printHttpProcessMap[printerName] = newProcess;
194 std::string devStr = "";
195 printHttpServerManager.DealUsbDevDetach(devStr);
196 }
197
198 } // namespace Print
199 } // namespace OHOS
200 #endif // IPPOVERUSB_ENABLE