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

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