Blame SOURCES/openscap-1.3.7-PR-1874-unit-test-read-common-sizet.patch

19219b
From 07486e9033d8cc1fd03962994b3359cb611a9ac9 Mon Sep 17 00:00:00 2001
19219b
From: =?UTF-8?q?Jan=20=C4=8Cern=C3=BD?= <jcerny@redhat.com>
19219b
Date: Fri, 22 Jul 2022 16:50:01 +0200
19219b
Subject: [PATCH 1/3] Add unit test for read_common_sizet function
19219b
19219b
The unit test will cover the missing set errno
19219b
to 0 which was the root cause of:
19219b
https://github.com/OpenSCAP/openscap/issues/1867
19219b
19219b
Therefore, this test can be used during verification of:
19219b
https://bugzilla.redhat.com/show_bug.cgi?id=2109485
19219b
---
19219b
 tests/API/probes/CMakeLists.txt   |  9 +++++
19219b
 tests/API/probes/test_memusage.c  | 67 +++++++++++++++++++++++++++++++
19219b
 tests/API/probes/test_memusage.sh |  9 +++++
19219b
 3 files changed, 85 insertions(+)
19219b
 create mode 100644 tests/API/probes/test_memusage.c
19219b
 create mode 100755 tests/API/probes/test_memusage.sh
19219b
19219b
diff --git a/tests/API/probes/CMakeLists.txt b/tests/API/probes/CMakeLists.txt
19219b
index ae3c7212a0..2ac4081ac2 100644
19219b
--- a/tests/API/probes/CMakeLists.txt
19219b
+++ b/tests/API/probes/CMakeLists.txt
19219b
@@ -38,3 +38,12 @@ target_include_directories(oval_fts_list PUBLIC
19219b
 )
19219b
 target_link_libraries(oval_fts_list openscap)
19219b
 add_oscap_test("fts.sh")
19219b
+
19219b
+add_oscap_test_executable(test_memusage
19219b
+	"test_memusage.c"
19219b
+	"${CMAKE_SOURCE_DIR}/src/common/bfind.c"
19219b
+)
19219b
+target_include_directories(test_memusage PUBLIC
19219b
+	"${CMAKE_SOURCE_DIR}/src/common"
19219b
+)
19219b
+add_oscap_test("test_memusage.sh")
19219b
diff --git a/tests/API/probes/test_memusage.c b/tests/API/probes/test_memusage.c
19219b
new file mode 100644
19219b
index 0000000000..5dced98f03
19219b
--- /dev/null
19219b
+++ b/tests/API/probes/test_memusage.c
19219b
@@ -0,0 +1,67 @@
19219b
+/*
19219b
+ * Copyright 2022 Red Hat Inc., Durham, North Carolina.
19219b
+ * All Rights Reserved.
19219b
+ *
19219b
+ * This library is free software; you can redistribute it and/or
19219b
+ * modify it under the terms of the GNU Lesser General Public
19219b
+ * License as published by the Free Software Foundation; either
19219b
+ * version 2.1 of the License, or (at your option) any later version.
19219b
+ *
19219b
+ * This library is distributed in the hope that it will be useful,
19219b
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
19219b
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19219b
+ * Lesser General Public License for more details.
19219b
+ *
19219b
+ * You should have received a copy of the GNU Lesser General Public
19219b
+ * License along with this library; if not, write to the Free Software
19219b
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19219b
+ *
19219b
+ * Authors:
19219b
+ *      "Jan Černý" <jcerny@redhat.com>
19219b
+ */
19219b
+
19219b
+#ifdef HAVE_CONFIG_H
19219b
+#include <config.h>
19219b
+#endif
19219b
+
19219b
+#include <stdio.h>
19219b
+#include "memusage.h"
19219b
+#include "memusage.c"
19219b
+#define OS_LINUX
19219b
+
19219b
+static int test_basic()
19219b
+{
19219b
+	size_t size;
19219b
+	char *strval = strdup("17 MB");
19219b
+	read_common_sizet(&size, strval);
19219b
+	free(strval);
19219b
+	return (size == 17);
19219b
+}
19219b
+
19219b
+static int test_errno()
19219b
+{
19219b
+	size_t size;
19219b
+	char *strval = strdup("17 MB");
19219b
+
19219b
+	/* Test that setting errno outside of the read_common_sizet function
19219b
+	 * doesn't influence the function and doesn't make the function fail.
19219b
+	 */
19219b
+	errno = EINVAL;
19219b
+
19219b
+	int ret = read_common_sizet(&size, strval);
19219b
+	free(strval);
19219b
+	return (ret != -1);
19219b
+}
19219b
+
19219b
+int main(int argc, char *argv[])
19219b
+{
19219b
+	if (!test_basic()) {
19219b
+		fprintf(stderr, "test_basic has failed\n");
19219b
+		return 1;
19219b
+	}
19219b
+	if (!test_errno()) {
19219b
+		fprintf(stderr, "test_errno has failed\n");
19219b
+		return 1;
19219b
+	}
19219b
+	return 0;
19219b
+}
19219b
diff --git a/tests/API/probes/test_memusage.sh b/tests/API/probes/test_memusage.sh
19219b
new file mode 100755
19219b
index 0000000000..4c76bdc0ac
19219b
--- /dev/null
19219b
+++ b/tests/API/probes/test_memusage.sh
19219b
@@ -0,0 +1,9 @@
19219b
+#!/usr/bin/env bash
19219b
+
19219b
+. $builddir/tests/test_common.sh
19219b
+
19219b
+if [ -n "${CUSTOM_OSCAP+x}" ] ; then
19219b
+    exit 255
19219b
+fi
19219b
+
19219b
+./test_memusage
19219b
19219b
From 2cc649d5e9fbf337bbfca69c21313657a5b8a7cf Mon Sep 17 00:00:00 2001
19219b
From: =?UTF-8?q?Jan=20=C4=8Cern=C3=BD?= <jcerny@redhat.com>
19219b
Date: Mon, 25 Jul 2022 09:00:36 +0200
19219b
Subject: [PATCH 2/3] Replace license by SPDX ID
19219b
19219b
---
19219b
 tests/API/probes/test_memusage.c | 22 +---------------------
