LCOV - code coverage report
Current view: top level - tests - atr.c (source / functions) Coverage Total Hit
Test: Code coverage Lines: 93.4 % 61 57
Test Date: 2025-12-04 16:15:14 Functions: 100.0 % 5 5
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 58.3 % 24 14

             Branch data     Line data    Source code
       1                 :             : /*
       2                 :             :  * Test general functionality of software emulated smart card
       3                 :             :  *
       4                 :             :  * Copyright 2022 Red Hat, Inc.
       5                 :             :  *
       6                 :             :  * Authors:
       7                 :             :  *  Jakub Jelen <jjelen@redhat.com>
       8                 :             :  *
       9                 :             :  * This code is licensed under the GNU LGPL, version 2.1 or later.
      10                 :             :  * See the COPYING file in the top-level directory.
      11                 :             :  */
      12                 :             : #include <glib.h>
      13                 :             : #include "vreader.c"
      14                 :             : #include "libcacard.h"
      15                 :             : #include "common.h"
      16                 :             : 
      17                 :             : #define ARGS "db=\"sql:%s\" use_hw=no soft=(,Test,CAC,,cert1,cert2,cert3)"
      18                 :             : 
      19                 :             : static GMainLoop *loop;
      20                 :             : static GThread *thread;
      21                 :             : static guint nreaders;
      22                 :             : static GMutex mutex;
      23                 :             : static GCond cond;
      24                 :             : 
      25                 :             : static gpointer
      26                 :           1 : events_thread(G_GNUC_UNUSED gpointer arg)
      27                 :             : {
      28                 :             :     unsigned int reader_id;
      29                 :             :     VEvent *event;
      30                 :             : 
      31                 :             :     while (1) {
      32                 :           4 :         event = vevent_wait_next_vevent();
      33         [ +  + ]:           4 :         if (event->type == VEVENT_LAST) {
      34                 :           1 :             vevent_delete(event);
      35                 :             :             break;
      36                 :             :         }
      37         [ +  - ]:           3 :         reader_id = vreader_get_id(event->reader);
      38         [ +  + ]:           3 :         if (reader_id == VSCARD_UNDEFINED_READER_ID) {
      39                 :           1 :             g_mutex_lock(&mutex);
      40         [ +  - ]:           1 :             vreader_set_id(event->reader, nreaders++);
      41                 :           1 :             g_cond_signal(&cond);
      42                 :           1 :             g_mutex_unlock(&mutex);
      43                 :             :             reader_id = vreader_get_id(event->reader);
      44                 :             :         }
      45         [ -  + ]:           3 :         switch (event->type) {
      46                 :             :         case VEVENT_READER_INSERT:
      47                 :             :         case VEVENT_READER_REMOVE:
      48                 :             :         case VEVENT_CARD_INSERT:
      49                 :             :         case VEVENT_CARD_REMOVE:
      50                 :             :             break;
      51                 :           0 :         case VEVENT_LAST:
      52                 :             :         default:
      53                 :           0 :             g_warn_if_reached();
      54                 :           0 :             break;
      55                 :             :         }
      56                 :           3 :         vevent_delete(event);
      57                 :             :     }
      58                 :             : 
      59                 :           1 :     return NULL;
      60                 :             : }
      61                 :             : 
      62                 :           1 : static void libcacard_init(void)
      63                 :             : {
      64                 :             :     VCardEmulOptions *command_line_options = NULL;
      65                 :           1 :     gchar *dbdir = g_test_build_filename(G_TEST_DIST, "db", NULL);
      66                 :           1 :     gchar *args = g_strdup_printf(ARGS, dbdir);
      67                 :             :     VReader *r;
      68                 :             :     VCardEmulError ret;
      69                 :             : 
      70                 :           1 :     thread = g_thread_new("test/events", events_thread, NULL);
      71                 :             : 
      72                 :           1 :     command_line_options = vcard_emul_options(args);
      73                 :           1 :     ret = vcard_emul_init(command_line_options);
      74         [ -  + ]:           1 :     g_assert_cmpint(ret, ==, VCARD_EMUL_OK);
      75                 :             : 
      76                 :           1 :     r = vreader_get_reader_by_name("Test");
      77         [ -  + ]:           1 :     g_assert_nonnull(r);
      78                 :           1 :     vreader_free(r); /* get by name ref */
      79                 :             : 
      80                 :           1 :     g_mutex_lock(&mutex);
      81         [ -  + ]:           1 :     while (nreaders == 0)
      82                 :           0 :         g_cond_wait(&cond, &mutex);
      83                 :           1 :     g_mutex_unlock(&mutex);
      84                 :             : 
      85                 :           1 :     g_free(args);
      86                 :           1 :     g_free(dbdir);
      87                 :           1 : }
      88                 :             : 
      89                 :           1 : static void libcacard_finalize(void)
      90                 :             : {
      91                 :           1 :     VReader *reader = vreader_get_reader_by_id(0);
      92                 :             : 
      93                 :             :     /* This actually still generates events */
      94         [ +  - ]:           1 :     if (reader) /*if /remove didn't run */
      95                 :           1 :         vreader_remove_reader(reader);
      96                 :             : 
      97                 :             :     /* This probably supposed to be a event that terminates the loop */
      98                 :           1 :     vevent_queue_vevent(vevent_new(VEVENT_LAST, reader, NULL));
      99                 :             : 
     100                 :             :     /* join */
     101                 :           1 :     g_thread_join(thread);
     102                 :             : 
     103                 :             :     /* Clean up */
     104                 :           1 :     vreader_free(reader);
     105                 :             : 
     106                 :           1 :     vcard_emul_finalize();
     107                 :           1 : }
     108                 :             : 
     109                 :             : #define MAX_ATR_LEN 100
     110                 :             : #define NSS_ATR "\x3B\x89\x00\x56\x43\x41\x52\x44\x5F\x4E\x53\x53"
     111                 :             : #define NSS_ATR_LEN (sizeof(NSS_ATR) - 1)
     112                 :           1 : static void test_atr_default(void)
     113                 :             : {
     114                 :           1 :     VReader *reader = vreader_get_reader_by_id(0);
     115                 :             :     VCard *card;
     116                 :           1 :     unsigned char *atr = g_malloc0(MAX_ATR_LEN);
     117                 :           1 :     int atr_len = MAX_ATR_LEN;
     118                 :             : 
     119                 :             :     /* Replace the CAC ATR handled with default one */
     120                 :           1 :     card = vreader_get_card(reader);
     121                 :           1 :     vcard_set_atr_func(card, NULL);
     122                 :           1 :     vcard_free(card);
     123                 :             : 
     124                 :             :     vreader_power_off(reader);
     125                 :             :     vreader_power_on(reader, atr, &atr_len);
     126   [ -  +  -  +  :           1 :     g_assert_cmpmem(atr, atr_len, NSS_ATR, NSS_ATR_LEN);
                   -  + ]
     127                 :             : 
     128                 :           1 :     vreader_free(reader); /* get by id ref */
     129                 :           1 :     g_free(atr);
     130                 :           1 : }
     131                 :             : 
     132                 :             : 
     133                 :           1 : int main(int argc, char *argv[])
     134                 :             : {
     135                 :             :     int ret;
     136                 :             : 
     137                 :           1 :     g_test_init(&argc, &argv, NULL);
     138                 :             : 
     139                 :           1 :     loop = g_main_loop_new(NULL, TRUE);
     140                 :             : 
     141                 :           1 :     libcacard_init();
     142                 :             : 
     143                 :           1 :     g_test_add_func("/atr/default", test_atr_default);
     144                 :             : 
     145                 :           1 :     ret = g_test_run();
     146                 :             : 
     147                 :           1 :     g_main_loop_unref(loop);
     148                 :             : 
     149                 :           1 :     libcacard_finalize();
     150                 :             :     return ret;
     151                 :             : }
     152                 :             : 
     153                 :             : /* vim: set ts=4 sw=4 tw=0 noet expandtab: */
        

Generated by: LCOV version 2.0-1