Patrick Georgi submitted this change.

View Change

Approvals: build bot (Jenkins): Verified Julius Werner: Looks good to me, approved Paul Fagerburg: Looks good to me, approved
tests: Add lib/string-test test case

Show a basic example of how unit testing can be applied for the coreboot
project. Add a test harness for lib/string.c module.

TEST=Install cmocka via appropriate command:
sudo apt-get install -y libcmocka-dev
sudo emerge dev-util/cmocka
yum install libcmocka-devel
* Build and run unit tests via `make unit-tests`
* Check the output to see that tests passed.

Signed-off-by: Jan Dabros <jsd@semihalf.com>
Change-Id: Ibf5554d1e99a393721a66bdd35af0122c2e412c4
Reviewed-on: https://review.coreboot.org/c/coreboot/+/40538
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Julius Werner <jwerner@chromium.org>
Reviewed-by: Paul Fagerburg <pfagerburg@chromium.org>
---
A tests/lib/Makefile.inc
A tests/lib/string-test.c
2 files changed, 73 insertions(+), 0 deletions(-)

diff --git a/tests/lib/Makefile.inc b/tests/lib/Makefile.inc
new file mode 100644
index 0000000..86ac323
--- /dev/null
+++ b/tests/lib/Makefile.inc
@@ -0,0 +1,20 @@
+##
+## This file is part of the coreboot project.
+##
+## This program is free software; you can redistribute it and/or modify
+## it under the terms of the GNU General Public License as published by
+## the Free Software Foundation; version 2 of the License.
+##
+## This program 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 General Public License for more details.
+##
+
+# object filest should be under build/tests/ build/test/src/ build/test/run/
+# two examples - first should be simply string.c, second should use -wrap
+
+tests-y += string-test
+
+string-test-srcs += tests/lib/string-test.c
+string-test-srcs += src/lib/string.c
diff --git a/tests/lib/string-test.c b/tests/lib/string-test.c
new file mode 100644
index 0000000..210aaba
--- /dev/null
+++ b/tests/lib/string-test.c
@@ -0,0 +1,53 @@
+#include <stdarg.h>
+#include <stddef.h>
+#include <setjmp.h>
+#include <cmocka.h>
+
+#include <string.h>
+
+/*
+ * Important note: In every particular test, don't use any string-related
+ * functions other than function under test. We are linking against
+ * src/lib/string.c not the standard library. This is important for proper test
+ * isolation. One can use __builtin_xxx for many of the most simple str*()
+ * functions, when non-coreboot one is required.
+ */
+
+struct strings_t {
+ char *str;
+ size_t size;
+} strings[] = {
+ {"coreboot", 8},
+ {"is\0very", 2}, /* strlen should be 2 because of the embedded \0 */
+ {"nice\n", 5}
+};
+
+static void test_strlen_strings(void **state)
+{
+ int i;
+
+ for (i = 0; i < ARRAY_SIZE(strings); i++)
+ assert_int_equal(strings[i].size, strlen(strings[i].str));
+}
+
+static void test_strdup(void **state)
+{
+ char str[] = "Hello coreboot\n";
+ char *duplicate;
+
+ duplicate = strdup(str);
+
+ /* There is a more suitable Cmocka's function 'assert_string_equal()', but it
+ is using strcmp() internally. */
+ assert_int_equal(0, memcmp(str, duplicate, __builtin_strlen(str)));
+}
+
+int main(void)
+{
+ const struct CMUnitTest tests[] = {
+ cmocka_unit_test(test_strlen_strings),
+ cmocka_unit_test(test_strdup),
+ };
+
+ return cmocka_run_group_tests(tests, NULL, NULL);
+}

To view, visit change 40538. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: coreboot
Gerrit-Branch: master
Gerrit-Change-Id: Ibf5554d1e99a393721a66bdd35af0122c2e412c4
Gerrit-Change-Number: 40538
Gerrit-PatchSet: 6
Gerrit-Owner: Name of user not set #1002873
Gerrit-Reviewer: Jett Rink <jettrink@chromium.org>
Gerrit-Reviewer: Julius Werner <jwerner@chromium.org>
Gerrit-Reviewer: Martin Roth <martinroth@google.com>
Gerrit-Reviewer: Patrick Georgi <pgeorgi@google.com>
Gerrit-Reviewer: Paul Fagerburg <pfagerburg@chromium.org>
Gerrit-Reviewer: build bot (Jenkins) <no-reply@coreboot.org>
Gerrit-CC: Paul Menzel <paulepanter@users.sourceforge.net>
Gerrit-MessageType: merged