Basic unit test for secure messaging functions

This commit is contained in:
Jakub Jelen 2021-05-04 13:46:20 +02:00
parent 0b45e78e4f
commit e4cf0e7b39
2 changed files with 77 additions and 0 deletions

View File

@ -32,6 +32,14 @@ compression_SOURCES = compression.c
compression_LDADD = $(LDADD) $(OPTIONAL_ZLIB_LIBS)
endif
if ENABLE_OPENSSL
noinst_PROGRAMS += sm
TESTS += sm
sm_SOURCES = sm.c
sm_LDADD = $(top_builddir)/src/sm/libsm.la $(LDADD)
endif
endif

69
src/tests/unittests/sm.c Normal file
View File

@ -0,0 +1,69 @@
/*
* sm.c: Unit tests for Secure Messaging
*
* Copyright (C) 2021 Red Hat, Inc.
*
* Author: Jakub Jelen <jjelen@redhat.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "torture.h"
#include "libopensc/log.c"
#include "sm/sm-common.h"
static void torture_sm_incr_ssc(void **state)
{
unsigned char in[] = {0x00, 0x00};
(void)state;
/* just make sure it does not crash */
sm_incr_ssc(NULL, 0);
/* zero-length input should not underflow the buffer */
sm_incr_ssc(in, 0);
/* shortest possible input */
in[0] = 0x42;
sm_incr_ssc(in, 1);
assert_int_equal(in[0], 0x43);
/* overflow to the second byte */
in[0] = 0x00;
in[1] = 0xff;
sm_incr_ssc(in, 2);
assert_int_equal(in[0], 0x01);
assert_int_equal(in[1], 0x00);
/* overflow */
in[0] = 0xff;
in[1] = 0xff;
sm_incr_ssc(in, 2);
assert_int_equal(in[0], 0x00);
assert_int_equal(in[1], 0x00);
}
int main(void)
{
int rc;
struct CMUnitTest tests[] = {
/* sm_incr_ssc */
cmocka_unit_test(torture_sm_incr_ssc),
};
rc = cmocka_run_group_tests(tests, NULL, NULL);
return rc;
}