styled mad tests

This commit is contained in:
iceman1001 committed 2026-06-29 23:27:03 +02:00
1 parent 2b51280725
commit 17ac10327f
2 files changed
+170 -126

No files matched your search

+6 -2
View File
@@ -688,7 +688,9 @@ int mad_app_write(const mad_ops_t *ops, uint16_t aid, bool swapmad, bool overrid
offset += sector_data_size;
}
PrintAndLogEx(SUCCESS, "Wrote %zu bytes to AID 0x%04X ( %s )", data_len, aid, _GREEN_("ok"));
if (ops->verbose) {
PrintAndLogEx(SUCCESS, "Wrote %zu bytes to AID 0x%04X ( %s )", data_len, aid, _GREEN_("ok"));
}
return PM3_SUCCESS;
}
@@ -723,6 +725,8 @@ int mad_app_verify(const mad_ops_t *ops, uint16_t aid, bool swapmad, bool overri
PrintAndLogEx(WARNING, "length mismatch: expected %zu, read %zu (data matches up to shorter)", expected_len, readback_len);
}
PrintAndLogEx(SUCCESS, "Verified %zu bytes ( %s )", cmp_len, _GREEN_("ok"));
if (ops->verbose) {
PrintAndLogEx(SUCCESS, "Verified %zu bytes ( %s )", cmp_len, _GREEN_("ok"));
}
return PM3_SUCCESS;
}
+164 -124
View File
@@ -26,28 +26,30 @@
#include <string.h>
#define ASSERT_EQ(msg, expected, actual) do { \
#define ASSERT_EQ(msg, expected, actual) \
if ((expected) != (actual)) { \
PrintAndLogEx(FAILED, " %s: expected %d, got %d", (msg), (int)(expected), (int)(actual)); \
return false; \
} \
} while (0)
}
#define ASSERT_EQ_HEX(msg, expected, actual) do { \
#define ASSERT_EQ_HEX(msg, expected, actual) \
if ((expected) != (actual)) { \
PrintAndLogEx(FAILED, " %s: expected 0x%04X, got 0x%04X", (msg), (unsigned)(expected), (unsigned)(actual)); \
return false; \
} \
} while (0)
}
#define ASSERT_TRUE(msg, cond) do { \
if (!(cond)) { \
#define ASSERT_TRUE(msg, cond) \
if ((cond) == false) { \
PrintAndLogEx(FAILED, " %s", (msg)); \
return false; \
} \
} while (0)
}
static void build_test_mad1(mad1_sector_t *s) {
if (s == NULL) {
return;
}
memset(s, 0, sizeof(*s));
s->manufacturer[0] = 0x01;
@@ -74,6 +76,10 @@ static void build_test_mad1(mad1_sector_t *s) {
}
static void build_test_mad2(mad2_sector_t *s) {
if (s == NULL) {
return;
}
memset(s, 0, sizeof(*s));
s->mad.info = 0x00;
@@ -96,8 +102,7 @@ static void build_test_mad2(mad2_sector_t *s) {
#define MOCK_CARD_SIZE (256 * 16)
static uint8_t g_mock_card[MOCK_CARD_SIZE];
static int mock_read_sector(uint8_t sector_no, uint8_t key_type,
const uint8_t *key, uint8_t *buf, bool verbose) {
static int mock_read_sector(uint8_t sector_no, uint8_t key_type, const uint8_t *key, uint8_t *buf, bool verbose) {
(void)key_type;
(void)key;
(void)verbose;
@@ -105,22 +110,27 @@ static int mock_read_sector(uint8_t sector_no, uint8_t key_type,
uint8_t nblocks = mfNumBlocksPerSector(sector_no);
uint32_t offset = first_block * 16;
uint32_t size = nblocks * 16;
if (offset + size > MOCK_CARD_SIZE)
if (offset + size > MOCK_CARD_SIZE) {
return PM3_ESOFT;
}
memcpy(buf, &g_mock_card[offset], size);
return PM3_SUCCESS;
}
static int mock_write_sector_data(uint8_t sector_no, uint8_t key_type,
const uint8_t *key, const uint8_t *data, bool verbose) {
static int mock_write_sector_data(uint8_t sector_no, uint8_t key_type, const uint8_t *key, const uint8_t *data, bool verbose) {
(void)key_type;
(void)key;
(void)verbose;
uint8_t first_block = mfFirstBlockOfSector(sector_no);
uint8_t ndata = mfNumBlocksPerSector(sector_no) - 1;
uint32_t offset = first_block * 16;
if (offset + ndata * 16 > MOCK_CARD_SIZE)
if (offset + ndata * 16 > MOCK_CARD_SIZE) {
return PM3_ESOFT;
}
memcpy(&g_mock_card[offset], data, ndata * 16);
return PM3_SUCCESS;
}
@@ -135,11 +145,15 @@ static void mock_card_init(void) {
// fill data sectors with deterministic per-byte pattern:
// byte value = (sector * 0x10) + (block_within_sector * 0x04) + byte_within_block
for (int sec = 1; sec <= 15; sec++) {
uint8_t first = mfFirstBlockOfSector(sec);
for (int b = 0; b < 3; b++) {
uint8_t *block = &g_mock_card[(first + b) * 16];
for (int i = 0; i < 16; i++)
for (int i = 0; i < 16; i++) {
block[i] = (uint8_t)((sec << 4) | (b << 2) | (i & 0x03));
}
}
// trailer
uint8_t *trailer = &g_mock_card[(first + 3) * 16];
@@ -154,11 +168,15 @@ static void mock_card_init(void) {
// fill sectors 17-18 (MAD2 NDEF) with pattern
for (int sec = 17; sec <= 18; sec++) {
uint8_t first = mfFirstBlockOfSector(sec);
for (int b = 0; b < 3; b++) {
uint8_t *block = &g_mock_card[(first + b) * 16];
for (int i = 0; i < 16; i++)
for (int i = 0; i < 16; i++) {
block[i] = (uint8_t)((sec << 4) | (b << 2) | (i & 0x03));
}
}
uint8_t *trailer = &g_mock_card[(first + 3) * 16];
memset(trailer, 0xFF, 16);
@@ -180,8 +198,8 @@ static mad_ops_t make_mock_ops(void) {
// --- Test cases ---
static bool test_struct_sizes(bool verbose) {
PrintAndLogEx(INFO, " struct sizes...");
static bool test_struct_sizes(void) {
PrintAndLogEx(INFO, "Struct sizes........................ " NOLF);
ASSERT_EQ("mad1_t", 32, sizeof(mad1_t));
ASSERT_EQ("mad2_t", 48, sizeof(mad2_t));
@@ -189,12 +207,12 @@ static bool test_struct_sizes(bool verbose) {
ASSERT_EQ("mad2_sector_t", 64, sizeof(mad2_sector_t));
ASSERT_EQ("mf_trailer_t", 16, sizeof(mf_trailer_t));
if (verbose) PrintAndLogEx(SUCCESS, " " _GREEN_("passed"));
PrintAndLogEx(NORMAL, "( " _GREEN_("ok") " )");
return true;
}
static bool test_mad1_crc_valid(bool verbose) {
PrintAndLogEx(INFO, " MAD1 CRC valid...");
static bool test_mad1_crc_valid(void) {
PrintAndLogEx(INFO, "MAD1 CRC valid...................... " NOLF);
mad1_sector_t s;
build_test_mad1(&s);
@@ -203,12 +221,12 @@ static bool test_mad1_crc_valid(bool verbose) {
ASSERT_EQ("MADCheck on valid sector", PM3_SUCCESS, res);
ASSERT_EQ("should report MAD2 (v2)", true, haveMAD2);
if (verbose) PrintAndLogEx(SUCCESS, " " _GREEN_("passed"));
PrintAndLogEx(NORMAL, "( " _GREEN_("ok") " )");
return true;
}
static bool test_mad1_crc_corrupt(bool verbose) {
PrintAndLogEx(INFO, " MAD1 CRC corrupt...");
PrintAndLogEx(INFO, "MAD1 CRC corrupt.................... " NOLF);
mad1_sector_t s;
build_test_mad1(&s);
s.mad.crc ^= 0xFF;
@@ -219,12 +237,12 @@ static bool test_mad1_crc_corrupt(bool verbose) {
ASSERT_TRUE("MADCheck should fail on corrupt CRC", res != PM3_SUCCESS);
if (verbose) PrintAndLogEx(SUCCESS, " " _GREEN_("passed"));
PrintAndLogEx(NORMAL, "( " _GREEN_("ok") " )");
return true;
}
static bool test_mad1_decode_entries(bool verbose) {
PrintAndLogEx(INFO, " MAD1 decode entries...");
static bool test_mad1_decode_entries(void) {
PrintAndLogEx(INFO, "MAD1 decode entries................. " NOLF);
mad1_sector_t s;
build_test_mad1(&s);
@@ -248,12 +266,13 @@ static bool test_mad1_decode_entries(bool verbose) {
ASSERT_EQ_HEX("free AID", 0x0000, list.entries[i].aid);
}
if (verbose) PrintAndLogEx(SUCCESS, " " _GREEN_("passed"));
PrintAndLogEx(NORMAL, "( " _GREEN_("ok") " )");
return true;
}
static bool test_mad2_decode(bool verbose) {
PrintAndLogEx(INFO, " MAD2 decode with MAD1 v2...");
static bool test_mad2_decode(void) {
PrintAndLogEx(INFO, "MAD2 decode with MAD1 v2............ " NOLF);
mad1_sector_t s0;
build_test_mad1(&s0);
mad2_sector_t s16;
@@ -281,12 +300,13 @@ static bool test_mad2_decode(bool verbose) {
ASSERT_EQ_HEX("MAD2 entry 1 AID", 0xE103, list.entries[MAD1_NUM_AIDS + 1].aid);
ASSERT_EQ_HEX("MAD2 entry 2 free", 0x0000, list.entries[MAD1_NUM_AIDS + 2].aid);
if (verbose) PrintAndLogEx(SUCCESS, " " _GREEN_("passed"));
PrintAndLogEx(NORMAL, "( " _GREEN_("ok") " )");
return true;
}
static bool test_read_mad2_sectors(bool verbose) {
PrintAndLogEx(INFO, " read AID spanning MAD1+MAD2...");
static bool test_read_mad2_sectors(void) {
PrintAndLogEx(INFO, "Read AID spanning MAD1, MAD2........ " NOLF);
mock_card_init();
mad_ops_t ops = make_mock_ops();
@@ -305,12 +325,13 @@ static bool test_read_mad2_sectors(bool verbose) {
// verify first byte of sector 18 (at offset 144+48=192)
ASSERT_EQ("sector 18 byte 0", (uint8_t)((18 << 4) | 0), data[192]);
if (verbose) PrintAndLogEx(SUCCESS, " " _GREEN_("passed"));
PrintAndLogEx(NORMAL, "( " _GREEN_("ok") " )");
return true;
}
static bool test_read_full_content(bool verbose) {
PrintAndLogEx(INFO, " read full AID content...");
static bool test_read_full_content(void) {
PrintAndLogEx(INFO, "Read full AID content............... " NOLF);
mock_card_init();
mad_ops_t ops = make_mock_ops();
@@ -331,8 +352,7 @@ static bool test_read_full_content(bool verbose) {
for (int i = 0; i < 16; i++) {
uint8_t expected = (uint8_t)((sec << 4) | (b << 2) | (i & 0x03));
if (data[off] != expected) {
PrintAndLogEx(FAILED, " byte %zu: expected 0x%02X, got 0x%02X (sector %d block %d byte %d)",
off, expected, data[off], sec, b, i);
PrintAndLogEx(FAILED, " byte %zu: expected 0x%02X, got 0x%02X (sector %d block %d byte %d)", off, expected, data[off], sec, b, i);
return false;
}
off++;
@@ -345,8 +365,7 @@ static bool test_read_full_content(bool verbose) {
for (int i = 0; i < 16; i++) {
uint8_t expected = (uint8_t)((sec << 4) | (b << 2) | (i & 0x03));
if (data[off] != expected) {
PrintAndLogEx(FAILED, " byte %zu: expected 0x%02X, got 0x%02X (sector %d block %d byte %d)",
off, expected, data[off], sec, b, i);
PrintAndLogEx(FAILED, " byte %zu: expected 0x%02X, got 0x%02X (sector %d block %d byte %d)", off, expected, data[off], sec, b, i);
return false;
}
off++;
@@ -355,12 +374,13 @@ static bool test_read_full_content(bool verbose) {
}
ASSERT_EQ("total bytes checked", 240, (int)off);
if (verbose) PrintAndLogEx(SUCCESS, " " _GREEN_("passed"));
PrintAndLogEx(NORMAL, "( " _GREEN_("ok") " )");
return true;
}
static bool test_read_single_sector_aid(bool verbose) {
PrintAndLogEx(INFO, " read single-sector AID...");
static bool test_read_single_sector_aid(void) {
PrintAndLogEx(INFO, "Read single-sector AID.............. " NOLF);
mock_card_init();
mad_ops_t ops = make_mock_ops();
@@ -384,20 +404,21 @@ static bool test_read_single_sector_aid(bool verbose) {
}
}
if (verbose) PrintAndLogEx(SUCCESS, " " _GREEN_("passed"));
PrintAndLogEx(NORMAL, "( " _GREEN_("ok") " )");
return true;
}
static bool test_write_full_roundtrip(bool verbose) {
PrintAndLogEx(INFO, " write full round-trip...");
static bool test_write_full_roundtrip(void) {
PrintAndLogEx(INFO, "Write full round-trip............... " NOLF);
mock_card_init();
mad_ops_t ops = make_mock_ops();
// write exactly 240 bytes (full NDEF capacity: MAD1 + MAD2)
uint8_t wdata[240];
for (int i = 0; i < 240; i++)
for (int i = 0; i < 240; i++) {
wdata[i] = (uint8_t)(i ^ 0x55);
}
int res = mad_app_write(&ops, 0xE103, false, false, wdata, sizeof(wdata));
@@ -419,12 +440,12 @@ static bool test_write_full_roundtrip(bool verbose) {
}
}
if (verbose) PrintAndLogEx(SUCCESS, " " _GREEN_("passed"));
PrintAndLogEx(NORMAL, "( " _GREEN_("ok") " )");
return true;
}
static bool test_write_partial_zero_pads(bool verbose) {
PrintAndLogEx(INFO, " write partial zero-pads remainder...");
static bool test_write_partial_zero_pads(void) {
PrintAndLogEx(INFO, "Write partial zero-pads remainder... " NOLF);
mock_card_init();
mad_ops_t ops = make_mock_ops();
@@ -434,7 +455,6 @@ static bool test_write_partial_zero_pads(bool verbose) {
uint8_t wdata[50];
memset(wdata, 0xAB, sizeof(wdata));
int res = mad_app_write(&ops, 0xE103, false, false, wdata, sizeof(wdata));
ASSERT_EQ("write return", PM3_SUCCESS, res);
@@ -465,20 +485,21 @@ static bool test_write_partial_zero_pads(bool verbose) {
}
}
if (verbose) PrintAndLogEx(SUCCESS, " " _GREEN_("passed"));
PrintAndLogEx(NORMAL, "( " _GREEN_("ok") " )");
return true;
}
static bool test_verify_match(bool verbose) {
PrintAndLogEx(INFO, " verify match...");
static bool test_verify_match(void) {
PrintAndLogEx(INFO, "Verify match........................ " NOLF);
mock_card_init();
mad_ops_t ops = make_mock_ops();
// write known data, then verify with same data
uint8_t wdata[240];
for (int i = 0; i < 240; i++)
for (int i = 0; i < 240; i++) {
wdata[i] = (uint8_t)(i * 3);
}
int res = mad_app_write(&ops, 0xE103, false, false, wdata, sizeof(wdata));
@@ -489,21 +510,22 @@ static bool test_verify_match(bool verbose) {
ASSERT_EQ("verify should pass", PM3_SUCCESS, res);
if (verbose) PrintAndLogEx(SUCCESS, " " _GREEN_("passed"));
PrintAndLogEx(NORMAL, "( " _GREEN_("ok") " )");
return true;
}
static bool test_verify_mismatch(bool verbose) {
PrintAndLogEx(INFO, " verify mismatch...");
PrintAndLogEx(INFO, "Verify mismatch..................... " NOLF);
mock_card_init();
mad_ops_t ops = make_mock_ops();
ops.verbose = verbose;
// write known data
uint8_t wdata[240];
for (int i = 0; i < 240; i++)
for (int i = 0; i < 240; i++) {
wdata[i] = (uint8_t)(i * 3);
}
int res = mad_app_write(&ops, 0xE103, false, false, wdata, sizeof(wdata));
@@ -514,17 +536,16 @@ static bool test_verify_mismatch(bool verbose) {
memcpy(bad, wdata, sizeof(bad));
bad[72] ^= 0xFF; // corrupt byte in sector 2
res = mad_app_verify(&ops, 0xE103, false, false, bad, sizeof(bad));
ASSERT_TRUE("verify should detect mismatch", res != PM3_SUCCESS);
if (verbose) PrintAndLogEx(SUCCESS, " " _GREEN_("passed"));
PrintAndLogEx(NORMAL, "( " _GREEN_("ok") " )");
return true;
}
static bool test_write_overflow(bool verbose) {
PrintAndLogEx(INFO, " write overflow...");
PrintAndLogEx(INFO, "Write overflow...................... " NOLF);
mock_card_init();
mad_ops_t ops = make_mock_ops();
ops.verbose = verbose;
@@ -536,12 +557,13 @@ static bool test_write_overflow(bool verbose) {
ASSERT_TRUE("should reject 241 bytes into 240-byte capacity", res != PM3_SUCCESS);
if (verbose) PrintAndLogEx(SUCCESS, " " _GREEN_("passed"));
PrintAndLogEx(NORMAL, "( " _GREEN_("ok") " )");
return true;
}
static bool test_aid_not_found(bool verbose) {
PrintAndLogEx(INFO, " AID not found...");
PrintAndLogEx(INFO, "AID not found....................... " NOLF);
mock_card_init();
mad_ops_t ops = make_mock_ops();
ops.verbose = verbose;
@@ -554,12 +576,13 @@ static bool test_aid_not_found(bool verbose) {
ASSERT_EQ("should succeed with 0 bytes", PM3_SUCCESS, res);
ASSERT_EQ("datalen should be 0", 0, (int)datalen);
if (verbose) PrintAndLogEx(SUCCESS, " " _GREEN_("passed"));
PrintAndLogEx(NORMAL, "( " _GREEN_("ok") " )");
return true;
}
static bool test_write_does_not_touch_trailer(bool verbose) {
PrintAndLogEx(INFO, " write does not touch trailer...");
static bool test_write_does_not_touch_trailer(void) {
PrintAndLogEx(INFO, "Write does not touch trailer........ " NOLF);
mock_card_init();
mad_ops_t ops = make_mock_ops();
@@ -576,23 +599,25 @@ static bool test_write_does_not_touch_trailer(bool verbose) {
ASSERT_EQ("write return", PM3_SUCCESS, res);
// trailer should be unchanged
ASSERT_TRUE("sector 1 trailer unchanged",
memcmp(trailer_before, &g_mock_card[(first + 3) * 16], 16) == 0);
ASSERT_TRUE("sector 1 trailer unchanged", memcmp(trailer_before, &g_mock_card[(first + 3) * 16], 16) == 0);
// also check sector 2 trailer
uint8_t first2 = mfFirstBlockOfSector(2);
uint8_t trailer2[16];
memcpy(trailer2, &g_mock_card[(first2 + 3) * 16], 16);
uint8_t expected_trailer[16];
memset(expected_trailer, 0xFF, 16);
ASSERT_TRUE("sector 2 trailer unchanged", memcmp(trailer2, expected_trailer, 16) == 0);
if (verbose) PrintAndLogEx(SUCCESS, " " _GREEN_("passed"));
PrintAndLogEx(NORMAL, "( " _GREEN_("ok") " )");
return true;
}
static bool test_mad_sector_untouched_after_write(bool verbose) {
PrintAndLogEx(INFO, " MAD sector untouched after write...");
static bool test_mad_sector_untouched_after_write(void) {
PrintAndLogEx(INFO, "MAD sector untouched after write.... " NOLF);
mock_card_init();
mad_ops_t ops = make_mock_ops();
@@ -608,15 +633,14 @@ static bool test_mad_sector_untouched_after_write(bool verbose) {
ASSERT_EQ("write return", PM3_SUCCESS, res);
// MAD sector 0 should be completely unchanged
ASSERT_TRUE("MAD sector 0 unchanged after write",
memcmp(mad_before, &g_mock_card[0], 64) == 0);
ASSERT_TRUE("MAD sector 0 unchanged after write", memcmp(mad_before, &g_mock_card[0], 64) == 0);
if (verbose) PrintAndLogEx(SUCCESS, " " _GREEN_("passed"));
PrintAndLogEx(NORMAL, "( " _GREEN_("ok") " )");
return true;
}
static bool test_encode_decode_roundtrip(bool verbose) {
PrintAndLogEx(INFO, " encode-decode round-trip...");
static bool test_encode_decode_roundtrip(void) {
PrintAndLogEx(INFO, "Encode - Decode round-trip.......... " NOLF);
// simulate CmdMADEncode: fill sector_aids[], build struct, decode
uint16_t sector_aids[40] = {0};
@@ -629,9 +653,13 @@ static bool test_encode_decode_roundtrip(bool verbose) {
// build MAD1 (same logic as CmdMADEncode)
mad1_sector_t s0;
memset(&s0, 0, sizeof(s0));
s0.mad.info = 0x00;
for (int i = 0; i < MAD1_NUM_AIDS; i++)
for (int i = 0; i < MAD1_NUM_AIDS; i++) {
s0.mad.aid[i] = sector_aids[i + 1];
}
s0.mad.crc = CRC8Mad((uint8_t *)&s0.mad.info, sizeof(mad1_t) - 1);
memcpy(s0.trailer.key_a, g_mifare_mad_key, MIFARE_KEY_SIZE);
s0.trailer.access[0] = 0x78;
@@ -643,11 +671,13 @@ static bool test_encode_decode_roundtrip(bool verbose) {
// verify CRC is valid
bool haveMAD2 = false;
int res = MADCheck(&s0, NULL, false, &haveMAD2);
ASSERT_EQ("CRC on encoded MAD1", PM3_SUCCESS, res);
// decode and verify every entry
mad_entry_list_t list = {0};
res = MADDecode(&s0, NULL, &list, false, false);
ASSERT_EQ("decode return", PM3_SUCCESS, res);
ASSERT_EQ("entry count", MAD1_NUM_AIDS, (int)list.len);
@@ -661,17 +691,15 @@ static bool test_encode_decode_roundtrip(bool verbose) {
ASSERT_EQ("GPB version", 1, s0.trailer.gpb & 0x03);
// verify trailer keys match MAD defaults
ASSERT_TRUE("key A is MAD key",
memcmp(s0.trailer.key_a, g_mifare_mad_key, MIFARE_KEY_SIZE) == 0);
ASSERT_TRUE("key B is MAD key B",
memcmp(s0.trailer.key_b, g_mifare_mad_key_b, MIFARE_KEY_SIZE) == 0);
ASSERT_TRUE("key A is MAD key", memcmp(s0.trailer.key_a, g_mifare_mad_key, MIFARE_KEY_SIZE) == 0);
ASSERT_TRUE("key B is MAD key B", memcmp(s0.trailer.key_b, g_mifare_mad_key_b, MIFARE_KEY_SIZE) == 0);
if (verbose) PrintAndLogEx(SUCCESS, " " _GREEN_("passed"));
PrintAndLogEx(NORMAL, "( " _GREEN_("ok") " )");
return true;
}
static bool test_encode_decode_mad2_roundtrip(bool verbose) {
PrintAndLogEx(INFO, " encode-decode MAD2 round-trip...");
static bool test_encode_decode_mad2_roundtrip(void) {
PrintAndLogEx(INFO, "Encode - Decode MAD2 round-trip..... " NOLF);
uint16_t sector_aids[40] = {0};
sector_aids[1] = 0xE103; // MAD1 NDEF
@@ -686,9 +714,12 @@ static bool test_encode_decode_mad2_roundtrip(bool verbose) {
mad1_sector_t s0;
memset(&s0, 0, sizeof(s0));
s0.mad.info = 0x00;
for (int i = 0; i < MAD1_NUM_AIDS; i++)
for (int i = 0; i < MAD1_NUM_AIDS; i++) {
s0.mad.aid[i] = sector_aids[i + 1];
}
s0.mad.crc = CRC8Mad((uint8_t *)&s0.mad.info, sizeof(mad1_t) - 1);
memcpy(s0.trailer.key_a, g_mifare_mad_key, MIFARE_KEY_SIZE);
s0.trailer.access[0] = 0x78;
s0.trailer.access[1] = 0x77;
@@ -700,8 +731,11 @@ static bool test_encode_decode_mad2_roundtrip(bool verbose) {
mad2_sector_t s16;
memset(&s16, 0, sizeof(s16));
s16.mad.info = 0x00;
for (int i = 0; i < MAD2_NUM_AIDS; i++)
for (int i = 0; i < MAD2_NUM_AIDS; i++) {
s16.mad.aid[i] = sector_aids[i + 17];
}
s16.mad.crc = CRC8Mad((uint8_t *)&s16.mad.info, sizeof(mad2_t) - 1);
memcpy(s16.trailer.key_a, g_mifare_mad_key, MIFARE_KEY_SIZE);
s16.trailer.access[0] = 0x78;
@@ -734,15 +768,16 @@ static bool test_encode_decode_mad2_roundtrip(bool verbose) {
ASSERT_EQ_HEX("MAD2 AID", sector_aids[i + 17], list.entries[MAD1_NUM_AIDS + i].aid);
}
if (verbose) PrintAndLogEx(SUCCESS, " " _GREEN_("passed"));
PrintAndLogEx(NORMAL, "( " _GREEN_("ok") " )");
return true;
}
static bool test_mad2_crc_independent(bool verbose) {
PrintAndLogEx(INFO, " MAD2 CRC independent...");
PrintAndLogEx(INFO, "MAD2 CRC independent................ " NOLF);
mad1_sector_t s0;
build_test_mad1(&s0);
mad2_sector_t s16;
build_test_mad2(&s16);
@@ -768,12 +803,12 @@ static bool test_mad2_crc_independent(bool verbose) {
res = MADCheck(&s0_bad, &s16, verbose, &haveMAD2);
ASSERT_TRUE("should fail with corrupt MAD1 CRC", res != PM3_SUCCESS);
if (verbose) PrintAndLogEx(SUCCESS, " " _GREEN_("passed"));
PrintAndLogEx(NORMAL, "( " _GREEN_("ok") " )");
return true;
}
static bool test_encode_gpb_version(bool verbose) {
PrintAndLogEx(INFO, " encode GPB version...");
static bool test_encode_gpb_version(void) {
PrintAndLogEx(INFO, "Encode GPB version.................. " NOLF);
// MAD1-only: GPB should have version=1
mad1_sector_t s_v1;
@@ -784,7 +819,9 @@ static bool test_encode_gpb_version(bool verbose) {
memcpy(s_v1.trailer.key_a, g_mifare_mad_key, MIFARE_KEY_SIZE);
bool haveMAD2 = false;
int res = MADCheck(&s_v1, NULL, false, &haveMAD2);
ASSERT_EQ("v1 check", PM3_SUCCESS, res);
ASSERT_EQ("v1 no MAD2", false, haveMAD2);
ASSERT_EQ("GPB version bits", 1, s_v1.trailer.gpb & 0x03);
@@ -798,16 +835,17 @@ static bool test_encode_gpb_version(bool verbose) {
memcpy(s_v2.trailer.key_a, g_mifare_mad_key, MIFARE_KEY_SIZE);
res = MADCheck(&s_v2, NULL, false, &haveMAD2);
ASSERT_EQ("v2 check", PM3_SUCCESS, res);
ASSERT_EQ("v2 has MAD2", true, haveMAD2);
ASSERT_EQ("GPB version bits", 2, s_v2.trailer.gpb & 0x03);
if (verbose) PrintAndLogEx(SUCCESS, " " _GREEN_("passed"));
PrintAndLogEx(NORMAL, "( " _GREEN_("ok") " )");
return true;
}
static bool test_encode_sector_overlap(bool verbose) {
PrintAndLogEx(INFO, " encode sector overlap detection...");
static bool test_encode_sector_overlap(void) {
PrintAndLogEx(INFO, "Encode sector overlap detection..... " NOLF);
// two different AIDs assigned to the same sector
uint16_t sector_aids[40] = {0};
@@ -823,43 +861,45 @@ static bool test_encode_sector_overlap(bool verbose) {
bool no_overlap = (sector_aids[5] == 0);
ASSERT_TRUE("sector 5 is free", no_overlap);
if (verbose) PrintAndLogEx(SUCCESS, " " _GREEN_("passed"));
PrintAndLogEx(NORMAL, "( " _GREEN_("ok") " )");
return true;
}
// --- Aggregator ---
int exec_mad_test(bool verbose) {
PrintAndLogEx(INFO, "--- " _CYAN_("MAD regression tests") " ---");
PrintAndLogEx(INFO, "--------- " _CYAN_("MAD regression tests") " ------------");
bool ok = true;
if (!test_struct_sizes(verbose)) ok = false;
if (!test_mad1_crc_valid(verbose)) ok = false;
if (!test_mad1_crc_corrupt(verbose)) ok = false;
if (!test_mad1_decode_entries(verbose)) ok = false;
if (!test_mad2_decode(verbose)) ok = false;
if (!test_read_mad2_sectors(verbose)) ok = false;
if (!test_read_full_content(verbose)) ok = false;
if (!test_read_single_sector_aid(verbose)) ok = false;
if (!test_write_full_roundtrip(verbose)) ok = false;
if (!test_write_partial_zero_pads(verbose)) ok = false;
if (!test_verify_match(verbose)) ok = false;
if (!test_verify_mismatch(verbose)) ok = false;
if (!test_write_overflow(verbose)) ok = false;
if (!test_aid_not_found(verbose)) ok = false;
if (!test_write_does_not_touch_trailer(verbose)) ok = false;
if (!test_mad_sector_untouched_after_write(verbose)) ok = false;
if (!test_encode_decode_roundtrip(verbose)) ok = false;
if (!test_encode_decode_mad2_roundtrip(verbose)) ok = false;
if (!test_mad2_crc_independent(verbose)) ok = false;
if (!test_encode_gpb_version(verbose)) ok = false;
if (!test_encode_sector_overlap(verbose)) ok = false;
ok |= test_struct_sizes();
ok |= test_mad1_crc_valid();
ok |= test_mad1_crc_corrupt(verbose);
ok |= test_mad1_decode_entries();
ok |= test_mad2_decode();
ok |= test_read_mad2_sectors();
ok |= test_read_full_content();
ok |= test_read_single_sector_aid();
ok |= test_write_full_roundtrip();
ok |= test_write_partial_zero_pads();
ok |= test_verify_match();
ok |= test_verify_mismatch(verbose);
ok |= test_write_overflow(verbose);
ok |= test_aid_not_found(verbose);
ok |= test_write_does_not_touch_trailer();
ok |= test_mad_sector_untouched_after_write(verbose);
ok |= test_encode_decode_roundtrip();
ok |= test_encode_decode_mad2_roundtrip();
ok |= test_mad2_crc_independent(verbose);
ok |= test_encode_gpb_version();
ok |= test_encode_sector_overlap();
PrintAndLogEx(INFO, "----------------------------");
if (ok)
PrintAndLogEx(INFO, "-------------------------------------------");
if (ok) {
PrintAndLogEx(SUCCESS, "Tests ( " _GREEN_("ok") " )");
else
} else {
PrintAndLogEx(FAILED, "Tests ( " _RED_("fail") " )");
}
PrintAndLogEx(NORMAL, "");
return ok ? PM3_SUCCESS : PM3_ESOFT;
}