Tests: Add test for embedded GZIP compressor
Signed-off-by: DL6ER <dl6er@dl6er.de>
This commit is contained in:
parent
024130a638
commit
5e67e488dc
40
src/args.c
40
src/args.c
|
@ -175,7 +175,8 @@ void parse_args(int argc, char* argv[])
|
|||
exit(sqlite3_shell_main(argc, argv));
|
||||
|
||||
// Compression feature
|
||||
if((argc == 3 || argc == 4) && strcmp(argv[1], "--compress") == 0)
|
||||
if((argc == 3 || argc == 4) &&
|
||||
(strcmp(argv[1], "gzip") == 0 || strcmp(argv[1], "--gzip") == 0))
|
||||
{
|
||||
// Enable stdout printing
|
||||
cli_mode = true;
|
||||
|
@ -183,18 +184,47 @@ void parse_args(int argc, char* argv[])
|
|||
|
||||
// Get input and output file names
|
||||
const char *infile = argv[2];
|
||||
bool is_gz = strEndsWith(infile, ".gz");
|
||||
char *outfile = NULL;
|
||||
if(argc == 4)
|
||||
outfile = argv[3];
|
||||
{
|
||||
// If an output file is given, we use it
|
||||
outfile = strdup(argv[3]);
|
||||
}
|
||||
else if(is_gz)
|
||||
{
|
||||
// If no output file is given, and this is a gzipped
|
||||
// file, we use the input file name without ".gz"
|
||||
// appended
|
||||
outfile = calloc(strlen(infile)-2, sizeof(char));
|
||||
strncpy(outfile, infile, strlen(infile)-3);
|
||||
}
|
||||
else
|
||||
{
|
||||
// If no output file is given, we use the input file name with ".gz" appended
|
||||
// If no output file is given, and this is not a gzipped
|
||||
// file, we use the input file name with ".gz" appended
|
||||
outfile = calloc(strlen(infile)+4, sizeof(char));
|
||||
strcpy(outfile, infile);
|
||||
strcat(outfile, ".gz");
|
||||
}
|
||||
// Compress file
|
||||
exit(deflate_file(infile, outfile, true) ? EXIT_SUCCESS : EXIT_FAILURE);
|
||||
|
||||
bool success = false;
|
||||
if(is_gz)
|
||||
{
|
||||
// If the input file is already gzipped, we decompress it
|
||||
success = inflate_file(infile, outfile, true);
|
||||
}
|
||||
else
|
||||
{
|
||||
// If the input file is not gzipped, we compress it
|
||||
success = deflate_file(infile, outfile, true);
|
||||
}
|
||||
|
||||
// Free allocated memory
|
||||
free(outfile);
|
||||
|
||||
// Return exit code
|
||||
exit(success ? EXIT_SUCCESS : EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// Deompression feature
|
||||
|
|
|
@ -138,6 +138,10 @@ bool deflate_file(const char *infile, const char *outfile, bool verbose)
|
|||
bool success = deflate_buffer(buffer_uncompressed, size_uncompressed,
|
||||
&buffer_compressed, &size_compressed);
|
||||
|
||||
// Free memory
|
||||
free(buffer_uncompressed);
|
||||
|
||||
// Check if compression was successful
|
||||
if(!success)
|
||||
{
|
||||
log_warn("Failed to compress %s", infile);
|
||||
|
@ -167,6 +171,8 @@ bool deflate_file(const char *infile, const char *outfile, bool verbose)
|
|||
}
|
||||
fclose(fp);
|
||||
|
||||
free(buffer_compressed);
|
||||
|
||||
if(verbose)
|
||||
{
|
||||
// Print compression ratio
|
||||
|
@ -391,7 +397,6 @@ bool inflate_file(const char *infile, const char *outfile, bool verbose)
|
|||
{
|
||||
log_warn("Failed to read %lu bytes from %s", (unsigned long)size_compressed, infile);
|
||||
fclose(fp);
|
||||
free(buffer_compressed);
|
||||
return false;
|
||||
}
|
||||
fclose(fp);
|
||||
|
@ -400,13 +405,14 @@ bool inflate_file(const char *infile, const char *outfile, bool verbose)
|
|||
mz_ulong size_uncompressed = 0;
|
||||
bool success = inflate_buffer(buffer_compressed, size_compressed,
|
||||
&buffer_uncompressed, &size_uncompressed);
|
||||
// Free memory
|
||||
free(buffer_compressed);
|
||||
|
||||
// Check if uncompression was successful
|
||||
if(!success)
|
||||
{
|
||||
log_warn("Failed to uncompress %s", infile);
|
||||
free(buffer_uncompressed);
|
||||
if(buffer_compressed)
|
||||
free(buffer_compressed);
|
||||
fclose(fp);
|
||||
return false;
|
||||
}
|
||||
|
@ -416,20 +422,18 @@ bool inflate_file(const char *infile, const char *outfile, bool verbose)
|
|||
if(fp == NULL)
|
||||
{
|
||||
log_warn("Failed to open %s: %s (%d)", outfile, strerror(errno), errno);
|
||||
free(buffer_uncompressed);
|
||||
free(buffer_compressed);
|
||||
return false;
|
||||
}
|
||||
if(fwrite(buffer_uncompressed, sizeof(char), size_uncompressed, fp) != size_uncompressed)
|
||||
{
|
||||
log_warn("Failed to write %lu bytes to %s", (unsigned long)size_uncompressed, outfile);
|
||||
fclose(fp);
|
||||
free(buffer_uncompressed);
|
||||
free(buffer_compressed);
|
||||
return false;
|
||||
}
|
||||
fclose(fp);
|
||||
|
||||
free(buffer_uncompressed);
|
||||
|
||||
if(verbose)
|
||||
{
|
||||
// Print compression ratio
|
||||
|
|
|
@ -1219,6 +1219,39 @@
|
|||
[[ ${lines[0]} == "true" ]]
|
||||
}
|
||||
|
||||
@test "Test embedded GZIP compressor" {
|
||||
run bash -c './pihole-FTL gzip test/pihole-FTL.db.sql'
|
||||
printf "Compression output:\n"
|
||||
printf "%s\n" "${lines[@]}"
|
||||
[[ $status == 0 ]]
|
||||
[[ ${lines[0]} == "Compressed test/pihole-FTL.db.sql ("* ]]
|
||||
printf "Uncompress (FTL) output:\n"
|
||||
run bash -c './pihole-FTL gzip test/pihole-FTL.db.sql.gz test/pihole-FTL.db.sql.1'
|
||||
printf "%s\n" "${lines[@]}"
|
||||
[[ $status == 0 ]]
|
||||
[[ ${lines[0]} == "Uncompressed test/pihole-FTL.db.sql.gz ("* ]]
|
||||
printf "Uncompress (gzip) output:\n"
|
||||
run bash -c 'gzip -dkc test/pihole-FTL.db.sql.gz > test/pihole-FTL.db.sql.2'
|
||||
printf "%s\n" "${lines[@]}"
|
||||
[[ $status == 0 ]]
|
||||
printf "Remove generated GZIP file:\n"
|
||||
run bash -c 'rm test/pihole-FTL.db.sql.gz'
|
||||
printf "%s\n" "${lines[@]}"
|
||||
[[ $status == 0 ]]
|
||||
printf "Compare uncompressed files (original vs. FTL uncompressed):\n"
|
||||
run bash -c 'cmp test/pihole-FTL.db.sql test/pihole-FTL.db.sql.1'
|
||||
printf "%s\n" "${lines[@]}"
|
||||
[[ $status == 0 ]]
|
||||
printf "Compare uncompressed files (original vs. gzip uncompressed):\n"
|
||||
run bash -c 'cmp test/pihole-FTL.db.sql test/pihole-FTL.db.sql.2'
|
||||
printf "%s\n" "${lines[@]}"
|
||||
[[ $status == 0 ]]
|
||||
printf "Remove generated files:\n"
|
||||
run bash -c 'rm test/pihole-FTL.db.sql.[1-2]'
|
||||
printf "%s\n" "${lines[@]}"
|
||||
[[ $status == 0 ]]
|
||||
}
|
||||
|
||||
@test "API validation" {
|
||||
run python3 test/api/checkAPI.py
|
||||
printf "%s\n" "${lines[@]}"
|
||||
|
|
Loading…
Reference in New Issue