diff --git a/test/Makefile.am b/test/Makefile.am index 74bbb85..5075c36 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -26,7 +26,8 @@ test_access_storm_la_SOURCES = test_access_storm.c test_access_storm_la_LIBADD = $(top_builddir)/libnfc/libnfc.la test_dep_active_la_SOURCES = test_dep_active.c -test_dep_active_la_LIBADD = $(top_builddir)/libnfc/libnfc.la +test_dep_active_la_LIBADD = $(top_builddir)/libnfc/libnfc.la \ + $(top_builddir)/utils/libnfcutils.la test_dep_passive_la_SOURCES = test_dep_passive.c test_dep_passive_la_LIBADD = $(top_builddir)/libnfc/libnfc.la diff --git a/test/test_dep_active.c b/test/test_dep_active.c index 65255c5..9528de3 100644 --- a/test/test_dep_active.c +++ b/test/test_dep_active.c @@ -4,6 +4,7 @@ #include #include "nfc/nfc.h" +#include "utils/nfc-utils.h" #define INITIATOR 0 #define TARGET 1 @@ -49,6 +50,7 @@ cut_teardown (void) struct thread_data { nfc_device *device; void *cut_test_context; + nfc_baud_rate nbr; }; void * @@ -84,10 +86,8 @@ target_thread (void *arg) size_t szRx = sizeof (abtRx); bool res = nfc_target_init (device, &nt, abtRx, &szRx); cut_assert_true (res, cut_message ("Can't initialize NFC device as target: %s", nfc_strerror (device))); - if (!res) { thread_res = -1; return (void*) thread_res; } - // First pass res = nfc_target_receive_bytes (device, abtRx, &szRx, 500); cut_assert_true (res, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); @@ -100,39 +100,6 @@ target_thread (void *arg) cut_assert_true (res, cut_message ("Can't send bytes to initiator: %s", nfc_strerror (device))); if (!res) { thread_res = -1; return (void*) thread_res; } - // Second pass - res = nfc_target_receive_bytes (device, abtRx, &szRx, 500); - cut_assert_true (res, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); - - cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); - if (!res) { thread_res = -1; return (void*) thread_res; } - - res = nfc_target_send_bytes (device, abtTx, sizeof(abtTx), 500); - cut_assert_true (res, cut_message ("Can't send bytes to initiator: %s", nfc_strerror (device))); - if (!res) { thread_res = -1; return (void*) thread_res; } - - // Third pass - res = nfc_target_receive_bytes (device, abtRx, &szRx, 500); - cut_assert_true (res, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); - - cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); - if (!res) { thread_res = -1; return (void*) thread_res; } - - res = nfc_target_send_bytes (device, abtTx, sizeof(abtTx), 500); - cut_assert_true (res, cut_message ("Can't send bytes to initiator: %s", nfc_strerror (device))); - if (!res) { thread_res = -1; return (void*) thread_res; } - - // Fourth pass - res = nfc_target_receive_bytes (device, abtRx, &szRx, 500); - cut_assert_true (res, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); - - cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); - if (!res) { thread_res = -1; return (void*) thread_res; } - - res = nfc_target_send_bytes (device, abtTx, sizeof(abtTx), 500); - cut_assert_true (res, cut_message ("Can't send bytes to initiator: %s", nfc_strerror (device))); - if (!res) { thread_res = -1; return (void*) thread_res; } - return (void *) thread_res; } @@ -142,25 +109,25 @@ initiator_thread (void *arg) intptr_t thread_res = 0; nfc_device *device = ((struct thread_data *) arg)->device; cut_set_current_test_context (((struct thread_data *) arg)->cut_test_context); + nfc_baud_rate nbr = (((struct thread_data *) arg)->nbr); /* * Wait some time for the other thread to initialise NFC device as target */ sleep (1); printf ("=========== INITIATOR %s =========\n", nfc_device_name (device)); - bool res = nfc_initiator_init (device); cut_assert_true (res, cut_message ("Can't initialize NFC device as initiator: %s", nfc_strerror (device))); if (!res) { thread_res = -1; return (void*) thread_res; } nfc_target nt; - // Active mode / 106Kbps - printf ("=========== INITIATOR %s (Active mode / 106Kbps) =========\n", nfc_device_name (device)); - res = nfc_initiator_select_dep_target (device, NDM_ACTIVE, NBR_106, NULL, &nt, 5000); + // Active mode + printf ("=========== INITIATOR %s (Active mode / %s Kbps) =========\n", nfc_device_name (device), str_nfc_baud_rate(nbr)); + res = nfc_initiator_select_dep_target (device, NDM_ACTIVE, nbr, NULL, &nt, 1000); cut_assert_true (res, cut_message ("Can't select any DEP target: %s", nfc_strerror (device))); cut_assert_equal_int (NMT_DEP, nt.nm.nmt, cut_message ("Invalid target modulation")); - cut_assert_equal_int (NBR_106, nt.nm.nbr, cut_message ("Invalid target baud rate")); + cut_assert_equal_int (nbr, nt.nm.nbr, cut_message ("Invalid target baud rate")); cut_assert_equal_memory ("\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA", 10, nt.nti.ndi.abtNFCID3, 10, cut_message ("Invalid target NFCID3")); cut_assert_equal_int (NDM_ACTIVE, nt.nti.ndi.ndm, cut_message ("Invalid target DEP mode")); cut_assert_equal_memory ("\x12\x34\x56\x78", 4, nt.nti.ndi.abtGB, nt.nti.ndi.szGB, cut_message ("Invalid target general bytes")); @@ -169,79 +136,12 @@ initiator_thread (void *arg) const uint8_t abtTx[] = "Hello DEP target!"; uint8_t abtRx[1024]; size_t szRx = sizeof (abtRx); - res = nfc_initiator_transceive_bytes (device, abtTx, sizeof (abtTx), abtRx, &szRx, 500); + res = nfc_initiator_transceive_bytes (device, abtTx, sizeof (abtTx), abtRx, &szRx, 5000); cut_assert_true (res, cut_message ("Can't transceive bytes to target: %s", nfc_strerror (device))); const uint8_t abtAttRx[] = "Hello DEP initiator!"; cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); if (!res) { thread_res = -1; return (void*) thread_res; } - - res = nfc_initiator_deselect_target (device); - cut_assert_true (res, cut_message ("Can't deselect target: %s", nfc_strerror (device))); - if (!res) { thread_res = -1; return (void*) thread_res; } - - // Active mode / 212Kbps (second pass) - printf ("=========== INITIATOR %s (Active mode / 212Kbps) =========\n", nfc_device_name (device)); - res = nfc_initiator_select_dep_target (device, NDM_ACTIVE, NBR_212, NULL, &nt, 1000); - cut_assert_true (res, cut_message ("Can't select any DEP target: %s", nfc_strerror (device))); - cut_assert_equal_int (NMT_DEP, nt.nm.nmt, cut_message ("Invalid target modulation")); - cut_assert_equal_int (NBR_212, nt.nm.nbr, cut_message ("Invalid target baud rate")); - cut_assert_equal_memory ("\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA", 10, nt.nti.ndi.abtNFCID3, 10, cut_message ("Invalid target NFCID3")); - cut_assert_equal_int (NDM_ACTIVE, nt.nti.ndi.ndm, cut_message ("Invalid target DEP mode")); - cut_assert_equal_memory ("\x12\x34\x56\x78", 4, nt.nti.ndi.abtGB, nt.nti.ndi.szGB, cut_message ("Invalid target general bytes")); - if (!res) { thread_res = -1; return (void*) thread_res; } - - szRx = sizeof (abtRx); - res = nfc_initiator_transceive_bytes (device, abtTx, sizeof (abtTx), abtRx, &szRx, 1000); - cut_assert_true (res, cut_message ("Can't transceive bytes to target: %s", nfc_strerror (device))); - - cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); - if (!res) { thread_res = -1; return (void*) thread_res; } - - res = nfc_initiator_deselect_target (device); - cut_assert_true (res, cut_message ("Can't deselect target: %s", nfc_strerror (device))); - if (!res) { thread_res = -1; return (void*) thread_res; } - - // Active mode / 212Kbps - printf ("=========== INITIATOR %s (Active mode / 212Kbps, second pass) =========\n", nfc_device_name (device)); - res = nfc_initiator_select_dep_target (device, NDM_ACTIVE, NBR_212, NULL, &nt, 1000); - cut_assert_true (res, cut_message ("Can't select any DEP target: %s", nfc_strerror (device))); - cut_assert_equal_int (NMT_DEP, nt.nm.nmt, cut_message ("Invalid target modulation")); - cut_assert_equal_int (NBR_212, nt.nm.nbr, cut_message ("Invalid target baud rate")); - cut_assert_equal_memory ("\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA", 10, nt.nti.ndi.abtNFCID3, 10, cut_message ("Invalid target NFCID3")); - cut_assert_equal_int (NDM_ACTIVE, nt.nti.ndi.ndm, cut_message ("Invalid target DEP mode")); - cut_assert_equal_memory ("\x12\x34\x56\x78", 4, nt.nti.ndi.abtGB, nt.nti.ndi.szGB, cut_message ("Invalid target general bytes")); - if (!res) { thread_res = -1; return (void*) thread_res; } - - szRx = sizeof (abtRx); - res = nfc_initiator_transceive_bytes (device, abtTx, sizeof (abtTx), abtRx, &szRx, 5000); - cut_assert_true (res, cut_message ("Can't transceive bytes to target: %s", nfc_strerror (device))); - - cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); - if (!res) { thread_res = -1; return (void*) thread_res; } - - res = nfc_initiator_deselect_target (device); - cut_assert_true (res, cut_message ("Can't deselect target: %s", nfc_strerror (device))); - if (!res) { thread_res = -1; return (void*) thread_res; } - - // Active mode / 424Kbps - printf ("=========== INITIATOR %s (Active mode / 424Kbps) =========\n", nfc_device_name (device)); - res = nfc_initiator_select_dep_target (device, NDM_ACTIVE, NBR_424, NULL, &nt, 1000); - cut_assert_true (res, cut_message ("Can't select any DEP target: %s", nfc_strerror (device))); - cut_assert_equal_int (NMT_DEP, nt.nm.nmt, cut_message ("Invalid target modulation")); - cut_assert_equal_int (NBR_424, nt.nm.nbr, cut_message ("Invalid target baud rate")); - cut_assert_equal_memory ("\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA", 10, nt.nti.ndi.abtNFCID3, 10, cut_message ("Invalid target NFCID3")); - cut_assert_equal_int (NDM_ACTIVE, nt.nti.ndi.ndm, cut_message ("Invalid target DEP mode")); - cut_assert_equal_memory ("\x12\x34\x56\x78", 4, nt.nti.ndi.abtGB, nt.nti.ndi.szGB, cut_message ("Invalid target general bytes")); - if (!res) { thread_res = -1; return (void*) thread_res; } - - szRx = sizeof (abtRx); - res = nfc_initiator_transceive_bytes (device, abtTx, sizeof (abtTx), abtRx, &szRx, 5000); - cut_assert_true (res, cut_message ("Can't transceive bytes to target: %s", nfc_strerror (device))); - - cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); - if (!res) { thread_res = -1; return (void*) thread_res; } - res = nfc_initiator_deselect_target (device); cut_assert_true (res, cut_message ("Can't deselect target: %s", nfc_strerror (device))); if (!res) { thread_res = -1; return (void*) thread_res; } @@ -253,27 +153,34 @@ void test_dep (void) { int res; + nfc_baud_rate nbrs[3] = { NBR_106, NBR_212, NBR_424}; CutTestContext *test_context = cut_get_current_test_context (); struct thread_data target_data = { .device = devices[TARGET], .cut_test_context = test_context, }; - if ((res = pthread_create (&(threads[TARGET]), NULL, target_thread, &target_data))) - cut_fail ("pthread_create() returned %d", res); - + struct thread_data initiator_data = { .device = devices[INITIATOR], .cut_test_context = test_context, }; - if ((res = pthread_create (&(threads[INITIATOR]), NULL, initiator_thread, &initiator_data))) - cut_fail ("pthread_create() returned %d", res); + + for (int i=0; i<3; i++) { + initiator_data.nbr = nbrs[i]; + + if ((res = pthread_create (&(threads[TARGET]), NULL, target_thread, &target_data))) + cut_fail ("pthread_create() returned %d", res); + if ((res = pthread_create (&(threads[INITIATOR]), NULL, initiator_thread, &initiator_data))) + cut_fail ("pthread_create() returned %d", res); - if ((res = pthread_join (threads[INITIATOR], (void *) &result[INITIATOR]))) - cut_fail ("pthread_join() returned %d", res); - if ((res = pthread_join (threads[TARGET], (void *) &result[TARGET]))) - cut_fail ("pthread_join() returned %d", res); + if ((res = pthread_join (threads[INITIATOR], (void *) &result[INITIATOR]))) + cut_fail ("pthread_join() returned %d", res); + if ((res = pthread_join (threads[TARGET], (void *) &result[TARGET]))) + cut_fail ("pthread_join() returned %d", res); - cut_assert_equal_int (0, result[INITIATOR], cut_message ("Unexpected initiator return code")); - cut_assert_equal_int (0, result[TARGET], cut_message ("Unexpected target return code")); + cut_assert_equal_int (0, result[INITIATOR], cut_message ("Unexpected initiator return code")); + cut_assert_equal_int (0, result[TARGET], cut_message ("Unexpected target return code")); + } + } diff --git a/utils/nfc-utils.h b/utils/nfc-utils.h index 50abdf3..4243ffc 100644 --- a/utils/nfc-utils.h +++ b/utils/nfc-utils.h @@ -94,6 +94,7 @@ void print_nfc_iso14443b2ct_info (const nfc_iso14443b2ct_info nci, bool verbo void print_nfc_felica_info (const nfc_felica_info nfi, bool verbose); void print_nfc_jewel_info (const nfc_jewel_info nji, bool verbose); void print_nfc_dep_info (const nfc_dep_info ndi, bool verbose); +const char * str_nfc_baud_rate (const nfc_baud_rate nbr); void print_nfc_target (const nfc_target nt, bool verbose);