Skip to content

Commit

Permalink
Unit test sector description (#51)
Browse files Browse the repository at this point in the history
- Test number of blocks inside the sector
- Test block 0 sector's address
  • Loading branch information
abobija authored Sep 21, 2024
1 parent d07566f commit 85361dc
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 19 deletions.
49 changes: 34 additions & 15 deletions src/picc/rc522_mifare.c
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ static esp_err_t rc522_mifare_transceive(
return ret;
}

static esp_err_t rc522_mifare_number_of_sectors(rc522_picc_type_t type, uint8_t *out_result)
static esp_err_t rc522_mifare_get_number_of_sectors(rc522_picc_type_t type, uint8_t *out_result)
{
RC522_CHECK(rc522_mifare_type_is_classic_compatible(type) == false);
RC522_CHECK(out_result == NULL);
Expand Down Expand Up @@ -238,6 +238,36 @@ inline static uint8_t rc522_mifare_get_sector_index_by_block_address(uint8_t blo
}
}

inline static esp_err_t rc522_mifare_get_sector_number_of_blocks(uint8_t sector_index, uint8_t *out_result)
{
RC522_CHECK(sector_index > RC522_MIFARE_SECTOR_INDEX_MAX);
RC522_CHECK(out_result == NULL);

if (sector_index < 32) {
*out_result = 4;
return ESP_OK;
}

*out_result = 16;

return ESP_OK;
}

inline static esp_err_t rc522_mifare_get_sector_block_0_address(uint8_t sector_index, uint8_t *out_result)
{
RC522_CHECK(sector_index > RC522_MIFARE_SECTOR_INDEX_MAX);
RC522_CHECK(out_result == NULL);

if (sector_index < 32) {
*out_result = sector_index * 4;
return ESP_OK;
}

*out_result = 128 + (sector_index - 32) * 16;

return ESP_OK;
}

esp_err_t rc522_mifare_get_sector_desc(uint8_t sector_index, rc522_mifare_sector_desc_t *out_sector_desc)
{
RC522_CHECK(sector_index > RC522_MIFARE_SECTOR_INDEX_MAX);
Expand All @@ -247,19 +277,8 @@ esp_err_t rc522_mifare_get_sector_desc(uint8_t sector_index, rc522_mifare_sector
memset(&desc, 0, sizeof(desc));

desc.index = sector_index;

// Determine position and size of sector.
if (desc.index < 32) { // Sectors 0..31 has 4 blocks each
desc.number_of_blocks = 4;
desc.block_0_address = desc.index * 4;
}
else if (desc.index < 40) { // Sectors 32-39 has 16 blocks each
desc.number_of_blocks = 16;
desc.block_0_address = 128 + (desc.index - 32) * 16;
}
else {
return ESP_FAIL;
}
RC522_RETURN_ON_ERROR(rc522_mifare_get_sector_number_of_blocks(sector_index, &desc.number_of_blocks));
RC522_RETURN_ON_ERROR(rc522_mifare_get_sector_block_0_address(sector_index, &desc.block_0_address));

memcpy(out_sector_desc, &desc, sizeof(rc522_mifare_sector_desc_t));

Expand Down Expand Up @@ -342,7 +361,7 @@ esp_err_t rc522_mifare_get_desc(rc522_picc_t *picc, rc522_mifare_desc_t *out_mif
rc522_mifare_desc_t desc;
memset(&desc, 0, sizeof(desc));

RC522_RETURN_ON_ERROR(rc522_mifare_number_of_sectors(picc->type, &desc.number_of_sectors));
RC522_RETURN_ON_ERROR(rc522_mifare_get_number_of_sectors(picc->type, &desc.number_of_sectors));

memcpy(out_mifare_desc, &desc, sizeof(rc522_mifare_desc_t));

Expand Down
69 changes: 65 additions & 4 deletions test/main/picc/test_mifare.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,67 @@

#include "picc/rc522_mifare.c"

TEST_CASE("test_Sector_desc_contains_correct_info", "[mifare]")
{
rc522_mifare_sector_desc_t desc;

TEST_ASSERT_EQUAL(ESP_OK, rc522_mifare_get_sector_desc(0, &desc));
TEST_ASSERT_EQUAL(0, desc.index);
TEST_ASSERT_EQUAL(0, desc.block_0_address);
TEST_ASSERT_EQUAL(4, desc.number_of_blocks);

TEST_ASSERT_EQUAL(ESP_OK, rc522_mifare_get_sector_desc(31, &desc));
TEST_ASSERT_EQUAL(31, desc.index);
TEST_ASSERT_EQUAL(124, desc.block_0_address);
TEST_ASSERT_EQUAL(4, desc.number_of_blocks);

TEST_ASSERT_EQUAL(ESP_OK, rc522_mifare_get_sector_desc(32, &desc));
TEST_ASSERT_EQUAL(32, desc.index);
TEST_ASSERT_EQUAL(128, desc.block_0_address);
TEST_ASSERT_EQUAL(16, desc.number_of_blocks);

TEST_ASSERT_EQUAL(ESP_OK, rc522_mifare_get_sector_desc(33, &desc));
TEST_ASSERT_EQUAL(33, desc.index);
TEST_ASSERT_EQUAL(144, desc.block_0_address);
TEST_ASSERT_EQUAL(16, desc.number_of_blocks);
}

TEST_CASE("test_Sector_block_0_address", "[mifare]")
{
uint8_t block_address = 0;

for (uint8_t sector_index = 0; sector_index < 31; sector_index++) {
TEST_ASSERT_EQUAL(ESP_OK, rc522_mifare_get_sector_block_0_address(sector_index, &block_address));
TEST_ASSERT_EQUAL(sector_index * 4, block_address);
}

for (uint8_t sector_index = 32; sector_index < 40; sector_index++) {
TEST_ASSERT_EQUAL(ESP_OK, rc522_mifare_get_sector_block_0_address(sector_index, &block_address));
TEST_ASSERT_EQUAL(128 + (sector_index - 32) * 16, block_address);
}
}

TEST_CASE("test_Sector_number_of_blocks", "[mifare]")
{
uint8_t number_of_blocks = 0;

for (uint8_t sector_index = 0; sector_index < 31; sector_index++) {
TEST_ASSERT_EQUAL(ESP_OK, rc522_mifare_get_sector_number_of_blocks(sector_index, &number_of_blocks));
TEST_ASSERT_EQUAL(4, number_of_blocks);
}

for (uint8_t sector_index = 32; sector_index < 40; sector_index++) {
TEST_ASSERT_EQUAL(ESP_OK, rc522_mifare_get_sector_number_of_blocks(sector_index, &number_of_blocks));
TEST_ASSERT_EQUAL(16, number_of_blocks);
}
}

TEST_CASE("test_Sector_number_of_blocks_returns_error_on_invalid_sector_index", "[mifare]")
{
uint8_t number_of_blocks;
TEST_ASSERT_NOT_EQUAL(ESP_OK, rc522_mifare_get_sector_number_of_blocks(40, &number_of_blocks));
}

TEST_CASE("test_Sector_number_by_block_address", "[mifare]")
{
uint8_t addr = 0;
Expand Down Expand Up @@ -41,28 +102,28 @@ TEST_CASE("test_Sector_number_by_block_address", "[mifare]")
TEST_CASE("test_MIFAREMini_has_5_sectors", "[mifare]")
{
uint8_t result;
TEST_ASSERT_EQUAL(ESP_OK, rc522_mifare_number_of_sectors(RC522_PICC_TYPE_MIFARE_MINI, &result));
TEST_ASSERT_EQUAL(ESP_OK, rc522_mifare_get_number_of_sectors(RC522_PICC_TYPE_MIFARE_MINI, &result));
TEST_ASSERT_EQUAL_UINT8(5, result);
}

TEST_CASE("test_MIFARE1K_has_16_sectors", "[mifare]")
{
uint8_t result;
TEST_ASSERT_EQUAL(ESP_OK, rc522_mifare_number_of_sectors(RC522_PICC_TYPE_MIFARE_1K, &result));
TEST_ASSERT_EQUAL(ESP_OK, rc522_mifare_get_number_of_sectors(RC522_PICC_TYPE_MIFARE_1K, &result));
TEST_ASSERT_EQUAL_UINT8(16, result);
}

TEST_CASE("test_MIFARE4K_has_40_sectors", "[mifare]")
{
uint8_t result;
TEST_ASSERT_EQUAL(ESP_OK, rc522_mifare_number_of_sectors(RC522_PICC_TYPE_MIFARE_4K, &result));
TEST_ASSERT_EQUAL(ESP_OK, rc522_mifare_get_number_of_sectors(RC522_PICC_TYPE_MIFARE_4K, &result));
TEST_ASSERT_EQUAL_UINT8(40, result);
}

TEST_CASE("test_Error_sector_numbers_for_no_MIFARE_picc", "[mifare]")
{
uint8_t result;
TEST_ASSERT_NOT_EQUAL(ESP_OK, rc522_mifare_number_of_sectors(RC522_PICC_TYPE_UNKNOWN, &result));
TEST_ASSERT_NOT_EQUAL(ESP_OK, rc522_mifare_get_number_of_sectors(RC522_PICC_TYPE_UNKNOWN, &result));
}

TEST_CASE("test_MIFARE1K_is_classic_compatible", "[mifare]")
Expand Down

0 comments on commit 85361dc

Please sign in to comment.