Add support for TLV streams.
- New API functions: tlv_encode(), tlv_decode(); - Documentation (man page); - Unit tests.
This commit is contained in:
parent
644a21ad9a
commit
b8049f110d
6 changed files with 358 additions and 4 deletions
|
|
@ -16,7 +16,8 @@ noinst_LTLIBRARIES = \
|
|||
test_mifare_classic.la \
|
||||
test_mifare_classic_create_trailer_block.la \
|
||||
test_mifare_classic_application.la \
|
||||
test_mifare_ultralight.la
|
||||
test_mifare_ultralight.la \
|
||||
test_tlv.la
|
||||
|
||||
AM_LDFLAGS = -module -rpath $(libdir) -avoid-version -no-undefined
|
||||
|
||||
|
|
@ -39,6 +40,9 @@ test_mifare_ultralight_la_SOURCES = test_mifare_ultralight.c \
|
|||
mifare_ultralight_fixture.h
|
||||
test_mifare_ultralight_la_LIBADD = $(top_builddir)/libfreefare/libfreefare.la
|
||||
|
||||
test_tlv_la_SOURCES = test_tlv.c
|
||||
test_tlv_la_LIBADD = $(top_builddir)/libfreefare/libfreefare.la
|
||||
|
||||
echo-cutter:
|
||||
@echo $(CUTTER)
|
||||
|
||||
|
|
|
|||
131
test/test_tlv.c
Normal file
131
test/test_tlv.c
Normal file
|
|
@ -0,0 +1,131 @@
|
|||
/*-
|
||||
* Copyright (C) 2010, Romain Tartiere, Romuald Conty.
|
||||
*
|
||||
* 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>
|
||||
|
||||
const uint8_t shortdata[] = "elephant"; /* read: "elephant\0" = 9 bytes! */
|
||||
const uint8_t eshortdata[] = "\x03" "\x09" "elephant";
|
||||
|
||||
/*
|
||||
* Many thanks to Charles Baudelaire for helping me
|
||||
* test things and helping you realize your f**king
|
||||
* OS / compiler does not support UTF-8 ;-)
|
||||
*/
|
||||
const uint8_t longdata[] = "Dans une terre grasse et pleine d'escargots\n"
|
||||
"Je veux creuser moi-même une fosse profonde,\n"
|
||||
"Où je puisse à loisir étaler mes vieux os\n"
|
||||
"Et dormir dans l'oubli comme un requin dans l'onde.\n"
|
||||
"Je hais les testaments et je hais les tombeaux;\n"
|
||||
"Plutôt que d'implorer une larme du monde,\n"
|
||||
"Vivant, j'aimerais mieux inviter les corbeaux\n"
|
||||
"À saigner tous les bouts de ma carcasse immonde.\n"
|
||||
"Ô vers! noirs compagnons sans oreille et sans yeux,\n"
|
||||
"Voyez venir à vous un mort libre et joyeux;\n"
|
||||
"Philosophes viveurs, fils de la pourriture,\n"
|
||||
"À travers ma ruine allez donc sans remords,\n"
|
||||
"Et dites-moi s'il est encor quelque torture\n"
|
||||
"Pour ce vieux corps sans âme et mort parmi les morts!\n";
|
||||
|
||||
const uint8_t elongdata[] = "\x07" "\xff\x02\x95"
|
||||
"Dans une terre grasse et pleine d'escargots\n"
|
||||
"Je veux creuser moi-même une fosse profonde,\n"
|
||||
"Où je puisse à loisir étaler mes vieux os\n"
|
||||
"Et dormir dans l'oubli comme un requin dans l'onde.\n"
|
||||
"Je hais les testaments et je hais les tombeaux;\n"
|
||||
"Plutôt que d'implorer une larme du monde,\n"
|
||||
"Vivant, j'aimerais mieux inviter les corbeaux\n"
|
||||
"À saigner tous les bouts de ma carcasse immonde.\n"
|
||||
"Ô vers! noirs compagnons sans oreille et sans yeux,\n"
|
||||
"Voyez venir à vous un mort libre et joyeux;\n"
|
||||
"Philosophes viveurs, fils de la pourriture,\n"
|
||||
"À travers ma ruine allez donc sans remords,\n"
|
||||
"Et dites-moi s'il est encor quelque torture\n"
|
||||
"Pour ce vieux corps sans âme et mort parmi les morts!\n";
|
||||
|
||||
void
|
||||
test_tlv_encode_short (void)
|
||||
{
|
||||
uint8_t *res;
|
||||
size_t osize;
|
||||
|
||||
res = tlv_encode (3, shortdata, sizeof (shortdata), &osize);
|
||||
cut_assert_equal_int (11, osize, cut_message ("Wrong encoded message length."));
|
||||
cut_assert_equal_int (3, res[0], cut_message ("Wrong type"));
|
||||
cut_assert_equal_int (9, res[1], cut_message ("Wrong value length"));
|
||||
cut_assert_equal_memory (res, osize, eshortdata, sizeof (eshortdata), cut_message ("Wrong encoded value"));
|
||||
free (res);
|
||||
}
|
||||
|
||||
void
|
||||
test_tlv_encode_long (void)
|
||||
{
|
||||
uint8_t *res;
|
||||
size_t osize;
|
||||
|
||||
res = tlv_encode (7, longdata, sizeof (longdata), &osize);
|
||||
cut_assert_equal_int (665, osize, cut_message ("Wrong encoded message length."));
|
||||
cut_assert_equal_int (7, res[0], cut_message ("Wrong type"));
|
||||
cut_assert_equal_int (0xff, res[1], cut_message ("Wrong value length"));
|
||||
cut_assert_equal_int (0x02, res[2], cut_message ("Wrong value length"));
|
||||
cut_assert_equal_int (0x95, res[3], cut_message ("Wrong value length"));
|
||||
cut_assert_equal_memory (res, osize, elongdata, sizeof (elongdata), cut_message ("Wrong encoded value"));
|
||||
free (res);
|
||||
}
|
||||
|
||||
void
|
||||
test_tlv_decode_short (void)
|
||||
{
|
||||
uint8_t *res;
|
||||
uint16_t size;
|
||||
uint8_t type;
|
||||
|
||||
res = tlv_decode (eshortdata, &type, &size);
|
||||
cut_assert_equal_int (3, type, cut_message ("Wrong type"));
|
||||
cut_assert_equal_int (9, size, cut_message ("Wrong value length"));
|
||||
cut_assert_equal_memory (res, size, shortdata, sizeof (shortdata), cut_message ("Wrong decoded value"));
|
||||
free (res);
|
||||
}
|
||||
|
||||
void
|
||||
test_tlv_decode_long (void)
|
||||
{
|
||||
uint8_t *res;
|
||||
uint16_t size;
|
||||
uint8_t type;
|
||||
|
||||
res = tlv_decode (elongdata, &type, &size);
|
||||
cut_assert_equal_int (7, type, cut_message ("Wrong type"));
|
||||
cut_assert_equal_int (0x295, size, cut_message ("Wrong value length"));
|
||||
cut_assert_equal_memory (res, size, longdata, sizeof (longdata), cut_message ("Wrong decoded value"));
|
||||
free (res);
|
||||
}
|
||||
|
||||
void
|
||||
test_tlv_rfu (void)
|
||||
{
|
||||
uint8_t *data = malloc (0xffff);
|
||||
cut_assert_not_null (data, cut_message ("Out of memory"));
|
||||
|
||||
uint8_t *res = tlv_encode (7, data, 0xffff, NULL);
|
||||
cut_assert_null (res, cut_message ("Size reserved for future use"));
|
||||
|
||||
free (data);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue