Skip to content

Commit

Permalink
test: unzip compressed zip file from windows compression with zlib in…
Browse files Browse the repository at this point in the history
…flate and minizip unzip
  • Loading branch information
enzoevers committed Dec 26, 2024
1 parent 14a4469 commit 987bc62
Show file tree
Hide file tree
Showing 10 changed files with 364 additions and 3 deletions.
Binary file not shown.
16 changes: 15 additions & 1 deletion Benchmark/Files/MultiTextFileAndSubDirZip/Readme.md
Original file line number Diff line number Diff line change
@@ -1 +1,15 @@
The zip file has been created with 7zip and using deflate compression level `store` to not compress the files.
The zip file has been created with the build in compressions option from the Windows 11 Pro 23H2 file explorer.

1. Select the files to zip.
2. Right-click > "Compress to..." > "Additional options"
3. For the `*_store.zip` file:
- Archive format: `ZIP`
- Compression method: `Store`
- Retain symbolic links: `checked` (this is the default)
4. For the `*_deflate.zip` file:
- Archive format: `ZIP`
- Compression method: `Deflate`
- Compression level: `6` (this is the default)
- Retain symbolic links: `checked` (this is the default)


Binary file not shown.
16 changes: 15 additions & 1 deletion Benchmark/Files/MultiTextFileZip/Readme.md
Original file line number Diff line number Diff line change
@@ -1 +1,15 @@
The zip file has been created with 7zip and using deflate compression level `store` to not compress the files.
The zip file has been created with the build in compressions option from the Windows 11 Pro 23H2 file explorer.

1. Select the files to zip.
2. Right-click > "Compress to..." > "Additional options"
3. For the `*_store.zip` file:
- Archive format: `ZIP`
- Compression method: `Store`
- Retain symbolic links: `checked` (this is the default)
4. For the `*_deflate.zip` file:
- Archive format: `ZIP`
- Compression method: `Deflate`
- Compression level: `6` (this is the default)
- Retain symbolic links: `checked` (this is the default)


16 changes: 15 additions & 1 deletion Benchmark/Files/SmallBasicTextFileZip/Readme.md
Original file line number Diff line number Diff line change
@@ -1 +1,15 @@
The zip file has been created with 7zip and using deflate compression level `store` to not compress the file.
The zip file has been created with the build in compressions option from the Windows 11 Pro 23H2 file explorer.

1. Select the files to zip.
2. Right-click > "Compress to..." > "Additional options"
3. For the `*_store.zip` file:
- Archive format: `ZIP`
- Compression method: `Store`
- Retain symbolic links: `checked` (this is the default)
4. For the `*_deflate.zip` file:
- Archive format: `ZIP`
- Compression method: `Deflate`
- Compression level: `6` (this is the default)
- Retain symbolic links: `checked` (this is the default)


Binary file not shown.
1 change: 1 addition & 0 deletions CoDeLib/Test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ add_executable(CoDeLib_Test
src/TestDeflateInflateZlib.c
src/TestFileUtils.c
src/TestUnZipMinizip.c
src/TestUnZipMinizipInflateZlib.c
src/TestZipContentInfo.c
)

Expand Down
312 changes: 312 additions & 0 deletions CoDeLib/Test/src/TestUnZipMinizipInflateZlib.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,312 @@
#include "TestUnZipMinizipInflateZlib.h"
#include "unity_fixture.h"
#include <CoDeLib/FileUtils/FileUtils.h>
#include <CoDeLib/Inflate_zlib/Inflate_zlib.h>
#include <CoDeLib/RaiiString/RaiiString.h>
#include <CoDeLib/UnZip_minizip/UnZip_minizip.h>
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>

static char *g_pFullPathToBenchmarkTestFiles;

void SetupTestUnZipMinizipInflateZlib(char *pFullPathToBenchmarkTestFiles) {
g_pFullPathToBenchmarkTestFiles = pFullPathToBenchmarkTestFiles;
}

TEST_GROUP(TestUnZipMinizipInflateZlib);

static RaiiString g_someUnZippedDirPath;
static RaiiString g_someZipPath;
static RaiiString g_pathToSmallBasicTextFileZipDeflate;
static RaiiString g_pathToSmallBasicTextFileZipSource;
static RaiiString g_pathToMultiTextFileZipDeflate;
static RaiiString g_pathToMultiTextFileZipSource;
static RaiiString g_pathToMultiTextFileAndSubDirZipDeflate;
static RaiiString g_pathToMultiTextFileAndSubDirZipSource;

