Add support for MIFARE UltraLight Tags.
Many thanks to Johann Dantant from SpringCard for giving me UltraLight cards. While here, fix a few other problems in the autostuff.
This commit is contained in:
parent
cdf4404dd9
commit
ef081454c7
8 changed files with 544 additions and 7 deletions
|
|
@ -15,24 +15,33 @@ noinst_LTLIBRARIES = \
|
|||
test_mad.la \
|
||||
test_mifare_classic.la \
|
||||
test_mifare_classic_create_trailer_block.la \
|
||||
test_mifare_classic_application.la
|
||||
test_mifare_classic_application.la \
|
||||
test_mifare_ultralight.la
|
||||
|
||||
AM_LDFLAGS = -module -rpath $(libdir) -avoid-version -no-undefined
|
||||
|
||||
test_mad_la_SOURCES = test_mad.c
|
||||
test_mad_la_LIBADD = $(top_srcdir)/libfreefare/libfreefare.la
|
||||
test_mad_la_LIBADD = $(top_builddir)/libfreefare/libfreefare.la
|
||||
|
||||
test_mifare_classic_la_SOURCES = test_mifare_classic.c \
|
||||
mifare_classic_fixture.c
|
||||
test_mifare_classic_la_LIBADD = $(top_srcdir)/libfreefare/libfreefare.la
|
||||
mifare_classic_fixture.c \
|
||||
mifare_classic_fixture.h
|
||||
test_mifare_classic_la_LIBADD = $(top_builddir)/libfreefare/libfreefare.la
|
||||
|
||||
test_mifare_classic_create_trailer_block_la_SOURCES = test_mifare_classic_create_trailer_block.c
|
||||
test_mifare_classic_create_trailer_block_la_LIBADD = $(top_srcdir)/libfreefare/libfreefare.la
|
||||
test_mifare_classic_create_trailer_block_la_LIBADD = $(top_builddir)/libfreefare/libfreefare.la
|
||||
|
||||
test_mifare_classic_application_la_SOURCES = test_mifare_classic_application.c
|
||||
test_mifare_classic_application_la_LIBADD = $(top_srcdir)/libfreefare/libfreefare.la
|
||||
test_mifare_classic_application_la_LIBADD = $(top_builddir)/libfreefare/libfreefare.la
|
||||
|
||||
test_mifare_ultralight_la_SOURCES = test_mifare_ultralight.c \
|
||||
mifare_ultralight_fixture.c \
|
||||
mifare_ultralight_fixture.h
|
||||
test_mifare_ultralight_la_LIBADD = $(top_builddir)/libfreefare/libfreefare.la
|
||||
|
||||
echo-cutter:
|
||||
@echo $(CUTTER)
|
||||
|
||||
EXTRA_DIST = run-test.sh
|
||||
|
||||
endif
|
||||
|
|
|
|||
58
test/mifare_ultralight_fixture.c
Normal file
58
test/mifare_ultralight_fixture.c
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
/*-
|
||||
* 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$
|
||||
*/
|
||||
|
||||
#include <cutter.h>
|
||||
#include <freefare.h>
|
||||
|
||||
static nfc_device_t *device = NULL;
|
||||
static MifareUltralightTag *tags = NULL;
|
||||
MifareUltralightTag tag = NULL;
|
||||
|
||||
void
|
||||
setup ()
|
||||
{
|
||||
int res;
|
||||
|
||||
device = nfc_connect (NULL);
|
||||
cut_assert_not_null (device, "No device found");
|
||||
|
||||
tags = mifare_ultralight_get_tags (device);
|
||||
cut_assert_not_null (tags ,"Error enumerating NFC tags");
|
||||
|
||||
cut_assert_not_null (tags[0], "No MIFARE CLassic tag on NFC device");
|
||||
|
||||
tag = tags[0];
|
||||
|
||||
res = mifare_ultralight_connect (tag);
|
||||
cut_assert_equal_int (0, res);
|
||||
}
|
||||
|
||||
void
|
||||
teardown ()
|
||||
{
|
||||
if (tag)
|
||||
mifare_ultralight_disconnect (tag);
|
||||
|
||||
if (tags)
|
||||
mifare_ultralight_free_tags (tags);
|
||||
|
||||
if (device)
|
||||
nfc_disconnect (device);
|
||||
}
|
||||
|
||||
20
test/mifare_ultralight_fixture.h
Normal file
20
test/mifare_ultralight_fixture.h
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
/*-
|
||||
* 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$
|
||||
*/
|
||||
|
||||
extern MifareUltralightTag tag;
|
||||
157
test/test_mifare_ultralight.c
Normal file
157
test/test_mifare_ultralight.c
Normal file
|
|
@ -0,0 +1,157 @@
|
|||
/*-
|
||||
* 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$
|
||||
*/
|
||||
|
||||
#include <cutter.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <sys/limits.h>
|
||||
|
||||
#include <freefare.h>
|
||||
#include "freefare_internal.h"
|
||||
|
||||
#include "mifare_ultralight_fixture.h"
|
||||
|
||||
void
|
||||
test_mifare_ultralight_write (void)
|
||||
{
|
||||
int res;
|
||||
|
||||
MifareUltralightPage initial;
|
||||
MifareUltralightPage page;
|
||||
MifareUltralightPage payload1 = { 0x12, 0x34, 0x56, 0x78 };
|
||||
MifareUltralightPage payload2 = { 0xaa, 0x55, 0x00, 0xff };
|
||||
|
||||
MifareUltralightPageNumber n = 7;
|
||||
|
||||
/* Read and save current value (should be { 0x00 0x00 0x00 0x00 }) */
|
||||
res = mifare_ultralight_read (tag, n, &initial);
|
||||
cut_assert_equal_int (0, res, "mifare_ultralight_read failed");
|
||||
|
||||
/* Write payload1 */
|
||||
res = mifare_ultralight_write (tag, n, payload1);
|
||||
cut_assert_equal_int (0, res, "mifare_ultralight_write failed");
|
||||
|
||||
/* Check it */
|
||||
res = mifare_ultralight_read (tag, n, &page);
|
||||
cut_assert_equal_int (0, res, "mifare_ultralight_read failed");
|
||||
cut_assert_equal_memory (payload1, sizeof (payload1), page, sizeof (page));
|
||||
|
||||
/* Write payload2 */
|
||||
res = mifare_ultralight_write (tag, n, payload2);
|
||||
cut_assert_equal_int (0, res, "mifare_ultralight_write failed");
|
||||
|
||||
/* Check it */
|
||||
res = mifare_ultralight_read (tag, n, &page);
|
||||
cut_assert_equal_int (0, res, "mifare_ultralight_read failed");
|
||||
cut_assert_equal_memory (payload2, sizeof (payload2), page, sizeof (page));
|
||||
|
||||
/* Write initial data */
|
||||
res = mifare_ultralight_write (tag, n, initial);
|
||||
cut_assert_equal_int (0, res, "mifare_ultralight_write failed");
|
||||
|
||||
/* While here check it (no reason to fail since the rest of the test passed) */
|
||||
res = mifare_ultralight_read (tag, n, &page);
|
||||
cut_assert_equal_int (0, res, "mifare_ultralight_read failed");
|
||||
cut_assert_equal_memory (initial, sizeof (initial), page, sizeof (page));
|
||||
}
|
||||
|
||||
void
|
||||
test_mifare_ultralight_invalid_page (void)
|
||||
{
|
||||
int res;
|
||||
MifareUltralightPage page = { 0x00, 0x00, 0x00, 0x00 };
|
||||
|
||||
res = mifare_ultralight_read (tag, 16, &page);
|
||||
cut_assert_equal_int (-1, res);
|
||||
cut_assert_equal_int (EINVAL, errno);
|
||||
|
||||
res = mifare_ultralight_write (tag, 16, page);
|
||||
cut_assert_equal_int (-1, res);
|
||||
cut_assert_equal_int (EINVAL, errno);
|
||||
}
|
||||
|
||||
void
|
||||
test_mifare_ultralight_cache (void)
|
||||
{
|
||||
int res;
|
||||
MifareUltralightPage page;
|
||||
|
||||
res = mifare_ultralight_read (tag, 0, &page);
|
||||
cut_assert_equal_int (0, res, "mifare_ultralight_read() failed");
|
||||
|
||||
/* Check cached pages consistency */
|
||||
for (int i = 0; i <= 3; i++) {
|
||||
cut_assert_equal_int (1, tag->cached_pages[i], cut_message ("Wrong page cache value for tag->cached_pages[%d]", i));
|
||||
}
|
||||
for (int i = 4; i < MIFARE_ULTRALIGHT_PAGE_COUNT; i++) {
|
||||
cut_assert_equal_int (0, tag->cached_pages[i], cut_message ("Wrong page cache value for tag->cached_pages[%d]", i));
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
test_mifare_ultralight_cache_hit (void)
|
||||
{
|
||||
int res;
|
||||
|
||||
MifareUltralightPage page1;
|
||||
MifareUltralightPage page2;
|
||||
|
||||
res = mifare_ultralight_read (tag, 0, &page1);
|
||||
cut_assert_equal_int (0, res, "mifare_ultralight_read() failed");
|
||||
|
||||
res = mifare_ultralight_read (tag, 0, &page2);
|
||||
cut_assert_equal_int (0, res, "mifare_ultralight_read() failed");
|
||||
cut_assert_equal_memory (page1, sizeof (page1), page2, sizeof (page2));
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
test_mifare_ultralight_cache_wrap (void)
|
||||
{
|
||||
int res;
|
||||
MifareUltralightPage page;
|
||||
|
||||
res = mifare_ultralight_read (tag, 15, &page);
|
||||
cut_assert_equal_int (0, res, "mifare_ultralight_read() failed");
|
||||
|
||||
/* Check cached pages consistency */
|
||||
for (int i = 0; i <= 2; i++) {
|
||||
cut_assert_equal_int (1, tag->cached_pages[i], cut_message ("Wrong page cache value for tag->cached_pages[%d]", i));
|
||||
}
|
||||
for (int i = 3; i <= 14; i++) {
|
||||
cut_assert_equal_int (0, tag->cached_pages[i], cut_message ("Wrong page cache value for tag->cached_pages[%d]", i));
|
||||
}
|
||||
for (int i = 15; i < MIFARE_ULTRALIGHT_PAGE_COUNT; i++) {
|
||||
cut_assert_equal_int (1, tag->cached_pages[i], cut_message ("Wrong page cache value for tag->cached_pages[%d]", i));
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
test_mifare_ultralight_get_uid (void)
|
||||
{
|
||||
char *uid;
|
||||
|
||||
uid = mifare_ultralight_get_uid (tag);
|
||||
|
||||
cut_assert_not_null (uid);
|
||||
cut_assert_equal_int (14, strlen (uid));
|
||||
|
||||
free (uid);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue