2010-02-19 11:43:06 +01:00
/*-
* 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 $
*/
2010-01-11 21:14:30 +01:00
# include <cutter.h>
# include <string.h>
# include <freefare.h>
2010-02-23 03:12:18 +01:00
# include "freefare_internal.h"
2010-01-11 21:14:30 +01:00
# include "mifare_classic_fixture.h"
void
test_mifare_classic_authenticate ( void )
{
int res ;
2010-02-18 19:12:36 +01:00
MifareClassicKey k = { 0xff , 0xff , 0xff , 0xff , 0xff , 0xff } ;
2010-01-11 21:14:30 +01:00
res = mifare_classic_authenticate ( tag , 0x00 , k , MFC_KEY_A ) ;
2010-02-11 12:29:57 +01:00
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_authenticate() failed " ) ) ;
2010-01-11 21:14:30 +01:00
}
void
test_mifare_classic_read_sector_0 ( void )
{
int res ;
cut_omit ( " Requires a particular NFC tag " ) ;
2010-02-18 19:12:36 +01:00
MifareClassicKey k = { 0xff , 0xff , 0xff , 0xff , 0xff , 0xff } ;
2010-01-11 21:14:30 +01:00
res = mifare_classic_authenticate ( tag , 0x00 , k , MFC_KEY_A ) ;
2010-02-11 12:29:57 +01:00
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_authenticate() failed " ) ) ;
2010-01-11 21:14:30 +01:00
MifareClassicBlock r ;
res = mifare_classic_read ( tag , 0x00 , & r ) ;
2010-02-11 12:29:57 +01:00
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_read() failed " ) ) ;
2010-01-11 21:14:30 +01:00
MifareClassicBlock e = { 0xba , 0xc7 , 0x7a , 0xfc , 0xfb , 0x88 , 0x04 , 0x00 , 0x46 , 0x5d , 0x55 , 0x96 , 0x41 , 0x10 , 0x19 , 0x08 } ;
2010-02-11 12:29:57 +01:00
cut_assert_equal_memory ( e , sizeof ( e ) , r , sizeof ( r ) , cut_message ( " Unexpected sector 0 value " ) ) ;
2010-01-11 21:14:30 +01:00
}
void
test_mifare_classic_get_data_block_permission ( void )
{
int res ;
2010-02-18 19:12:36 +01:00
MifareClassicKey k = { 0xff , 0xff , 0xff , 0xff , 0xff , 0xff } ;
2010-01-11 21:14:30 +01:00
res = mifare_classic_authenticate ( tag , 0x04 , k , MFC_KEY_A ) ;
2010-02-11 12:29:57 +01:00
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
test_mifare_classic_get_trailer_permission ( void )
{
int res ;
2010-02-18 19:12:36 +01:00
MifareClassicKey k = { 0xff , 0xff , 0xff , 0xff , 0xff , 0xff } ;
2010-01-11 21:14:30 +01:00
res = mifare_classic_authenticate ( tag , 0x07 , k , MFC_KEY_A ) ;
2010-02-11 12:29:57 +01:00
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 " ) ) ;
2010-02-18 18:07:36 +01:00
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 " ) ) ;
2010-02-11 12:29:57 +01:00
cut_assert_equal_int ( 1 , mifare_classic_get_trailer_block_permission ( tag , 0x07 , MCAB_READ_ACCESS_BITS , MFC_KEY_A ) , cut_message ( " Wrong permission " ) ) ;
2010-02-18 18:07:36 +01:00
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 " ) ) ;
2010-02-11 12:29:57 +01:00
cut_assert_equal_int ( 0 , mifare_classic_get_trailer_block_permission ( tag , 0x07 , MCAB_READ_KEYB , MFC_KEY_B ) , cut_message ( " Wrong permission " ) ) ;
2010-02-18 18:07:36 +01:00
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
}
void
test_mifare_classic_read_mad ( void )
{
2010-02-19 15:40:28 +01:00
cut_omit ( " A blank MIFARE Classic does not have a MAD. This test has to be moved in an appropriate test case. " ) ;
2010-01-11 21:14:30 +01:00
Mad mad = mad_read ( tag ) ;
2010-02-11 12:29:57 +01:00
cut_assert_not_null ( mad , cut_message ( " mad_read() failed " ) ) ;
2010-01-11 21:14:30 +01:00
}
void
test_mifare_classic_format ( void )
{
int res ;
MifareClassicKey k = { 0xff , 0xff , 0xff , 0xff , 0xff , 0xff } ;
res = mifare_classic_authenticate ( tag , 0x3c , k , MFC_KEY_A ) ;
2010-02-11 12:29:57 +01:00
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 ;
memset ( empty , ' \x00 ' , sizeof ( empty ) ) ;
res = mifare_classic_write ( tag , 0x3c , data ) ;
2010-02-11 12:29:57 +01:00
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_write() failed " ) ) ;
2010-01-11 21:14:30 +01:00
res = mifare_classic_write ( tag , 0x3d , data ) ;
2010-02-11 12:29:57 +01:00
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_write() failed " ) ) ;
2010-01-11 21:14:30 +01:00
res = mifare_classic_write ( tag , 0x3e , data ) ;
2010-02-11 12:29:57 +01:00
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_write() failed " ) ) ;
2010-01-11 21:14:30 +01:00
2010-02-23 15:22:23 +01:00
res = mifare_classic_format_sector ( tag , 0x3c ) ;
2010-02-11 12:29:57 +01:00
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_format_sector() failed " ) ) ;
2010-01-11 21:14:30 +01:00
res = mifare_classic_read ( tag , 0x3c , & data ) ;
2010-02-11 12:29:57 +01:00
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
res = mifare_classic_read ( tag , 0x3d , & data ) ;
2010-02-11 12:29:57 +01:00
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
res = mifare_classic_read ( tag , 0x3e , & data ) ;
2010-02-11 12:29:57 +01:00
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
res = mifare_classic_read ( tag , 0x3f , & data ) ;
2010-02-11 12:29:57 +01:00
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
test_mifare_classic_value_block_increment ( void )
{
int res ;
MifareClassicBlockNumber block = 0x04 ;
2010-02-18 19:12:36 +01:00
MifareClassicKey k = { 0xff , 0xff , 0xff , 0xff , 0xff , 0xff } ;
2010-01-11 21:14:30 +01:00
res = mifare_classic_authenticate ( tag , block , k , MFC_KEY_A ) ;
2010-02-11 12:29:57 +01:00
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_authenticate() failed " ) ) ;
2010-01-11 21:14:30 +01:00
res = mifare_classic_init_value ( tag , block , 1000 , 0x00 ) ;
2010-02-11 12:29:57 +01:00
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 ;
res = mifare_classic_read_value ( tag , block , & value , & adr ) ;
2010-02-11 12:29:57 +01:00
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 */
res = mifare_classic_increment ( tag , block , 1 ) ;
2010-02-11 12:29:57 +01:00
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_increment() failed " ) ) ;
2010-01-11 21:14:30 +01:00
res = mifare_classic_transfer ( tag , block ) ;
2010-02-11 12:29:57 +01:00
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_transfer() failed " ) ) ;
2010-01-11 21:14:30 +01:00
res = mifare_classic_read_value ( tag , block , & value , & adr ) ;
2010-02-11 12:29:57 +01:00
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 */
res = mifare_classic_increment ( tag , block , 10 ) ;
2010-02-11 12:29:57 +01:00
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_increment() failed " ) ) ;
2010-01-11 21:14:30 +01:00
res = mifare_classic_transfer ( tag , block ) ;
2010-02-11 12:29:57 +01:00
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_transfer() failed " ) ) ;
2010-01-11 21:14:30 +01:00
res = mifare_classic_read_value ( tag , block , & value , & adr ) ;
2010-02-11 12:29:57 +01:00
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
test_mifare_classic_value_block_decrement ( void )
{
int res ;
MifareClassicBlockNumber block = 0x04 ;
2010-02-18 19:12:36 +01:00
MifareClassicKey k = { 0xff , 0xff , 0xff , 0xff , 0xff , 0xff } ;
2010-01-11 21:14:30 +01:00
res = mifare_classic_authenticate ( tag , block , k , MFC_KEY_A ) ;
2010-02-11 12:29:57 +01:00
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_authenticate() failed " ) ) ;
2010-01-11 21:14:30 +01:00
res = mifare_classic_init_value ( tag , block , 1000 , 0x00 ) ;
2010-02-11 12:29:57 +01:00
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 ;
res = mifare_classic_read_value ( tag , block , & value , & adr ) ;
2010-02-11 12:29:57 +01:00
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 */
res = mifare_classic_decrement ( tag , block , 1 ) ;
2010-02-11 12:29:57 +01:00
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_decrement() failed " ) ) ;
2010-01-11 21:14:30 +01:00
res = mifare_classic_transfer ( tag , block ) ;
2010-02-11 12:29:57 +01:00
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_transfer() failed " ) ) ;
2010-01-11 21:14:30 +01:00
res = mifare_classic_read_value ( tag , block , & value , & adr ) ;
2010-02-11 12:29:57 +01:00
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
res = mifare_classic_decrement ( tag , block , 1000 ) ;
2010-02-11 12:29:57 +01:00
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_decrement() failed " ) ) ;
2010-01-11 21:14:30 +01:00
res = mifare_classic_transfer ( tag , block ) ;
2010-02-11 12:29:57 +01:00
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_transfer() failed " ) ) ;
2010-01-11 21:14:30 +01:00
res = mifare_classic_read_value ( tag , block , & value , & adr ) ;
2010-02-11 12:29:57 +01:00
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
test_mifare_classic_value_block_restore ( void )
{
int res ;
MifareClassicBlockNumber block = 0x04 ;
2010-02-18 19:12:36 +01:00
MifareClassicKey k = { 0xff , 0xff , 0xff , 0xff , 0xff , 0xff } ;
2010-01-11 21:14:30 +01:00
res = mifare_classic_authenticate ( tag , block , k , MFC_KEY_A ) ;
2010-02-11 12:29:57 +01:00
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
} ;
res = mifare_classic_write ( tag , block , sample ) ;
2010-02-11 12:29:57 +01:00
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_write() failed " ) ) ;
2010-01-11 21:14:30 +01:00
res = mifare_classic_read ( tag , block , & data ) ;
2010-02-11 12:29:57 +01:00
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
res = mifare_classic_write ( tag , block + 1 , nul ) ;
2010-02-11 12:29:57 +01:00
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_write() failed " ) ) ;
2010-01-11 21:14:30 +01:00
res = mifare_classic_read ( tag , block + 1 , & data ) ;
2010-02-11 12:29:57 +01:00
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
res = mifare_classic_restore ( tag , block ) ;
2010-02-11 12:29:57 +01:00
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_restore() failed " ) ) ;
2010-01-11 21:14:30 +01:00
res = mifare_classic_transfer ( tag , block + 1 ) ;
2010-02-11 12:29:57 +01:00
cut_assert_equal_int ( 0 , res , cut_message ( " mifare_classic_transfer() failed " ) ) ;
2010-01-11 21:14:30 +01:00
res = mifare_classic_read ( tag , block + 1 , & data ) ;
2010-02-11 12:29:57 +01:00
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
test_mifare_classic_get_uid ( void )
{
char * uid ;
2010-03-30 18:24:37 +02:00
uid = freefare_get_tag_uid ( tag ) ;
2010-02-22 18:30:50 +01:00
cut_assert_not_null ( uid , cut_message ( " mifare_classic_get_uid() failed " ) ) ;
cut_assert_equal_int ( 8 , strlen ( uid ) , cut_message ( " Wrong UID length " ) ) ;
free ( uid ) ;
}
2010-02-23 03:12:18 +01:00
void
test_mifare_classic_sector_boundaries ( void )
{
cut_notify ( " No MIFARE Classic target is required for this test " ) ;
for ( int i = 0 ; i < 32 ; i + + ) {
for ( int j = 0 ; j < 4 ; j + + ) {
cut_assert_equal_int ( 4 * i , mifare_classic_first_sector_block ( 4 * i ) , cut_message ( " Wrong first block number for block %d " , i ) ) ;
cut_assert_equal_int ( 4 * i + 3 , mifare_classic_last_sector_block ( 4 * i + j ) , cut_message ( " Wrong last block number for block %d " , i ) ) ;
}
}
for ( int i = 0 ; i < 8 ; i + + ) {
for ( int j = 0 ; j < 16 ; j + + ) {
cut_assert_equal_int ( 128 + 16 * i , mifare_classic_first_sector_block ( 128 + 16 * i ) , cut_message ( " Wrong last block number for block %d " , i ) ) ;
cut_assert_equal_int ( 128 + 16 * i + 15 , mifare_classic_last_sector_block ( 128 + 16 * i + j ) , cut_message ( " Wrong last block number for block %d " , i ) ) ;
}
}
}
2010-03-30 18:18:58 +02:00