TEST_SETUP(TestUnZipMinizipInflateZlib) {
g_someUnZippedDirPath =
RaiiStringCreateFromCString("SomePath/someUnZippedDirPath.zip");
g_someZipPath = RaiiStringCreateFromCString("SomePath/someZipPath.zip");

g_pathToSmallBasicTextFileZipDeflate =
RaiiStringCreateFromCString(g_pFullPathToBenchmarkTestFiles);
RaiiStringAppend_cString(&g_pathToSmallBasicTextFileZipDeflate,
"/SmallBasicTextFileZip/"
"SmallBasicTextFile_deflate.zip");

g_pathToSmallBasicTextFileZipSource =
RaiiStringCreateFromCString(g_pFullPathToBenchmarkTestFiles);
RaiiStringAppend_cString(&g_pathToSmallBasicTextFileZipSource,
"/SmallBasicTextFileZip/");

g_pathToMultiTextFileZipDeflate =
RaiiStringCreateFromCString(g_pFullPathToBenchmarkTestFiles);
RaiiStringAppend_cString(&g_pathToMultiTextFileZipDeflate,
"/MultiTextFileZip/"
"MultiTextFileZip_deflate.zip");

g_pathToMultiTextFileZipSource =
RaiiStringCreateFromCString(g_pFullPathToBenchmarkTestFiles);
RaiiStringAppend_cString(&g_pathToMultiTextFileZipSource,
"/MultiTextFileZip/");

g_pathToMultiTextFileAndSubDirZipDeflate =
RaiiStringCreateFromCString(g_pFullPathToBenchmarkTestFiles);
RaiiStringAppend_cString(&g_pathToMultiTextFileAndSubDirZipDeflate,
"/MultiTextFileAndSubDirZip/"
"MultiTextFileAndSubDirZip_deflate.zip");

g_pathToMultiTextFileAndSubDirZipSource =
RaiiStringCreateFromCString(g_pFullPathToBenchmarkTestFiles);
RaiiStringAppend_cString(&g_pathToMultiTextFileAndSubDirZipSource,
"/MultiTextFileAndSubDirZip/");
}

TEST_TEAR_DOWN(TestUnZipMinizipInflateZlib) {
RaiiStringClean(&g_someUnZippedDirPath);
RaiiStringClean(&g_someZipPath);
RaiiStringClean(&g_pathToSmallBasicTextFileZipDeflate);
RaiiStringClean(&g_pathToSmallBasicTextFileZipSource);
RaiiStringClean(&g_pathToMultiTextFileZipDeflate);
RaiiStringClean(&g_pathToMultiTextFileZipSource);
RaiiStringClean(&g_pathToMultiTextFileAndSubDirZipDeflate);
RaiiStringClean(&g_pathToMultiTextFileAndSubDirZipSource);

if (PathExists("./tmp/")) {
TEST_ASSERT_TRUE(RecursiveRmdir("./tmp/"));
}
}

//==============================
// UnZip() + Inflate()
//==============================

TEST(TestUnZipMinizipInflateZlib,
test_UnZipAndInflateSingleFileZipWritesDataCorrectlyToTheFile) {
RAII_ZIPCONTENTINFO zipInfo =
ZipContentInfoCreate(&g_pathToSmallBasicTextFileZipDeflate);

RAII_STRING pathToUnzippedFile = RaiiStringCreateFromCString(
"./tmp/SmallBasicTextFile_deflate_unZipped/");

const UNZIP_RETURN_CODES statusUnzip =
unzip_minizip.UnZip(&zipInfo, &pathToUnzippedFile);
TEST_ASSERT_EQUAL(UNZIP_SUCCESS, statusUnzip);

RAII_STRING pathToUnzippedFilesWithFile =
RaiiStringCreateFromCString(pathToUnzippedFile.pString);
RaiiStringAppend_cString(&pathToUnzippedFilesWithFile,
"SmallBasicTextFile.txt");

RAII_STRING pathToUnzippedInflatedFilesWithFile =
RaiiStringCreateFromCString(pathToUnzippedFile.pString);
RaiiStringAppend_cString(&pathToUnzippedInflatedFilesWithFile,
"SmallBasicTextFile.decompressed.txt");

RAII_STRING pathToSourceWithFilesWithFile = RaiiStringCreateFromCString(
g_pathToSmallBasicTextFileZipSource.pString);
RaiiStringAppend_cString(&pathToSourceWithFilesWithFile,
"SmallBasicTextFile.txt");

FILE *pFileUnzipped = NULL;
FILE *pFileUnzippedInflated = NULL;

OpenFileWithMode(&pFileUnzipped, &pathToUnzippedFilesWithFile, "rb");

// Note: It is important to open the file in binary mode.
// Otherwise, a \r (cariage return) character will implicitely
// add a \n (newline) character.
OpenFileWithMode(&pFileUnzippedInflated,
&pathToUnzippedInflatedFilesWithFile, "w+b");

const INFLATE_RETURN_CODES statusInflate =
inflate_zlib.Inflate(pFileUnzipped, pFileUnzippedInflated, NULL);
TEST_ASSERT_EQUAL(INFLATE_SUCCESS, statusInflate);

fclose(pFileUnzipped);

FILE *pFileSource = NULL;
OpenFileWithMode(&pFileSource, &pathToSourceWithFilesWithFile, "rb");

TEST_ASSERT_TRUE(FilesAreEqual(pFileSource, pFileUnzippedInflated));

fclose(pFileSource);
fclose(pFileUnzippedInflated);
}

