[Spice-devel] [PATCH libcacard v2 24/35] tests: Select OID

Jakub Jelen jjelen at redhat.com
Thu Aug 2 09:43:56 UTC 2018


Signed-off-by: Jakub Jelen <jjelen at redhat.com>
Reviewed-by: Robert Relyea <rrelyea at redhat.com>
---
 tests/common.c    | 43 +++++++++++++++++++++++++++++++++++++++++--
 tests/common.h    |  3 +++
 tests/libcacard.c | 41 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 85 insertions(+), 2 deletions(-)

diff --git a/tests/common.c b/tests/common.c
index 89c566a..34d411f 100644
--- a/tests/common.c
+++ b/tests/common.c
@@ -18,6 +18,45 @@ int key_bits = 0;
 int hw_tests = 0;
 
 
+static void select_coid(VReader *reader, unsigned char *coid,
+                        int expect_success)
+{
+    VReaderStatus status;
+    int dwRecvLength = APDUBufSize;
+    uint8_t pbRecvBuffer[APDUBufSize];
+    uint8_t selfile[] = {
+        0x00, 0xa4, 0x02, 0x00, 0x02, 0x00, 0x00
+    };
+    size_t selfile_len = sizeof(selfile);
+
+    memcpy(&selfile[5], coid, 2);
+
+    g_debug("%s: Select OID 0x%02x 0x%02x", __func__, coid[0], coid[1]);
+    g_assert_nonnull(reader);
+    status = vreader_xfr_bytes(reader,
+                               selfile, selfile_len,
+                               pbRecvBuffer, &dwRecvLength);
+    g_assert_cmpint(status, ==, VREADER_OK);
+    if (expect_success) {
+        g_assert_cmphex(pbRecvBuffer[dwRecvLength-2], ==, VCARD7816_SW1_SUCCESS);
+        g_assert_cmphex(pbRecvBuffer[dwRecvLength-1], ==, 0x00);
+    } else {
+        g_assert_cmphex(pbRecvBuffer[dwRecvLength-2], ==, VCARD7816_SW1_P1_P2_ERROR);
+        g_assert_cmphex(pbRecvBuffer[dwRecvLength-1], ==, 0x82);
+    }
+}
+
+void select_coid_good(VReader *reader, unsigned char *coid)
+{
+    select_coid(reader, coid, 1);
+}
+
+void select_coid_bad(VReader *reader, unsigned char *coid)
+{
+    select_coid(reader, coid, 0);
+}
+
+
 void select_aid(VReader *reader, unsigned char *aid, unsigned int aid_len)
 {
     VReaderStatus status;
@@ -31,7 +70,7 @@ void select_aid(VReader *reader, unsigned char *aid, unsigned int aid_len)
     g_assert_cmpint(aid_len, ==, 7);
     memcpy(&selfile[5], aid, aid_len);
 
-    g_debug("%s: Add applet with AID 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x",
+    g_debug("%s: Select applet with AID 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x",
         __func__, aid[0], aid[1], aid[2], aid[3], aid[4], aid[5], aid[6]);
     g_assert_nonnull(reader);
     status = vreader_xfr_bytes(reader,
@@ -278,7 +317,7 @@ void read_buffer(VReader *reader, uint8_t type, int object_type)
         g_assert_cmphex(pbRecvBuffer[dwRecvLength-2], ==, VCARD7816_SW1_SUCCESS);
         g_assert_cmphex(pbRecvBuffer[dwRecvLength-1], ==, 0x00);
 
-        memcpy(data + offset - 2, pbRecvBuffer, dwReadLength - 2);
+        memcpy(data + offset - 2, pbRecvBuffer, dwReadLength);
         offset += dwLength;
         dwLength -= dwReadLength;
     } while (dwLength != 0);
diff --git a/tests/common.h b/tests/common.h
index 871b013..0c19ff8 100644
--- a/tests/common.h
+++ b/tests/common.h
@@ -24,6 +24,9 @@ enum {
     TEST_EMPTY = 6,
 };
 
+void select_coid_good(VReader *reader, unsigned char *coid);
+void select_coid_bad(VReader *reader, unsigned char *coid);
+
 void select_aid(VReader *reader, unsigned char *aid, unsigned int aid_len);
 void select_applet(VReader *reader, int type);
 
diff --git a/tests/libcacard.c b/tests/libcacard.c
index 8c63152..f4524ff 100644
--- a/tests/libcacard.c
+++ b/tests/libcacard.c
@@ -507,6 +507,46 @@ static void test_remove(void)
     g_assert_null(reader);
 }
 
+static void test_select_coid(void)
+{
+    VReader *reader = vreader_get_reader_by_id(0);
+    unsigned char coid[] = "\xDB\x00";
+    uint8_t acf_aid[] = {
+        0xA0, 0x00, 0x00, 0x01, 0x16, 0x30, 0x00
+    };
+
+    /* select the CCC */
+    select_applet(reader, TEST_CCC);
+
+    /* get properties */
+    get_properties(reader, TEST_CCC);
+
+    /* select existing OID */
+    select_coid_good(reader, coid);
+
+    /* select non-existing OID */
+    coid[1] = 0xDB;
+    select_coid_bad(reader, coid);
+
+    /* select the ACF */
+    select_aid(reader, acf_aid, sizeof(acf_aid));
+
+    /* select existing default OID */
+    coid[0] = 0x30;
+    coid[1] = 0x00;
+    select_coid_good(reader, coid);
+
+    /* select existing non-default OID */
+    coid[0] = 0x90;
+    select_coid_good(reader, coid);
+
+    /* select non-existing OID */
+    coid[1] = 0x90;
+    select_coid_bad(reader, coid);
+
+    vreader_free(reader); /* get by id ref */
+}
+
 static void libcacard_finalize(void)
 {
     VReader *reader = vreader_get_reader_by_id(0);
@@ -535,6 +575,7 @@ int main(int argc, char *argv[])
     g_test_add_func("/libcacard/list", test_list);
     g_test_add_func("/libcacard/card-remove-insert", test_card_remove_insert);
     g_test_add_func("/libcacard/xfer", test_xfer);
+    g_test_add_func("/libcacard/select-coid", test_select_coid);
     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);
-- 
2.17.1



More information about the Spice-devel mailing list