diff options
Diffstat (limited to 'tests/cgptlib_test.c')
-rw-r--r-- | tests/cgptlib_test.c | 152 |
1 files changed, 97 insertions, 55 deletions
diff --git a/tests/cgptlib_test.c b/tests/cgptlib_test.c index f37a2725..0336c8e7 100644 --- a/tests/cgptlib_test.c +++ b/tests/cgptlib_test.c @@ -256,11 +256,16 @@ static int ParameterTests(void) } cases[] = { {512, DEFAULT_DRIVE_SECTORS, GPT_SUCCESS}, {520, DEFAULT_DRIVE_SECTORS, GPT_ERROR_INVALID_SECTOR_SIZE}, + {123, DEFAULT_DRIVE_SECTORS, GPT_ERROR_INVALID_SECTOR_SIZE}, + {4097, DEFAULT_DRIVE_SECTORS, GPT_ERROR_INVALID_SECTOR_SIZE}, + {256, DEFAULT_DRIVE_SECTORS, GPT_ERROR_INVALID_SECTOR_SIZE}, {512, 0, GPT_ERROR_INVALID_SECTOR_NUMBER}, {512, 10, GPT_ERROR_INVALID_SECTOR_NUMBER}, {512, GPT_PMBR_SECTORS + GPT_HEADER_SECTORS * 2 + TOTAL_ENTRIES_SIZE / DEFAULT_SECTOR_SIZE * 2, GPT_SUCCESS}, - {4096, DEFAULT_DRIVE_SECTORS, GPT_ERROR_INVALID_SECTOR_SIZE}, + {4096, DEFAULT_DRIVE_SECTORS, GPT_SUCCESS}, + {2048, DEFAULT_DRIVE_SECTORS, GPT_SUCCESS}, + {8192, DEFAULT_DRIVE_SECTORS, GPT_SUCCESS}, }; int i; @@ -364,15 +369,18 @@ static int SignatureTest(void) GptHeader *h2 = (GptHeader *)gpt->secondary_header; int i; - EXPECT(1 == CheckHeader(NULL, 0, gpt->streaming_drive_sectors, gpt->gpt_drive_sectors, 0)); + EXPECT(1 == CheckHeader(NULL, 0, gpt->streaming_drive_sectors, + gpt->gpt_drive_sectors, 0, gpt->sector_bytes)); for (i = 0; i < 8; ++i) { BuildTestGptData(gpt); h1->signature[i] ^= 0xff; h2->signature[i] ^= 0xff; RefreshCrc32(gpt); - EXPECT(1 == CheckHeader(h1, 0, gpt->streaming_drive_sectors, gpt->gpt_drive_sectors, 0)); - EXPECT(1 == CheckHeader(h2, 1, gpt->streaming_drive_sectors, gpt->gpt_drive_sectors, 0)); + EXPECT(1 == CheckHeader(h1, 0, gpt->streaming_drive_sectors, + gpt->gpt_drive_sectors, 0, gpt->sector_bytes)); + EXPECT(1 == CheckHeader(h2, 1, gpt->streaming_drive_sectors, + gpt->gpt_drive_sectors, 0, gpt->sector_bytes)); } return TEST_OK; @@ -406,10 +414,12 @@ static int RevisionTest(void) h2->revision = cases[i].value_to_test; RefreshCrc32(gpt); - EXPECT(CheckHeader(h1, 0, gpt->streaming_drive_sectors, gpt->gpt_drive_sectors, 0) == - cases[i].expect_rv); - EXPECT(CheckHeader(h2, 1, gpt->streaming_drive_sectors, gpt->gpt_drive_sectors, 0) == - cases[i].expect_rv); + EXPECT(CheckHeader(h1, 0, gpt->streaming_drive_sectors, + gpt->gpt_drive_sectors, 0, gpt->sector_bytes) == + cases[i].expect_rv); + EXPECT(CheckHeader(h2, 1, gpt->streaming_drive_sectors, + gpt->gpt_drive_sectors, 0, + gpt->sector_bytes) == cases[i].expect_rv); } return TEST_OK; } @@ -439,10 +449,12 @@ static int SizeTest(void) h2->size = cases[i].value_to_test; RefreshCrc32(gpt); - EXPECT(CheckHeader(h1, 0, gpt->streaming_drive_sectors, gpt->gpt_drive_sectors, 0) == - cases[i].expect_rv); - EXPECT(CheckHeader(h2, 1, gpt->streaming_drive_sectors, gpt->gpt_drive_sectors, 0) == - cases[i].expect_rv); + EXPECT(CheckHeader(h1, 0, gpt->streaming_drive_sectors, + gpt->gpt_drive_sectors, 0, gpt->sector_bytes) == + cases[i].expect_rv); + EXPECT(CheckHeader(h2, 1, gpt->streaming_drive_sectors, + gpt->gpt_drive_sectors, 0, gpt->sector_bytes) == + cases[i].expect_rv); } return TEST_OK; } @@ -458,12 +470,16 @@ static int CrcFieldTest(void) /* Modify a field that the header verification doesn't care about */ h1->entries_crc32++; h2->entries_crc32++; - EXPECT(1 == CheckHeader(h1, 0, gpt->streaming_drive_sectors, gpt->gpt_drive_sectors, 0)); - EXPECT(1 == CheckHeader(h2, 1, gpt->streaming_drive_sectors, gpt->gpt_drive_sectors, 0)); + EXPECT(1 == CheckHeader(h1, 0, gpt->streaming_drive_sectors, + gpt->gpt_drive_sectors, 0, gpt->sector_bytes)); + EXPECT(1 == CheckHeader(h2, 1, gpt->streaming_drive_sectors, + gpt->gpt_drive_sectors, 0, gpt->sector_bytes)); /* Refresh the CRC; should pass now */ RefreshCrc32(gpt); - EXPECT(0 == CheckHeader(h1, 0, gpt->streaming_drive_sectors, gpt->gpt_drive_sectors, 0)); - EXPECT(0 == CheckHeader(h2, 1, gpt->streaming_drive_sectors, gpt->gpt_drive_sectors, 0)); + EXPECT(0 == CheckHeader(h1, 0, gpt->streaming_drive_sectors, + gpt->gpt_drive_sectors, 0, gpt->sector_bytes)); + EXPECT(0 == CheckHeader(h2, 1, gpt->streaming_drive_sectors, + gpt->gpt_drive_sectors, 0, gpt->sector_bytes)); return TEST_OK; } @@ -479,8 +495,10 @@ static int ReservedFieldsTest(void) h1->reserved_zero ^= 0x12345678; /* whatever random */ h2->reserved_zero ^= 0x12345678; /* whatever random */ RefreshCrc32(gpt); - EXPECT(1 == CheckHeader(h1, 0, gpt->streaming_drive_sectors, gpt->gpt_drive_sectors, 0)); - EXPECT(1 == CheckHeader(h2, 1, gpt->streaming_drive_sectors, gpt->gpt_drive_sectors, 0)); + EXPECT(1 == CheckHeader(h1, 0, gpt->streaming_drive_sectors, + gpt->gpt_drive_sectors, 0, gpt->sector_bytes)); + EXPECT(1 == CheckHeader(h2, 1, gpt->streaming_drive_sectors, + gpt->gpt_drive_sectors, 0, gpt->sector_bytes)); #ifdef PADDING_CHECKED /* TODO: padding check is currently disabled */ @@ -488,8 +506,10 @@ static int ReservedFieldsTest(void) h1->padding[12] ^= 0x34; /* whatever random */ h2->padding[56] ^= 0x78; /* whatever random */ RefreshCrc32(gpt); - EXPECT(1 == CheckHeader(h1, 0, gpt->streaming_drive_sectors, gpt->gpt_drive_sectors, 0)); - EXPECT(1 == CheckHeader(h2, 1, gpt->streaming_drive_sectors, gpt->gpt_drive_sectors, 0)); + EXPECT(1 == CheckHeader(h1, 0, gpt->streaming_drive_sectors, + gpt->gpt_drive_sectors, 0, gpt->sector_bytes)); + EXPECT(1 == CheckHeader(h2, 1, gpt->streaming_drive_sectors, + gpt->gpt_drive_sectors, 0, gpt->sector_bytes)); #endif return TEST_OK; @@ -527,9 +547,11 @@ static int SizeOfPartitionEntryTest(void) { cases[i].value_to_test; RefreshCrc32(gpt); - EXPECT(CheckHeader(h1, 0, gpt->streaming_drive_sectors, gpt->gpt_drive_sectors, 0) == + EXPECT(CheckHeader(h1, 0, gpt->streaming_drive_sectors, + gpt->gpt_drive_sectors, 0, gpt->sector_bytes) == cases[i].expect_rv); - EXPECT(CheckHeader(h2, 1, gpt->streaming_drive_sectors, gpt->gpt_drive_sectors, 0) == + EXPECT(CheckHeader(h2, 1, gpt->streaming_drive_sectors, + gpt->gpt_drive_sectors, 0, gpt->sector_bytes) == cases[i].expect_rv); } @@ -550,13 +572,17 @@ static int NumberOfPartitionEntriesTest(void) h1->number_of_entries--; h2->number_of_entries /= 2; /* Because we halved h2 entries, its entries_lba is going to change. */ - h2->entries_lba = h2->my_lba - CalculateEntriesSectors(h2); + h2->entries_lba = h2->my_lba - CalculateEntriesSectors(h2, gpt->sector_bytes); RefreshCrc32(gpt); - EXPECT(1 == CheckHeader(h1, 0, gpt->streaming_drive_sectors, gpt->gpt_drive_sectors, 0)); - EXPECT(1 == CheckHeader(h2, 1, gpt->streaming_drive_sectors, gpt->gpt_drive_sectors, 0)); + EXPECT(1 == CheckHeader(h1, 0, gpt->streaming_drive_sectors, + gpt->gpt_drive_sectors, 0, gpt->sector_bytes)); + EXPECT(1 == CheckHeader(h2, 1, gpt->streaming_drive_sectors, + gpt->gpt_drive_sectors, 0, gpt->sector_bytes)); /* But it's okay to have less if the GPT structs are stored elsewhere. */ - EXPECT(0 == CheckHeader(h1, 0, gpt->streaming_drive_sectors, gpt->gpt_drive_sectors, GPT_FLAG_EXTERNAL)); - EXPECT(0 == CheckHeader(h2, 1, gpt->streaming_drive_sectors, gpt->gpt_drive_sectors, GPT_FLAG_EXTERNAL)); + EXPECT(0 == CheckHeader(h1, 0, gpt->streaming_drive_sectors, + gpt->gpt_drive_sectors, GPT_FLAG_EXTERNAL, gpt->sector_bytes)); + EXPECT(0 == CheckHeader(h2, 1, gpt->streaming_drive_sectors, + gpt->gpt_drive_sectors, GPT_FLAG_EXTERNAL, gpt->sector_bytes)); return TEST_OK; } @@ -571,37 +597,47 @@ static int MyLbaTest(void) /* myLBA depends on primary vs secondary flag */ BuildTestGptData(gpt); - EXPECT(1 == CheckHeader(h1, 1, gpt->streaming_drive_sectors, gpt->gpt_drive_sectors, 0)); - EXPECT(1 == CheckHeader(h2, 0, gpt->streaming_drive_sectors, gpt->gpt_drive_sectors, 0)); + EXPECT(1 == CheckHeader(h1, 1, gpt->streaming_drive_sectors, + gpt->gpt_drive_sectors, 0, gpt->sector_bytes)); + EXPECT(1 == CheckHeader(h2, 0, gpt->streaming_drive_sectors, + gpt->gpt_drive_sectors, 0, gpt->sector_bytes)); BuildTestGptData(gpt); h1->my_lba--; h2->my_lba--; RefreshCrc32(gpt); - EXPECT(1 == CheckHeader(h1, 0, gpt->streaming_drive_sectors, gpt->gpt_drive_sectors, 0)); - EXPECT(1 == CheckHeader(h2, 1, gpt->streaming_drive_sectors, gpt->gpt_drive_sectors, 0)); + EXPECT(1 == CheckHeader(h1, 0, gpt->streaming_drive_sectors, + gpt->gpt_drive_sectors, 0, gpt->sector_bytes)); + EXPECT(1 == CheckHeader(h2, 1, gpt->streaming_drive_sectors, + gpt->gpt_drive_sectors, 0, gpt->sector_bytes)); BuildTestGptData(gpt); h1->my_lba = 2; h2->my_lba--; RefreshCrc32(gpt); - EXPECT(1 == CheckHeader(h1, 0, gpt->streaming_drive_sectors, gpt->gpt_drive_sectors, 0)); - EXPECT(1 == CheckHeader(h2, 1, gpt->streaming_drive_sectors, gpt->gpt_drive_sectors, 0)); + EXPECT(1 == CheckHeader(h1, 0, gpt->streaming_drive_sectors, + gpt->gpt_drive_sectors, 0, gpt->sector_bytes)); + EXPECT(1 == CheckHeader(h2, 1, gpt->streaming_drive_sectors, + gpt->gpt_drive_sectors, 0, gpt->sector_bytes)); /* We should ignore the alternate_lba field entirely */ BuildTestGptData(gpt); h1->alternate_lba++; h2->alternate_lba++; RefreshCrc32(gpt); - EXPECT(0 == CheckHeader(h1, 0, gpt->streaming_drive_sectors, gpt->gpt_drive_sectors, 0)); - EXPECT(0 == CheckHeader(h2, 1, gpt->streaming_drive_sectors, gpt->gpt_drive_sectors, 0)); + EXPECT(0 == CheckHeader(h1, 0, gpt->streaming_drive_sectors, + gpt->gpt_drive_sectors, 0, gpt->sector_bytes)); + EXPECT(0 == CheckHeader(h2, 1, gpt->streaming_drive_sectors, + gpt->gpt_drive_sectors, 0, gpt->sector_bytes)); BuildTestGptData(gpt); h1->alternate_lba--; h2->alternate_lba--; RefreshCrc32(gpt); - EXPECT(0 == CheckHeader(h1, 0, gpt->streaming_drive_sectors, gpt->gpt_drive_sectors, 0)); - EXPECT(0 == CheckHeader(h2, 1, gpt->streaming_drive_sectors, gpt->gpt_drive_sectors, 0)); + EXPECT(0 == CheckHeader(h1, 0, gpt->streaming_drive_sectors, + gpt->gpt_drive_sectors, 0, gpt->sector_bytes)); + EXPECT(0 == CheckHeader(h2, 1, gpt->streaming_drive_sectors, + gpt->gpt_drive_sectors, 0, gpt->sector_bytes)); BuildTestGptData(gpt); h1->entries_lba++; @@ -611,19 +647,23 @@ static int MyLbaTest(void) * We support a padding between primary GPT header and its entries. So * this still passes. */ - EXPECT(0 == CheckHeader(h1, 0, gpt->streaming_drive_sectors, gpt->gpt_drive_sectors, 0)); + EXPECT(0 == CheckHeader(h1, 0, gpt->streaming_drive_sectors, + gpt->gpt_drive_sectors, 0, gpt->sector_bytes)); /* * But the secondary table should fail because it would overlap the * header, which is now lying after its entry array. */ - EXPECT(1 == CheckHeader(h2, 1, gpt->streaming_drive_sectors, gpt->gpt_drive_sectors, 0)); + EXPECT(1 == CheckHeader(h2, 1, gpt->streaming_drive_sectors, + gpt->gpt_drive_sectors, 0, gpt->sector_bytes)); BuildTestGptData(gpt); h1->entries_lba--; h2->entries_lba--; RefreshCrc32(gpt); - EXPECT(1 == CheckHeader(h1, 0, gpt->streaming_drive_sectors, gpt->gpt_drive_sectors, 0)); - EXPECT(1 == CheckHeader(h2, 1, gpt->streaming_drive_sectors, gpt->gpt_drive_sectors, 0)); + EXPECT(1 == CheckHeader(h1, 0, gpt->streaming_drive_sectors, + gpt->gpt_drive_sectors, 0, gpt->sector_bytes)); + EXPECT(1 == CheckHeader(h2, 1, gpt->streaming_drive_sectors, + gpt->gpt_drive_sectors, 0, gpt->sector_bytes)); return TEST_OK; } @@ -672,9 +712,11 @@ static int FirstUsableLbaAndLastUsableLbaTest(void) h2->last_usable_lba = cases[i].secondary_last_usable_lba; RefreshCrc32(gpt); - EXPECT(CheckHeader(h1, 0, gpt->streaming_drive_sectors, gpt->gpt_drive_sectors, 0) == + EXPECT(CheckHeader(h1, 0, gpt->streaming_drive_sectors, + gpt->gpt_drive_sectors, 0, gpt->sector_bytes) == cases[i].primary_rv); - EXPECT(CheckHeader(h2, 1, gpt->streaming_drive_sectors, gpt->gpt_drive_sectors, 0) == + EXPECT(CheckHeader(h2, 1, gpt->streaming_drive_sectors, + gpt->gpt_drive_sectors, 0, gpt->sector_bytes) == cases[i].secondary_rv); } @@ -846,7 +888,7 @@ static int SanityCheckTest(void) /* Invalid sector size should fail */ BuildTestGptData(gpt); - gpt->sector_bytes = 1024; + gpt->sector_bytes = 1023; EXPECT(GPT_ERROR_INVALID_SECTOR_SIZE == GptSanityCheck(gpt)); /* Modify headers */ @@ -1614,26 +1656,26 @@ static int CheckHeaderOffDevice() // GPT is stored on the same device so first usable lba should not // start at 0. EXPECT(1 == CheckHeader(primary_header, 0, gpt->streaming_drive_sectors, - gpt->gpt_drive_sectors, 0)); + gpt->gpt_drive_sectors, 0, gpt->sector_bytes)); // But off device, it is okay to accept this GPT header. EXPECT(0 == CheckHeader(primary_header, 0, gpt->streaming_drive_sectors, - gpt->gpt_drive_sectors, GPT_FLAG_EXTERNAL)); + gpt->gpt_drive_sectors, GPT_FLAG_EXTERNAL, gpt->sector_bytes)); BuildTestGptData(gpt); primary_header->number_of_entries = 100; RefreshCrc32(gpt); // Normally, number of entries is 128. So this should fail. EXPECT(1 == CheckHeader(primary_header, 0, gpt->streaming_drive_sectors, - gpt->gpt_drive_sectors, 0)); + gpt->gpt_drive_sectors, 0, gpt->sector_bytes)); // But off device, it is okay. EXPECT(0 == CheckHeader(primary_header, 0, gpt->streaming_drive_sectors, - gpt->gpt_drive_sectors, GPT_FLAG_EXTERNAL)); + gpt->gpt_drive_sectors, GPT_FLAG_EXTERNAL, gpt->sector_bytes)); primary_header->number_of_entries = MIN_NUMBER_OF_ENTRIES - 1; RefreshCrc32(gpt); // However, too few entries is not good. EXPECT(1 == CheckHeader(primary_header, 0, gpt->streaming_drive_sectors, - gpt->gpt_drive_sectors, GPT_FLAG_EXTERNAL)); + gpt->gpt_drive_sectors, GPT_FLAG_EXTERNAL, gpt->sector_bytes)); // Repeat for secondary header. BuildTestGptData(gpt); @@ -1641,25 +1683,25 @@ static int CheckHeaderOffDevice() secondary_header->first_usable_lba = 0; RefreshCrc32(gpt); EXPECT(1 == CheckHeader(secondary_header, 1, gpt->streaming_drive_sectors, - gpt->gpt_drive_sectors, 0)); + gpt->gpt_drive_sectors, 0, gpt->sector_bytes)); EXPECT(0 == CheckHeader(secondary_header, 1, gpt->streaming_drive_sectors, - gpt->gpt_drive_sectors, GPT_FLAG_EXTERNAL)); + gpt->gpt_drive_sectors, GPT_FLAG_EXTERNAL, gpt->sector_bytes)); BuildTestGptData(gpt); secondary_header->number_of_entries = 100; /* Because we change number of entries, we need to also update entrie_lba. */ secondary_header->entries_lba = secondary_header->my_lba - - CalculateEntriesSectors(secondary_header); + CalculateEntriesSectors(secondary_header, gpt->sector_bytes); RefreshCrc32(gpt); EXPECT(1 == CheckHeader(secondary_header, 1, gpt->streaming_drive_sectors, - gpt->gpt_drive_sectors, 0)); + gpt->gpt_drive_sectors, 0, gpt->sector_bytes)); EXPECT(0 == CheckHeader(secondary_header, 1, gpt->streaming_drive_sectors, - gpt->gpt_drive_sectors, GPT_FLAG_EXTERNAL)); + gpt->gpt_drive_sectors, GPT_FLAG_EXTERNAL, gpt->sector_bytes)); secondary_header->number_of_entries = MIN_NUMBER_OF_ENTRIES - 1; RefreshCrc32(gpt); EXPECT(1 == CheckHeader(secondary_header, 1, gpt->streaming_drive_sectors, - gpt->gpt_drive_sectors, GPT_FLAG_EXTERNAL)); + gpt->gpt_drive_sectors, GPT_FLAG_EXTERNAL, gpt->sector_bytes)); return TEST_OK; } |