[Spice-devel] [PATCH libcacard 17/45] tests: Direct responses in the test suite.
Jakub Jelen
jjelen at redhat.com
Tue Jul 31 14:50:11 UTC 2018
Rewrite tests to read buffers directly without explicit GET RESPONSE APDU requests.
Test GET RESPONSE APDU separately.
Signed-off-by: Jakub Jelen <jjelen at redhat.com>
Reviewed-by: Robert Relyea <rrelyea at redhat.com>
---
tests/libcacard.c | 171 ++++++++++++++++++++++++++++------------------
1 file changed, 106 insertions(+), 65 deletions(-)
diff --git a/tests/libcacard.c b/tests/libcacard.c
index 38a5ae7..adbf0f8 100644
--- a/tests/libcacard.c
+++ b/tests/libcacard.c
@@ -367,33 +367,20 @@ static void get_acr(VReader *reader)
static void read_buffer(VReader *reader, uint8_t type)
{
- int dwRecvLength = APDUBufSize, dwLength, dwTagLength, offset;
+ int dwRecvLength = APDUBufSize, dwLength, dwReadLength, offset;
VReaderStatus status;
uint8_t pbRecvBuffer[APDUBufSize];
- uint8_t getresp[] = {
- /* Get Response (max we can get) */
- 0x00, 0xc0, 0x00, 0x00, 0xFF
- };
uint8_t read_buffer[] = {
- /*Read Buffer OFFSET TYPE LENGTH */
- 0x80, 0x52, 0x00, 0x00, 0x02, 0x01, 0x02
+ /*Read Buffer OFFSET TYPE LENGTH a_Le */
+ 0x80, 0x52, 0x00, 0x00, 0x02, 0x01, 0x02, 0x02
};
- dwRecvLength = 2;
+ dwRecvLength = 4;
read_buffer[5] = type;
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);
-
- 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);
@@ -402,50 +389,81 @@ static void read_buffer(VReader *reader, uint8_t type)
offset = 0x02;
do {
/* This returns only success -- get response is needed to get the actual data */
- dwRecvLength = 2;
- dwTagLength = MIN(255, dwLength);
+ dwReadLength = MIN(255, dwLength);
+ dwRecvLength = dwReadLength+2;
read_buffer[2] = (unsigned char) ((offset >> 8) & 0xff);
read_buffer[3] = (unsigned char) (offset & 0xff);
- read_buffer[6] = (unsigned char) (dwTagLength);
+ read_buffer[6] = (unsigned char) (dwReadLength);
+ read_buffer[7] = (unsigned char) (dwReadLength);
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);
-
- dwRecvLength = dwTagLength + 2;
- 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[dwLength], ==, VCARD7816_SW1_SUCCESS);
- g_assert_cmphex(pbRecvBuffer[dwLength+1], ==, 0x00);
+ g_assert_cmpint(dwRecvLength, ==, dwReadLength + 2);
+ g_assert_cmphex(pbRecvBuffer[dwRecvLength-2], ==, VCARD7816_SW1_SUCCESS);
+ g_assert_cmphex(pbRecvBuffer[dwRecvLength-1], ==, 0x00);
- dwLength -= dwTagLength;
+ dwLength -= dwReadLength;
+ offset += dwLength;
} while (dwLength != 0);
}
-static void test_cac_pki(void)
+static void select_aid(VReader *reader, int type)
{
- VReader *reader = vreader_get_reader_by_id(0);
VReaderStatus status;
int dwRecvLength = APDUBufSize;
uint8_t pbRecvBuffer[APDUBufSize];
- uint8_t selfile0[] = {
+ uint8_t selfile_ccc[] = {
+ /* Select CCC Applet */
+ 0x00, 0xa4, 0x04, 0x00, 0x07, 0xa0, 0x00, 0x00, 0x01, 0x16, 0xDB, 0x00
+ };
+ uint8_t selfile_aca[] = {
+ /* Select ACA Applet */
+ 0x00, 0xa4, 0x04, 0x00, 0x07, 0xa0, 0x00, 0x00, 0x00, 0x79, 0x03, 0x00
+ };
+ uint8_t selfile_pki[] = {
/* Select first PKI Applet */
0x00, 0xa4, 0x04, 0x00, 0x07, 0xa0, 0x00, 0x00, 0x00, 0x79, 0x01, 0x00
};
+ uint8_t *selfile = NULL;
+ size_t selfile_len = 0;
+
+ switch (type) {
+ case TEST_PKI:
+ selfile = selfile_pki;
+ selfile_len = sizeof(selfile_pki);
+ break;
+
+ case TEST_CCC:
+ selfile = selfile_ccc;
+ selfile_len = sizeof(selfile_ccc);
+ break;
+
+ case TEST_ACA:
+ selfile = selfile_aca;
+ selfile_len = sizeof(selfile_aca);
+ break;
+
+ default:
+ g_assert_not_reached();
+ }
+ g_assert_nonnull(selfile);
g_assert_nonnull(reader);
status = vreader_xfr_bytes(reader,
- selfile0, sizeof(selfile0),
+ selfile, selfile_len,
pbRecvBuffer, &dwRecvLength);
g_assert_cmpint(status, ==, VREADER_OK);
g_assert_cmphex(pbRecvBuffer[0], ==, VCARD7816_SW1_SUCCESS);
g_assert_cmphex(pbRecvBuffer[1], ==, 0x00);
+}
+
+static void test_cac_pki(void)
+{
+ VReader *reader = vreader_get_reader_by_id(0);
+
+ /* select the first PKI applet */
+ select_aid(reader, TEST_PKI);
/* get properties */
get_properties(reader, TEST_PKI);
@@ -462,21 +480,9 @@ static void test_cac_pki(void)
static void test_cac_ccc(void)
{
VReader *reader = vreader_get_reader_by_id(0);
- VReaderStatus status;
- int dwRecvLength = APDUBufSize;
- uint8_t pbRecvBuffer[APDUBufSize];
- uint8_t selfile0[] = {
- /* Select first PKI Applet */
- 0x00, 0xa4, 0x04, 0x00, 0x07, 0xa0, 0x00, 0x00, 0x01, 0x16, 0xDB, 0x00
- };
- g_assert_nonnull(reader);
- status = vreader_xfr_bytes(reader,
- selfile0, sizeof(selfile0),
- pbRecvBuffer, &dwRecvLength);
- g_assert_cmpint(status, ==, VREADER_OK);
- g_assert_cmphex(pbRecvBuffer[0], ==, VCARD7816_SW1_SUCCESS);
- g_assert_cmphex(pbRecvBuffer[1], ==, 0x00);
+ /* select the CCC */
+ select_aid(reader, TEST_CCC);
/* get properties */
get_properties(reader, TEST_CCC);
@@ -493,21 +499,9 @@ static void test_cac_ccc(void)
static void test_cac_aca(void)
{
VReader *reader = vreader_get_reader_by_id(0);
- VReaderStatus status;
- int dwRecvLength = APDUBufSize;
- uint8_t pbRecvBuffer[APDUBufSize];
- uint8_t selfile0[] = {
- /* Select first PKI Applet */
- 0x00, 0xa4, 0x04, 0x00, 0x07, 0xa0, 0x00, 0x00, 0x00, 0x79, 0x03, 0x00
- };
- g_assert_nonnull(reader);
- status = vreader_xfr_bytes(reader,
- selfile0, sizeof(selfile0),
- pbRecvBuffer, &dwRecvLength);
- g_assert_cmpint(status, ==, VREADER_OK);
- g_assert_cmphex(pbRecvBuffer[0], ==, VCARD7816_SW1_SUCCESS);
- g_assert_cmphex(pbRecvBuffer[1], ==, 0x00);
+ /* select the ACA */
+ select_aid(reader, TEST_ACA);
/* get properties */
get_properties(reader, TEST_ACA);
@@ -534,6 +528,52 @@ 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_aid(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 */
+}
+
int main(int argc, char *argv[])
{
int ret;
@@ -550,6 +590,7 @@ int main(int argc, char *argv[])
g_test_add_func("/libcacard/cac-pki", test_cac_pki);
g_test_add_func("/libcacard/cac-ccc", test_cac_ccc);
g_test_add_func("/libcacard/cac-aca", test_cac_aca);
+ g_test_add_func("/libcacard/get-response", test_get_response);
g_test_add_func("/libcacard/remove", test_remove);
ret = g_test_run();
--
2.17.1
More information about the Spice-devel
mailing list