Skip to content

Commit acd44e2

Browse files
Add better unit tests to pkg_editor_test.cpp
Past tests did not cover the scenario where the compressed file has a size of 0. This commit adds randomness to the test (i.e. random file size/name). Additionally, all the unpack tests cover cases with large files (size ~10MB) and empty files.
1 parent 8aada1e commit acd44e2

File tree

2 files changed

+68
-29
lines changed

2 files changed

+68
-29
lines changed

lib/pkg_editor/test/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# SPDX-License-Identifier: BSD-3-Clause
33

44
add_executable(pkg_editor_test pkg_editor_test.cpp)
5-
target_link_libraries(pkg_editor_test PRIVATE CppUTest pkg_editor)
5+
target_link_libraries(pkg_editor_test PRIVATE CppUTest pkg_editor -lstdc++fs)
66
add_test(pkg_editor_test pkg_editor_test -v)
77

88
if(ZLIB_FOUND)

lib/pkg_editor/test/pkg_editor_test.cpp

Lines changed: 67 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,14 @@
1212
#pragma warning(pop)
1313
#endif
1414

15+
#include <dirent.h>
16+
#include <experimental/filesystem>
17+
#include <iterator>
18+
#include <random>
1519
#include <stdio.h>
1620
#include <stdlib.h>
1721
#include <string.h>
22+
#include <sys/stat.h>
1823

1924
#include "pkg_editor/pkg_editor.h"
2025
#include <assert.h>
@@ -40,13 +45,16 @@
4045
#define PACK_UNPACK_FILE ".pack_unpack"
4146
#define PACK_UNPACK_DIR ".pack_unpack_dir"
4247

48+
namespace fs = std::experimental::filesystem::v1;
49+
4350
static void l_remove_file(const char *filename) {
4451
#ifdef _WIN32
4552
_unlink(filename);
4653
#else
4754
unlink(filename);
4855
#endif
49-
}
56+
} // namespace
57+
// fs=std::experimental::filesystem::v1staticvoidl_remove_file(constchar*filename)
5058

5159
static void l_remove_sample_file(void) { l_remove_file(SAMPLE_FILE); }
5260

