Skip to content

Commit

Permalink
unit tests: check result of update_crc #131
Browse files Browse the repository at this point in the history
  • Loading branch information
dankamongmen committed Dec 10, 2020
1 parent 3c1df97 commit 4155af2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/gpt.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ int update_crc(gpt_header *head, const gpt_entry *gpes){
}
head->partcrc = crc32(0, (const void*)gpes, head->partcount * head->partsize);
head->crc = 0;
head->crc = crc32(0, (void*)head, hs);
head->crc = crc32(0, (const void*)head, hs);
return 0;
}

Expand Down
16 changes: 14 additions & 2 deletions tests/gpt.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#include "main.h"
#include "gpt.h"
#include <cstring>
#include <zlib.h>
#include <cstring>
#include <arpa/inet.h>

#define UUID "\x5E\x86\x90\xEF\xD0\x30\x03\x46\x99\x3D\x54\x6E\xB0\xE7\x1B\x0D"

Expand Down Expand Up @@ -44,6 +45,17 @@ TEST_CASE("GPT") {
CHECK(0 == memcmp(head.disk_guid, UUID, sizeof(head.disk_guid)));
}

SUBCASE("CRC32Sanity") {
const unsigned char sample[] = "123456789";
uint32_t crc = crc32(0L, Z_NULL, 0);
crc = crc32(crc, sample, sizeof(sample) - 1);
CHECK(crc == 0xCBF43926);
crc = crc32(0L, Z_NULL, 0);
uint32_t db = htonl(0xdeadbeef);
crc = crc32(crc, (const unsigned char*)&db, 4);
CHECK(crc == 2090640218);
}

// Verify both CRCs, and the reserved area following HeaderCRC32
SUBCASE("CRC32") {
gpt_header head;
Expand All @@ -55,7 +67,7 @@ TEST_CASE("GPT") {
CHECK(128 <= head.partcount);
auto entries = new gpt_entry[head.partcount];
memset(entries, 0, sizeof(*entries) * head.partcount);
update_crc(&head, entries);
CHECK(0 == update_crc(&head, entries));
CHECK(2006165414 == head.crc);
CHECK(0 == head.reserved);
CHECK(2874462854 == head.partcrc);
Expand Down

0 comments on commit 4155af2

Please sign in to comment.