[Spice-devel] [PATCH libcacard v2 27/35] tests: GP Card Manager and responses on SELECT APDU
Jakub Jelen
jjelen at redhat.com
Thu Aug 2 09:43:59 UTC 2018
Signed-off-by: Jakub Jelen <jjelen at redhat.com>
Reviewed-by: Robert Relyea <rrelyea at redhat.com>
---
tests/common.c | 20 +++++++++++++++++---
tests/common.h | 2 ++
tests/libcacard.c | 35 +++++++++++++++++++++++++++++++++++
3 files changed, 54 insertions(+), 3 deletions(-)
diff --git a/tests/common.c b/tests/common.c
index 40e5525..ecd16c0 100644
--- a/tests/common.c
+++ b/tests/common.c
@@ -57,7 +57,8 @@ void select_coid_bad(VReader *reader, unsigned char *coid)
}
-void select_aid(VReader *reader, unsigned char *aid, unsigned int aid_len)
+int select_aid_response(VReader *reader, unsigned char *aid,
+ unsigned int aid_len, int response_len)
{
VReaderStatus status;
int dwRecvLength = APDUBufSize;
@@ -77,8 +78,21 @@ void select_aid(VReader *reader, unsigned char *aid, unsigned int aid_len)
selfile, selfile_len,
pbRecvBuffer, &dwRecvLength);
g_assert_cmpint(status, ==, VREADER_OK);
- g_assert_cmphex(pbRecvBuffer[dwRecvLength-2], ==, VCARD7816_SW1_RESPONSE_BYTES);
- g_assert_cmphex(pbRecvBuffer[dwRecvLength-1], >, 0);
+ if (response_len > 0) {
+ /* we expect specific amount of response bytes */
+ g_assert_cmphex(pbRecvBuffer[dwRecvLength-2], ==, VCARD7816_SW1_RESPONSE_BYTES);
+ g_assert_cmphex(pbRecvBuffer[dwRecvLength-1], ==, response_len);
+ } else {
+ /* the default response length is 13 (FCI stub) */
+ g_assert_cmphex(pbRecvBuffer[dwRecvLength-2], ==, VCARD7816_SW1_RESPONSE_BYTES);
+ g_assert_cmphex(pbRecvBuffer[dwRecvLength-1], ==, 0x0d);
+ }
+ return pbRecvBuffer[dwRecvLength-2];
+}
+
+void select_aid(VReader *reader, unsigned char *aid, unsigned int aid_len)
+{
+ (void) select_aid_response(reader, aid, aid_len, 0);
}
void get_properties_coid(VReader *reader, const unsigned char coid[2],
diff --git a/tests/common.h b/tests/common.h
index 21144a1..c02ea6b 100644
--- a/tests/common.h
+++ b/tests/common.h
@@ -27,6 +27,8 @@ enum {
void select_coid_good(VReader *reader, unsigned char *coid);
void select_coid_bad(VReader *reader, unsigned char *coid);
+int select_aid_response(VReader *reader, unsigned char *aid,
+ unsigned int aid_len, int response);
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 f4524ff..b41a56c 100644
--- a/tests/libcacard.c
+++ b/tests/libcacard.c
@@ -547,6 +547,40 @@ static void test_select_coid(void)
vreader_free(reader); /* get by id ref */
}
+static void test_gp_applet(void)
+{
+ int dwRecvLength = APDUBufSize;
+ VReaderStatus status;
+ uint8_t pbRecvBuffer[APDUBufSize];
+ uint8_t gp_aid[] = {
+ 0xA0, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00
+ };
+ uint8_t getresp[] = {
+ /* Get Response (max we can get) */
+ 0x00, 0xc0, 0x00, 0x00, 0x00
+ };
+ VReader *reader = vreader_get_reader_by_id(0);
+
+ /* select GP and wait for the response bytes */
+ select_aid_response(reader, gp_aid, sizeof(gp_aid), 0x1b);
+
+ /* read the response from the card */
+ dwRecvLength = APDUBufSize;
+ status = vreader_xfr_bytes(reader,
+ getresp, sizeof(getresp),
+ pbRecvBuffer, &dwRecvLength);
+ g_assert_cmpint(status, ==, VREADER_OK);
+ g_assert_cmpint(dwRecvLength, >, 2);
+ g_assert_cmphex(pbRecvBuffer[dwRecvLength-2], ==, VCARD7816_SW1_SUCCESS);
+ g_assert_cmphex(pbRecvBuffer[dwRecvLength-1], ==, 0x00);
+
+ /* We made sure the selection of other applets does not return anything
+ * in select_aid()
+ */
+
+ vreader_free(reader); /* get by id ref */
+}
+
static void libcacard_finalize(void)
{
VReader *reader = vreader_get_reader_by_id(0);
@@ -583,6 +617,7 @@ int main(int argc, char *argv[])
g_test_add_func("/libcacard/login", test_login);
g_test_add_func("/libcacard/sign", test_sign);
g_test_add_func("/libcacard/empty-applets", test_empty_applets);
+ g_test_add_func("/libcacard/gp-applet", test_gp_applet);
g_test_add_func("/libcacard/remove", test_remove);
ret = g_test_run();
--
2.17.1
More information about the Spice-devel
mailing list