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

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