Skip to content

Commit

Permalink
Merge pull request #1874 from jan-cerny/read_common_sizet
Browse files Browse the repository at this point in the history
Add unit test for read_common_sizet function
  • Loading branch information
evgenyz authored Jul 25, 2022
2 parents e4091e1 + caadd89 commit 963edcd
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
9 changes: 9 additions & 0 deletions tests/API/probes/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,12 @@ target_include_directories(oval_fts_list PUBLIC
)
target_link_libraries(oval_fts_list openscap)
add_oscap_test("fts.sh")

add_oscap_test_executable(test_memusage
"test_memusage.c"
"${CMAKE_SOURCE_DIR}/src/common/bfind.c"
)
target_include_directories(test_memusage PUBLIC
"${CMAKE_SOURCE_DIR}/src/common"
)
add_oscap_test("test_memusage.sh")
73 changes: 73 additions & 0 deletions tests/API/probes/test_memusage.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// SPDX-License-Identifier: LGPL-2.1-or-later

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include "memusage.h"
#include "memusage.c"
#define OS_LINUX

static int test_basic()
{
size_t size;
char *strval = strdup("17 kB\n");
int ret = read_common_sizet(&size, strval);
free(strval);
return (size == 17 && ret == 0);
}

static int test_no_unit()
{
size_t size;
char *strval = strdup("42");
int ret = read_common_sizet(&size, strval);
free(strval);
return (ret == -1);
}

static int test_invalid_number()
{
size_t size;
char *strval = strdup("www kB\n");
int ret = read_common_sizet(&size, strval);
free(strval);
return (size == 0 && ret == 0);
}

static int test_errno()
{
size_t size;
char *strval = strdup("17 kB\n");

/* Test that setting errno outside of the read_common_sizet function
* doesn't influence the function and doesn't make the function fail.
*/
errno = EINVAL;

int ret = read_common_sizet(&size, strval);
free(strval);
return (ret != -1);
}

int main(int argc, char *argv[])
{
if (!test_basic()) {
fprintf(stderr, "test_basic has failed\n");
return 1;
}
if (!test_no_unit()) {
fprintf(stderr, "test_no_unit has failed\n");
return 1;
}
if (!test_invalid_number()) {
fprintf(stderr, "test_invalid_number has failed\n");
return 1;
}
if (!test_errno()) {
fprintf(stderr, "test_errno has failed\n");
return 1;
}
return 0;
}
9 changes: 9 additions & 0 deletions tests/API/probes/test_memusage.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env bash

. $builddir/tests/test_common.sh

if [ -n "${CUSTOM_OSCAP+x}" ] ; then
exit 255
fi

./test_memusage

0 comments on commit 963edcd

Please sign in to comment.