19219b
 1 file changed, 1 insertion(+), 21 deletions(-)
19219b
19219b
diff --git a/tests/API/probes/test_memusage.c b/tests/API/probes/test_memusage.c
19219b
index 5dced98f03..db2915f6d5 100644
19219b
--- a/tests/API/probes/test_memusage.c
19219b
+++ b/tests/API/probes/test_memusage.c
19219b
@@ -1,24 +1,4 @@
19219b
-/*
19219b
- * Copyright 2022 Red Hat Inc., Durham, North Carolina.
19219b
- * All Rights Reserved.
19219b
- *
19219b
- * This library is free software; you can redistribute it and/or
19219b
- * modify it under the terms of the GNU Lesser General Public
19219b
- * License as published by the Free Software Foundation; either
19219b
- * version 2.1 of the License, or (at your option) any later version.
19219b
- *
19219b
- * This library is distributed in the hope that it will be useful,
19219b
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
19219b
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19219b
- * Lesser General Public License for more details.
19219b
- *
19219b
- * You should have received a copy of the GNU Lesser General Public
19219b
- * License along with this library; if not, write to the Free Software
19219b
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19219b
- *
19219b
- * Authors:
19219b
- *      "Jan Černý" <jcerny@redhat.com>
19219b
- */
19219b
+// SPDX-License-Identifier: LGPL-2.1-or-later
19219b
 
19219b
 #ifdef HAVE_CONFIG_H
19219b
 #include <config.h>
19219b
19219b
From caadd89e61f5d70e251180055686a3b52c763c66 Mon Sep 17 00:00:00 2001
19219b
From: =?UTF-8?q?Jan=20=C4=8Cern=C3=BD?= <jcerny@redhat.com>
19219b
Date: Mon, 25 Jul 2022 09:00:45 +0200
19219b
Subject: [PATCH 3/3] Improve unit test for read_common_sizet
19219b
19219b
Check for multiple different situations.
19219b
---
19219b
 tests/API/probes/test_memusage.c | 34 ++++++++++++++++++++++++++++----
19219b
 1 file changed, 30 insertions(+), 4 deletions(-)
19219b
19219b
diff --git a/tests/API/probes/test_memusage.c b/tests/API/probes/test_memusage.c
19219b
index db2915f6d5..b9db865d45 100644
19219b
--- a/tests/API/probes/test_memusage.c
19219b
+++ b/tests/API/probes/test_memusage.c
19219b
@@ -12,16 +12,34 @@
19219b
 static int test_basic()
19219b
 {
19219b
 	size_t size;
19219b
-	char *strval = strdup("17 MB");
19219b
-	read_common_sizet(&size, strval);
19219b
+	char *strval = strdup("17 kB\n");
19219b
+	int ret = read_common_sizet(&size, strval);
19219b
 	free(strval);
19219b
-	return (size == 17);
19219b
+	return (size == 17 && ret == 0);
19219b
+}
19219b
+
19219b
+static int test_no_unit()
19219b
+{
19219b
+	size_t size;
19219b
+	char *strval = strdup("42");
19219b
+	int ret = read_common_sizet(&size, strval);
19219b
+	free(strval);
19219b
+	return (ret == -1);
19219b
+}
19219b
+
19219b
+static int test_invalid_number()
19219b
+{
19219b
+	size_t size;
19219b
+	char *strval = strdup("www kB\n");
19219b
+	int ret = read_common_sizet(&size, strval);
19219b
+	free(strval);
19219b
+	return (size == 0 && ret == 0);
19219b
 }
19219b
 
19219b
 static int test_errno()
19219b
 {
19219b
 	size_t size;
19219b
-	char *strval = strdup("17 MB");
19219b
+	char *strval = strdup("17 kB\n");
19219b
 
19219b
 	/* Test that setting errno outside of the read_common_sizet function
19219b
 	 * doesn't influence the function and doesn't make the function fail.
19219b
@@ -39,6 +57,14 @@ int main(int argc, char *argv[])
19219b
 		fprintf(stderr, "test_basic has failed\n");
19219b
 		return 1;
19219b
 	}
19219b
+	if (!test_no_unit()) {
19219b
+		fprintf(stderr, "test_no_unit has failed\n");
19219b
+		return 1;
19219b
+	}
19219b
+	if (!test_invalid_number()) {
19219b
+		fprintf(stderr, "test_invalid_number has failed\n");
19219b
+		return 1;
19219b
+	}
19219b
 	if (!test_errno()) {
19219b
 		fprintf(stderr, "test_errno has failed\n");
19219b
 		return 1;