Add unit tests for mad_read() and mad_write().

This commit is contained in:
Romain Tartiere 2010-07-01 14:10:09 +00:00
parent db0dd52339
commit 92ae6036d2
3 changed files with 179 additions and 8 deletions

View file

@ -26,6 +26,7 @@ test_mad_la_SOURCES = test_mad.c
test_mad_la_LIBADD = $(top_builddir)/libfreefare/libfreefare.la
test_mifare_classic_la_SOURCES = test_mifare_classic.c \
test_mifare_classic_mad.c \
mifare_classic_fixture.c \
mifare_classic_fixture.h
test_mifare_classic_la_LIBADD = $(top_builddir)/libfreefare/libfreefare.la

View file

@ -98,14 +98,6 @@ test_mifare_classic_get_trailer_permission (void)
cut_assert_equal_int (0, mifare_classic_get_trailer_block_permission(tag, 0x07, MCAB_WRITE_KEYB, MFC_KEY_B), cut_message ("Wrong permission"));
}
void
test_mifare_classic_read_mad (void)
{
cut_omit ("A blank MIFARE Classic does not have a MAD. This test has to be moved in an appropriate test case.");
Mad mad = mad_read (tag);
cut_assert_not_null (mad, cut_message ("mad_read() failed"));
}
void
test_mifare_classic_format (void)
{

View file

@ -0,0 +1,178 @@
/*-
* Copyright (C) 2010, Romain Tartiere.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
* $Id: mifare_ultralight_fixture.c 264 2010-04-26 09:11:28Z romain.tartiere $
*/
#include <cutter.h>
#include <freefare.h>
#include "mifare_classic_fixture.h"
void
test_mifare_classic_mad (void)
{
MifareClassicKey key_a_transport = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
MifareClassicKey key_b_sector_00 = { 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff };
MifareClassicKey key_b_sector_10 = { 0x1a, 0x98, 0x2c, 0x7e, 0x45 ,0x9a };
MifareClassicBlock tb;
Mad mad;
int res;
/* __ __ _ ___ _
* | \/ | /_\ | \__ _/ |
* | |\/| |/ _ \| |) \ V / |
* |_| |_/_/ \_\___/ \_/|_|
*/
mad = mad_new (1);
cut_assert_not_null (mad, cut_message ("mad_new() failed"));
// Prepare sector 0x00 for writing a MAD.
res = mifare_classic_authenticate (tag, 0x00, key_a_transport, MFC_KEY_A);
cut_assert_equal_int (0, res, cut_message ("mifare_classic_authenticate() failed"));
mifare_classic_trailer_block (&tb, key_a_transport, 00, 00, 00, 06, 0x00, key_b_sector_00);
res = mifare_classic_write (tag, 0x03, tb);
cut_assert_equal_int (0, res, cut_message ("mifare_classic_write() failed"));
// Write the empty MAD
res = mad_write (tag, mad, key_b_sector_00, NULL);
cut_assert_equal_int (0, res, cut_message ("mad_write() failed"));
// Check the empty MAD
MifareClassicBlock ref_01 = { 0xce, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
MifareClassicBlock ref_02 = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
MifareClassicBlock data;
res = mifare_classic_authenticate (tag, 0x01, mad_public_key_a, MFC_KEY_A);
cut_assert_equal_int (0, res, cut_message ("mifare_classic_authenticate() failed"));
res = mifare_classic_read (tag, 0x01, &data);
cut_assert_equal_int (0, res, cut_message ("mad_read() failed"));
cut_assert_equal_memory (ref_01, sizeof (ref_01), data, sizeof (data), cut_message ("Wrong data"));
res = mifare_classic_read (tag, 0x02, &data);
cut_assert_equal_int (0, res, cut_message ("mad_read() failed"));
cut_assert_equal_memory (ref_02, sizeof (ref_02), data, sizeof (data), cut_message ("Wrong data"));
Mad mad2 = mad_read (tag);
cut_assert_not_null (mad2, cut_message ("mad_read() failed"));
cut_assert_equal_memory (mad, sizeof (mad), mad2, sizeof (mad2), cut_message ("Wrong MAD"));
mad_free (mad);
mad_free (mad2);
// Revert to the transport configuration
res = mifare_classic_authenticate (tag, 0x00, key_b_sector_00, MFC_KEY_B);
cut_assert_equal_int (0, res, cut_message ("mifare_classic_authenticate() failed"));
res = mifare_classic_format_sector (tag, 0x00);
cut_assert_equal_int (0, res, cut_message ("mifare_classic_format_sector() failed"));
/* __ __ _ ___ ___
* | \/ | /_\ | \__ _|_ )
* | |\/| |/ _ \| |) \ V // /
* |_| |_/_/ \_\___/ \_//___|
*/
if (freefare_get_tag_type (tag) != CLASSIC_4K) {
cut_omit ("MADv2 requires a MIFARE Classic 4K to be tested");
}
mad = mad_new (2);
cut_assert_not_null (mad, cut_message ("mad_new() failed"));
// Prepare sector 0x00 for writing a MAD.
res = mifare_classic_authenticate (tag, 0x00, key_a_transport, MFC_KEY_A);
cut_assert_equal_int (0, res, cut_message ("mifare_classic_authenticate() failed"));
mifare_classic_trailer_block (&tb, key_a_transport, 00, 00, 00, 06, 0x00, key_b_sector_00);
res = mifare_classic_write (tag, 0x03, tb);
cut_assert_equal_int (0, res, cut_message ("mifare_classic_write() failed"));
// Prepare sector 0x10 for writing a MAD.
res = mifare_classic_authenticate (tag, 0x40, key_a_transport, MFC_KEY_A);
cut_assert_equal_int (0, res, cut_message ("mifare_classic_authenticate() failed"));
mifare_classic_trailer_block (&tb, key_a_transport, 00, 00, 00, 06, 0x00, key_b_sector_10);
res = mifare_classic_write (tag, 0x43, tb);
cut_assert_equal_int (0, res, cut_message ("mifare_classic_write() failed"));
// Write the empty MAD
res = mad_write (tag, mad, key_b_sector_00, key_b_sector_10);
cut_assert_equal_int (0, res, cut_message ("mad_write() failed"));
// Check the empty MAD
res = mifare_classic_authenticate (tag, 0x01, mad_public_key_a, MFC_KEY_A);
cut_assert_equal_int (0, res, cut_message ("mifare_classic_authenticate() failed"));
res = mifare_classic_read (tag, 0x01, &data);
cut_assert_equal_int (0, res, cut_message ("mad_read() failed"));
cut_assert_equal_memory (ref_01, sizeof (ref_01), data, sizeof (data), cut_message ("Wrong data"));
res = mifare_classic_read (tag, 0x02, &data);
cut_assert_equal_int (0, res, cut_message ("mad_read() failed"));
cut_assert_equal_memory (ref_02, sizeof (ref_02), data, sizeof (data), cut_message ("Wrong data"));
MifareClassicBlock ref_40 = { 0x16, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
MifareClassicBlock ref_41 = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
MifareClassicBlock ref_42 = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
res = mifare_classic_authenticate (tag, 0x40, mad_public_key_a, MFC_KEY_A);
cut_assert_equal_int (0, res, cut_message ("mifare_classic_authenticate() failed"));
res = mifare_classic_read (tag, 0x40, &data);
cut_assert_equal_int (0, res, cut_message ("mad_read() failed"));
cut_assert_equal_memory (ref_40, sizeof (ref_01), data, sizeof (data), cut_message ("Wrong data"));
res = mifare_classic_read (tag, 0x41, &data);
cut_assert_equal_int (0, res, cut_message ("mad_read() failed"));
cut_assert_equal_memory (ref_41, sizeof (ref_02), data, sizeof (data), cut_message ("Wrong data"));
res = mifare_classic_read (tag, 0x42, &data);
cut_assert_equal_int (0, res, cut_message ("mad_read() failed"));
cut_assert_equal_memory (ref_42, sizeof (ref_02), data, sizeof (data), cut_message ("Wrong data"));
mad2 = mad_read (tag);
cut_assert_not_null (mad2, cut_message ("mad_read() failed"));
cut_assert_equal_memory (mad, sizeof (mad), mad2, sizeof (mad2), cut_message ("Wrong MAD"));
mad_free (mad);
mad_free (mad2);
// Revert to the transport configuration
res = mifare_classic_authenticate (tag, 0x00, key_b_sector_00, MFC_KEY_B);
cut_assert_equal_int (0, res, cut_message ("mifare_classic_authenticate() failed"));
res = mifare_classic_format_sector (tag, 0x00);
cut_assert_equal_int (0, res, cut_message ("mifare_classic_format_sector() failed"));
res = mifare_classic_authenticate (tag, 0x40, key_b_sector_10, MFC_KEY_B);
cut_assert_equal_int (0, res, cut_message ("mifare_classic_authenticate() failed"));
res = mifare_classic_format_sector (tag, 0x40);
cut_assert_equal_int (0, res, cut_message ("mifare_classic_format_sector() failed"));
}