LCOV - code coverage report
Current view: top level - tests - unit_cac.c (source / functions) Coverage Total Hit
Test: Code coverage Lines: 100.0 % 22 22
Test Date: 2025-12-04 16:15:14 Functions: 100.0 % 3 3
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 50.0 % 18 9

             Branch data     Line data    Source code
       1                 :             : /*
       2                 :             :  * Unit-test low-level CAC code
       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 "cac-aca.c"
      14                 :             : 
      15                 :             : struct acr_applet a = {
      16                 :             :     .id = 0x42, .num_objects = 1, .objects = {
      17                 :             :             {.id = {0xab, 0xcd}, .num_ins = 1, .ins = {
      18                 :             :                 {.code = 0x01, .acrid = 0x02, .config = ACR_INS_CONFIG_P2, .p1 = 0x03, .p2 = 0x04, .data1 = 0x05}
      19                 :             :             }
      20                 :             :         }
      21                 :             :     }
      22                 :             : };
      23                 :             : 
      24                 :           1 : static void test_acr_applet_object_encode(void)
      25                 :             : {
      26                 :             :     unsigned int buflen, objlen;
      27                 :             :     unsigned char *buf = NULL, *p = NULL;
      28                 :             : 
      29                 :             :     /* Too short */
      30                 :             :     buflen = 0;
      31                 :           1 :     p = acr_applet_object_encode(a.objects, buf, buflen, &objlen);
      32         [ -  + ]:           1 :     g_assert_null(p);
      33                 :             : 
      34                 :             :     buflen = 16;
      35                 :           1 :     buf = g_malloc(buflen);
      36                 :             : 
      37                 :             :     /* Still too short */
      38                 :             :     buflen = 2;
      39                 :           1 :     p = acr_applet_object_encode(a.objects, buf, buflen, &objlen);
      40         [ -  + ]:           1 :     g_assert_null(p);
      41                 :             : 
      42                 :             :     /* Still too short */
      43                 :             :     buflen = 4;
      44                 :           1 :     p = acr_applet_object_encode(a.objects, buf, buflen, &objlen);
      45         [ -  + ]:           1 :     g_assert_null(p);
      46                 :             : 
      47                 :             :     /* This should do */
      48                 :             :     buflen = 16;
      49                 :           1 :     p = acr_applet_object_encode(a.objects, buf, buflen, &objlen);
      50   [ -  +  -  +  :           1 :     g_assert_cmpmem(buf, objlen, "\xab\xcd\x01\x02\x04\x02", 6);
                   -  + ]
      51                 :           1 :     g_free(buf);
      52                 :           1 : }
      53                 :             : 
      54                 :           1 : static void test_acr_applet_encode(void)
      55                 :             : {
      56                 :             :     unsigned int outlen;
      57                 :             :     unsigned char *p = NULL;
      58                 :             : 
      59                 :             :     /* Wrong arguments */
      60                 :             :     p = acr_applet_encode(&a, NULL);
      61                 :             :     g_assert_null(p);
      62                 :             : 
      63                 :           1 :     p = acr_applet_encode(&a, &outlen);
      64   [ -  +  -  +  :           1 :     g_assert_cmpmem(p, outlen, "\x42\x01\x82\x06\xab\xcd\x01\x02\x04\x02", 10);
                   -  + ]
      65                 :           1 :     g_free(p);
      66                 :           1 : }
      67                 :             : 
      68                 :           1 : int main(int argc, char *argv[])
      69                 :             : {
      70                 :             :     int ret;
      71                 :             : 
      72                 :           1 :     g_test_init(&argc, &argv, NULL);
      73                 :             : 
      74                 :           1 :     g_test_add_func("/unittests/cac/acr_applet_object_encode", test_acr_applet_object_encode);
      75                 :           1 :     g_test_add_func("/unittests/cac/acr_applet_encode", test_acr_applet_encode);
      76                 :           1 :     ret = g_test_run();
      77                 :             : 
      78                 :             :     return ret;
      79                 :             : }
      80                 :             : 
      81                 :             : /* vim: set ts=4 sw=4 tw=0 noet expandtab: */
        

Generated by: LCOV version 2.0-1