TEST(TestUnZipMinizipInflateZlib,
test_UnZipAndInflateMultiFileZipWritesDataCorrectlyToTheFiles) {
RAII_ZIPCONTENTINFO zipInfo =
ZipContentInfoCreate(&g_pathToMultiTextFileZipDeflate);

RAII_STRING pathToUnzippedFiles =
RaiiStringCreateFromCString("./tmp/MultiTextFileZip_deflate_unZipped/");

const UNZIP_RETURN_CODES statusUnzip =
unzip_minizip.UnZip(&zipInfo, &pathToUnzippedFiles);
TEST_ASSERT_EQUAL(UNZIP_SUCCESS, statusUnzip);

RaiiString pExpectedPaths[] = {
RaiiStringCreateFromCString("TextFileOne.txt"),
RaiiStringCreateFromCString("TextFileTwo.txt"),
RaiiStringCreateFromCString("ThirdTextFile.txt"),
};
const size_t expectedFileCount =
sizeof(pExpectedPaths) / sizeof(pExpectedPaths[0]);

RaiiString pPathsToDecompressedFiles[] = {
RaiiStringCreateFromCString("TextFileOne.decompressed.txt"),
RaiiStringCreateFromCString("TextFileTwo.decompressed.txt"),
RaiiStringCreateFromCString("ThirdTextFile.decompressed.txt"),
};

for (size_t i = 0; i < expectedFileCount; ++i) {

RAII_STRING pathToUnzippedFilesWithFile =
RaiiStringCreateFromCString(pathToUnzippedFiles.pString);
RaiiStringAppend_cString(&pathToUnzippedFilesWithFile,
pExpectedPaths[i].pString);

RAII_STRING pathToUnzippedInflatedFilesWithFile =
RaiiStringCreateFromCString(pathToUnzippedFiles.pString);
RaiiStringAppend_cString(&pathToUnzippedInflatedFilesWithFile,
pPathsToDecompressedFiles[i].pString);

RAII_STRING pathToSourceWithFilesWithFile =
RaiiStringCreateFromCString(g_pathToMultiTextFileZipSource.pString);
RaiiStringAppend_cString(&pathToSourceWithFilesWithFile,
pExpectedPaths[i].pString);

FILE *pFileUnzipped = NULL;
FILE *pFileUnzippedInflated = NULL;

OpenFileWithMode(&pFileUnzipped, &pathToUnzippedFilesWithFile, "rb");

// Note: It is important to open the file in binary mode.
// Otherwise, a \r (cariage return) character will implicitely
// add a \n (newline) character.
OpenFileWithMode(&pFileUnzippedInflated,
&pathToUnzippedInflatedFilesWithFile, "w+b");

const INFLATE_RETURN_CODES statusInflate =
inflate_zlib.Inflate(pFileUnzipped, pFileUnzippedInflated, NULL);
TEST_ASSERT_EQUAL(INFLATE_SUCCESS, statusInflate);

fclose(pFileUnzipped);

FILE *pFileSource = NULL;
OpenFileWithMode(&pFileSource, &pathToSourceWithFilesWithFile, "rb");

TEST_ASSERT_TRUE(FilesAreEqual(pFileSource, pFileUnzippedInflated));

fclose(pFileSource);
fclose(pFileUnzippedInflated);
}

for (size_t i = 0; i < expectedFileCount; ++i) {
RaiiStringClean(&pExpectedPaths[i]);
RaiiStringClean(&pPathsToDecompressedFiles[i]);
}
}

TEST(TestUnZipMinizipInflateZlib,
test_UnZipAndInflateMultiFileAndSubDirZipWritesDataCorrectlyToTheFiles) {
RAII_ZIPCONTENTINFO zipInfo =
ZipContentInfoCreate(&g_pathToMultiTextFileAndSubDirZipDeflate);

RAII_STRING pathToUnzippedFiles = RaiiStringCreateFromCString(
"./tmp/MultiTextFileAndSubDirZip_deflate_unZipped/");

const UNZIP_RETURN_CODES statusUnzip =
unzip_minizip.UnZip(&zipInfo, &pathToUnzippedFiles);
TEST_ASSERT_EQUAL(UNZIP_SUCCESS, statusUnzip);

RaiiString pExpectedPaths[] = {
RaiiStringCreateFromCString("DirFileOne/TextFileOne.txt"),
RaiiStringCreateFromCString("DirOfDirs/OtherDir/DummyFile.txt"),
RaiiStringCreateFromCString("DirOfDirs/OtherDir/FilleInDirectory.txt"),
RaiiStringCreateFromCString(
"DirOfDirs/SubDirWithCopyOfTopLevelFile/TextFile.txt"),
RaiiStringCreateFromCString("TextFile.txt"),
};
const size_t expectedFileCount =
sizeof(pExpectedPaths) / sizeof(pExpectedPaths[0]);

RaiiString pPathsToDecompressedFiles[] = {
RaiiStringCreateFromCString("DirFileOne/TextFileOne.decompressed.txt"),
RaiiStringCreateFromCString(
"DirOfDirs/OtherDir/DummyFile.decompressed.txt"),
RaiiStringCreateFromCString(
"DirOfDirs/OtherDir/FilleInDirectory.decompressed.txt"),
RaiiStringCreateFromCString(
"DirOfDirs/SubDirWithCopyOfTopLevelFile/TextFile.decompressed.txt"),
RaiiStringCreateFromCString("TextFile.decompressed.txt"),
};

for (size_t i = 0; i < expectedFileCount; ++i) {

RAII_STRING pathToUnzippedFilesWithFile =
RaiiStringCreateFromCString(pathToUnzippedFiles.pString);
RaiiStringAppend_cString(&pathToUnzippedFilesWithFile,
pExpectedPaths[i].pString);

RAII_STRING pathToUnzippedInflatedFilesWithFile =
RaiiStringCreateFromCString(pathToUnzippedFiles.pString);
RaiiStringAppend_cString(&pathToUnzippedInflatedFilesWithFile,
pPathsToDecompressedFiles[i].pString);

RAII_STRING pathToSourceWithFilesWithFile = RaiiStringCreateFromCString(
g_pathToMultiTextFileAndSubDirZipSource.pString);
RaiiStringAppend_cString(&pathToSourceWithFilesWithFile,
pExpectedPaths[i].pString);

FILE *pFileUnzipped = NULL;
FILE *pFileUnzippedInflated = NULL;

OpenFileWithMode(&pFileUnzipped, &pathToUnzippedFilesWithFile, "rb");

// Note: It is important to open the file in binary mode.
// Otherwise, a \r (cariage return) character will implicitely
// add a \n (newline) character.
OpenFileWithMode(&pFileUnzippedInflated,
&pathToUnzippedInflatedFilesWithFile, "w+b");

const INFLATE_RETURN_CODES statusInflate =
inflate_zlib.Inflate(pFileUnzipped, pFileUnzippedInflated, NULL);
TEST_ASSERT_EQUAL(INFLATE_SUCCESS, statusInflate);

fclose(pFileUnzipped);

FILE *pFileSource = NULL;
OpenFileWithMode(&pFileSource, &pathToSourceWithFilesWithFile, "rb");

TEST_ASSERT_TRUE(FilesAreEqual(pFileSource, pFileUnzippedInflated));

fclose(pFileSource);
fclose(pFileUnzippedInflated);
}

for (size_t i = 0; i < expectedFileCount; ++i) {
RaiiStringClean(&pExpectedPaths[i]);
RaiiStringClean(&pPathsToDecompressedFiles[i]);
}
}

//==============================
// TEST_GROUP_RUNNER
//==============================

TEST_GROUP_RUNNER(TestUnZipMinizipInflateZlib) {
RUN_TEST_CASE(
TestUnZipMinizipInflateZlib,
test_UnZipAndInflateSingleFileZipWritesDataCorrectlyToTheFile);
RUN_TEST_CASE(
TestUnZipMinizipInflateZlib,
test_UnZipAndInflateMultiFileZipWritesDataCorrectlyToTheFiles);
RUN_TEST_CASE(
TestUnZipMinizipInflateZlib,
test_UnZipAndInflateMultiFileAndSubDirZipWritesDataCorrectlyToTheFiles);
}
3 changes: 3 additions & 0 deletions CoDeLib/Test/src/TestUnZipMinizipInflateZlib.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

void SetupTestUnZipMinizipInflateZlib(char *pFullPathToBenchmarkTestFiles);
Loading

0 comments on commit 987bc62

Please sign in to comment.