From 4155af285de0c2fa8eb7e2c117da50d717b604e3 Mon Sep 17 00:00:00 2001 From: nick black Date: Thu, 10 Dec 2020 04:37:33 -0500 Subject: [PATCH] unit tests: check result of update_crc #131 --- src/gpt.c | 2 +- tests/gpt.cpp | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/gpt.c b/src/gpt.c index 62f9df26..c4152e4d 100644 --- a/src/gpt.c +++ b/src/gpt.c @@ -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; } diff --git a/tests/gpt.cpp b/tests/gpt.cpp index cd3b8a2b..596ebc0e 100644 --- a/tests/gpt.cpp +++ b/tests/gpt.cpp @@ -1,7 +1,8 @@ #include "main.h" #include "gpt.h" -#include #include +#include +#include #define UUID "\x5E\x86\x90\xEF\xD0\x30\x03\x46\x99\x3D\x54\x6E\xB0\xE7\x1B\x0D" @@ -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; @@ -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);