-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
615 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,308 @@ | ||
#include "TestZipMinizip.h" | ||
#include "unity_fixture.h" | ||
#include <CoDeLib/FileUtils/FileUtils.h> | ||
#include <CoDeLib/RaiiString/RaiiString.h> | ||
#include <CoDeLib/Zip_minizip/Zip_minizip.h> | ||
#include <assert.h> | ||
#include <stdbool.h> | ||
#include <stdio.h> | ||
#include <string.h> | ||
|
||
// https://en.wikipedia.org/wiki/ZIP_(file_format)#Design | ||
|
||
typedef struct { | ||
/*! | ||
@brief The offset in the zip file where the signature should be, relative to | ||
the corresponding collection. | ||
*/ | ||
size_t offset; | ||
size_t bytesCount; | ||
const char *pBytes; | ||
} ZipStructureKnownValues; | ||
|
||
typedef struct { | ||
ZipStructureKnownValues signature; | ||
ZipStructureKnownValues versionMinimum; | ||
ZipStructureKnownValues generalPurposeBitFlag; | ||
ZipStructureKnownValues compressionMethod; | ||
ZipStructureKnownValues lastModificationFileTime; | ||
ZipStructureKnownValues lastModificationFileDate; | ||
ZipStructureKnownValues crc32; | ||
ZipStructureKnownValues compressedSize; | ||
ZipStructureKnownValues uncompressedSize; | ||
ZipStructureKnownValues filenameLength; | ||
ZipStructureKnownValues extraFieldLength; | ||
ZipStructureKnownValues filename; | ||
ZipStructureKnownValues extraField; | ||
} LocalFileHeaderContent; | ||
|
||
typedef struct { | ||
ZipStructureKnownValues signature; | ||
ZipStructureKnownValues crc32; | ||
ZipStructureKnownValues compressedSize; | ||
ZipStructureKnownValues uncompressedSize; | ||
} DataDescriptorContent; | ||
|
||
typedef struct { | ||
ZipStructureKnownValues signature; | ||
ZipStructureKnownValues sizeOfExtraFieldChunk; | ||
ZipStructureKnownValues uncompressedSize; | ||
ZipStructureKnownValues compressedSize; | ||
ZipStructureKnownValues offsetLocalHeaderRecord; | ||
ZipStructureKnownValues diskStartNumber; | ||
} Zip64ExtendedInformationExtraFieldContent; | ||
|
||
typedef struct { | ||
ZipStructureKnownValues signature; | ||
ZipStructureKnownValues versionMadeBy; | ||
ZipStructureKnownValues versionNeededToExtract; | ||
ZipStructureKnownValues generalPurposeBitFlag; | ||
ZipStructureKnownValues compressionMethod; | ||
ZipStructureKnownValues lastModificationFileTime; | ||
ZipStructureKnownValues lastModificationFileDate; | ||
ZipStructureKnownValues crc32; | ||
ZipStructureKnownValues compressedSize; | ||
ZipStructureKnownValues uncompressedSize; | ||
ZipStructureKnownValues filenameLength; | ||
ZipStructureKnownValues extraFieldLength; | ||
ZipStructureKnownValues fileCommentLength; | ||
ZipStructureKnownValues diskNumberStart; | ||
ZipStructureKnownValues internalFileAttributes; | ||
ZipStructureKnownValues externalFileAttributes; | ||
ZipStructureKnownValues relativeOffsetOfLocalHeader; | ||
ZipStructureKnownValues filename; | ||
ZipStructureKnownValues extraField; | ||
ZipStructureKnownValues fileComment; | ||
} CentralDirectoryFileHeaderContent; | ||
|
||
typedef struct { | ||
ZipStructureKnownValues signature; | ||
ZipStructureKnownValues diskNumber; | ||
ZipStructureKnownValues diskWithStartOfCentralDirectory; | ||
ZipStructureKnownValues numberOfCentralDirectoryRecordsOnThisDisk; | ||
ZipStructureKnownValues totalNumberOfCentralDirectoryRecords; | ||
ZipStructureKnownValues sizeOfCentralDirectory; | ||
ZipStructureKnownValues offsetOfStartOfCentralDirectory; | ||
ZipStructureKnownValues commentLength; | ||
ZipStructureKnownValues comment; | ||
} EndOfCentralDirectoryRecordContent; | ||
|
||
typedef struct { | ||
ZipStructureKnownValues signature; | ||
ZipStructureKnownValues sizeOfEocd64Min12; | ||
ZipStructureKnownValues versionMadeBy; | ||
ZipStructureKnownValues versionNeededToExtract; | ||
ZipStructureKnownValues diskNumber; | ||
ZipStructureKnownValues diskWithStartOfCentralDirectory; | ||
ZipStructureKnownValues numberOfCentralDirectoryRecordsOnThisDisk; | ||
ZipStructureKnownValues totalNumberOfCentralDirectoryRecords; | ||
ZipStructureKnownValues sizeOfCentralDirectory; | ||
ZipStructureKnownValues offsetOfStartOfCentralDirectory; | ||
ZipStructureKnownValues comment; | ||
} Zip64EndOfCentralDirectoryRecordContent; | ||
|
||
static const LocalFileHeaderContent g_localFileHeaderContent_Zip64Deflate | ||
__attribute__((unused)) = { | ||
.signature = {0, 4, "\x50\x4b\x03\x04"}, | ||
.versionMinimum = {4, 2, NULL}, | ||
.generalPurposeBitFlag = {6, 2, NULL}, | ||
.compressionMethod = {8, 2, "\x08\x00"}, | ||
.lastModificationFileTime = {10, 2, NULL}, | ||
.lastModificationFileDate = {12, 2, NULL}, | ||
.crc32 = {14, 4, NULL}, | ||
.compressedSize = {18, 4, "\xff\xff\xff\xff"}, | ||
.uncompressedSize = {22, 4, "\xff\xff\xff\xff"}, | ||
.filenameLength = {26, 2, NULL}, | ||
.extraFieldLength = {28, 2, NULL}, | ||
.filename = {30, 0, NULL}, | ||
.extraField = {30, 0, NULL}, | ||
}; | ||
|
||
static const DataDescriptorContent g_dataDescriptorContentWithSignature_Zip64 | ||
__attribute__((unused)) = { | ||
.signature = {0, 4, "\x50\x4b\x07\x08"}, | ||
.crc32 = {4, 4, NULL}, | ||
.compressedSize = {8, 8, "\xff\xff\xff\xff\xff\xff\xff\xff"}, | ||
.uncompressedSize = {16, 8, "\xff\xff\xff\xff\xff\xff\xff\xff"}, | ||
}; | ||
|
||
static char *g_pFullPathToBenchmarkTestFiles; | ||
|
||
void SetupTestZipMinizip(char *pFullPathToBenchmarkTestFiles) { | ||
g_pFullPathToBenchmarkTestFiles = pFullPathToBenchmarkTestFiles; | ||
} | ||
|
||
TEST_GROUP(TestZipMinizip); | ||
|
||
static RaiiString g_pathToSmallBasicTextFileZipDeflate; | ||
static RaiiString g_pathToSmallBasicTextFileZipSource; | ||
static RaiiString g_pathToMultiTextFileZipSource; | ||
static RaiiString g_pathToMultiTextFileAndSubDirZipSource; | ||
|
||
TEST_SETUP(TestZipMinizip) { | ||
g_pathToSmallBasicTextFileZipDeflate = | ||
RaiiStringCreateFromCString(g_pFullPathToBenchmarkTestFiles); | ||
RaiiStringAppend_cString(&g_pathToSmallBasicTextFileZipDeflate, | ||
"/SmallBasicTextFileZip/" | ||
"SmallBasicTextFile_deflate.zip"); | ||
|
||
g_pathToSmallBasicTextFileZipSource = | ||
RaiiStringCreateFromCString(g_pFullPathToBenchmarkTestFiles); | ||
RaiiStringAppend_cString(&g_pathToSmallBasicTextFileZipSource, | ||
"/SmallBasicTextFileZip/"); | ||
|
||
g_pathToMultiTextFileZipSource = | ||
RaiiStringCreateFromCString(g_pFullPathToBenchmarkTestFiles); | ||
RaiiStringAppend_cString(&g_pathToMultiTextFileZipSource, | ||
"/MultiTextFileZip/"); | ||
|
||
g_pathToMultiTextFileAndSubDirZipSource = | ||
RaiiStringCreateFromCString(g_pFullPathToBenchmarkTestFiles); | ||
RaiiStringAppend_cString(&g_pathToMultiTextFileAndSubDirZipSource, | ||
"/MultiTextFileAndSubDirZip/"); | ||
} | ||
|
||
TEST_TEAR_DOWN(TestZipMinizip) { | ||
RaiiStringClean(&g_pathToSmallBasicTextFileZipDeflate); | ||
RaiiStringClean(&g_pathToSmallBasicTextFileZipSource); | ||
RaiiStringClean(&g_pathToMultiTextFileZipSource); | ||
RaiiStringClean(&g_pathToMultiTextFileAndSubDirZipSource); | ||
|
||
if (PathExists("./tmp/")) { | ||
TEST_ASSERT_TRUE(RecursiveRmdir("./tmp/")); | ||
} | ||
} | ||
|
||
//============================== | ||
// Zip() | ||
//============================== | ||
|
||
TEST(TestZipMinizip, test_Zip_ReturnsErrorIfOutputZipPathIsNullptr) { | ||
const char *pInputPathArray[] = { | ||
"123", | ||
"asdf", | ||
}; | ||
|
||
const ZIP_RETURN_CODES statusZip = | ||
zip_minizip.Zip(NULL, &pInputPathArray[0], 2); | ||
TEST_ASSERT_EQUAL(ZIP_ERROR, statusZip); | ||
} | ||
|
||
TEST(TestZipMinizip, test_Zip_ReturnsErrorIfInputPathArrayIsNullptr) { | ||
const char dummyString[] = "123"; | ||
|
||
const ZIP_RETURN_CODES statusZip = | ||
zip_minizip.Zip(&dummyString[0], NULL, 2); | ||
TEST_ASSERT_EQUAL(ZIP_ERROR, statusZip); | ||
} | ||
|
||
TEST(TestZipMinizip, test_Zip_ReturnsErrorIfProvidedSizeIsZero) { | ||
const char dummyString[] = "123"; | ||
const char *pInputPathArray[] = { | ||
"123", | ||
"asdf", | ||
}; | ||
|
||
const ZIP_RETURN_CODES statusZip = | ||
zip_minizip.Zip(&dummyString[0], &pInputPathArray[0], 0); | ||
TEST_ASSERT_EQUAL(ZIP_ERROR, statusZip); | ||
} | ||
|
||
TEST(TestZipMinizip, test_Zip_ReturnsErrorIfZipFileAlreadyExists) { | ||
RAII_STRING dummyZipPath = RaiiStringCreateFromCString("./tmp/"); | ||
RecursiveMkdir(dummyZipPath.pString); | ||
|
||
RaiiStringAppend_cString(&dummyZipPath, "SomeZip.zip"); | ||
FILE *pDummyFile = fopen(dummyZipPath.pString, "w"); | ||
TEST_ASSERT_NOT_NULL(pDummyFile); | ||
fclose(pDummyFile); | ||
|
||
const char *pInputPathArray[] = { | ||
"123", | ||
}; | ||
|
||
const ZIP_RETURN_CODES statusZip = | ||
zip_minizip.Zip(dummyZipPath.pString, &pInputPathArray[0], 1); | ||
TEST_ASSERT_EQUAL(ZIP_ERROR, statusZip); | ||
} | ||
|
||
TEST(TestZipMinizip, | ||
test_Zip_CreatesZipFileAfterZippingInCorrectPathProvidingRelativePath) { | ||
RAII_STRING inputTextFilePath = RaiiStringCreateFromCString( | ||
g_pathToSmallBasicTextFileZipSource.pString); | ||
RaiiStringAppend_cString(&inputTextFilePath, "SmallBasicTextFile.txt"); | ||
|
||
const char *pInputPathArray[] = { | ||
inputTextFilePath.pString, | ||
}; | ||
|
||
RAII_STRING outputZipPath = | ||
RaiiStringCreateFromCString("./tmp/SmallBasicTextFile.zip"); | ||
|
||
TEST_ASSERT_FALSE(PathExists(outputZipPath.pString)); | ||
|
||
const ZIP_RETURN_CODES statusZip = | ||
zip_minizip.Zip(outputZipPath.pString, &pInputPathArray[0], 1); | ||
TEST_ASSERT_EQUAL(ZIP_SUCCESS, statusZip); | ||
|
||
TEST_ASSERT_TRUE(PathExists(outputZipPath.pString)); | ||
} | ||
|
||
TEST(TestZipMinizip, | ||
test_Zip_CreatesZipFileAfterZippingInCorrectPathProvidingAbsolutePath) { | ||
RAII_STRING inputTextFilePath = RaiiStringCreateFromCString( | ||
g_pathToSmallBasicTextFileZipSource.pString); | ||
RaiiStringAppend_cString(&inputTextFilePath, "SmallBasicTextFile.txt"); | ||
|
||
const char *pInputPathArray[] = { | ||
inputTextFilePath.pString, | ||
}; | ||
|
||
char cwdBuffer[MAX_PATH_LENGTH_WTH_TERMINATOR]; | ||
GetCurrentWorkingDirectory(&cwdBuffer[0], MAX_PATH_LENGTH_WTH_TERMINATOR); | ||
|
||
RAII_STRING outputZipPath = RaiiStringCreateFromCString(&cwdBuffer[0]); | ||
RaiiStringAppend_cString(&outputZipPath, "tmp/SmallBasicTextFile.zip"); | ||
|
||
TEST_ASSERT_FALSE(PathExists(outputZipPath.pString)); | ||
|
||
const ZIP_RETURN_CODES statusZip = | ||
zip_minizip.Zip(outputZipPath.pString, &pInputPathArray[0], 1); | ||
TEST_ASSERT_EQUAL(ZIP_SUCCESS, statusZip); | ||
|
||
TEST_ASSERT_TRUE(PathExists(outputZipPath.pString)); | ||
} | ||
|
||
// TODO: Create tests that test that single files and directories are added to | ||
// the zip file. | ||
|
||
// TEST(TestZipMinizip, test_Zip_ExternallyCreatedZipHasExpectedHeaderBytes) { | ||
// TEST_IGNORE_MESSAGE("Not implemented yet"); | ||
// } | ||
|
||
// TEST(TestZipMinizip, test_Zip_CreatedZipHasCorrentHeaderBytes) { | ||
// TEST_IGNORE_MESSAGE("Not implemented yet"); | ||
// } | ||
|
||
//============================== | ||
// TEST_GROUP_RUNNER | ||
//============================== | ||
|
||
TEST_GROUP_RUNNER(TestZipMinizip) { | ||
// Zip() | ||
RUN_TEST_CASE(TestZipMinizip, | ||
test_Zip_ReturnsErrorIfOutputZipPathIsNullptr); | ||
RUN_TEST_CASE(TestZipMinizip, | ||
test_Zip_ReturnsErrorIfInputPathArrayIsNullptr); | ||
RUN_TEST_CASE(TestZipMinizip, test_Zip_ReturnsErrorIfProvidedSizeIsZero); | ||
RUN_TEST_CASE(TestZipMinizip, test_Zip_ReturnsErrorIfZipFileAlreadyExists); | ||
RUN_TEST_CASE( | ||
TestZipMinizip, | ||
test_Zip_CreatesZipFileAfterZippingInCorrectPathProvidingRelativePath); | ||
RUN_TEST_CASE( | ||
TestZipMinizip, | ||
test_Zip_CreatesZipFileAfterZippingInCorrectPathProvidingAbsolutePath); | ||
// RUN_TEST_CASE(TestZipMinizip, | ||
// test_Zip_ExternallyCreatedZipHasExpectedHeaderBytes); | ||
// RUN_TEST_CASE(TestZipMinizip, test_Zip_CreatedZipHasCorrentHeaderBytes); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#pragma once | ||
|
||
void SetupTestZipMinizip(char *pFullPathToBenchmarkTestFiles); |
Oops, something went wrong.