Added tests and fixed reading modules and striping names functionality
This commit is contained in:
parent
4e20f39ecd
commit
ff37c5e208
2
Makefile
2
Makefile
@ -1,4 +1,4 @@
|
|||||||
UNITYPATH = test/unity/
|
UNITYPATH = test/unity
|
||||||
INC := -I include
|
INC := -I include
|
||||||
UNITY := -I $(UNITYPATH)
|
UNITY := -I $(UNITYPATH)
|
||||||
|
|
||||||
|
@ -15,7 +15,7 @@ typedef struct ModuleStruct
|
|||||||
} ModuleStruct;
|
} ModuleStruct;
|
||||||
|
|
||||||
int ReadSingleModule(int fd, ModuleStruct* module);
|
int ReadSingleModule(int fd, ModuleStruct* module);
|
||||||
|
int WriteSingleModule(int fd, ModuleStruct* module, char* filename);
|
||||||
void SafetyCheck(int status, char* message);
|
void SafetyCheck(int status, char* message);
|
||||||
char* StripFilename(char* filename);
|
char* StripFilename(char* filename);
|
||||||
void Stuffy(int argc, char* argv[]);
|
void Stuffy(int argc, char* argv[]);
|
||||||
|
61
src/stuffy.c
61
src/stuffy.c
@ -12,10 +12,34 @@ int ReadSingleModule(int fd, ModuleStruct* module)
|
|||||||
{
|
{
|
||||||
int readSize;
|
int readSize;
|
||||||
long long int moduleSize;
|
long long int moduleSize;
|
||||||
readSize = read(fd, &(module->moduleHeader), sizeof(HeaderStruct));
|
readSize = read(fd, &(module->moduleHeader), sizeof(module->moduleHeader));
|
||||||
moduleSize = (long long) module->moduleHeader.moduleInfo.st_size;
|
moduleSize = (long long) module->moduleHeader.moduleInfo.st_size;
|
||||||
module->moduleData = malloc(moduleSize);
|
module->moduleData = malloc(moduleSize);
|
||||||
read(fd, &module->moduleData, moduleSize);
|
readSize = read(fd, module->moduleData, moduleSize);
|
||||||
|
return readSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Put a module header into a HeaderStruct and read (skip) module data
|
||||||
|
// Returns size of data read when reading a module header
|
||||||
|
int WriteSingleModule(int fd, ModuleStruct* module, char* filename)
|
||||||
|
{
|
||||||
|
int readSize;
|
||||||
|
long long int moduleSize;
|
||||||
|
readSize = read(fd, &(module->moduleHeader), sizeof(module->moduleHeader));
|
||||||
|
moduleSize = (long long) module->moduleHeader.moduleInfo.st_size;
|
||||||
|
module->moduleData = malloc(moduleSize);
|
||||||
|
readSize = read(fd, module->moduleData, moduleSize);
|
||||||
|
|
||||||
|
|
||||||
|
// strcpy(module.moduleHeader.moduleName, newFilename);
|
||||||
|
// stat(filename, &(module.moduleHeader.moduleInfo));
|
||||||
|
// write(archiveFile, &module.moduleHeader, sizeof(module.moduleHeader));
|
||||||
|
// moduleSize = (long long) module.moduleHeader.moduleInfo.st_size;
|
||||||
|
// char moduleData[moduleSize];
|
||||||
|
// write(archiveFile, moduleData, moduleSize);
|
||||||
|
// close(archiveFile);
|
||||||
|
|
||||||
|
|
||||||
return readSize;
|
return readSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -102,8 +126,8 @@ int IsFileArchived(char* archiveName, char* filename)
|
|||||||
do
|
do
|
||||||
{
|
{
|
||||||
readSize = ReadSingleModule(archiveFile, &(module));
|
readSize = ReadSingleModule(archiveFile, &(module));
|
||||||
printf("Size of data: %ld", sizeof(module.moduleData));
|
|
||||||
free(module.moduleData);
|
free(module.moduleData);
|
||||||
|
module.moduleData = NULL;
|
||||||
printf("%s\n", newFilename);
|
printf("%s\n", newFilename);
|
||||||
if (!strcmp(module.moduleHeader.moduleName, filename))
|
if (!strcmp(module.moduleHeader.moduleName, filename))
|
||||||
{
|
{
|
||||||
@ -123,34 +147,31 @@ int IsFileArchived(char* archiveName, char* filename)
|
|||||||
// file already inside the archive
|
// file already inside the archive
|
||||||
void AddToArchive(char* archiveName, char* filename)
|
void AddToArchive(char* archiveName, char* filename)
|
||||||
{
|
{
|
||||||
SafetyCheck(IsFileArchived(archiveName, filename),
|
// SafetyCheck(IsFileArchived(archiveName, filename),
|
||||||
"File already exists in archive, try removing file first.");
|
// "File already exists in archive, try removing file first.");
|
||||||
int archiveFile = open(archiveName, O_RDWR | O_CREAT, 0644);
|
int archiveFile = open(archiveName, O_RDWR | O_CREAT, 0644);
|
||||||
SafetyCheck((archiveFile < 0), "Archive failed to open.");
|
SafetyCheck((archiveFile < 0), "Archive failed to open.");
|
||||||
ssize_t readSize;
|
ssize_t readSize;
|
||||||
long long int moduleSize;
|
// long long int moduleSize;
|
||||||
ModuleStruct module;
|
ModuleStruct module;
|
||||||
readSize = read(archiveFile, &module.moduleHeader, sizeof(module.moduleHeader));
|
// readSize = read(archiveFile, &module.moduleHeader, sizeof(module.moduleHeader));
|
||||||
do
|
do
|
||||||
readSize = ReadSingleModule(archiveFile, &(module));
|
readSize = ReadSingleModule(archiveFile, &(module));
|
||||||
while (readSize > 0);
|
while (readSize > 0);
|
||||||
char* newFilename = StripFilename(filename);
|
char* newFilename = StripFilename(filename);
|
||||||
strcpy(module.moduleHeader.moduleName, newFilename);
|
WriteSingleModule(archiveFile, &module, newFilename);
|
||||||
stat(filename, &(module.moduleHeader.moduleInfo));
|
|
||||||
write(archiveFile, &module.moduleHeader, sizeof(module.moduleHeader));
|
|
||||||
moduleSize = (long long) module.moduleHeader.moduleInfo.st_size;
|
|
||||||
char moduleData[moduleSize];
|
|
||||||
write(archiveFile, moduleData, moduleSize);
|
|
||||||
close(archiveFile);
|
|
||||||
free(newFilename);
|
free(newFilename);
|
||||||
free(module.moduleData);
|
free(module.moduleData);
|
||||||
return;
|
// return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check through archive for file, if file exists with name, remove it
|
// Check through archive for file, if file exists with name, remove it
|
||||||
// If file not in archive, print "somefile was not found."
|
// If file not in archive, print "somefile was not found."
|
||||||
void RemoveFromArchive(char* archiveName, char* filename)
|
void RemoveFromArchive(char* archiveName, char* filename)
|
||||||
{
|
{
|
||||||
|
printf("'RemoveFromArchive()' function is still broken.\n");
|
||||||
|
return;
|
||||||
|
|
||||||
ssize_t readSize;
|
ssize_t readSize;
|
||||||
long long int moduleSize;
|
long long int moduleSize;
|
||||||
char* newFileName;
|
char* newFileName;
|
||||||
@ -186,6 +207,13 @@ void ListArchive(char* archiveName)
|
|||||||
ssize_t readSize;
|
ssize_t readSize;
|
||||||
long long int moduleSize;
|
long long int moduleSize;
|
||||||
SafetyCheck((archiveFile < 0), "Archive failed to open.");
|
SafetyCheck((archiveFile < 0), "Archive failed to open.");
|
||||||
|
// ModuleStruct module;
|
||||||
|
// do
|
||||||
|
// {
|
||||||
|
// readSize = ReadSingleModule(archiveFile, &module);
|
||||||
|
// printf("Name: %s | Size: %lld\n", module.moduleHeader.moduleName, moduleSize);
|
||||||
|
// }
|
||||||
|
// while (readSize > 0);
|
||||||
HeaderStruct moduleHeader;
|
HeaderStruct moduleHeader;
|
||||||
readSize = read(archiveFile, &moduleHeader, sizeof(moduleHeader));
|
readSize = read(archiveFile, &moduleHeader, sizeof(moduleHeader));
|
||||||
while (readSize > 0)
|
while (readSize > 0)
|
||||||
@ -203,5 +231,6 @@ void ListArchive(char* archiveName)
|
|||||||
// Extracted data remains in archive
|
// Extracted data remains in archive
|
||||||
void ExtractArchive(char* argv[])
|
void ExtractArchive(char* argv[])
|
||||||
{
|
{
|
||||||
;
|
printf("'ExtractArchive()' function is still broken.\n");
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
51
test/test.c
51
test/test.c
@ -1,7 +1,9 @@
|
|||||||
|
#include <fcntl.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include "unity.h"
|
#include "unity/unity.h"
|
||||||
#include "stuffy.h"
|
#include "stuffy.h"
|
||||||
|
|
||||||
|
ModuleStruct moduleTest;
|
||||||
int argc = 0;
|
int argc = 0;
|
||||||
char* argv[6];
|
char* argv[6];
|
||||||
|
|
||||||
@ -33,30 +35,65 @@ void Test_StuffyArgument_Should_ReturnCorrectly(void)
|
|||||||
TEST_ASSERT_EQUAL(-1, StuffyArgument(4, argv));
|
TEST_ASSERT_EQUAL(-1, StuffyArgument(4, argv));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Test_AddToArchive_Should_AddCorrectly(void)
|
void Test_StripFilename_Should_ChangeName(void)
|
||||||
{
|
{
|
||||||
;
|
TEST_ASSERT_EQUAL_STRING("mystuffyarchive.test", StripFilename("test/files/mystuffyarchive.test"));
|
||||||
|
TEST_ASSERT_EQUAL_STRING("temp1.test", StripFilename("test/files/temp1.test"));
|
||||||
|
TEST_ASSERT_EQUAL_STRING("temp2.test", StripFilename("test/files/temp2.test"));
|
||||||
|
TEST_ASSERT_EQUAL_STRING("temp3.test", StripFilename("test/files/temp3.test"));
|
||||||
|
TEST_ASSERT_EQUAL_STRING("temp4.test", StripFilename("test/files/temp4.test"));
|
||||||
|
TEST_ASSERT_EQUAL_STRING("temp5.test", StripFilename("test/files/temp5.test"));
|
||||||
|
}
|
||||||
|
|
||||||
|
void Test_ReadSingleModule_Should_ReadCorrectly(void)
|
||||||
|
{
|
||||||
|
int fd = open(argv[2], O_RDONLY);
|
||||||
|
TEST_ASSERT_EQUAL(80, ReadSingleModule(fd, &moduleTest));
|
||||||
|
TEST_ASSERT_EQUAL(25, ReadSingleModule(fd, &moduleTest));
|
||||||
|
TEST_ASSERT_EQUAL(27, ReadSingleModule(fd, &moduleTest));
|
||||||
|
TEST_ASSERT_EQUAL(27, ReadSingleModule(fd, &moduleTest));
|
||||||
|
close(fd);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Test_ReadSingleModule_Should_ReadNewArchive(void)
|
||||||
|
{
|
||||||
|
TEST_IGNORE_MESSAGE("Implementation Required: AddToArchive");
|
||||||
|
int fd = open("test/files/newstuffyarchive.test", O_RDONLY | O_CREAT, 0644);
|
||||||
|
AddToArchive("test/files/newstuffyarchive.test", "temp4.test");
|
||||||
|
TEST_ASSERT_EQUAL(27, ReadSingleModule(fd, &moduleTest));
|
||||||
|
AddToArchive("test/files/newstuffyarchive.test", "temp3.test");
|
||||||
|
TEST_ASSERT_EQUAL(27, ReadSingleModule(fd, &moduleTest));
|
||||||
|
AddToArchive("test/files/newstuffyarchive.test", "temp2.test");
|
||||||
|
TEST_ASSERT_EQUAL(25, ReadSingleModule(fd, &moduleTest));
|
||||||
|
AddToArchive("test/files/newstuffyarchive.test", "temp.test");
|
||||||
|
TEST_ASSERT_EQUAL(80, ReadSingleModule(fd, &moduleTest));
|
||||||
|
close(fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Test_RemoveFromArchive_Should_RemoveCorrectly(void)
|
void Test_RemoveFromArchive_Should_RemoveCorrectly(void)
|
||||||
{
|
{
|
||||||
;
|
TEST_IGNORE_MESSAGE("Implementation Required: RemoveFromArchive");
|
||||||
}
|
}
|
||||||
|
|
||||||
void Test_ListArchive_Should_ListCorrectly(void)
|
void Test_ListArchive_Should_ListCorrectly(void)
|
||||||
{
|
{
|
||||||
;
|
TEST_IGNORE_MESSAGE("Implementation Required: ListArchive");
|
||||||
}
|
}
|
||||||
|
|
||||||
void Test_ExtractArchive_Should_ExtractCorrectly(void)
|
void Test_ExtractArchive_Should_ExtractCorrectly(void)
|
||||||
{
|
{
|
||||||
;
|
TEST_IGNORE_MESSAGE("Implementation Required: ExtractArchive");
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
UNITY_BEGIN();
|
UNITY_BEGIN();
|
||||||
remove("test/files/mystuffyarchive.test");
|
|
||||||
RUN_TEST(Test_StuffyArgument_Should_ReturnCorrectly);
|
RUN_TEST(Test_StuffyArgument_Should_ReturnCorrectly);
|
||||||
|
RUN_TEST(Test_StripFilename_Should_ChangeName);
|
||||||
|
RUN_TEST(Test_ReadSingleModule_Should_ReadCorrectly);
|
||||||
|
RUN_TEST(Test_ReadSingleModule_Should_ReadNewArchive);
|
||||||
|
RUN_TEST(Test_RemoveFromArchive_Should_RemoveCorrectly);
|
||||||
|
RUN_TEST(Test_ListArchive_Should_ListCorrectly);
|
||||||
|
RUN_TEST(Test_ExtractArchive_Should_ExtractCorrectly);
|
||||||
return UNITY_END();
|
return UNITY_END();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user