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 #include "print_json_util.h"
24
25 using namespace testing;
26 using namespace testing::ext;
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_OTHER = "other";
43 static const std::string PRINTER_STATE_OFFLINE = "offline";
44
45 using MockTestFunc = std::function<void(PrintCupsClient &printCupsClient, MockPrintCupsWrapper &mock)>;
46
47 class PrintCupsWrapperTest : public testing::Test {
48 public:
49 static void SetUpTestCase(void);
50 static void TearDownTestCase(void);
51 void SetUp();
52 void TearDown();
53 void DoMockTest(MockTestFunc func);
54 };
55
SetUpTestCase(void)56 void PrintCupsWrapperTest::SetUpTestCase(void)
57 {}
58
TearDownTestCase(void)59 void PrintCupsWrapperTest::TearDownTestCase(void)
60 {}
61
SetUp(void)62 void PrintCupsWrapperTest::SetUp(void)
63 {
64 static int32_t testNo = 0;
65 PRINT_HILOGI("PrintCupsWrapperTest_%{public}d", ++testNo);
66 }
67
TearDown(void)68 void PrintCupsWrapperTest::TearDown(void)
69 {}
70
DoMockTest(MockTestFunc func)71 void PrintCupsWrapperTest::DoMockTest(MockTestFunc func)
72 {
73 if (func == nullptr) {
74 PRINT_HILOGE("test func is null");
75 return;
76 }
77 PrintCupsClient printCupsClient;
78 if (printCupsClient.printAbility_ != nullptr) {
79 delete printCupsClient.printAbility_;
80 printCupsClient.printAbility_ = nullptr;
81 }
82 auto mock = new (std::nothrow) MockPrintCupsWrapper();
83 if (mock == nullptr) {
84 PRINT_HILOGE("mock is null");
85 return;
86 }
87 printCupsClient.printAbility_ = mock;
88 func(printCupsClient, *mock);
89 }
90
91 /**
92 * @tc.name: PrintCupsWrapperTest_0001
93 * @tc.desc: QueryPrinterAttrList
94 * @tc.type: FUNC
95 * @tc.require:
96 */
97 HWTEST_F(PrintCupsWrapperTest, PrintCupsWrapperTest_0001, TestSize.Level1)
98 {
99 PrintCupsClient printCupsClient;
100 if (printCupsClient.printAbility_ != nullptr) {
101 delete printCupsClient.printAbility_;
102 printCupsClient.printAbility_ = nullptr;
103 }
104 std::string printerName = "testPrinterName";
105 std::string printerUri;
106 std::string printerId;
107 std::string ppdName;
108 std::vector<std::string> keyList;
109 std::vector<std::string> valueList;
110 EXPECT_EQ(printCupsClient.QueryPrinterAttrList(printerName, keyList, valueList), E_PRINT_SERVER_FAILURE);
111 printCupsClient.QueryPPDInformation(printerId.c_str(), ppdName);
112 EXPECT_EQ(printCupsClient.DeleteCupsPrinter(printerName.c_str()), E_PRINT_SERVER_FAILURE);
113 PrinterCapability printerCap;
114 EXPECT_EQ(printCupsClient.QueryPrinterCapabilityByUri(printerUri, printerId, printerCap), E_PRINT_SERVER_FAILURE);
115 EXPECT_EQ(printCupsClient.SetDefaultPrinter(printerName.c_str()), E_PRINT_SERVER_FAILURE);
116 printCupsClient.QueryJobState(nullptr, nullptr);
117 EXPECT_EQ(printCupsClient.QueryPrinterCapabilityFromPPD(printerName, printerCap, ""), E_PRINT_SERVER_FAILURE);
118 EXPECT_FALSE(printCupsClient.ModifyCupsPrinterUri(printerName, printerUri));
119 }
120
121 /**
122 * @tc.name: PrintCupsWrapperTest_0002
123 * @tc.desc: QueryPrinterAttrList
124 * @tc.type: FUNC
125 * @tc.require:
126 */
127 HWTEST_F(PrintCupsWrapperTest, PrintCupsWrapperTest_0002, TestSize.Level1)
128 {
__anon241ac4a30102(PrintCupsClient &printCupsClient, MockPrintCupsWrapper &mock) 129 MockTestFunc testFunc = [this](PrintCupsClient &printCupsClient, MockPrintCupsWrapper &mock) {
130 EXPECT_CALL(mock, GetNamedDest(_, _, _)).Times(1).WillOnce(Return(nullptr));
131 std::string printerName = "testPrinterName";
132 std::vector<std::string> keyList = {"key1", "key2"};
133 std::vector<std::string> valueList;
134 EXPECT_EQ(printCupsClient.QueryPrinterAttrList(printerName, keyList, valueList), E_PRINT_SERVER_FAILURE);
135 };
136 DoMockTest(testFunc);
137 }
138
139 /**
140 * @tc.name: PrintCupsWrapperTest_0003
141 * @tc.desc: QueryPrinterAttrList
142 * @tc.type: FUNC
143 * @tc.require:
144 */
145 HWTEST_F(PrintCupsWrapperTest, PrintCupsWrapperTest_0003, TestSize.Level1)
146 {
147 cups_dest_t cupsDests = {0};
__anon241ac4a30202(PrintCupsClient &printCupsClient, MockPrintCupsWrapper &mock) 148 MockTestFunc testFunc = [this, &cupsDests](PrintCupsClient &printCupsClient, MockPrintCupsWrapper &mock) {
149 EXPECT_CALL(mock, GetNamedDest(_, _, _)).Times(1).WillOnce(Return(&cupsDests));
150 EXPECT_CALL(mock, FreeDests(_, _)).WillRepeatedly(Return());
151 std::string printerName = "testPrinterName";
152 std::vector<std::string> keyList = {"key1", "key2"};
153 std::vector<std::string> valueList;
154 EXPECT_EQ(printCupsClient.QueryPrinterAttrList(printerName, keyList, valueList), E_PRINT_NONE);
155 };
156 DoMockTest(testFunc);
157 }
158
159 /**
160 * @tc.name: PrintCupsWrapperTest_0005
161 * @tc.desc: QueryJobState
162 * @tc.type: FUNC
163 * @tc.require:
164 */
165 HWTEST_F(PrintCupsWrapperTest, PrintCupsWrapperTest_0005, TestSize.Level1)
166 {
__anon241ac4a30302(PrintCupsClient &printCupsClient, MockPrintCupsWrapper &mock) 167 MockTestFunc testFunc = [this](PrintCupsClient &printCupsClient, MockPrintCupsWrapper &mock) {
168 auto param = std::make_shared<JobMonitorParam>(nullptr, "", 0, "", "", "", nullptr);
169 JobStatus jobStatus = {0};
170 printCupsClient.QueryJobState(CUPS_HTTP_DEFAULT, nullptr);
171 printCupsClient.QueryJobState(CUPS_HTTP_DEFAULT, param);
172 printCupsClient.QueryJobState(CUPS_HTTP_DEFAULT, param);
173 EXPECT_EQ(jobStatus.job_state, 0);
174 EXPECT_EQ(printCupsClient.CheckPrinterOnline(nullptr, 1), false);
175 EXPECT_EQ(printCupsClient.CheckPrinterOnline(param, 1), false);
176 };
177 DoMockTest(testFunc);
178 }
179
180 /**
181 * @tc.name: PrintCupsWrapperTest_0010
182 * @tc.desc: QueryPrinterInfoByPrinterId
183 * @tc.type: FUNC
184 * @tc.require:
185 */
186 HWTEST_F(PrintCupsWrapperTest, PrintCupsWrapperTest_0010, TestSize.Level1)
187 {
188 PrintCupsClient printCupsClient;
189 if (printCupsClient.printAbility_ != nullptr) {
190 delete printCupsClient.printAbility_;
191 printCupsClient.printAbility_ = nullptr;
192 }
193 std::string printerId = "testPrinterId";
194 PrinterInfo info;
195 EXPECT_EQ(printCupsClient.QueryPrinterInfoByPrinterId(printerId, info), E_PRINT_SERVER_FAILURE);
196 }
197
198 /**
199 * @tc.name: PrintCupsWrapperTest_0011
200 * @tc.desc: QueryPrinterInfoByPrinterId
201 * @tc.type: FUNC
202 * @tc.require:
203 */
204 HWTEST_F(PrintCupsWrapperTest, PrintCupsWrapperTest_0011, TestSize.Level1)
205 {
__anon241ac4a30402(PrintCupsClient &printCupsClient, MockPrintCupsWrapper &mock) 206 MockTestFunc testFunc = [this](PrintCupsClient &printCupsClient, MockPrintCupsWrapper &mock) {
207 EXPECT_CALL(mock, GetNamedDest(_, _, _)).Times(1).WillOnce(Return(nullptr));
208 std::string printerId = "testPrinterId";
209 PrinterInfo info;
210 EXPECT_EQ(printCupsClient.QueryPrinterInfoByPrinterId(printerId, info), E_PRINT_SERVER_FAILURE);
211 };
212 DoMockTest(testFunc);
213 }
214
215 /**
216 * @tc.name: PrintCupsWrapperTest_0012
217 * @tc.desc: QueryPrinterInfoByPrinterId
218 * @tc.type: FUNC
219 * @tc.require:
220 */
221 HWTEST_F(PrintCupsWrapperTest, PrintCupsWrapperTest_0012, TestSize.Level1)
222 {
223 cups_dest_t cupsDests = {0};
__anon241ac4a30502(PrintCupsClient &printCupsClient, MockPrintCupsWrapper &mock) 224 MockTestFunc testFunc = [this, &cupsDests](PrintCupsClient &printCupsClient, MockPrintCupsWrapper &mock) {
225 EXPECT_CALL(mock, GetNamedDest(_, _, _)).Times(1).WillOnce(Return(&cupsDests));
226 EXPECT_CALL(mock, FreeDests(_, _)).WillRepeatedly(Return());
227 std::string printerId = "testPrinterId";
228 PrinterInfo info;
229 EXPECT_EQ(printCupsClient.QueryPrinterInfoByPrinterId(printerId, info), E_PRINT_NONE);
230 };
231 DoMockTest(testFunc);
232 }
233
234 /**
235 * @tc.name: PrintCupsWrapperTest_0013
236 * @tc.desc: QueryPrinterInfoByPrinterId
237 * @tc.type: FUNC
238 * @tc.require:
239 */
240 HWTEST_F(PrintCupsWrapperTest, PrintCupsWrapperTest_0013, TestSize.Level1)
241 {
242 cups_dest_t cupsDests = {0};
__anon241ac4a30602(PrintCupsClient &printCupsClient, MockPrintCupsWrapper &mock) 243 MockTestFunc testFunc = [this, &cupsDests](PrintCupsClient &printCupsClient, MockPrintCupsWrapper &mock) {
244 EXPECT_CALL(mock, GetNamedDest(_, _, _)).Times(1).WillOnce(Return(&cupsDests));
245 EXPECT_CALL(mock, FreeDests(_, _)).WillRepeatedly(Return());
246 std::string printerId = "testPrinterId";
247 PrinterInfo info;
248 std::string option = "test option";
249 info.SetOption(option);
250 EXPECT_EQ(printCupsClient.QueryPrinterInfoByPrinterId(printerId, info), E_PRINT_INVALID_PARAMETER);
251 };
252 DoMockTest(testFunc);
253 }
254
255 /**
256 * @tc.name: PrintCupsWrapperTest_0014
257 * @tc.desc: QueryPrinterInfoByPrinterId
258 * @tc.type: FUNC
259 * @tc.require:
260 */
261 HWTEST_F(PrintCupsWrapperTest, PrintCupsWrapperTest_0014, TestSize.Level1)
262 {
263 cups_dest_t cupsDests = {0};
__anon241ac4a30702(PrintCupsClient &printCupsClient, MockPrintCupsWrapper &mock) 264 MockTestFunc testFunc = [this, &cupsDests](PrintCupsClient &printCupsClient, MockPrintCupsWrapper &mock) {
265 EXPECT_CALL(mock, GetNamedDest(_, _, _)).Times(1).WillOnce(Return(&cupsDests));
266 EXPECT_CALL(mock, FreeDests(_, _)).WillRepeatedly(Return());
267 std::string printerId = "testPrinterId";
268 PrinterInfo info;
269 Json::Value option;
270 option["printerName"] = "name";
271 option["printerUri"] = "uri";
272 info.SetOption(PrintJsonUtil::WriteString(option));
273 printCupsClient.QueryPrinterInfoByPrinterId(printerId, info);
274 };
275 DoMockTest(testFunc);
276 }
277
278 /**
279 * @tc.name: PrintCupsWrapperTest_0015
280 * @tc.desc: QueryPrinterInfoByPrinterId
281 * @tc.type: FUNC
282 * @tc.require:
283 */
284 HWTEST_F(PrintCupsWrapperTest, PrintCupsWrapperTest_0015, TestSize.Level1)
285 {
286 cups_dest_t cupsDests = {0};
__anon241ac4a30802(PrintCupsClient &printCupsClient, MockPrintCupsWrapper &mock) 287 MockTestFunc testFunc = [this, &cupsDests](PrintCupsClient &printCupsClient, MockPrintCupsWrapper &mock) {
288 EXPECT_CALL(mock, GetNamedDest(_, _, _)).Times(1).WillOnce(Return(&cupsDests));
289 EXPECT_CALL(mock, FreeDests(_, _)).WillRepeatedly(Return());
290 std::string printerId = "testPrinterId";
291 PrinterInfo info;
292 Json::Value option;
293 option["printerName"] = "name";
294 info.SetOption(PrintJsonUtil::WriteString(option));
295 printCupsClient.QueryPrinterInfoByPrinterId(printerId, info);
296 };
297 DoMockTest(testFunc);
298 }
299
300 /**
301 * @tc.name: PrintCupsWrapperTest_0020
302 * @tc.desc: CheckPrinterMakeModel
303 * @tc.type: FUNC
304 * @tc.require:
305 */
306 HWTEST_F(PrintCupsWrapperTest, PrintCupsWrapperTest_0020, TestSize.Level1)
307 {
308 PrintCupsClient printCupsClient;
309 if (printCupsClient.printAbility_ != nullptr) {
310 delete printCupsClient.printAbility_;
311 printCupsClient.printAbility_ = nullptr;
312 }
313 JobParameters jobParams;
314 bool driverMissing = false;
315 EXPECT_EQ(printCupsClient.CheckPrinterMakeModel(&jobParams, driverMissing), false);
316 printCupsClient.DumpJobParameters(nullptr);
317 }
318
319 /**
320 * @tc.name: PrintCupsWrapperTest_0021
321 * @tc.desc: CheckPrinterMakeModel
322 * @tc.type: FUNC
323 * @tc.require:
324 */
325 HWTEST_F(PrintCupsWrapperTest, PrintCupsWrapperTest_0021, TestSize.Level1)
326 {
__anon241ac4a30902(PrintCupsClient &printCupsClient, MockPrintCupsWrapper &mock) 327 MockTestFunc testFunc = [this](PrintCupsClient &printCupsClient, MockPrintCupsWrapper &mock) {
328 EXPECT_CALL(mock, GetNamedDest(_, _, _)).WillRepeatedly(Return(nullptr));
329 JobParameters jobParams;
330 bool driverMissing = false;
331 EXPECT_EQ(printCupsClient.CheckPrinterMakeModel(&jobParams, driverMissing), false);
332 };
333 DoMockTest(testFunc);
334 }
335
336 /**
337 * @tc.name: PrintCupsWrapperTest_0022
338 * @tc.desc: CheckPrinterMakeModel
339 * @tc.type: FUNC
340 * @tc.require:
341 */
342 HWTEST_F(PrintCupsWrapperTest, PrintCupsWrapperTest_0022, TestSize.Level1)
343 {
344 cups_dest_t cupsDests = {0};
__anon241ac4a30a02(PrintCupsClient &printCupsClient, MockPrintCupsWrapper &mock) 345 MockTestFunc testFunc = [this, &cupsDests](PrintCupsClient &printCupsClient, MockPrintCupsWrapper &mock) {
346 EXPECT_CALL(mock, GetNamedDest(_, _, _)).WillRepeatedly(Return(&cupsDests));
347 EXPECT_CALL(mock, FreeDests(_, _)).WillRepeatedly(Return());
348 JobParameters jobParams;
349 bool driverMissing = false;
350 EXPECT_EQ(printCupsClient.CheckPrinterMakeModel(&jobParams, driverMissing), false);
351 };
352 DoMockTest(testFunc);
353 }
354
355 /**
356 * @tc.name: PrintCupsWrapperTest_0030
357 * @tc.desc: CheckPrinterMakeModel
358 * @tc.type: FUNC
359 * @tc.require:
360 */
361 HWTEST_F(PrintCupsWrapperTest, PrintCupsWrapperTest_0030, TestSize.Level1)
362 {
363 PrintCupsClient printCupsClient;
364 if (printCupsClient.printAbility_ != nullptr) {
365 delete printCupsClient.printAbility_;
366 printCupsClient.printAbility_ = nullptr;
367 }
368 std::string name = "testName";
369 std::string uri = "testUri";
370 std::string ppd = "testPpd";
371 EXPECT_EQ(printCupsClient.IsPrinterExist(name.c_str(), uri.c_str(), ppd.c_str()), false);
372 }
373
374 /**
375 * @tc.name: PrintCupsWrapperTest_0031
376 * @tc.desc: CheckPrinterMakeModel
377 * @tc.type: FUNC
378 * @tc.require:
379 */
380 HWTEST_F(PrintCupsWrapperTest, PrintCupsWrapperTest_0031, TestSize.Level1)
381 {
__anon241ac4a30b02(PrintCupsClient &printCupsClient, MockPrintCupsWrapper &mock) 382 MockTestFunc testFunc = [this](PrintCupsClient &printCupsClient, MockPrintCupsWrapper &mock) {
383 EXPECT_CALL(mock, GetNamedDest(_, _, _)).WillRepeatedly(Return(nullptr));
384 std::string name = "testName";
385 std::string uri = "testUri";
386 std::string ppd = "testPpd";
387 EXPECT_EQ(printCupsClient.IsPrinterExist(name.c_str(), uri.c_str(), ppd.c_str()), false);
388 };
389 DoMockTest(testFunc);
390 }
391
392 /**
393 * @tc.name: PrintCupsWrapperTest_0032
394 * @tc.desc: CheckPrinterMakeModel
395 * @tc.type: FUNC
396 * @tc.require:
397 */
398 HWTEST_F(PrintCupsWrapperTest, PrintCupsWrapperTest_0032, TestSize.Level1)
399 {
400 cups_dest_t cupsDests = {0};
__anon241ac4a30c02(PrintCupsClient &printCupsClient, MockPrintCupsWrapper &mock) 401 MockTestFunc testFunc = [this, &cupsDests](PrintCupsClient &printCupsClient, MockPrintCupsWrapper &mock) {
402 EXPECT_CALL(mock, GetNamedDest(_, _, _)).WillRepeatedly(Return(&cupsDests));
403 EXPECT_CALL(mock, FreeDests(_, _)).WillRepeatedly(Return());
404 std::string name = "testName";
405 std::string uri = "testUri";
406 std::string ppd = "testPpd";
407 EXPECT_EQ(printCupsClient.IsPrinterExist(name.c_str(), uri.c_str(), ppd.c_str()), false);
408 };
409 DoMockTest(testFunc);
410 }
411
412 /**
413 * @tc.name: PrintCupsWrapperTest_0080
414 * @tc.desc: QueryPPDInformation
415 * @tc.type: FUNC
416 * @tc.require:
417 */
418 HWTEST_F(PrintCupsWrapperTest, PrintCupsWrapperTest_0080, TestSize.Level1)
419 {
420 OHOS::Print::PrintCupsClient printCupsClient;
421 int32_t ret = printCupsClient.InitCupsResources();
422 EXPECT_EQ(ret, E_PRINT_NONE);
423 std::string makeModel;
424 std::string ppdName;
425 EXPECT_FALSE(printCupsClient.QueryPPDInformation(makeModel, ppdName));
426 std::vector<std::string> ppds;
427 printCupsClient.ParsePPDInfo(nullptr, ppds);
428 EXPECT_EQ(ppds.size(), 0);
429 printCupsClient.StopCupsdService();
430 }
431
432 /**
433 * @tc.name: PrintCupsWrapperTest_0081
434 * @tc.desc: QueryPPDInformation
435 * @tc.type: FUNC
436 * @tc.require:
437 */
438 HWTEST_F(PrintCupsWrapperTest, PrintCupsWrapperTest_0081, TestSize.Level1)
439 {
440 OHOS::Print::PrintCupsClient printCupsClient;
441 int32_t ret = printCupsClient.InitCupsResources();
442 EXPECT_EQ(ret, E_PRINT_NONE);
443 std::string makeModel = "testmodel";
444 std::string ppdName;
445 EXPECT_FALSE(printCupsClient.QueryPPDInformation(makeModel, ppdName));
446 printCupsClient.StopCupsdService();
447 EXPECT_TRUE(ppdName.empty());
448 }
449
450 /**
451 * @tc.name: PrintCupsWrapperTest_0082
452 * @tc.desc: ParsePPDInfo
453 * @tc.type: FUNC
454 * @tc.require:
455 */
456 HWTEST_F(PrintCupsWrapperTest, PrintCupsWrapperTest_0082, TestSize.Level1)
457 {
458 OHOS::Print::PrintCupsClient printCupsClient;
459 std::vector<std::string> ppds;
460 ipp_t* response = ippNew();
461 EXPECT_NE(response, nullptr);
462 ippAddString(response, IPP_TAG_PRINTER, IPP_TAG_TEXT, "printer-location", nullptr, "en_us");
463 ippAddString(response, IPP_TAG_PRINTER, IPP_TAG_TEXT, "ppd-make-and-model", nullptr, "testmodel");
464 ippAddString(response, IPP_TAG_PRINTER, IPP_TAG_NAME, "ppd-name", nullptr, "testppd");
465 printCupsClient.ParsePPDInfo(response, ppds);
466 ippDelete(response);
467 }
468
469 /**
470 * @tc.name: PrintCupsWrapperTest_0083
471 * @tc.desc: AddPrinterToCups
472 * @tc.type: FUNC
473 * @tc.require:
474 */
475 HWTEST_F(PrintCupsWrapperTest, PrintCupsWrapperTest_0083, TestSize.Level1)
476 {
477 OHOS::Print::PrintCupsClient printCupsClient;
478 int32_t ret = printCupsClient.InitCupsResources();
479 EXPECT_EQ(ret, E_PRINT_NONE);
480 std::string uri = "testuri";
481 std::string name = "testname";
482 std::string makeModel = "testmodel";
483 printCupsClient.AddPrinterToCups(uri, name, makeModel);
484 printCupsClient.DeleteCupsPrinter(name.c_str());
485 printCupsClient.StopCupsdService();
486 }
487
488 /**
489 * @tc.name: PrintCupsWrapperTest_0085
490 * @tc.desc: GetIpAddress
491 * @tc.type: FUNC
492 * @tc.require:
493 */
494 HWTEST_F(PrintCupsWrapperTest, PrintCupsWrapperTest_0085, TestSize.Level1)
495 {
496 OHOS::Print::PrintCupsClient printCupsClient;
497 unsigned int num = 0x12345678;
498 std::string ip = printCupsClient.GetIpAddress(num);
499 EXPECT_STREQ(ip.c_str(), "18.52.86.120");
500 }
501
502 /**
503 * @tc.name: PrintCupsWrapperTest_0087
504 * @tc.desc: GetNextJob
505 * @tc.type: FUNC
506 * @tc.require:
507 */
508 HWTEST_F(PrintCupsWrapperTest, PrintCupsWrapperTest_0087, TestSize.Level1)
509 {
510 PrintCupsClient printCupsClient;
511 EXPECT_EQ(printCupsClient.GetNextJob(), nullptr);
512 JobParameters *jobParams1 = new JobParameters();
513 JobParameters *jobParams2 = new JobParameters();
514 printCupsClient.jobQueue_.push_back(jobParams1);
515 printCupsClient.jobQueue_.push_back(jobParams2);
516 EXPECT_EQ(printCupsClient.GetNextJob(), jobParams1);
517 EXPECT_EQ(printCupsClient.GetNextJob(), nullptr);
518 }
519
520 /**
521 * @tc.name: PrintCupsWrapperTest_0088
522 * @tc.desc: CopyDirectory
523 * @tc.type: FUNC
524 * @tc.require:
525 */
526 HWTEST_F(PrintCupsWrapperTest, PrintCupsWrapperTest_0088, TestSize.Level1)
527 {
528 PrintCupsClient printCupsClient;
529 std::string srcDir = "/system/bin/cups/";
530 std::string dstDir1 = "/data/local/tmp/cups_cp";
531 std::string dstDir2 = "/data/local/tmp/cups_sm";
532 printCupsClient.CopyDirectory(nullptr, dstDir1.c_str());
533 printCupsClient.SymlinkDirectory(nullptr, dstDir2.c_str());
534 printCupsClient.CopyDirectory(srcDir.c_str(), dstDir1.c_str());
535 printCupsClient.SymlinkDirectory(srcDir.c_str(), dstDir2.c_str());
536 mode_t permissions = S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH;
537 EXPECT_EQ(printCupsClient.ChangeFilterPermission(dstDir1, permissions), true);
538 }
539
540 /**
541 * @tc.name: PrintCupsWrapperTest_0089
542 * @tc.desc: QueryPrinterCapabilityFromPpd
543 * @tc.type: FUNC
544 * @tc.require:
545 */
546 HWTEST_F(PrintCupsWrapperTest, PrintCupsWrapperTest_0089, TestSize.Level1)
547 {
548 cups_dest_t cupsDests = {0};
549 cups_dinfo_t cupsDinfo = {0};
550 MockTestFunc testFunc =
__anon241ac4a30d02(PrintCupsClient &printCupsClient, MockPrintCupsWrapper &mock) 551 [this, &cupsDests, &cupsDinfo](PrintCupsClient &printCupsClient, MockPrintCupsWrapper &mock) {
552 EXPECT_CALL(mock, GetNamedDest(_, _, _)).WillRepeatedly(Return(&cupsDests));
553 EXPECT_CALL(mock, CopyDestInfo(_, _)).WillRepeatedly(Return(&cupsDinfo));
554 std::string printerName = "testName";
555 PrinterCapability printerCaps;
556 EXPECT_EQ(printCupsClient.QueryPrinterCapabilityFromPPD(printerName, printerCaps, ""), E_PRINT_NONE);
557 };
558 DoMockTest(testFunc);
559 }
560
561 /**
562 * @tc.name: PrintCupsWrapperTest_0090
563 * @tc.desc: QueryPrinterCapabilityFromPpd
564 * @tc.type: FUNC
565 * @tc.require:
566 */
567 HWTEST_F(PrintCupsWrapperTest, PrintCupsWrapperTest_0090, TestSize.Level1)
568 {
569 cups_dest_t cupsDests = {0};
570 cups_dinfo_t cupsDinfo = {0};
571 MockTestFunc testFunc =
__anon241ac4a30e02(PrintCupsClient &printCupsClient, MockPrintCupsWrapper &mock) 572 [this, &cupsDests, &cupsDinfo](PrintCupsClient &printCupsClient, MockPrintCupsWrapper &mock) {
573 EXPECT_CALL(mock, GetNamedDest(_, _, _)).WillOnce(Return(nullptr)).WillRepeatedly(Return(&cupsDests));
574 EXPECT_CALL(mock, CopyDestInfo(_, _)).WillOnce(Return(nullptr)).WillRepeatedly(Return(&cupsDinfo));
575 std::string printerName = "testName";
576 PrinterCapability printerCaps;
577 EXPECT_EQ(printCupsClient.QueryPrinterCapabilityFromPPD(printerName, printerCaps, ""), E_PRINT_SERVER_FAILURE);
578 EXPECT_EQ(printCupsClient.QueryPrinterCapabilityFromPPD(printerName, printerCaps, ""), E_PRINT_SERVER_FAILURE);
579 EXPECT_EQ(printCupsClient.QueryPrinterCapabilityFromPPD(printerName, printerCaps, ""), E_PRINT_NONE);
580 };
581 DoMockTest(testFunc);
582 }
583
584 /**
585 * @tc.name: Modify_ShouldReturnFail_WhenPrinterNotExist
586 * @tc.desc: ModifyCupsPrinterUri
587 * @tc.type: FUNC
588 * @tc.require:
589 */
590 HWTEST_F(PrintCupsWrapperTest, Modify_ShouldReturnFail_WhenPrinterNotExist, TestSize.Level1)
591 {
__anon241ac4a30f02(PrintCupsClient &printCupsClient, MockPrintCupsWrapper &mock) 592 MockTestFunc testFunc = [this](PrintCupsClient &printCupsClient, MockPrintCupsWrapper &mock) {
593 EXPECT_CALL(mock, GetNamedDest(_, _, _)).WillOnce(Return(nullptr));
594 std::string printerName = "testName";
595 std::string printerUri = "testUri";
596 EXPECT_FALSE(printCupsClient.ModifyCupsPrinterUri(printerName, printerUri));
597 };
598 DoMockTest(testFunc);
599 }
600
601 /**
602 * @tc.name: Modify_ShouldReturnSuccess_WhenPrinterExist
603 * @tc.desc: ModifyCupsPrinterUri
604 * @tc.type: FUNC
605 * @tc.require:
606 */
607 HWTEST_F(PrintCupsWrapperTest, Modify_ShouldReturnSuccess_WhenPrinterExist, TestSize.Level1)
608 {
__anon241ac4a31002(PrintCupsClient &printCupsClient, MockPrintCupsWrapper &mock) 609 MockTestFunc testFunc = [this](PrintCupsClient &printCupsClient, MockPrintCupsWrapper &mock) {
610 cups_dest_t cupsDest = {0};
611 ipp_t cupsRequest = {};
612 EXPECT_CALL(mock, GetNamedDest(_, _, _)).WillOnce(Return(&cupsDest));
613 EXPECT_CALL(mock, DoRequest(_, _, _)).WillOnce(Return(&cupsRequest));
614 EXPECT_CALL(mock, FreeRequest(_)).Times(1);
615 std::string printerName = "testName";
616 std::string printerUri = "testUri";
617 _cupsSetError(IPP_STATUS_OK, nullptr, 0);
618 EXPECT_TRUE(printCupsClient.ModifyCupsPrinterUri(printerName, printerUri));
619 };
620 DoMockTest(testFunc);
621 }
622
623 /**
624 * @tc.name: Modify_ShouldReturnSuccess_WhenBsuniPrinterExist
625 * @tc.desc: ModifyCupsPrinterUri
626 * @tc.type: FUNC
627 * @tc.require:
628 */
629 HWTEST_F(PrintCupsWrapperTest, Modify_ShouldReturnSuccess_WhenBsuniPrinterExist, TestSize.Level1)
630 {
__anon241ac4a31102(PrintCupsClient &printCupsClient, MockPrintCupsWrapper &mock) 631 MockTestFunc testFunc = [this](PrintCupsClient &printCupsClient, MockPrintCupsWrapper &mock) {
632 cups_dest_t cupsDest = {0};
633 ipp_t cupsRequest = {};
634 cupsDest.num_options = cupsAddOption("printer-make-and-model", BSUNI_PPD_NAME.c_str(), cupsDest.num_options,
635 &cupsDest.options);
636 EXPECT_CALL(mock, GetNamedDest(_, _, _)).WillOnce(Return(&cupsDest));
637 EXPECT_CALL(mock, DoRequest(_, _, _)).WillOnce(Return(&cupsRequest));
638 EXPECT_CALL(mock, FreeRequest(_)).Times(1);
639 std::string printerName = "testName";
640 std::string printerUri = "testUri";
641 _cupsSetError(IPP_STATUS_OK, nullptr, 0);
642 EXPECT_TRUE(printCupsClient.ModifyCupsPrinterUri(printerName, printerUri));
643 cupsFreeOptions(cupsDest.num_options, cupsDest.options);
644 };
645 DoMockTest(testFunc);
646 }
647
648 /**
649 * @tc.name: Modify_ShouldReturnSuccess_WhenOtherPrinterExist
650 * @tc.desc: ModifyCupsPrinterUri
651 * @tc.type: FUNC
652 * @tc.require:
653 */
654 HWTEST_F(PrintCupsWrapperTest, Modify_ShouldReturnSuccess_WhenOtherPrinterExist, TestSize.Level1)
655 {
__anon241ac4a31202(PrintCupsClient &printCupsClient, MockPrintCupsWrapper &mock) 656 MockTestFunc testFunc = [this](PrintCupsClient &printCupsClient, MockPrintCupsWrapper &mock) {
657 cups_dest_t cupsDest = {0};
658 ipp_t cupsRequest = {};
659 cupsDest.num_options = cupsAddOption("printer-make-and-model", "test", cupsDest.num_options,
660 &cupsDest.options);
661 EXPECT_CALL(mock, GetNamedDest(_, _, _)).WillOnce(Return(&cupsDest));
662 EXPECT_CALL(mock, DoRequest(_, _, _)).WillOnce(Return(&cupsRequest));
663 EXPECT_CALL(mock, FreeRequest(_)).Times(1);
664 std::string printerName = "testName";
665 std::string printerUri = "testUri";
666 _cupsSetError(IPP_STATUS_OK, nullptr, 0);
667 EXPECT_TRUE(printCupsClient.ModifyCupsPrinterUri(printerName, printerUri));
668 cupsFreeOptions(cupsDest.num_options, cupsDest.options);
669 };
670 DoMockTest(testFunc);
671 }
672
673 /**
674 * @tc.name: Modify_ShouldReturnFail_WhenResponseError
675 * @tc.desc: ModifyCupsPrinterUri
676 * @tc.type: FUNC
677 * @tc.require:
678 */
679 HWTEST_F(PrintCupsWrapperTest, Modify_ShouldReturnFail_WhenResponseError, TestSize.Level1)
680 {
__anon241ac4a31302(PrintCupsClient &printCupsClient, MockPrintCupsWrapper &mock) 681 MockTestFunc testFunc = [this](PrintCupsClient &printCupsClient, MockPrintCupsWrapper &mock) {
682 cups_dest_t cupsDest = {0};
683 ipp_t cupsRequest = {};
684 EXPECT_CALL(mock, GetNamedDest(_, _, _)).WillOnce(Return(&cupsDest));
685 EXPECT_CALL(mock, DoRequest(_, _, _)).WillOnce(Return(&cupsRequest));
686 EXPECT_CALL(mock, FreeRequest(_)).Times(1);
687 std::string printerName = "testName";
688 std::string printerUri = "testUri";
689 _cupsSetError(IPP_STATUS_ERROR_BAD_REQUEST, "test error", 0);
690 EXPECT_FALSE(printCupsClient.ModifyCupsPrinterUri(printerName, printerUri));
691 };
692 DoMockTest(testFunc);
693 }
694
695 /**
696 * @tc.name: PrintCupsWrapperTest_0001
697 * @tc.desc: QueryPrinterCapabilityFromPPD
698 * @tc.type: FUNC
699 * @tc.require:
700 */
701 HWTEST_F(PrintCupsWrapperTest, QueryPrinterCapabilityFromPPD_PpdNameNotEmpty_ReturnIOError, TestSize.Level1)
702 {
703 PrintCupsClient printCupsClient;
704 if (printCupsClient.printAbility_ != nullptr) {
705 delete printCupsClient.printAbility_;
706 printCupsClient.printAbility_ = nullptr;
707 }
708 std::string printerName = "testPrinterName";
709 std::string ppdName = "testPpdName";
710 PrinterCapability printerCap;
711 EXPECT_EQ(printCupsClient.QueryPrinterCapabilityFromPPD(printerName, printerCap, ppdName), E_PRINT_FILE_IO);
712 }
713 } // namespace Print
714 } // namespace OHOS