12
12
#pragma warning(pop)
13
13
#endif
14
14
15
+ // Replace this library with <filesystem> once we move to GCC 8 or newer
16
+ // versions
17
+ #define _SILENCE_EXPERIMENTAL_FILESYSTEM_DEPRECATION_WARNING 1
18
+ #include < experimental/filesystem>
19
+
20
+ #include < climits>
21
+ #include < random>
15
22
#include < stdio.h>
16
23
#include < stdlib.h>
17
24
#include < string.h>
25
+ #include < string>
18
26
19
27
#include " pkg_editor/pkg_editor.h"
20
28
#include < assert.h>
40
48
#define PACK_UNPACK_FILE " .pack_unpack"
41
49
#define PACK_UNPACK_DIR " .pack_unpack_dir"
42
50
51
+ using random_bytes_engine =
52
+ std::independent_bits_engine<std::default_random_engine, CHAR_BIT,
53
+ unsigned int >;
54
+ namespace fs = std::experimental::filesystem::v1;
55
+
43
56
static void l_remove_file (const char *filename) {
44
57
#ifdef _WIN32
45
58
_unlink (filename);
@@ -379,7 +392,7 @@ TEST(package, pack) {
379
392
CHECK_EQUAL (1 , result);
380
393
}
381
394
382
- static bool files_same (const char * f1, const char * f2) {
395
+ static bool files_same (const fs::path f1, const fs::path f2) {
383
396
std::ifstream file1 (f1, std::ifstream::ate | std::ifstream::binary);
384
397
std::ifstream file2 (f2, std::ifstream::ate | std::ifstream::binary);
385
398
file1.exceptions (std::ifstream::failbit | std::ifstream::badbit);
@@ -398,11 +411,55 @@ static bool files_same(const char *f1, const char *f2) {
398
411
return std::equal (begin1, std::istreambuf_iterator<char >(), begin2);
399
412
}
400
413
414
+ static void generate_random_file (const fs::path &name, size_t size) {
415
+ random_bytes_engine rbe;
416
+ std::ofstream file (name);
417
+ file.exceptions (std::ifstream::failbit | std::ifstream::badbit);
418
+ std::generate_n (std::ostreambuf_iterator<char >(file), size,
419
+ [&] { return (char )rbe (); });
420
+ }
421
+
422
+ // Creates a folder consisting of a certain number of files with random contents
423
+ // and random sizes
424
+ static std::vector<fs::path> generate_tmp_folder (const fs::path &tmpdir) {
425
+ std::vector<fs::path> files;
426
+
427
+ std::mt19937 gen{};
428
+ std::uniform_int_distribution dis{0 , 100000 };
429
+
430
+ fs::remove_all (tmpdir);
431
+ fs::create_directory (tmpdir);
432
+
433
+ // Guarantee we always have one large and one empty file
434
+ generate_random_file (files.emplace_back (tmpdir / " empty_file" ), 0 );
435
+ generate_random_file (files.emplace_back (tmpdir / " large_file" ), 10000000 );
436
+
437
+ const int num_random_files = 8 ;
438
+ for (int i = 0 ; i < num_random_files; i++) {
439
+ std::string filename = " file" + std::to_string (i);
440
+ generate_random_file (files.emplace_back (tmpdir / filename), dis (gen));
441
+ }
442
+
443
+ return files;
444
+ }
445
+
446
+ static bool is_same_tmpdir (const std::vector<fs::path> &files,
447
+ const fs::path &unpack_dir) {
448
+ return std::all_of (files.begin (), files.end (), [&](const fs::path &path) {
449
+ fs::path unpacked_file_path = unpack_dir / path;
450
+ return files_same (path, unpacked_file_path);
451
+ });
452
+ }
453
+
401
454
TEST (package, unpack) {
402
455
int result;
456
+ fs::path tmpdir = " tmp" ;
457
+ std::string tmpdir_string = tmpdir.string ();
458
+ const char *tmpdir_c_str = tmpdir_string.c_str ();
459
+ std::vector<fs::path> files = generate_tmp_folder (tmpdir);
403
460
404
461
// Create a known good input.
405
- const char *test_input[] = {" include " , " src " , " test " , NULL };
462
+ const char *test_input[] = {tmpdir_c_str , NULL };
406
463
result = acl_pkg_pack (PACK_UNPACK_FILE, test_input);
407
464
CHECK_EQUAL (1 , result);
408
465
@@ -411,17 +468,17 @@ TEST(package, unpack) {
411
468
CHECK_EQUAL (1 , result);
412
469
413
470
// Compare some files to be sure that they are the same.
414
- CHECK_EQUAL (true ,
415
- files_same (" CMakeLists.txt" , PACK_UNPACK_DIR " /CMakeLists.txt" ));
416
- CHECK_EQUAL (true , files_same (" test/pkg_editor_test.cpp" ,
417
- PACK_UNPACK_DIR " /test/pkg_editor_test.cpp" ));
471
+ CHECK (is_same_tmpdir (files, PACK_UNPACK_DIR));
418
472
}
419
473
420
474
TEST (package, unpack_buffer) {
421
475
int result;
422
-
476
+ fs::path tmpdir = " tmp" ;
477
+ std::string tmpdir_string = tmpdir.string ();
478
+ const char *tmpdir_c_str = tmpdir_string.c_str ();
479
+ std::vector<fs::path> files = generate_tmp_folder (tmpdir);
423
480
// Create a known good input.
424
- const char *test_input[] = {" include" , " src" , " test" , NULL };
481
+ const char *test_input[] = {" include" , " src" , " test" , tmpdir_c_str, NULL };
425
482
result = acl_pkg_pack (PACK_UNPACK_FILE, test_input);
426
483
CHECK_EQUAL (1 , result);
427
484
@@ -447,13 +504,18 @@ TEST(package, unpack_buffer) {
447
504
PACK_UNPACK_DIR " /src/pkg_editor.c" ));
448
505
CHECK_EQUAL (true , files_same (" test/pkg_editor_test.cpp" ,
449
506
PACK_UNPACK_DIR " /test/pkg_editor_test.cpp" ));
507
+ CHECK (is_same_tmpdir (files, PACK_UNPACK_DIR));
450
508
}
451
509
452
510
TEST (package, unpack_buffer_stdin) {
453
511
int result;
512
+ fs::path tmpdir = " tmp" ;
513
+ std::string tmpdir_string = tmpdir.string ();
514
+ const char *tmpdir_c_str = tmpdir_string.c_str ();
515
+ std::vector<fs::path> files = generate_tmp_folder (tmpdir);
454
516
455
517
// Create a known good input.
456
- const char *test_input[] = {" include" , " src" , " test" , NULL };
518
+ const char *test_input[] = {" include" , " src" , " test" , tmpdir_c_str, NULL };
457
519
result = acl_pkg_pack (PACK_UNPACK_FILE, test_input);
458
520
CHECK_EQUAL (1 , result);
459
521
@@ -496,5 +558,6 @@ TEST(package, unpack_buffer_stdin) {
496
558
PACK_UNPACK_DIR " /src/pkg_editor.c" ));
497
559
CHECK_EQUAL (true , files_same (" test/pkg_editor_test.cpp" ,
498
560
PACK_UNPACK_DIR " /test/pkg_editor_test.cpp" ));
561
+ CHECK (is_same_tmpdir (files, PACK_UNPACK_DIR));
499
562
}
500
563
#endif
0 commit comments