2010-01-11 21:14:30 +01:00
# include <cutter.h>
# include <string.h>
2010-07-26 23:48:18 +02:00
# include <strings.h>
2010-01-11 21:14:30 +01:00
# include <freefare.h>
2010-02-23 03:12:18 +01:00
# include "freefare_internal.h"
2010-01-11 21:14:30 +01:00
2016-01-20 20:31:17 +01:00
# include "fixture.h"
2010-01-11 21:14:30 +01:00
void
2017-06-27 13:58:31 +02:00
test_mifare_classic_authenticate ( void )
2010-01-11 21:14:30 +01:00
{
int res ;
2010-02-18 19:12:36 +01:00
MifareClassicKey k = { 0xff , 0xff , 0xff , 0xff , 0xff , 0xff } ;
2017-06-27 13:58:31 +02:00
res = mifare_classic_authenticate ( tag , 0x00 , k , MFC_KEY_A ) ;
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_authenticate() failed " ) ) ;
2010-01-11 21:14:30 +01:00
}
void
2017-06-27 13:58:31 +02:00
test_mifare_classic_get_data_block_permission ( void )
2010-01-11 21:14:30 +01:00
{
int res ;
2010-02-18 19:12:36 +01:00
MifareClassicKey k = { 0xff , 0xff , 0xff , 0xff , 0xff , 0xff } ;
2017-06-27 13:58:31 +02:00
res = mifare_classic_authenticate ( tag , 0x04 , k , MFC_KEY_A ) ;
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_authenticate() failed " ) ) ;
cut_assert_equal_int ( 1 , mifare_classic_get_data_block_permission ( tag , 0x04 , MCAB_R , MFC_KEY_A ) , cut_message ( " Wrong permission " ) ) ;
cut_assert_equal_int ( 1 , mifare_classic_get_data_block_permission ( tag , 0x04 , MCAB_R , MFC_KEY_B ) , cut_message ( " Wrong permission " ) ) ;
cut_assert_equal_int ( 1 , mifare_classic_get_data_block_permission ( tag , 0x04 , MCAB_W , MFC_KEY_A ) , cut_message ( " Wrong permission " ) ) ;
cut_assert_equal_int ( 1 , mifare_classic_get_data_block_permission ( tag , 0x04 , MCAB_W , MFC_KEY_B ) , cut_message ( " Wrong permission " ) ) ;
cut_assert_equal_int ( 1 , mifare_classic_get_data_block_permission ( tag , 0x04 , MCAB_D , MFC_KEY_A ) , cut_message ( " Wrong permission " ) ) ;
cut_assert_equal_int ( 1 , mifare_classic_get_data_block_permission ( tag , 0x04 , MCAB_D , MFC_KEY_B ) , cut_message ( " Wrong permission " ) ) ;
cut_assert_equal_int ( 1 , mifare_classic_get_data_block_permission ( tag , 0x04 , MCAB_I , MFC_KEY_A ) , cut_message ( " Wrong permission " ) ) ;
cut_assert_equal_int ( 1 , mifare_classic_get_data_block_permission ( tag , 0x04 , MCAB_I , MFC_KEY_B ) , cut_message ( " Wrong permission " ) ) ;
cut_assert_equal_int ( - 1 , mifare_classic_get_trailer_block_permission ( tag , 0x04 , MCAB_READ_KEYA , MFC_KEY_A ) , cut_message ( " Wrong permission " ) ) ;
cut_assert_equal_int ( - 1 , mifare_classic_get_trailer_block_permission ( tag , 0x04 , MCAB_READ_KEYA , MFC_KEY_B ) , cut_message ( " Wrong permission " ) ) ;
cut_assert_equal_int ( - 1 , mifare_classic_get_trailer_block_permission ( tag , 0x04 , MCAB_WRITE_KEYA , MFC_KEY_A ) , cut_message ( " Wrong permission " ) ) ;
cut_assert_equal_int ( - 1 , mifare_classic_get_trailer_block_permission ( tag , 0x04 , MCAB_WRITE_KEYA , MFC_KEY_B ) , cut_message ( " Wrong permission " ) ) ;
cut_assert_equal_int ( - 1 , mifare_classic_get_trailer_block_permission ( tag , 0x04 , MCAB_READ_ACCESS_BITS , MFC_KEY_A ) , cut_message ( " Wrong permission " ) ) ;
cut_assert_equal_int ( - 1 , mifare_classic_get_trailer_block_permission ( tag , 0x04 , MCAB_READ_ACCESS_BITS , MFC_KEY_B ) , cut_message ( " Wrong permission " ) ) ;
cut_assert_equal_int ( - 1 , mifare_classic_get_trailer_block_permission ( tag , 0x04 , MCAB_WRITE_ACCESS_BITS , MFC_KEY_A ) , cut_message ( " Wrong permission " ) ) ;
cut_assert_equal_int ( - 1 , mifare_classic_get_trailer_block_permission ( tag , 0x04 , MCAB_WRITE_ACCESS_BITS , MFC_KEY_B ) , cut_message ( " Wrong permission " ) ) ;
cut_assert_equal_int ( - 1 , mifare_classic_get_trailer_block_permission ( tag , 0x04 , MCAB_READ_KEYB , MFC_KEY_A ) , cut_message ( " Wrong permission " ) ) ;
cut_assert_equal_int ( - 1 , mifare_classic_get_trailer_block_permission ( tag , 0x04 , MCAB_READ_KEYB , MFC_KEY_B ) , cut_message ( " Wrong permission " ) ) ;
cut_assert_equal_int ( - 1 , mifare_classic_get_trailer_block_permission ( tag , 0x04 , MCAB_WRITE_KEYB , MFC_KEY_A ) , cut_message ( " Wrong permission " ) ) ;
cut_assert_equal_int ( - 1 , mifare_classic_get_trailer_block_permission ( tag , 0x04 , MCAB_WRITE_KEYB , MFC_KEY_B ) , cut_message ( " Wrong permission " ) ) ;
2010-01-11 21:14:30 +01:00
}
void
2017-06-27 13:58:31 +02:00
test_mifare_classic_get_trailer_permission ( void )
2010-01-11 21:14:30 +01:00
{
int res ;
2010-02-18 19:12:36 +01:00
MifareClassicKey k = { 0xff , 0xff , 0xff , 0xff , 0xff , 0xff } ;
2017-06-27 13:58:31 +02:00
res = mifare_classic_authenticate ( tag , 0x07 , k , MFC_KEY_A ) ;
cut_assert_equal_int ( res , 0 , cut_message ( " mifare_classic_authenticate() failed " ) ) ;
cut_assert_equal_int ( - 1 , mifare_classic_get_data_block_permission ( tag , 0x07 , MCAB_R , MFC_KEY_A ) , cut_message ( " Wrong permission " ) ) ;
cut_assert_equal_int ( - 1 , mifare_classic_get_data_block_permission ( tag , 0x07 , MCAB_R , MFC_KEY_B ) , cut_message ( " Wrong permission " ) ) ;
cut_assert_equal_int ( - 1 , mifare_classic_get_data_block_permission ( tag , 0x07 , MCAB_W , MFC_KEY_A ) , cut_message ( " Wrong permission " ) ) ;
cut_assert_equal_int ( - 1 , mifare_classic_get_data_block_permission ( tag , 0x07 , MCAB_W , MFC_KEY_B ) , cut_message ( " Wrong permission " ) ) ;
cut_assert_equal_int ( - 1 , mifare_classic_get_data_block_permission ( tag , 0x07 , MCAB_D , MFC_KEY_A ) , cut_message ( " Wrong permission " ) ) ;
cut_assert_equal_int ( - 1 , mifare_classic_get_data_block_permission ( tag , 0x07 , MCAB_D , MFC_KEY_B ) , cut_message ( " Wrong permission " ) ) ;
cut_assert_equal_int ( - 1 , mifare_classic_get_data_block_permission ( tag , 0x07 , MCAB_I , MFC_KEY_A ) , cut_message ( " Wrong permission " ) ) ;
cut_assert_equal_int ( - 1 , mifare_classic_get_data_block_permission ( tag , 0x07 , MCAB_I , MFC_KEY_B ) , cut_message ( " Wrong permission " ) ) ;
cut_assert_equal_int ( 0 , mifare_classic_get_trailer_block_permission ( tag , 0x07 , MCAB_READ_KEYA , MFC_KEY_A ) , cut_message ( " Wrong permission " ) ) ;
cut_assert_equal_int ( 0 , mifare_classic_get_trailer_block_permission ( tag , 0x07 , MCAB_READ_KEYA , MFC_KEY_B ) , cut_message ( " Wrong permission " ) ) ;
cut_assert_equal_int ( 1 , mifare_classic_get_trailer_block_permission ( tag , 0x07 , MCAB_WRITE_KEYA , MFC_KEY_A ) , cut_message ( " Wrong permission " ) ) ;
cut_assert_equal_int ( 0 , mifare_classic_get_trailer_block_permission ( tag , 0x07 , MCAB_WRITE_KEYA , MFC_KEY_B ) , cut_message ( " Wrong permission " ) ) ;
cut_assert_equal_int ( 1 , mifare_classic_get_trailer_block_permission ( tag , 0x07 , MCAB_READ_ACCESS_BITS , MFC_KEY_A ) , cut_message ( " Wrong permission " ) ) ;
cut_assert_equal_int ( 0 , mifare_classic_get_trailer_block_permission ( tag , 0x07 , MCAB_READ_ACCESS_BITS , MFC_KEY_B ) , cut_message ( " Wrong permission " ) ) ;
cut_assert_equal_int ( 1 , mifare_classic_get_trailer_block_permission ( tag , 0x07 , MCAB_WRITE_ACCESS_BITS , MFC_KEY_A ) , cut_message ( " Wrong permission " ) ) ;
cut_assert_equal_int ( 0 , mifare_classic_get_trailer_block_permission ( tag , 0x07 , MCAB_WRITE_ACCESS_BITS , MFC_KEY_B ) , cut_message ( " Wrong permission " ) ) ;
cut_assert_equal_int ( 1 , mifare_classic_get_trailer_block_permission ( tag , 0x07 , MCAB_READ_KEYB , MFC_KEY_A ) , cut_message ( " Wrong permission " ) ) ;
cut_assert_equal_int ( 0 , mifare_classic_get_trailer_block_permission ( tag , 0x07 , MCAB_READ_KEYB , MFC_KEY_B ) , cut_message ( " Wrong permission " ) ) ;
cut_assert_equal_int ( 1 , mifare_classic_get_trailer_block_permission ( tag , 0x07 , MCAB_WRITE_KEYB , MFC_KEY_A ) , cut_message ( " Wrong permission " ) ) ;
cut_assert_equal_int ( 0 , mifare_classic_get_trailer_block_permission ( tag , 0x07 , MCAB_WRITE_KEYB , MFC_KEY_B ) , cut_message ( " Wrong permission " ) ) ;
2010-01-11 21:14:30 +01:00
}
2012-05-14 16:11:26 +02:00
void
2017-06-27 13:58:31 +02:00
test_mifare_classic_format_first_sector ( void )
2012-05-14 16:11:26 +02:00
{
int res ;
MifareClassicKey k = { 0xff , 0xff , 0xff , 0xff , 0xff , 0xff } ;
2017-06-27 13:58:31 +02:00
res = mifare_classic_authenticate ( tag , 0x00 , k , MFC_KEY_A ) ;
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_authenticate() failed " ) ) ;
2012-05-14 16:11:26 +02:00
MifareClassicBlock data = {
0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 ,
0x08 , 0x09 , 0x0a , 0x0b , 0x0c , 0x0d , 0x0e , 0x0f
} ;
MifareClassicBlock empty ;
2017-06-27 13:58:31 +02:00
memset ( empty , ' \x00 ' , sizeof ( empty ) ) ;
2012-05-14 16:11:26 +02:00
MifareClassicBlock b0 ;
2017-06-27 13:58:31 +02:00
res = mifare_classic_read ( tag , 0x00 , & b0 ) ;
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_read() failed " ) ) ;
2012-05-14 16:11:26 +02:00
2017-06-27 13:58:31 +02:00
res = mifare_classic_write ( tag , 0x00 , data ) ;
cut_assert_equal_int ( - 1 , res , cut_message ( " mifare_classic_write() succeeded " ) ) ;
2012-05-14 16:11:26 +02:00
2017-06-27 13:58:31 +02:00
res = mifare_classic_disconnect ( tag ) ;
res = mifare_classic_connect ( tag ) ;
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_connect() failed " ) ) ;
res = mifare_classic_authenticate ( tag , 0x00 , k , MFC_KEY_A ) ;
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_authenticate() failed " ) ) ;
2012-05-14 16:11:26 +02:00
2017-06-27 13:58:31 +02:00
res = mifare_classic_write ( tag , 0x01 , data ) ;
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_write() failed " ) ) ;
res = mifare_classic_write ( tag , 0x02 , data ) ;
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_write() failed " ) ) ;
2012-05-14 16:11:26 +02:00
2017-06-27 13:58:31 +02:00
res = mifare_classic_format_sector ( tag , 0x00 ) ;
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_format_sector() failed " ) ) ;
2012-05-14 16:11:26 +02:00
2017-06-27 13:58:31 +02:00
res = mifare_classic_read ( tag , 0x00 , & data ) ;
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_read() failed " ) ) ;
cut_assert_equal_memory ( data , sizeof ( data ) , b0 , sizeof ( b0 ) , cut_message ( " Data changed in first block (block 1/3) " ) ) ;
2012-05-14 16:11:26 +02:00
2017-06-27 13:58:31 +02:00
res = mifare_classic_read ( tag , 0x01 , & data ) ;
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_read() failed " ) ) ;
cut_assert_equal_memory ( data , sizeof ( data ) , empty , sizeof ( data ) , cut_message ( " Wrong data in formatted sector (block 2/3) " ) ) ;
2012-05-14 16:11:26 +02:00
2017-06-27 13:58:31 +02:00
res = mifare_classic_read ( tag , 0x02 , & data ) ;
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_read() failed " ) ) ;
cut_assert_equal_memory ( data , sizeof ( data ) , empty , sizeof ( data ) , cut_message ( " Wrong data in formatted sector (block 3/3) " ) ) ;
2012-05-14 16:11:26 +02:00
2017-06-27 13:58:31 +02:00
res = mifare_classic_read ( tag , 0x03 , & data ) ;
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_read() failed " ) ) ;
cut_assert_equal_memory ( data , sizeof ( data ) , " \x00 \x00 \x00 \x00 \x00 \x00 \xff \x07 \x80 \x69 \xff \xff \xff \xff \xff \xff " , sizeof ( data ) , cut_message ( " Wrong permissions in formatted sector " ) ) ;
2012-05-14 16:11:26 +02:00
}
2010-01-11 21:14:30 +01:00
void
2017-06-27 13:58:31 +02:00
test_mifare_classic_format ( void )
2010-01-11 21:14:30 +01:00
{
int res ;
MifareClassicKey k = { 0xff , 0xff , 0xff , 0xff , 0xff , 0xff } ;
2017-06-27 13:58:31 +02:00
res = mifare_classic_authenticate ( tag , 0x3c , k , MFC_KEY_A ) ;
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_authenticate() failed " ) ) ;
2010-01-11 21:14:30 +01:00
MifareClassicBlock data = {
0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 ,
0x08 , 0x09 , 0x0a , 0x0b , 0x0c , 0x0d , 0x0e , 0x0f
} ;
MifareClassicBlock empty ;
2017-06-27 13:58:31 +02:00
memset ( empty , 0 , sizeof ( empty ) ) ;
2010-01-11 21:14:30 +01:00
2017-06-27 13:58:31 +02:00
res = mifare_classic_write ( tag , 0x3c , data ) ;
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_write() failed " ) ) ;
res = mifare_classic_write ( tag , 0x3d , data ) ;
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_write() failed " ) ) ;
res = mifare_classic_write ( tag , 0x3e , data ) ;
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_write() failed " ) ) ;
2010-01-11 21:14:30 +01:00
2017-06-27 13:58:31 +02:00
res = mifare_classic_format_sector ( tag , mifare_classic_block_sector ( 0x3c ) ) ;
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_format_sector() failed " ) ) ;
2010-01-11 21:14:30 +01:00
2017-06-27 13:58:31 +02:00
res = mifare_classic_read ( tag , 0x3c , & data ) ;
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_read() failed " ) ) ;
cut_assert_equal_memory ( data , sizeof ( data ) , empty , sizeof ( data ) , cut_message ( " Wrong data in formatted sector (block 1/3) " ) ) ;
2010-01-11 21:14:30 +01:00
2017-06-27 13:58:31 +02:00
res = mifare_classic_read ( tag , 0x3d , & data ) ;
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_read() failed " ) ) ;
cut_assert_equal_memory ( data , sizeof ( data ) , empty , sizeof ( data ) , cut_message ( " Wrong data in formatted sector (block 2/3) " ) ) ;
2010-01-11 21:14:30 +01:00
2017-06-27 13:58:31 +02:00
res = mifare_classic_read ( tag , 0x3e , & data ) ;
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_read() failed " ) ) ;
cut_assert_equal_memory ( data , sizeof ( data ) , empty , sizeof ( data ) , cut_message ( " Wrong data in formatted sector (block 3/3) " ) ) ;
2010-01-11 21:14:30 +01:00
2017-06-27 13:58:31 +02:00
res = mifare_classic_read ( tag , 0x3f , & data ) ;
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_read() failed " ) ) ;
cut_assert_equal_memory ( data , sizeof ( data ) , " \x00 \x00 \x00 \x00 \x00 \x00 \xff \x07 \x80 \x69 \xff \xff \xff \xff \xff \xff " , sizeof ( data ) , cut_message ( " Wrong permissions in formatted sector " ) ) ;
2010-01-11 21:14:30 +01:00
}
void
2017-06-27 13:58:31 +02:00
test_mifare_classic_value_block_increment ( void )
2010-01-11 21:14:30 +01:00
{
int res ;
MifareClassicBlockNumber block = 0x04 ;
2010-02-18 19:12:36 +01:00
MifareClassicKey k = { 0xff , 0xff , 0xff , 0xff , 0xff , 0xff } ;
2017-06-27 13:58:31 +02:00
res = mifare_classic_authenticate ( tag , block , k , MFC_KEY_A ) ;
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_authenticate() failed " ) ) ;
2010-01-11 21:14:30 +01:00
2017-06-27 13:58:31 +02:00
res = mifare_classic_init_value ( tag , block , 1000 , 0x00 ) ;
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_init_value() failed " ) ) ;
2010-01-11 21:14:30 +01:00
/* Initialize value block */
int32_t value ;
MifareClassicBlockNumber adr ;
2017-06-27 13:58:31 +02:00
res = mifare_classic_read_value ( tag , block , & value , & adr ) ;
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_read_value() failed " ) ) ;
cut_assert_equal_int ( 1000 , value , cut_message ( " Wrong value block value " ) ) ;
cut_assert_equal_int ( 0x00 , adr , cut_message ( " Wrong value block address " ) ) ;
2010-01-11 21:14:30 +01:00
/* Increment by 1 */
2017-06-27 13:58:31 +02:00
res = mifare_classic_increment ( tag , block , 1 ) ;
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_increment() failed " ) ) ;
2010-01-11 21:14:30 +01:00
2017-06-27 13:58:31 +02:00
res = mifare_classic_transfer ( tag , block ) ;
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_transfer() failed " ) ) ;
2010-01-11 21:14:30 +01:00
2017-06-27 13:58:31 +02:00
res = mifare_classic_read_value ( tag , block , & value , & adr ) ;
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_read_value() failed " ) ) ;
cut_assert_equal_int ( 1001 , value , cut_message ( " Wrong value block value " ) ) ;
cut_assert_equal_int ( 0x00 , adr , cut_message ( " Wrong value block address " ) ) ;
2010-01-11 21:14:30 +01:00
/* Increment by 10 */
2017-06-27 13:58:31 +02:00
res = mifare_classic_increment ( tag , block , 10 ) ;
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_increment() failed " ) ) ;
2010-01-11 21:14:30 +01:00
2017-06-27 13:58:31 +02:00
res = mifare_classic_transfer ( tag , block ) ;
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_transfer() failed " ) ) ;
2010-01-11 21:14:30 +01:00
2017-06-27 13:58:31 +02:00
res = mifare_classic_read_value ( tag , block , & value , & adr ) ;
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_read_value() failed " ) ) ;
cut_assert_equal_int ( 1011 , value , cut_message ( " Wrong value block value " ) ) ;
cut_assert_equal_int ( 0x00 , adr , cut_message ( " Wrong value block address " ) ) ;
2010-01-11 21:14:30 +01:00
}
void
2017-06-27 13:58:31 +02:00
test_mifare_classic_value_block_decrement ( void )
2010-01-11 21:14:30 +01:00
{
int res ;
MifareClassicBlockNumber block = 0x04 ;
2010-02-18 19:12:36 +01:00
MifareClassicKey k = { 0xff , 0xff , 0xff , 0xff , 0xff , 0xff } ;
2017-06-27 13:58:31 +02:00
res = mifare_classic_authenticate ( tag , block , k , MFC_KEY_A ) ;
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_authenticate() failed " ) ) ;
res = mifare_classic_init_value ( tag , block , 1000 , 0x00 ) ;
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_init_value() failed " ) ) ;
2010-01-11 21:14:30 +01:00
/* Initialize value block */
int32_t value ;
MifareClassicBlockNumber adr ;
2017-06-27 13:58:31 +02:00
res = mifare_classic_read_value ( tag , block , & value , & adr ) ;
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_read_value() failed " ) ) ;
cut_assert_equal_int ( 1000 , value , cut_message ( " Wrong value block value " ) ) ;
cut_assert_equal_int ( 0x00 , adr , cut_message ( " Wrong value block address " ) ) ;
2010-01-11 21:14:30 +01:00
/* Decrement */
2017-06-27 13:58:31 +02:00
res = mifare_classic_decrement ( tag , block , 1 ) ;
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_decrement() failed " ) ) ;
2010-01-11 21:14:30 +01:00
2017-06-27 13:58:31 +02:00
res = mifare_classic_transfer ( tag , block ) ;
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_transfer() failed " ) ) ;
2010-01-11 21:14:30 +01:00
2017-06-27 13:58:31 +02:00
res = mifare_classic_read_value ( tag , block , & value , & adr ) ;
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_read_value() failed " ) ) ;
cut_assert_equal_int ( 999 , value , cut_message ( " Wrong value block value " ) ) ;
cut_assert_equal_int ( 0x00 , adr , cut_message ( " Wrong value block address " ) ) ;
2010-01-11 21:14:30 +01:00
2017-06-27 13:58:31 +02:00
res = mifare_classic_decrement ( tag , block , 1000 ) ;
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_decrement() failed " ) ) ;
2010-01-11 21:14:30 +01:00
2017-06-27 13:58:31 +02:00
res = mifare_classic_transfer ( tag , block ) ;
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_transfer() failed " ) ) ;
2010-01-11 21:14:30 +01:00
2017-06-27 13:58:31 +02:00
res = mifare_classic_read_value ( tag , block , & value , & adr ) ;
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_read_value() failed " ) ) ;
cut_assert_equal_int ( - 1 , value , cut_message ( " Wrong value block value " ) ) ;
cut_assert_equal_int ( 0x00 , adr , cut_message ( " Wrong value block address " ) ) ;
2010-01-11 21:14:30 +01:00
}
void
2017-06-27 13:58:31 +02:00
test_mifare_classic_value_block_restore ( void )
2010-01-11 21:14:30 +01:00
{
int res ;
MifareClassicBlockNumber block = 0x04 ;
2010-02-18 19:12:36 +01:00
MifareClassicKey k = { 0xff , 0xff , 0xff , 0xff , 0xff , 0xff } ;
2017-06-27 13:58:31 +02:00
res = mifare_classic_authenticate ( tag , block , k , MFC_KEY_A ) ;
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_authenticate() failed " ) ) ;
2010-01-11 21:14:30 +01:00
/* Restore */
MifareClassicBlock data ;
MifareClassicBlock sample = {
0xe8 , 0x03 , 0x00 , 0x00 ,
0x17 , 0xfc , 0xff , 0xff ,
0xe8 , 0x03 , 0x00 , 0x00 ,
0x00 ,
0xff ,
0x00 ,
0xff
} ;
MifareClassicBlock nul = {
0x00 , 0x00 , 0x00 , 0x00 ,
0xff , 0xff , 0xff , 0xff ,
0x00 , 0x00 , 0x00 , 0x00 ,
0x00 ,
0xff ,
0x00 ,
0xff
} ;
2017-06-27 13:58:31 +02:00
res = mifare_classic_write ( tag , block , sample ) ;
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_write() failed " ) ) ;
2010-01-11 21:14:30 +01:00
2017-06-27 13:58:31 +02:00
res = mifare_classic_read ( tag , block , & data ) ;
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_read() failed " ) ) ;
cut_assert_equal_memory ( sample , sizeof ( sample ) , data , sizeof ( data ) , cut_message ( " Wrong value block contents " ) ) ;
2010-01-11 21:14:30 +01:00
2017-06-27 13:58:31 +02:00
res = mifare_classic_write ( tag , block + 1 , nul ) ;
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_write() failed " ) ) ;
2010-01-11 21:14:30 +01:00
2017-06-27 13:58:31 +02:00
res = mifare_classic_read ( tag , block + 1 , & data ) ;
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_read() failed " ) ) ;
cut_assert_equal_memory ( nul , sizeof ( sample ) , data , sizeof ( data ) , cut_message ( " Wrong value block contents " ) ) ;
2010-01-11 21:14:30 +01:00
2017-06-27 13:58:31 +02:00
res = mifare_classic_restore ( tag , block ) ;
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_restore() failed " ) ) ;
2010-01-11 21:14:30 +01:00
2017-06-27 13:58:31 +02:00
res = mifare_classic_transfer ( tag , block + 1 ) ;
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_transfer() failed " ) ) ;
2010-01-11 21:14:30 +01:00
2017-06-27 13:58:31 +02:00
res = mifare_classic_read ( tag , block + 1 , & data ) ;
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_read() failed " ) ) ;
cut_assert_equal_memory ( sample , sizeof ( sample ) , data , sizeof ( data ) , cut_message ( " Wrong value block contents " ) ) ;
2010-01-11 21:14:30 +01:00
}
2010-02-22 18:30:50 +01:00
void
2017-06-27 13:58:31 +02:00
test_mifare_classic_get_uid ( void )
2010-02-22 18:30:50 +01:00
{
char * uid ;
2017-06-27 13:58:31 +02:00
uid = freefare_get_tag_uid ( tag ) ;
2010-02-22 18:30:50 +01:00
2017-06-27 13:58:31 +02:00
cut_assert_not_null ( uid , cut_message ( " freefare_get_tag_uid() failed " ) ) ;
cut_assert ( ( ( strlen ( uid ) = = 8 ) | | ( strlen ( uid ) = = 14 ) ) , cut_message ( " Wrong UID length " ) ) ;
2010-02-22 18:30:50 +01:00
2017-06-27 13:58:31 +02:00
free ( uid ) ;
2010-02-22 18:30:50 +01:00
}
2010-02-23 03:12:18 +01:00
2010-07-01 12:57:59 +02:00
void
2017-06-27 13:58:31 +02:00
test_mifare_classic_get_tag_friendly_name ( void )
2010-07-01 12:57:59 +02:00
{
2017-06-27 13:58:31 +02:00
const char * name = freefare_get_tag_friendly_name ( tag ) ;
2010-07-01 12:57:59 +02:00
2017-06-27 13:58:31 +02:00
cut_assert_not_null ( name , cut_message ( " freefare_get_tag_friendly_name() failed " ) ) ;
2010-07-01 12:57:59 +02:00
}