[CAPI Changed] Enum value names and scan cloud
[platform/upstream/csr-framework.git] / test / test-api-content-screening-async.cpp
1 /*
2  *  Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License
15  */
16 /*
17  * @file        test-api-content-screening-async.cpp
18  * @author      Kyungwook Tak (k.tak@samsung.com)
19  * @version     1.0
20  * @brief       CSR Content screening async API test
21  */
22 #include <csr-content-screening.h>
23
24 #include <condition_variable>
25 #include <thread>
26 #include <mutex>
27 #include <vector>
28 #include <boost/test/unit_test.hpp>
29
30 #include "test-common.h"
31 #include "test-helper.h"
32 #include "test-resource.h"
33
34 #define ASSERT_CALLBACK(actual, scan, detect, complete, cancel, error)                  \
35         do {                                                                                \
36                 if (scan >= 0)                                                                  \
37                         ASSERT_IF_MSG(actual.scannedCnt, scan, "scanned count mismatch.");          \
38                 if (detect >= 0)                                                                \
39                         ASSERT_IF_MSG(actual.detectedCnt, detect, "detected count mismatch.");      \
40                 if (complete >= 0)                                                              \
41                         ASSERT_IF_MSG(actual.completedCnt, complete, "completed count mismatch.");  \
42                 if (cancel >= 0)                                                                \
43                         ASSERT_IF_MSG(actual.cancelledCnt, cancel, "cancelled count mismatch.");    \
44                 if (error >= 0)                                                                 \
45                         ASSERT_IF_MSG(actual.errorCnt, error, "error count mismatch.");             \
46                 break;                                                                          \
47         } while (false)
48
49 namespace {
50
51 struct AsyncTestContext {
52         std::mutex m;
53         std::mutex m_vec;
54         std::condition_variable cv;
55         int scannedCnt;
56         int detectedCnt;
57         int completedCnt;
58         int cancelledCnt;
59         int errorCnt;
60         std::vector<std::string> scannedList;
61         std::vector<csr_cs_malware_h> detectedList;
62         int errorCode;
63
64         AsyncTestContext() :
65                 scannedCnt(0),
66                 detectedCnt(0),
67                 completedCnt(0),
68                 cancelledCnt(0),
69                 errorCnt(0) {}
70 };
71
72 void on_scanned(const char *file, void *userdata)
73 {
74         BOOST_MESSAGE("on_scanned. file[" << file << "] scanned!");
75         auto ctx = reinterpret_cast<AsyncTestContext *>(userdata);
76
77         std::lock_guard<std::mutex> l(ctx->m_vec);
78
79         ctx->scannedCnt++;
80         ctx->scannedList.push_back(file);
81 }
82
83 void on_detected(csr_cs_malware_h detected, void *userdata)
84 {
85         Test::ScopedCstr file_name;
86         ASSERT_IF(csr_cs_malware_get_file_name(detected, &file_name.ptr), CSR_ERROR_NONE);
87         BOOST_MESSAGE("on_detected. file[" << file_name.ptr << "] detected!");
88         auto ctx = reinterpret_cast<AsyncTestContext *>(userdata);
89
90         std::lock_guard<std::mutex> l(ctx->m_vec);
91
92         ctx->detectedCnt++;
93         ctx->detectedList.push_back(detected);
94 }
95
96 void on_error(int ec, void *userdata)
97 {
98         BOOST_MESSAGE("on_error. async request done with error code[" << ec << "]");
99         auto ctx = reinterpret_cast<AsyncTestContext *>(userdata);
100
101         ctx->errorCnt++;
102         ctx->errorCode = ec;
103         ctx->cv.notify_one();
104 }
105
106 void on_completed(void *userdata)
107 {
108         BOOST_MESSAGE("on_completed. async request completed succesfully.");
109         auto ctx = reinterpret_cast<AsyncTestContext *>(userdata);
110         ctx->completedCnt++;
111         ctx->cv.notify_one();
112 }
113
114 void on_cancelled(void *userdata)
115 {
116         BOOST_MESSAGE("on_cancelled. async request canceled!");
117         auto ctx = reinterpret_cast<AsyncTestContext *>(userdata);
118         ctx->cancelledCnt++;
119         ctx->cv.notify_one();
120 }
121
122 void set_default_callback(csr_cs_context_h context)
123 {
124         ASSERT_IF(csr_cs_set_detected_cb(context, on_detected), CSR_ERROR_NONE);
125         ASSERT_IF(csr_cs_set_completed_cb(context, on_completed), CSR_ERROR_NONE);
126         ASSERT_IF(csr_cs_set_cancelled_cb(context, on_cancelled), CSR_ERROR_NONE);
127         ASSERT_IF(csr_cs_set_error_cb(context, on_error), CSR_ERROR_NONE);
128         ASSERT_IF(csr_cs_set_file_scanned_cb(context, on_scanned), CSR_ERROR_NONE);
129 }
130
131 void install_test_files()
132 {
133         Test::copy_file(TEST_FILE_HIGH, TEST_FILE_MEDIA);
134         Test::copy_file(TEST_FILE_HIGH, TEST_FILE_TMP);
135
136         Test::make_dir(TEST_FAKE_APP_ROOT);
137         Test::copy_file(TEST_FILE_HIGH, TEST_FAKE_APP_FILE);
138 }
139
140 void uninstall_test_files()
141 {
142         Test::remove_file(TEST_FILE_MEDIA);
143         Test::remove_file(TEST_FILE_TMP);
144         Test::remove_file(TEST_FAKE_APP_FILE);
145         Test::remove_file(TEST_FAKE_APP_ROOT);
146 }
147
148 void uninstall_test_apps()
149 {
150         Test::uninstall_app(TEST_WGT_PKG_ID);
151         Test::uninstall_app(TEST_TPK_PKG_ID);
152         Test::uninstall_app(TEST_SAFE_WGT_PKG_ID);
153 }
154
155 void install_test_apps()
156 {
157         uninstall_test_apps();
158
159         ASSERT_INSTALL_APP(TEST_WGT_PATH, TEST_WGT_TYPE);
160         ASSERT_INSTALL_APP(TEST_TPK_PATH, TEST_TPK_TYPE);
161         ASSERT_INSTALL_APP(TEST_SAFE_WGT_PATH, TEST_SAFE_WGT_TYPE);
162 }
163
164 } // end of namespace
165
166 BOOST_AUTO_TEST_SUITE(API_CONTENT_SCREENING_ASYNC)
167
168 BOOST_AUTO_TEST_CASE(set_callbacks_positive)
169 {
170         EXCEPTION_GUARD_START
171
172         auto c = Test::Context<csr_cs_context_h>();
173         auto context = c.get();
174
175         set_default_callback(context);
176
177         EXCEPTION_GUARD_END
178 }
179
180 BOOST_AUTO_TEST_CASE(set_callbacks_negative)
181 {
182         EXCEPTION_GUARD_START
183
184         auto c = Test::Context<csr_cs_context_h>();
185         auto context = c.get();
186
187         ASSERT_IF(csr_cs_set_detected_cb(nullptr, on_detected), CSR_ERROR_INVALID_HANDLE);
188         ASSERT_IF(csr_cs_set_detected_cb(context, nullptr), CSR_ERROR_INVALID_PARAMETER);
189         ASSERT_IF(csr_cs_set_completed_cb(nullptr, on_completed), CSR_ERROR_INVALID_HANDLE);
190         ASSERT_IF(csr_cs_set_completed_cb(context, nullptr), CSR_ERROR_INVALID_PARAMETER);
191         ASSERT_IF(csr_cs_set_cancelled_cb(nullptr, on_cancelled), CSR_ERROR_INVALID_HANDLE);
192         ASSERT_IF(csr_cs_set_cancelled_cb(context, nullptr), CSR_ERROR_INVALID_PARAMETER);
193         ASSERT_IF(csr_cs_set_error_cb(nullptr, on_error), CSR_ERROR_INVALID_HANDLE);
194         ASSERT_IF(csr_cs_set_error_cb(context, nullptr), CSR_ERROR_INVALID_PARAMETER);
195         ASSERT_IF(csr_cs_set_file_scanned_cb(nullptr, on_scanned), CSR_ERROR_INVALID_HANDLE);
196         ASSERT_IF(csr_cs_set_file_scanned_cb(context, nullptr), CSR_ERROR_INVALID_PARAMETER);
197
198         EXCEPTION_GUARD_END
199 }
200
201 BOOST_AUTO_TEST_CASE(scan_files_async_positive)
202 {
203         EXCEPTION_GUARD_START
204
205         auto c = Test::Context<csr_cs_context_h>();
206         auto context = c.get();
207
208         set_default_callback(context);
209
210         const char *files[4] = {
211                 TEST_FILE_HIGH,
212                 TEST_FILE_MEDIUM,
213                 TEST_FILE_LOW,
214                 TEST_FILE_NORMAL
215         };
216
217         AsyncTestContext testCtx;
218
219         ASSERT_IF(
220                 csr_cs_scan_files_async(context, files, sizeof(files) / sizeof(const char *),
221                                                                 &testCtx),
222                 CSR_ERROR_NONE);
223
224         std::unique_lock<std::mutex> l(testCtx.m);
225         testCtx.cv.wait(l);
226         l.unlock();
227
228         ASSERT_CALLBACK(testCtx, -1, 3, 1, 0, 0); //scanned, detected, completed, cancelled, error
229
230         ASSERT_DETECTED_IN_LIST(testCtx.detectedList,
231                 TEST_FILE_HIGH, MALWARE_HIGH_NAME, MALWARE_HIGH_SEVERITY, MALWARE_HIGH_DETAILED_URL);
232         ASSERT_DETECTED_IN_LIST_EXT(testCtx.detectedList,
233                 TEST_FILE_HIGH, false, nullptr);
234
235         ASSERT_DETECTED_IN_LIST(testCtx.detectedList,
236                 TEST_FILE_MEDIUM, MALWARE_MEDIUM_NAME, MALWARE_MEDIUM_SEVERITY, MALWARE_MEDIUM_DETAILED_URL);
237         ASSERT_DETECTED_IN_LIST_EXT(testCtx.detectedList,
238                 TEST_FILE_MEDIUM, false, nullptr);
239
240         ASSERT_DETECTED_IN_LIST(testCtx.detectedList,
241                 TEST_FILE_LOW, MALWARE_LOW_NAME, MALWARE_LOW_SEVERITY, MALWARE_LOW_DETAILED_URL);
242         ASSERT_DETECTED_IN_LIST_EXT(testCtx.detectedList,
243                 TEST_FILE_LOW, false, nullptr);
244
245         EXCEPTION_GUARD_END
246 }
247
248 BOOST_AUTO_TEST_CASE(scan_files_async_negative)
249 {
250         EXCEPTION_GUARD_START
251
252         auto c = Test::Context<csr_cs_context_h>();
253         auto context = c.get();
254
255         const char *files[3] = {
256                 TEST_FILE_HIGH,
257                 TEST_FILE_NORMAL,
258                 TEST_FILE_MEDIUM
259         };
260
261         AsyncTestContext testCtx;
262
263         ASSERT_IF(
264                 csr_cs_scan_files_async(nullptr, files, sizeof(files) / sizeof(const char *),
265                                                                 nullptr),
266                 CSR_ERROR_INVALID_HANDLE);
267
268         ASSERT_IF(
269                 csr_cs_scan_files_async(context, nullptr, sizeof(files) / sizeof(const char *),
270                                                                 nullptr),
271                 CSR_ERROR_INVALID_PARAMETER);
272
273         EXCEPTION_GUARD_END
274 }
275
276 BOOST_AUTO_TEST_CASE(scan_dir_positive)
277 {
278         EXCEPTION_GUARD_START
279
280         auto c = Test::Context<csr_cs_context_h>();
281         auto context = c.get();
282
283         set_default_callback(context);
284
285         AsyncTestContext testCtx;
286
287         ASSERT_IF(csr_cs_scan_dir_async(context, TEST_DIR_MALWARES, &testCtx), CSR_ERROR_NONE);
288
289         std::unique_lock<std::mutex> l(testCtx.m);
290         testCtx.cv.wait(l);
291         l.unlock();
292
293         //scanned, detected, completed, cancelled, error
294         ASSERT_CALLBACK(testCtx, -1, 0, 1, 0, 0);
295
296         EXCEPTION_GUARD_END
297 }
298
299 BOOST_AUTO_TEST_CASE(scan_dir_negative)
300 {
301         EXCEPTION_GUARD_START
302
303         auto c = Test::Context<csr_cs_context_h>();
304         auto context = c.get();
305
306         AsyncTestContext testCtx;
307
308         ASSERT_IF(csr_cs_scan_dir_async(nullptr, TEST_DIR_MALWARES, nullptr),
309                 CSR_ERROR_INVALID_HANDLE);
310         ASSERT_IF(csr_cs_scan_dir_async(context, nullptr, nullptr),
311                 CSR_ERROR_INVALID_PARAMETER);
312
313         EXCEPTION_GUARD_END
314 }
315
316 BOOST_AUTO_TEST_CASE(scan_dir_root)
317 {
318         EXCEPTION_GUARD_START
319
320         auto c = Test::Context<csr_cs_context_h>();
321         auto context = c.get();
322
323         install_test_files();
324         install_test_apps();
325
326         set_default_callback(context);
327
328         AsyncTestContext testCtx;
329
330         ASSERT_IF(csr_cs_scan_dir_async(context, TEST_DIR_ROOT, &testCtx), CSR_ERROR_NONE);
331
332         std::unique_lock<std::mutex> l(testCtx.m);
333         testCtx.cv.wait(l);
334         l.unlock();
335
336         Test::uninstall_app(TEST_WGT_PKG_ID);
337         Test::uninstall_app(TEST_TPK_PKG_ID);
338
339         //scanned, detected, completed, cancelled, error
340         ASSERT_CALLBACK(testCtx, -1, 12, 1, 0, 0);
341
342         ASSERT_DETECTED_IN_LIST(testCtx.detectedList,
343                 TEST_FILE_HIGH, MALWARE_HIGH_NAME, MALWARE_HIGH_SEVERITY, MALWARE_HIGH_DETAILED_URL);
344         ASSERT_DETECTED_IN_LIST_EXT(testCtx.detectedList,
345                 TEST_FILE_HIGH, false, nullptr);
346
347         ASSERT_DETECTED_IN_LIST(testCtx.detectedList,
348                 TEST_FILE_MEDIUM, MALWARE_MEDIUM_NAME, MALWARE_MEDIUM_SEVERITY, MALWARE_MEDIUM_DETAILED_URL);
349         ASSERT_DETECTED_IN_LIST_EXT(testCtx.detectedList,
350                 TEST_FILE_MEDIUM, false, nullptr);
351
352         ASSERT_DETECTED_IN_LIST(testCtx.detectedList,
353                 TEST_FILE_LOW, MALWARE_LOW_NAME, MALWARE_LOW_SEVERITY, MALWARE_LOW_DETAILED_URL);
354         ASSERT_DETECTED_IN_LIST_EXT(testCtx.detectedList,
355                 TEST_FILE_LOW, false, nullptr);
356
357         ASSERT_DETECTED_IN_LIST(testCtx.detectedList,
358                 TEST_FILE_MEDIA, MALWARE_HIGH_NAME, MALWARE_HIGH_SEVERITY, MALWARE_HIGH_DETAILED_URL);
359         ASSERT_DETECTED_IN_LIST_EXT(testCtx.detectedList,
360                 TEST_FILE_MEDIA, false, nullptr);
361
362         ASSERT_DETECTED_IN_LIST(testCtx.detectedList,
363                 TEST_FILE_TMP, MALWARE_HIGH_NAME, MALWARE_HIGH_SEVERITY, MALWARE_HIGH_DETAILED_URL);
364         ASSERT_DETECTED_IN_LIST_EXT(testCtx.detectedList,
365                 TEST_FILE_TMP, false, nullptr);
366
367         ASSERT_DETECTED_IN_LIST(testCtx.detectedList,
368                 TEST_WGT_APP_ROOT, MALWARE_HIGH_NAME, MALWARE_HIGH_SEVERITY, MALWARE_HIGH_DETAILED_URL);
369         ASSERT_DETECTED_IN_LIST_EXT(testCtx.detectedList,
370                 TEST_WGT_APP_ROOT, true, TEST_WGT_PKG_ID);
371
372         ASSERT_DETECTED_IN_LIST(testCtx.detectedList,
373                 TEST_TPK_APP_ROOT, MALWARE_HIGH_NAME, MALWARE_HIGH_SEVERITY, MALWARE_HIGH_DETAILED_URL);
374         ASSERT_DETECTED_IN_LIST_EXT(testCtx.detectedList,
375                 TEST_TPK_APP_ROOT, true, TEST_TPK_PKG_ID);
376
377         ASSERT_DETECTED_IN_LIST(testCtx.detectedList,
378                 TEST_FAKE_APP_FILE, MALWARE_HIGH_NAME, MALWARE_HIGH_SEVERITY, MALWARE_HIGH_DETAILED_URL);
379         ASSERT_DETECTED_IN_LIST_EXT(testCtx.detectedList,
380                 TEST_FAKE_APP_FILE, false, nullptr);
381
382         EXCEPTION_GUARD_END
383 }
384
385 BOOST_AUTO_TEST_CASE(scan_dir_media)
386 {
387         EXCEPTION_GUARD_START
388
389         auto c = Test::Context<csr_cs_context_h>();
390         auto context = c.get();
391
392         install_test_files();
393
394         set_default_callback(context);
395
396         AsyncTestContext testCtx;
397
398         ASSERT_IF(csr_cs_scan_dir_async(context, TEST_DIR_MEDIA, &testCtx), CSR_ERROR_NONE);
399
400         std::unique_lock<std::mutex> l(testCtx.m);
401         testCtx.cv.wait(l);
402         l.unlock();
403
404         // scanned, detected, completed, cancelled, error
405         ASSERT_CALLBACK(testCtx, -1, 1, 1, 0, 0);
406
407         ASSERT_DETECTED_IN_LIST(testCtx.detectedList,
408                 TEST_FILE_MEDIA, MALWARE_HIGH_NAME, MALWARE_HIGH_SEVERITY, MALWARE_HIGH_DETAILED_URL);
409         ASSERT_DETECTED_IN_LIST_EXT(testCtx.detectedList,
410                 TEST_FILE_MEDIA, false, nullptr);
411
412         EXCEPTION_GUARD_END
413 }
414
415 BOOST_AUTO_TEST_CASE(scan_dir_tmp)
416 {
417         EXCEPTION_GUARD_START
418
419         auto c = Test::Context<csr_cs_context_h>();
420         auto context = c.get();
421
422         install_test_files();
423
424         set_default_callback(context);
425
426         AsyncTestContext testCtx;
427
428         ASSERT_IF(csr_cs_scan_dir_async(context, TEST_DIR_TMP, &testCtx), CSR_ERROR_NONE);
429
430         std::unique_lock<std::mutex> l(testCtx.m);
431         testCtx.cv.wait(l);
432         l.unlock();
433
434         //scanned, detected, completed, cancelled, error
435         ASSERT_CALLBACK(testCtx, -1, 1, 1, 0, 0);
436
437         ASSERT_DETECTED_IN_LIST(testCtx.detectedList,
438                 TEST_FILE_TMP, MALWARE_HIGH_NAME, MALWARE_HIGH_SEVERITY, MALWARE_HIGH_DETAILED_URL);
439         ASSERT_DETECTED_IN_LIST_EXT(testCtx.detectedList,
440                 TEST_FILE_TMP, false, nullptr);
441
442         EXCEPTION_GUARD_END
443 }
444
445 BOOST_AUTO_TEST_CASE(scan_dir_apps)
446 {
447         EXCEPTION_GUARD_START
448
449         auto c = Test::Context<csr_cs_context_h>();
450         auto context = c.get();
451
452         install_test_files();
453         install_test_apps();
454
455         set_default_callback(context);
456
457         AsyncTestContext testCtx;
458
459         ASSERT_SUCCESS(csr_cs_scan_dir_async(context, TEST_DIR_APPS, &testCtx));
460
461         std::unique_lock<std::mutex> l(testCtx.m);
462         testCtx.cv.wait(l);
463         l.unlock();
464
465         //scanned, detected, completed, cancelled, error
466         ASSERT_CALLBACK(testCtx, -1, 3, 1, 0, 0);
467
468         ASSERT_DETECTED_IN_LIST(testCtx.detectedList,
469                 TEST_WGT_APP_ROOT, MALWARE_HIGH_NAME, MALWARE_HIGH_SEVERITY, MALWARE_HIGH_DETAILED_URL);
470         ASSERT_DETECTED_IN_LIST_EXT(testCtx.detectedList,
471                 TEST_WGT_APP_ROOT, true, TEST_WGT_PKG_ID);
472
473         ASSERT_DETECTED_IN_LIST(testCtx.detectedList,
474                 TEST_TPK_APP_ROOT, MALWARE_HIGH_NAME, MALWARE_HIGH_SEVERITY, MALWARE_HIGH_DETAILED_URL);
475         ASSERT_DETECTED_IN_LIST_EXT(testCtx.detectedList,
476                 TEST_TPK_APP_ROOT, true, TEST_TPK_PKG_ID);
477
478         ASSERT_DETECTED_IN_LIST(testCtx.detectedList,
479                 TEST_FAKE_APP_FILE, MALWARE_HIGH_NAME, MALWARE_HIGH_SEVERITY, MALWARE_HIGH_DETAILED_URL);
480         ASSERT_DETECTED_IN_LIST_EXT(testCtx.detectedList,
481                 TEST_FAKE_APP_FILE, false, nullptr);
482
483         uninstall_test_apps();
484
485         EXCEPTION_GUARD_END
486 }
487
488 BOOST_AUTO_TEST_CASE(scan_dir_wgt)
489 {
490         EXCEPTION_GUARD_START
491
492         auto c = Test::Context<csr_cs_context_h>();
493         auto context = c.get();
494
495         Test::uninstall_app(TEST_WGT_PKG_ID);
496         ASSERT_INSTALL_APP(TEST_WGT_PATH, TEST_WGT_TYPE);
497
498         set_default_callback(context);
499
500         AsyncTestContext testCtx;
501
502         ASSERT_IF(csr_cs_scan_dir_async(context, TEST_WGT_APP_ROOT, &testCtx), CSR_ERROR_NONE);
503
504         std::unique_lock<std::mutex> l(testCtx.m);
505         testCtx.cv.wait(l);
506         l.unlock();
507
508         //scanned, detected, completed, cancelled, error
509         ASSERT_CALLBACK(testCtx, -1, 1, 1, 0, 0);
510
511         ASSERT_DETECTED_IN_LIST(testCtx.detectedList,
512                 TEST_WGT_APP_ROOT, MALWARE_HIGH_NAME, MALWARE_HIGH_SEVERITY, MALWARE_HIGH_DETAILED_URL);
513         ASSERT_DETECTED_IN_LIST_EXT(testCtx.detectedList,
514                 TEST_WGT_APP_ROOT, true, TEST_WGT_PKG_ID);
515
516         Test::uninstall_app(TEST_WGT_PKG_ID);
517
518         EXCEPTION_GUARD_END
519 }
520
521 BOOST_AUTO_TEST_CASE(scan_dir_tpk)
522 {
523         EXCEPTION_GUARD_START
524
525         auto c = Test::Context<csr_cs_context_h>();
526         auto context = c.get();
527
528         Test::uninstall_app(TEST_TPK_PKG_ID);
529         ASSERT_INSTALL_APP(TEST_TPK_PATH, TEST_TPK_TYPE);
530
531         set_default_callback(context);
532
533         AsyncTestContext testCtx;
534
535         ASSERT_IF(csr_cs_scan_dir_async(context, TEST_TPK_APP_ROOT, &testCtx), CSR_ERROR_NONE);
536
537         std::unique_lock<std::mutex> l(testCtx.m);
538         testCtx.cv.wait(l);
539         l.unlock();
540
541         //scanned, detected, completed, cancelled, error
542         ASSERT_CALLBACK(testCtx, -1, 1, 1, 0, 0);
543
544         ASSERT_DETECTED_IN_LIST(testCtx.detectedList,
545                 TEST_TPK_APP_ROOT, MALWARE_HIGH_NAME, MALWARE_HIGH_SEVERITY, MALWARE_HIGH_DETAILED_URL);
546         ASSERT_DETECTED_IN_LIST_EXT(testCtx.detectedList,
547                 TEST_TPK_APP_ROOT, true, TEST_TPK_PKG_ID);
548
549         Test::uninstall_app(TEST_TPK_PKG_ID);
550
551         EXCEPTION_GUARD_END
552 }
553
554 BOOST_AUTO_TEST_CASE(scan_dirs_positive)
555 {
556         EXCEPTION_GUARD_START
557
558         auto c = Test::Context<csr_cs_context_h>();
559         auto context = c.get();
560
561         set_default_callback(context);
562
563         AsyncTestContext testCtx;
564
565         const char *dirs[1] = {
566                 TEST_DIR_MALWARES
567         };
568
569         ASSERT_IF(
570                 csr_cs_scan_dirs_async(context, dirs, sizeof(dirs) / sizeof(const char *),
571                                                            &testCtx),
572                 CSR_ERROR_NONE);
573
574         std::unique_lock<std::mutex> l(testCtx.m);
575         testCtx.cv.wait(l);
576         l.unlock();
577
578         //scanned, detected, completed, cancelled, error
579         ASSERT_CALLBACK(testCtx, -1, 0, 1, 0, 0);
580
581         EXCEPTION_GUARD_END
582 }
583
584 BOOST_AUTO_TEST_CASE(scan_dirs_negative)
585 {
586         EXCEPTION_GUARD_START
587
588         auto c = Test::Context<csr_cs_context_h>();
589         auto context = c.get();
590
591         const char *dirs[1] = {
592                 TEST_DIR_MALWARES
593         };
594
595         ASSERT_IF(
596                 csr_cs_scan_dirs_async(nullptr, dirs, sizeof(dirs) / sizeof(const char *),
597                                                            nullptr),
598                 CSR_ERROR_INVALID_HANDLE);
599
600         ASSERT_IF(
601                 csr_cs_scan_dirs_async(context, nullptr, sizeof(dirs) / sizeof(const char *),
602                                                            nullptr),
603                 CSR_ERROR_INVALID_PARAMETER);
604
605         EXCEPTION_GUARD_END
606 }
607
608
609 BOOST_AUTO_TEST_CASE(scan_cancel_positiive)
610 {
611         EXCEPTION_GUARD_START
612
613         auto c = Test::Context<csr_cs_context_h>();
614         auto context = c.get();
615
616         set_default_callback(context);
617
618         AsyncTestContext testCtx;
619
620         const char *dirs[1] = {
621                 "/"
622         };
623
624         // touch a file : in case of no target file to scan, we cannot cancel to scan.
625         Test::touch_file(TEST_FILE_HIGH);
626
627         ASSERT_IF(csr_cs_scan_dirs_async(context, dirs,
628                                                                          sizeof(dirs) / sizeof(const char *), &testCtx),
629                           CSR_ERROR_NONE);
630
631         // TODO: check the reason
632         // Without sleep, sometimes the first run of this TC fails with core dump.
633         std::this_thread::sleep_for(std::chrono::milliseconds(100));
634
635         ASSERT_IF(csr_cs_cancel_scanning(context), CSR_ERROR_NONE);
636
637         std::this_thread::sleep_for(std::chrono::milliseconds(100));
638
639         //scanned, detected, completed, cancelled, error
640         ASSERT_CALLBACK(testCtx, -1, 0, 0, 1, 0);
641
642         EXCEPTION_GUARD_END
643 }
644
645 BOOST_AUTO_TEST_CASE(scan_cancel_negative)
646 {
647         EXCEPTION_GUARD_START
648
649         auto c = Test::Context<csr_cs_context_h>();
650         auto context = c.get();
651
652         ASSERT_IF(csr_cs_cancel_scanning(nullptr), CSR_ERROR_INVALID_HANDLE);
653         ASSERT_IF(csr_cs_cancel_scanning(context), CSR_ERROR_NO_TASK);
654
655         EXCEPTION_GUARD_END
656 }
657
658 BOOST_AUTO_TEST_CASE(delta_scan_basic)
659 {
660         EXCEPTION_GUARD_START
661
662         Test::initialize_db();
663
664         auto c = Test::Context<csr_cs_context_h>();
665         auto context = c.get();
666
667         install_test_files();
668         install_test_apps();
669
670         set_default_callback(context);
671
672         // Base Scan
673         AsyncTestContext testBaseCtx;
674
675         ASSERT_IF(csr_cs_scan_dir_async(context, TEST_DIR_APPS, &testBaseCtx), CSR_ERROR_NONE);
676
677         std::unique_lock<std::mutex> lBase(testBaseCtx.m);
678         testBaseCtx.cv.wait(lBase);
679         lBase.unlock();
680
681         // scanned, detected, completed, cancelled, error
682         ASSERT_CALLBACK(testBaseCtx, -1, 3, 1, 0, 0);
683         BOOST_REQUIRE_MESSAGE(testBaseCtx.scannedCnt != 0,
684                         "Base Scan count should not be zero");
685
686         // Rescan the same dir
687         AsyncTestContext testRescanCtx;
688
689         ASSERT_IF(csr_cs_scan_dir_async(context, TEST_DIR_APPS, &testRescanCtx),
690                           CSR_ERROR_NONE);
691
692         std::unique_lock<std::mutex> lRescan(testRescanCtx.m);
693         testRescanCtx.cv.wait(lRescan);
694         lRescan.unlock();
695
696         // scanned, detected, completed, cancelled, error
697         ASSERT_CALLBACK(testRescanCtx, 0, 3, 1, 0, 0);
698
699         ASSERT_DETECTED_IN_LIST(testRescanCtx.detectedList,
700                 TEST_WGT_APP_ROOT, MALWARE_HIGH_NAME, MALWARE_HIGH_SEVERITY, MALWARE_HIGH_DETAILED_URL);
701         ASSERT_DETECTED_IN_LIST_EXT(testRescanCtx.detectedList,
702                 TEST_WGT_APP_ROOT, true, TEST_WGT_PKG_ID);
703
704         ASSERT_DETECTED_IN_LIST(testRescanCtx.detectedList,
705                 TEST_TPK_APP_ROOT, MALWARE_HIGH_NAME, MALWARE_HIGH_SEVERITY, MALWARE_HIGH_DETAILED_URL);
706         ASSERT_DETECTED_IN_LIST_EXT(testRescanCtx.detectedList,
707                 TEST_TPK_APP_ROOT, true, TEST_TPK_PKG_ID);
708
709         ASSERT_DETECTED_IN_LIST(testRescanCtx.detectedList,
710                 TEST_FAKE_APP_FILE, MALWARE_HIGH_NAME, MALWARE_HIGH_SEVERITY, MALWARE_HIGH_DETAILED_URL);
711         ASSERT_DETECTED_IN_LIST_EXT(testRescanCtx.detectedList,
712                 TEST_FAKE_APP_FILE, false, nullptr);
713
714         // Rescan the sub dir
715         AsyncTestContext testRescanSubCtx;
716
717         ASSERT_IF(csr_cs_scan_dir_async(context, TEST_WGT_APP_ROOT, &testRescanSubCtx), CSR_ERROR_NONE);
718
719         std::unique_lock<std::mutex> lRescanSub(testRescanSubCtx.m);
720         testRescanSubCtx.cv.wait(lRescanSub);
721         lRescanSub.unlock();
722
723         // scanned, detected, completed, cancelled, error
724         ASSERT_CALLBACK(testRescanSubCtx, 0, 1, 1, 0, 0);
725
726         ASSERT_DETECTED_IN_LIST(testRescanSubCtx.detectedList,
727                 TEST_WGT_APP_ROOT, MALWARE_HIGH_NAME, MALWARE_HIGH_SEVERITY, MALWARE_HIGH_DETAILED_URL);
728         ASSERT_DETECTED_IN_LIST_EXT(testRescanSubCtx.detectedList,
729                 TEST_WGT_APP_ROOT, true, TEST_WGT_PKG_ID);
730
731         uninstall_test_apps();
732
733         EXCEPTION_GUARD_END
734 }
735
736 BOOST_AUTO_TEST_CASE(delta_scan_changed_after_scan)
737 {
738         EXCEPTION_GUARD_START
739
740         Test::initialize_db();
741
742         auto c = Test::Context<csr_cs_context_h>();
743         auto context = c.get();
744
745         install_test_files();
746         install_test_apps();
747
748         set_default_callback(context);
749
750         // Base Scan
751         AsyncTestContext testBaseCtx;
752
753         ASSERT_IF(csr_cs_scan_dir_async(context, TEST_DIR_APPS, &testBaseCtx), CSR_ERROR_NONE);
754
755         std::unique_lock<std::mutex> lBase(testBaseCtx.m);
756         testBaseCtx.cv.wait(lBase);
757         lBase.unlock();
758
759         // scanned, detected, completed, cancelled, error
760         ASSERT_CALLBACK(testBaseCtx, -1, 3, 1, 0, 0);
761         BOOST_REQUIRE_MESSAGE(testBaseCtx.scannedCnt != 0,
762                         "Base Scan count should not be zero");
763
764         // changed
765         Test::uninstall_app(TEST_SAFE_WGT_PKG_ID);
766         ASSERT_INSTALL_APP(TEST_SAFE_WGT_PATH, TEST_SAFE_WGT_TYPE);
767
768         // Rescan the same dir
769         AsyncTestContext testRescanCtx;
770
771         ASSERT_IF(csr_cs_scan_dir_async(context, TEST_DIR_APPS, &testRescanCtx),
772                           CSR_ERROR_NONE);
773
774         std::unique_lock<std::mutex> lRescan(testRescanCtx.m);
775         testRescanCtx.cv.wait(lRescan);
776         lRescan.unlock();
777
778         // scanned, detected, completed, cancelled, error
779         ASSERT_CALLBACK(testRescanCtx, 1, 3, 1, 0, 0);
780
781         ASSERT_DETECTED_IN_LIST(testRescanCtx.detectedList,
782                 TEST_WGT_APP_ROOT, MALWARE_HIGH_NAME, MALWARE_HIGH_SEVERITY, MALWARE_HIGH_DETAILED_URL);
783         ASSERT_DETECTED_IN_LIST_EXT(testRescanCtx.detectedList,
784                 TEST_WGT_APP_ROOT, true, TEST_WGT_PKG_ID);
785
786         ASSERT_DETECTED_IN_LIST(testRescanCtx.detectedList,
787                 TEST_TPK_APP_ROOT, MALWARE_HIGH_NAME, MALWARE_HIGH_SEVERITY, MALWARE_HIGH_DETAILED_URL);
788         ASSERT_DETECTED_IN_LIST_EXT(testRescanCtx.detectedList,
789                 TEST_TPK_APP_ROOT, true, TEST_TPK_PKG_ID);
790
791         ASSERT_DETECTED_IN_LIST(testRescanCtx.detectedList,
792                 TEST_FAKE_APP_FILE, MALWARE_HIGH_NAME, MALWARE_HIGH_SEVERITY, MALWARE_HIGH_DETAILED_URL);
793         ASSERT_DETECTED_IN_LIST_EXT(testRescanCtx.detectedList,
794                 TEST_FAKE_APP_FILE, false, nullptr);
795
796         uninstall_test_apps();
797
798         EXCEPTION_GUARD_END
799 }
800
801 BOOST_AUTO_TEST_CASE(canonicalize_files_absolute_path)
802 {
803         EXCEPTION_GUARD_START
804
805         Test::initialize_db();
806
807         auto c = Test::Context<csr_cs_context_h>();
808         auto context = c.get();
809
810         install_test_files();
811
812         set_default_callback(context);
813
814         // four files are all same as realpath.
815         const char *files[4] = {
816                 TEST_FILE_NORMAL,
817                 TEST_DIR "/./test_normal_file",
818                 TEST_DIR "/.././././csr-test/test_normal_file",
819                 TEST_DIR "/././.././csr-test/test_normal_file"
820         };
821
822         AsyncTestContext testCtx;
823
824         ASSERT_IF(csr_cs_scan_files_async(context, files, sizeof(files) / sizeof(const char *),
825                                                                           &testCtx),
826                           CSR_ERROR_NONE);
827
828         std::unique_lock<std::mutex> l(testCtx.m);
829         testCtx.cv.wait(l);
830         l.unlock();
831
832         ASSERT_CALLBACK(testCtx, 1, 0, 1, 0, 0);
833         ASSERT_IF(testCtx.scannedList.size(), static_cast<std::size_t>(1));
834         ASSERT_IF(testCtx.scannedList.front(), static_cast<const char *>(TEST_FILE_NORMAL));
835
836         EXCEPTION_GUARD_END
837 }
838
839 BOOST_AUTO_TEST_CASE(canonicalize_files_relative_path)
840 {
841         EXCEPTION_GUARD_START
842
843         Test::initialize_db();
844
845         auto c = Test::Context<csr_cs_context_h>();
846         auto context = c.get();
847
848         install_test_files();
849
850         set_default_callback(context);
851
852         // four files are all same as realpath.
853         const char *files[4] = {
854                 "test_normal_file",
855                 "./test_normal_file",
856                 ".././././csr-test/test_normal_file",
857                 "././.././csr-test/test_normal_file"
858         };
859
860         AsyncTestContext testCtx;
861
862         Test::ScopedChDir scopedCd(TEST_DIR);
863
864         ASSERT_IF(csr_cs_scan_files_async(context, files, sizeof(files) / sizeof(const char *),
865                                                                           &testCtx),
866                           CSR_ERROR_NONE);
867
868         std::unique_lock<std::mutex> l(testCtx.m);
869         testCtx.cv.wait(l);
870         l.unlock();
871
872         ASSERT_CALLBACK(testCtx, 1, 0, 1, 0, 0);
873         ASSERT_IF(testCtx.scannedList.size(), static_cast<std::size_t>(1));
874         ASSERT_IF(testCtx.scannedList.front(), static_cast<const char *>(TEST_FILE_NORMAL));
875
876         EXCEPTION_GUARD_END
877 }
878
879 BOOST_AUTO_TEST_CASE(multiple_async_dispatch_negative)
880 {
881         EXCEPTION_GUARD_START
882
883         Test::initialize_db();
884
885         auto c = Test::Context<csr_cs_context_h>();
886         auto context = c.get();
887
888         install_test_files();
889         install_test_apps();
890
891         set_default_callback(context);
892
893         AsyncTestContext testCtx;
894
895         // first call. it'll running in background asynchronously.
896         ASSERT_SUCCESS(csr_cs_scan_dir_async(context, TEST_DIR_APPS, &testCtx));
897
898         AsyncTestContext testCtx2;
899
900         // second call while first call is running in background. It should blocked because
901         // only one operation can be running asynchronously on one handle.
902         ASSERT_IF(csr_cs_scan_dir_async(context, TEST_DIR_APPS, &testCtx2),
903                           CSR_ERROR_BUSY);
904
905         std::unique_lock<std::mutex> l(testCtx.m);
906         testCtx.cv.wait(l);
907         l.unlock();
908
909         ASSERT_CALLBACK(testCtx, -1, 3, 1, 0, 0);
910
911         EXCEPTION_GUARD_END
912 }
913
914 BOOST_AUTO_TEST_SUITE_END()