@@ -382,6 +390,8 @@ TEST(package, pack) {
382390
static bool files_same(const char *f1, const char *f2) {
383391
std::ifstream file1(f1, std::ifstream::ate | std::ifstream::binary);
384392
std::ifstream file2(f2, std::ifstream::ate | std::ifstream::binary);
393+
file1.exceptions(std::ifstream::failbit | std::ifstream::badbit);
394+
file2.exceptions(std::ifstream::failbit | std::ifstream::badbit);
385395
const std::ifstream::pos_type fileSize = file1.tellg();
386396

387397
if (fileSize != file2.tellg()) {
@@ -396,11 +406,54 @@ static bool files_same(const char *f1, const char *f2) {
396406
return std::equal(begin1, std::istreambuf_iterator<char>(), begin2);
397407
}
398408

409+
static void generate_random_file(std::string name, size_t size) {
410+
std::mt19937 gen{std::random_device()()};
411+
std::uniform_int_distribution<> dis(0, 255);
412+
std::ofstream file(name);
413+
std::generate_n(std::ostream_iterator<char>(file, ""), size,
414+
[&] { return dis(gen); });
415+
}
416+
417+
/**
418+
* Generates a "tmp" folder in the cwd with num_files files.
419+
* Puts their file paths relative to the cwd in generated_files.
420+
*/
421+
static void generate_tmp_folder(int num_files, std::string generated_files[]) {
422+
fs::remove_all("tmp");
423+
fs::create_directory("tmp");
424+
425+
generated_files[num_files - 1] = "./tmp/empty_file";
426+
generate_random_file(generated_files[num_files - 1], 0);
427+
if (num_files == 1) {
428+
return;
429+
}
430+
431+
generated_files[num_files - 2] = "./tmp/large_file";
432+
generate_random_file(generated_files[num_files - 2], 10000000); // ~10MB
433+
434+
num_files -= 2;
435+
436+
for (int i = 0; i < num_files; i++) {
437+
generated_files[i] = "." + std::string(tmpnam(NULL));
438+
generate_random_file(generated_files[i], rand() % 100000);
439+
}
440+
}
441+
442+
static void check_tmp_folder(int num_files, std::string generated_files[],
443+
std::string unpack_dir) {
444+
for (int i = 0; i < num_files; i++) { //
445+
std::string unpacked_file_path = unpack_dir + "/" + generated_files[i];
446+
files_same(generated_files[i].c_str(), unpacked_file_path.c_str());
447+
}
448+
}
449+
399450
TEST(package, unpack) {
400451
int result;
401-
452+
int num_rand_files = 10;
453+
std::string files[num_rand_files];
454+
generate_tmp_folder(num_rand_files, files);
402455
// Create a known good input.
403-
const char *test_input[] = {"include", "src", "test", NULL};
456+
const char *test_input[] = {"tmp", NULL};
404457
result = acl_pkg_pack(PACK_UNPACK_FILE, test_input);
405458
CHECK_EQUAL(1, result);
406459

@@ -409,17 +462,17 @@ TEST(package, unpack) {
409462
CHECK_EQUAL(1, result);
410463

411464
// Compare some files to be sure that they are the same.
412-
CHECK_EQUAL(true,
413-
files_same("CMakeLists.txt", PACK_UNPACK_DIR "/CMakeLists.txt"));
414-
CHECK_EQUAL(true, files_same("test/pkg_editor_test.cpp",
415-
PACK_UNPACK_DIR "/test/pkg_editor_test.cpp"));
465+
check_tmp_folder(num_rand_files, files, PACK_UNPACK_DIR);
416466
}
417467

418468
TEST(package, unpack_buffer) {
419469
int result;
470+
int num_rand_files = 10;
471+
std::string files[num_rand_files];
472+
generate_tmp_folder(num_rand_files, files);
420473

421474
// Create a known good input.
422-
const char *test_input[] = {"include", "src", "test", NULL};
475+
const char *test_input[] = {"include", "src", "test", "tmp", NULL};
423476
result = acl_pkg_pack(PACK_UNPACK_FILE, test_input);
424477
CHECK_EQUAL(1, result);
425478

@@ -445,13 +498,16 @@ TEST(package, unpack_buffer) {
445498
PACK_UNPACK_DIR "/src/pkg_editor.c"));
446499
CHECK_EQUAL(true, files_same("test/pkg_editor_test.cpp",
447500
PACK_UNPACK_DIR "/test/pkg_editor_test.cpp"));
501+
check_tmp_folder(num_rand_files, files, PACK_UNPACK_DIR);
448502
}
449503

450504
TEST(package, unpack_buffer_stdin) {
451505
int result;
452-
506+
int num_rand_files = 10;
507+
std::string files[num_rand_files];
508+
generate_tmp_folder(num_rand_files, files);
453509
// Create a known good input.
454-
const char *test_input[] = {"include", "src", "test", NULL};
510+
const char *test_input[] = {"include", "src", "test", "tmp", NULL};
455511
result = acl_pkg_pack(PACK_UNPACK_FILE, test_input);
456512
CHECK_EQUAL(1, result);
457513

@@ -494,23 +550,6 @@ TEST(package, unpack_buffer_stdin) {
494550
PACK_UNPACK_DIR "/src/pkg_editor.c"));
495551
CHECK_EQUAL(true, files_same("test/pkg_editor_test.cpp",
496552
PACK_UNPACK_DIR "/test/pkg_editor_test.cpp"));
553+
check_tmp_folder(num_rand_files, files, PACK_UNPACK_DIR);
497554
}
498-
499-
TEST(package, unpack_empty) {
500-
int result;
501-
502-
// Create a known good input.
503-
const char *test_input[] = {"test", NULL};
504-
FILE *f = fopen("test/empty", "wb");
505-
fclose(f);
506-
result = acl_pkg_pack(PACK_UNPACK_FILE, test_input);
507-
CHECK_EQUAL(1, result);
508-
509-
// Now read it back.
510-
result = acl_pkg_unpack(PACK_UNPACK_FILE, PACK_UNPACK_DIR);
511-
CHECK_EQUAL(1, result);
512-
513-
CHECK_EQUAL(true, files_same("test/empty", PACK_UNPACK_DIR "/test/empty"));
514-
}
515-
516555
#endif

0 commit comments

Comments
 (0)