[Spice-devel] [PATCH libcacard v2 23/35] tests: Extend the hwtests with get_response
Jakub Jelen
jjelen at redhat.com
Thu Aug 2 09:43:55 UTC 2018
Signed-off-by: Jakub Jelen <jjelen at redhat.com>
Reviewed-by: Robert Relyea <rrelyea at redhat.com>
---
tests/common.c | 73 +++++++++++++++++++++++++++++++++++++++++++++++
tests/common.h | 2 ++
tests/hwtests.c | 23 +++++++++++++++
tests/libcacard.c | 46 -----------------------------
4 files changed, 98 insertions(+), 46 deletions(-)
diff --git a/tests/common.c b/tests/common.c
index 21a3099..89c566a 100644
--- a/tests/common.c
+++ b/tests/common.c
@@ -509,6 +509,79 @@ void test_empty_applets(void)
vreader_free(reader); /* get by id ref */
}
+/*
+ * Check that access method without provided buffer returns valid
+ * SW and allow us to get the response with the following APDU
+ *
+ * opensc-tool -s 00A4040007A000000116DB00 -s 80520000020102 -s 00C0000002 \
+ * -s 00520002020134 -s 00C0000034
+ */
+void test_get_response(void)
+{
+ VReader *reader = vreader_get_reader_by_id(0);
+ int dwRecvLength = APDUBufSize, dwLength;
+ VReaderStatus status;
+ uint8_t pbRecvBuffer[APDUBufSize];
+ uint8_t getresp[] = {
+ /* Get Response (max we can get) */
+ 0x00, 0xc0, 0x00, 0x00, 0x00
+ };
+ uint8_t read_buffer[] = {
+ /*Read Buffer OFFSET TYPE LENGTH */
+ 0x00, 0x52, 0x00, 0x00, 0x02, 0x01, 0x02 /* no L_e */
+ };
+
+ /* select CCC */
+ select_applet(reader, TEST_CCC);
+
+ /* read buffer without response buffer */
+ dwRecvLength = 2;
+ status = vreader_xfr_bytes(reader,
+ read_buffer, sizeof(read_buffer),
+ pbRecvBuffer, &dwRecvLength);
+ g_assert_cmpint(status, ==, VREADER_OK);
+ g_assert_cmpint(dwRecvLength, ==, 2);
+ g_assert_cmpint(pbRecvBuffer[0], ==, VCARD7816_SW1_RESPONSE_BYTES);
+ g_assert_cmpint(pbRecvBuffer[1], ==, 0x02);
+
+ /* fetch the actual response */
+ dwRecvLength = 4;
+ getresp[4] = 0x02;
+ status = vreader_xfr_bytes(reader,
+ getresp, sizeof(getresp),
+ pbRecvBuffer, &dwRecvLength);
+ g_assert_cmpint(status, ==, VREADER_OK);
+ g_assert_cmpint(dwRecvLength, ==, 4);
+ g_assert_cmphex(pbRecvBuffer[2], ==, VCARD7816_SW1_SUCCESS);
+ g_assert_cmphex(pbRecvBuffer[3], ==, 0x00);
+
+ /* the same with offset */
+ dwLength = (pbRecvBuffer[0] & 0xff) | ((pbRecvBuffer[1] << 8) & 0xff);
+ dwRecvLength = dwLength + 2;
+ read_buffer[3] = 0x02; // offset
+ read_buffer[6] = dwLength;
+ status = vreader_xfr_bytes(reader,
+ read_buffer, sizeof(read_buffer),
+ pbRecvBuffer, &dwRecvLength);
+ g_assert_cmpint(status, ==, VREADER_OK);
+ g_assert_cmpint(dwRecvLength, ==, 2);
+ g_assert_cmpint(pbRecvBuffer[0], ==, VCARD7816_SW1_RESPONSE_BYTES);
+ g_assert_cmpint(pbRecvBuffer[1], ==, dwLength);
+
+ /* fetch the actual response */
+ dwRecvLength = dwLength + 2;
+ getresp[4] = dwLength;
+ status = vreader_xfr_bytes(reader,
+ getresp, sizeof(getresp),
+ pbRecvBuffer, &dwRecvLength);
+ g_assert_cmpint(status, ==, VREADER_OK);
+ g_assert_cmpint(dwRecvLength, ==, dwLength + 2);
+ g_assert_cmphex(pbRecvBuffer[dwRecvLength-2], ==, VCARD7816_SW1_SUCCESS);
+ g_assert_cmphex(pbRecvBuffer[dwRecvLength-1], ==, 0x00);
+
+ vreader_free(reader); /* get by id ref */
+}
+
int
isHWTests(void)
{
diff --git a/tests/common.h b/tests/common.h
index 4ba2619..871b013 100644
--- a/tests/common.h
+++ b/tests/common.h
@@ -36,6 +36,8 @@ void do_sign(VReader *reader);
void test_empty_applets(void);
+void test_get_response(void);
+
int isHWTests(void);
void setHWTests(int);
diff --git a/tests/hwtests.c b/tests/hwtests.c
index fe64927..7beebac 100644
--- a/tests/hwtests.c
+++ b/tests/hwtests.c
@@ -104,6 +104,7 @@ static void test_list(void)
if (vreader_card_is_present(r) == VREADER_OK) {
cards++;
}
+ vreader_free(r);
}
if (cards == 0) {
vreader_list_delete(list);
@@ -151,6 +152,7 @@ static void test_passthrough_applets(void)
/* Skip the HW tests without physical card */
if (vreader_card_is_present(reader) != VREADER_OK) {
+ vreader_free(reader);
g_test_skip("No physical card found");
return;
}
@@ -192,6 +194,7 @@ static void test_login(void)
/* Skip the HW tests without physical card */
if (vreader_card_is_present(reader) != VREADER_OK) {
+ vreader_free(reader);
g_test_skip("No physical card found");
return;
}
@@ -210,6 +213,7 @@ static void test_sign(void)
/* Skip the HW tests without physical card */
if (vreader_card_is_present(reader) != VREADER_OK) {
+ vreader_free(reader);
g_test_skip("No physical card found");
return;
}
@@ -243,9 +247,27 @@ static void test_empty_applets_hw(void) {
vreader_free(reader); /* get by id ref */
+ /* run the actual test */
test_empty_applets();
}
+static void test_get_response_hw(void) {
+
+ VReader *reader = vreader_get_reader_by_id(0);
+
+ /* Skip the HW tests without physical card */
+ if (vreader_card_is_present(reader) != VREADER_OK) {
+ vreader_free(reader);
+ g_test_skip("No physical card found");
+ return;
+ }
+
+ vreader_free(reader); /* get by id ref */
+
+ /* run the actual test */
+ test_get_response();
+}
+
static void libcacard_finalize(void)
{
VReader *reader = vreader_get_reader_by_id(0);
@@ -276,6 +298,7 @@ int main(int argc, char *argv[])
g_test_add_func("/hw-tests/login", test_login);
g_test_add_func("/hw-tests/sign", test_sign);
g_test_add_func("/hw-tests/empty-applets", test_empty_applets_hw);
+ g_test_add_func("/hw-tests/get-response", test_get_response_hw);
ret = g_test_run();
diff --git a/tests/libcacard.c b/tests/libcacard.c
index 2aea3f0..8c63152 100644
--- a/tests/libcacard.c
+++ b/tests/libcacard.c
@@ -507,52 +507,6 @@ static void test_remove(void)
g_assert_null(reader);
}
-/*
- * Check that access method without provided buffer returns valid
- * SW and allow us to get the response with the following APDU
- */
-static void test_get_response(void)
-{
- VReader *reader = vreader_get_reader_by_id(0);
- int dwRecvLength = APDUBufSize;
- VReaderStatus status;
- uint8_t pbRecvBuffer[APDUBufSize];
- uint8_t getresp[] = {
- /* Get Response (max we can get) */
- 0x00, 0xc0, 0x00, 0x00, 0x00
- };
- uint8_t read_buffer[] = {
- /*Read Buffer OFFSET TYPE LENGTH */
- 0x80, 0x52, 0x00, 0x00, 0x02, 0x01, 0x02 /* no L_e */
- };
-
- /* select CCC */
- select_applet(reader, TEST_CCC);
-
- /* read buffer without response buffer */
- dwRecvLength = 2;
- read_buffer[5] = 0x01;
- status = vreader_xfr_bytes(reader,
- read_buffer, sizeof(read_buffer),
- pbRecvBuffer, &dwRecvLength);
- g_assert_cmpint(status, ==, VREADER_OK);
- g_assert_cmpint(dwRecvLength, ==, 2);
- g_assert_cmpint(pbRecvBuffer[0], ==, VCARD7816_SW1_RESPONSE_BYTES);
- g_assert_cmpint(pbRecvBuffer[1], ==, 0x02);
-
- /* fetch the actual response */
- dwRecvLength = 4;
- status = vreader_xfr_bytes(reader,
- getresp, sizeof(getresp),
- pbRecvBuffer, &dwRecvLength);
- g_assert_cmpint(status, ==, VREADER_OK);
- g_assert_cmpint(dwRecvLength, ==, 4);
- g_assert_cmphex(pbRecvBuffer[2], ==, VCARD7816_SW1_SUCCESS);
- g_assert_cmphex(pbRecvBuffer[3], ==, 0x00);
-
- vreader_free(reader); /* get by id ref */
-}
-
static void libcacard_finalize(void)
{
VReader *reader = vreader_get_reader_by_id(0);
--
2.17.1
More information about the Spice-devel
mailing list