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 
16 #include <gtest/gtest.h>
17 #define private public
18 #include "print_cups_client.h"
19 #undef private
20 #include "print_constant.h"
21 #include "print_log.h"
22 #include "mock/mock_print_cups_wrapper.h"
23 
24 using namespace testing;
25 using namespace testing::ext;
26 using json = nlohmann::json;
27 
28 namespace OHOS {
29 namespace Print {
30 static const std::string PRINTER_STATE_NONE = "none";
31 static const std::string PRINTER_STATE_MEDIA_EMPTY = "media-empty";
32 static const std::string PRINTER_STATE_MEDIA_JAM = "media-jam";
33 static const std::string PRINTER_STATE_PAUSED = "paused";
34 static const std::string PRINTER_STATE_TONER_LOW = "toner-low";
35 static const std::string PRINTER_STATE_TONER_EMPTY = "toner-empty";
36 static const std::string PRINTER_STATE_DOOR_EMPTY = "door-open";
37 static const std::string PRINTER_STATE_MEDIA_NEEDED = "media-needed";
38 static const std::string PRINTER_STATE_MARKER_LOW = "marker-supply-low";
39 static const std::string PRINTER_STATE_MARKER_EMPTY = "marker-supply-empty";
40 static const std::string PRINTER_STATE_INK_EMPTY = "marker-ink-almost-empty";
41 static const std::string PRINTER_STATE_COVER_OPEN = "cover-open";
42 static const std::string PRINTER_STATE_OFFLINE = "offline";
43 
44 using MockTestFunc = std::function<void(PrintCupsClient &printCupsClient, MockPrintCupsWrapper &mock)>;
45 
46 class PrintCupsWrapperTest : public testing::Test {
47 public:
48     static void SetUpTestCase(void);
49     static void TearDownTestCase(void);
50     void SetUp();
51     void TearDown();
52     void DoMockTest(MockTestFunc func);
53 };
54 
SetUpTestCase(void)55 void PrintCupsWrapperTest::SetUpTestCase(void)
56 {}
57 
TearDownTestCase(void)58 void PrintCupsWrapperTest::TearDownTestCase(void)
59 {}
60 
SetUp(void)61 void PrintCupsWrapperTest::SetUp(void)
62 {
63     static int32_t testNo = 0;
64     PRINT_HILOGI("PrintCupsWrapperTest_%{public}d", ++testNo);
65 }
66 
TearDown(void)67 void PrintCupsWrapperTest::TearDown(void)
68 {}
69 
DoMockTest(MockTestFunc func)70 void PrintCupsWrapperTest::DoMockTest(MockTestFunc func)
71 {
72     if (func == nullptr) {
73         PRINT_HILOGE("test func is null");
74         return;
75     }
76     PrintCupsClient printCupsClient;
77     if (printCupsClient.printAbility_ != nullptr) {
78         delete printCupsClient.printAbility_;
79         printCupsClient.printAbility_ = nullptr;
80     }
81     auto mock = new (std::nothrow) MockPrintCupsWrapper();
82     if (mock == nullptr) {
83         PRINT_HILOGE("mock is null");
84         return;
85     }
86     printCupsClient.printAbility_ = mock;
87     func(printCupsClient, *mock);
88 }
89 
90 /**
91  * @tc.name: PrintCupsWrapperTest_0001
92  * @tc.desc: QueryPrinterAttrList
93  * @tc.type: FUNC
94  * @tc.require:
95  */
96 HWTEST_F(PrintCupsWrapperTest, PrintCupsWrapperTest_0001, TestSize.Level1)
97 {
98     PrintCupsClient printCupsClient;
99     if (printCupsClient.printAbility_ != nullptr) {
100         delete printCupsClient.printAbility_;
101         printCupsClient.printAbility_ = nullptr;
102     }
103     std::string printerName = "testPrinterName";
104     std::string printUri;
105     std::string printerId;
106     std::vector<std::string> keyList;
107     std::vector<std::string> valueList;
108     EXPECT_EQ(printCupsClient.QueryPrinterAttrList(printerName, keyList, valueList), E_PRINT_SERVER_FAILURE);
109     printCupsClient.QueryPPDInformation(printerId.c_str(), valueList);
110     EXPECT_EQ(printCupsClient.DeleteCupsPrinter(printerName.c_str()), E_PRINT_SERVER_FAILURE);
111     PrinterCapability printerCap;
112     EXPECT_EQ(printCupsClient.QueryPrinterCapabilityByUri(printUri, printerId, printerCap), E_PRINT_SERVER_FAILURE);
113     EXPECT_EQ(printCupsClient.SetDefaultPrinter(printerName.c_str()), E_PRINT_SERVER_FAILURE);
114     printCupsClient.QueryJobState(nullptr, nullptr, nullptr);
115 }
116 
117 /**
118  * @tc.name: PrintCupsWrapperTest_0010
119  * @tc.desc: QueryPrinterInfoByPrinterId
120  * @tc.type: FUNC
121  * @tc.require:
122  */
123 HWTEST_F(PrintCupsWrapperTest, PrintCupsWrapperTest_0010, TestSize.Level1)
124 {
125     PrintCupsClient printCupsClient;
126     if (printCupsClient.printAbility_ != nullptr) {
127         delete printCupsClient.printAbility_;
128         printCupsClient.printAbility_ = nullptr;
129     }
130     std::string printerId = "testPrinterId";
131     PrinterInfo info;
132     EXPECT_EQ(printCupsClient.QueryPrinterInfoByPrinterId(printerId, info), E_PRINT_SERVER_FAILURE);
133 }
134 
135 /**
136  * @tc.name: PrintCupsWrapperTest_0020
137  * @tc.desc: CheckPrinterMakeModel
138  * @tc.type: FUNC
139  * @tc.require:
140  */
141 HWTEST_F(PrintCupsWrapperTest, PrintCupsWrapperTest_0020, TestSize.Level1)
142 {
143     PrintCupsClient printCupsClient;
144     if (printCupsClient.printAbility_ != nullptr) {
145         delete printCupsClient.printAbility_;
146         printCupsClient.printAbility_ = nullptr;
147     }
148     JobParameters jobParams;
149     EXPECT_EQ(printCupsClient.CheckPrinterMakeModel(&jobParams), false);
150     printCupsClient.DumpJobParameters(nullptr);
151 }
152 
153 /**
154  * @tc.name: PrintCupsWrapperTest_0030
155  * @tc.desc: CheckPrinterMakeModel
156  * @tc.type: FUNC
157  * @tc.require:
158  */
159 HWTEST_F(PrintCupsWrapperTest, PrintCupsWrapperTest_0030, TestSize.Level1)
160 {
161     PrintCupsClient printCupsClient;
162     if (printCupsClient.printAbility_ != nullptr) {
163         delete printCupsClient.printAbility_;
164         printCupsClient.printAbility_ = nullptr;
165     }
166     std::string name = "testName";
167     std::string uri = "testUri";
168     std::string ppd = "testPpd";
169     EXPECT_EQ(printCupsClient.IsPrinterExist(name.c_str(), uri.c_str(), ppd.c_str()), false);
170 }
171 
172 /**
173  * @tc.name: PrintCupsWrapperTest_0080
174  * @tc.desc: QueryPPDInformation
175  * @tc.type: FUNC
176  * @tc.require:
177  */
178 HWTEST_F(PrintCupsWrapperTest, PrintCupsWrapperTest_0080, TestSize.Level1)
179 {
180     OHOS::Print::PrintCupsClient printCupsClient;
181     int32_t ret = printCupsClient.InitCupsResources();
182     EXPECT_EQ(ret, E_PRINT_NONE);
183     std::vector<std::string> ppds;
184     printCupsClient.QueryPPDInformation(nullptr, ppds);
185     printCupsClient.ParsePPDInfo(nullptr, nullptr, nullptr, ppds);
186     printCupsClient.StopCupsdService();
187 }
188 
189 /**
190  * @tc.name: PrintCupsWrapperTest_0081
191  * @tc.desc: QueryPPDInformation
192  * @tc.type: FUNC
193  * @tc.require:
194  */
195 HWTEST_F(PrintCupsWrapperTest, PrintCupsWrapperTest_0081, TestSize.Level1)
196 {
197     OHOS::Print::PrintCupsClient printCupsClient;
198     int32_t ret = printCupsClient.InitCupsResources();
199     EXPECT_EQ(ret, E_PRINT_NONE);
200     std::string makeModel = "testmodel";
201     std::vector<std::string> ppds;
202     printCupsClient.QueryPPDInformation(makeModel.c_str(), ppds);
203     printCupsClient.StopCupsdService();
204 }
205 
206 /**
207  * @tc.name: PrintCupsWrapperTest_0082
208  * @tc.desc: ParsePPDInfo
209  * @tc.type: FUNC
210  * @tc.require:
211  */
212 HWTEST_F(PrintCupsWrapperTest, PrintCupsWrapperTest_0082, TestSize.Level1)
213 {
214     OHOS::Print::PrintCupsClient printCupsClient;
215     std::vector<std::string> ppds;
216     ipp_t* response = ippNew();
217     EXPECT_NE(response, nullptr);
218     ippAddString(response, IPP_TAG_PRINTER, IPP_TAG_TEXT, "printer-location", nullptr, "en_us");
219     ippAddString(response, IPP_TAG_PRINTER, IPP_TAG_TEXT, "ppd-make-and-model", nullptr, "testmodel");
220     ippAddString(response, IPP_TAG_PRINTER, IPP_TAG_NAME, "ppd-name", nullptr, "testppd");
221     printCupsClient.ParsePPDInfo(response, nullptr, nullptr, ppds);
222     printCupsClient.ParsePPDInfo(response, "testmodel", nullptr, ppds);
223     printCupsClient.ParsePPDInfo(response, "testmodel", "testppd", ppds);
224     ippDelete(response);
225 }
226 
227 /**
228  * @tc.name: PrintCupsWrapperTest_0083
229  * @tc.desc: AddPrinterToCups
230  * @tc.type: FUNC
231  * @tc.require:
232  */
233 HWTEST_F(PrintCupsWrapperTest, PrintCupsWrapperTest_0083, TestSize.Level1)
234 {
235     OHOS::Print::PrintCupsClient printCupsClient;
236     int32_t ret = printCupsClient.InitCupsResources();
237     EXPECT_EQ(ret, E_PRINT_NONE);
238     std::string uri = "testuri";
239     std::string name = "testname";
240     std::string makeModel = "testmodel";
241     printCupsClient.AddPrinterToCups(uri, name, makeModel);
242     printCupsClient.DeleteCupsPrinter(name.c_str());
243     printCupsClient.StopCupsdService();
244 }
245 
246 /**
247  * @tc.name: PrintCupsWrapperTest_0084
248  * @tc.desc: GetBlockedSubstate
249  * @tc.type: FUNC
250  * @tc.require:
251  */
252 HWTEST_F(PrintCupsWrapperTest, PrintCupsWrapperTest_0084, TestSize.Level1)
253 {
254     OHOS::Print::PrintCupsClient printCupsClient;
255     JobStatus jobStatus = {0};
256     constexpr size_t len = sizeof(jobStatus.printer_state_reasons);
257     strcpy_s(jobStatus.printer_state_reasons, len, PRINTER_STATE_MEDIA_EMPTY.c_str());
258     printCupsClient.GetBlockedSubstate(&jobStatus);
259     strcpy_s(jobStatus.printer_state_reasons, len, PRINTER_STATE_MEDIA_JAM.c_str());
260     printCupsClient.GetBlockedSubstate(&jobStatus);
261     strcpy_s(jobStatus.printer_state_reasons, len, PRINTER_STATE_TONER_EMPTY.c_str());
262     printCupsClient.GetBlockedSubstate(&jobStatus);
263     strcpy_s(jobStatus.printer_state_reasons, len, PRINTER_STATE_TONER_LOW.c_str());
264     printCupsClient.GetBlockedSubstate(&jobStatus);
265     strcpy_s(jobStatus.printer_state_reasons, len, PRINTER_STATE_MARKER_EMPTY.c_str());
266     printCupsClient.GetBlockedSubstate(&jobStatus);
267     strcpy_s(jobStatus.printer_state_reasons, len, PRINTER_STATE_INK_EMPTY.c_str());
268     printCupsClient.GetBlockedSubstate(&jobStatus);
269     strcpy_s(jobStatus.printer_state_reasons, len, PRINTER_STATE_MARKER_LOW.c_str());
270     printCupsClient.GetBlockedSubstate(&jobStatus);
271     strcpy_s(jobStatus.printer_state_reasons, len, PRINTER_STATE_DOOR_EMPTY.c_str());
272     printCupsClient.GetBlockedSubstate(&jobStatus);
273     strcpy_s(jobStatus.printer_state_reasons, len, PRINTER_STATE_COVER_OPEN.c_str());
274     printCupsClient.GetBlockedSubstate(&jobStatus);
275 }
276 
277 /**
278  * @tc.name: PrintCupsWrapperTest_0085
279  * @tc.desc: GetIpAddress
280  * @tc.type: FUNC
281  * @tc.require:
282  */
283 HWTEST_F(PrintCupsWrapperTest, PrintCupsWrapperTest_0085, TestSize.Level1)
284 {
285     OHOS::Print::PrintCupsClient printCupsClient;
286     unsigned int num = 0x12345678;
287     std::string ip = printCupsClient.GetIpAddress(num);
288     EXPECT_STREQ(ip.c_str(), "18.52.86.120");
289 }
290 
291 /**
292  * @tc.name: PrintCupsWrapperTest_0086
293  * @tc.desc: UpdateBorderlessJobParameter
294  * @tc.type: FUNC
295  * @tc.require:
296  */
297 HWTEST_F(PrintCupsWrapperTest, PrintCupsWrapperTest_0086, TestSize.Level1)
298 {
299     OHOS::Print::PrintCupsClient printCupsClient;
300     nlohmann::json optionJson;
301     JobParameters *jobParams = nullptr;
302     printCupsClient.UpdateBorderlessJobParameter(optionJson, jobParams);
303     jobParams = new JobParameters();
304     printCupsClient.UpdateBorderlessJobParameter(optionJson, jobParams);
305     optionJson["isBorderless"] = false;
306     printCupsClient.UpdateBorderlessJobParameter(optionJson, jobParams);
307     EXPECT_EQ(jobParams->borderless, 0);
308 
309     optionJson["isBorderless"] = true;
310     printCupsClient.UpdateBorderlessJobParameter(optionJson, jobParams);
311     EXPECT_EQ(jobParams->borderless, 1);
312 
313     optionJson["documentCategory"] = 1;
314     printCupsClient.UpdateBorderlessJobParameter(optionJson, jobParams);
315     EXPECT_EQ(jobParams->borderless, optionJson["documentCategory"]);
316     delete jobParams;
317 }
318 
319 /**
320  * @tc.name: PrintCupsWrapperTest_0087
321  * @tc.desc: GetNextJob
322  * @tc.type: FUNC
323  * @tc.require:
324  */
325 HWTEST_F(PrintCupsWrapperTest, PrintCupsWrapperTest_0087, TestSize.Level1)
326 {
327     PrintCupsClient printCupsClient;
328     EXPECT_EQ(printCupsClient.GetNextJob(), nullptr);
329     JobParameters *jobParams1 = new JobParameters();
330     JobParameters *jobParams2 = new JobParameters();
331     printCupsClient.jobQueue_.push_back(jobParams1);
332     printCupsClient.jobQueue_.push_back(jobParams2);
333     EXPECT_EQ(printCupsClient.GetNextJob(), jobParams1);
334     EXPECT_EQ(printCupsClient.GetNextJob(), nullptr);
335     delete jobParams1;
336     delete jobParams2;
337 }
338 
339 /**
340  * @tc.name: PrintCupsWrapperTest_0088
341  * @tc.desc: CopyDirectory
342  * @tc.type: FUNC
343  * @tc.require:
344  */
345 HWTEST_F(PrintCupsWrapperTest, PrintCupsWrapperTest_0088, TestSize.Level1)
346 {
347     PrintCupsClient printCupsClient;
348     std::string srcDir = "/system/bin/cups/";
349     std::string dstDir1 = "/data/local/tmp/cups_cp";
350     std::string dstDir2 = "/data/local/tmp/cups_sm";
351     printCupsClient.CopyDirectory(nullptr, dstDir1.c_str());
352     printCupsClient.SymlinkDirectory(nullptr, dstDir2.c_str());
353     printCupsClient.CopyDirectory(srcDir.c_str(), dstDir1.c_str());
354     printCupsClient.SymlinkDirectory(srcDir.c_str(), dstDir2.c_str());
355     mode_t permissions = S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH;
356     EXPECT_EQ(printCupsClient.ChangeFilterPermission(dstDir1, permissions), true);
357 }
358 
359 }  // namespace Print
360 }  // namespace OHOS