Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit test for read_common_sizet function #1874

Merged
merged 3 commits into from
Jul 25, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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")
67 changes: 67 additions & 0 deletions tests/API/probes/test_memusage.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2022 Red Hat Inc., Durham, North Carolina.
evgenyz marked this conversation as resolved.
Show resolved Hide resolved
* All Rights Reserved.
*
* 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 Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Authors:
* "Jan Černý" <jcerny@redhat.com>
*/

#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 MB");
read_common_sizet(&size, strval);
free(strval);
return (size == 17);
}

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

/* 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_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