All of lore.kernel.org
 help / color / mirror / Atom feed
From: Knut Omang <knut.omang@oracle.com>
To: linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: linux-doc@vger.kernel.org, linux-kbuild@vger.kernel.org,
	Shuah Khan <shuah@kernel.org>, Jonathan Corbet <corbet@lwn.net>,
	Masahiro Yamada <yamada.masahiro@socionext.com>,
	Michal Marek <michal.lkml@markovi.net>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Shreyans Devendra Doshi <0xinfosect0r@gmail.com>,
	Alan Maguire <alan.maguire@oracle.com>,
	Brendan Higgins <brendanhiggins@google.com>,
	Kevin Hilman <khilman@baylibre.com>,
	Hidenori Yamaji <hidenori.yamaji@sony.com>,
	Frank Rowand <frowand.list@gmail.com>,
	Timothy Bird <Tim.Bird@sony.com>,
	Luis Chamberlain <mcgrof@kernel.org>,
	"Theodore Ts'o" <tytso@mit.edu>, Daniel Vetter <daniel@ffwll.ch>,
	Stephen Boyd <sboyd@kernel.org>,
	Knut Omang <knut.omang@oracle.com>
Subject: [RFC 15/19] ktf: Some simple examples
Date: Tue, 13 Aug 2019 08:09:30 +0200	[thread overview]
Message-ID: <930ceefe7f08e47c66cc43404d6e67b310cbec60.1565676440.git-series.knut.omang@oracle.com> (raw)
In-Reply-To: <cover.92d76bb4f6dcedc971d0b72a49e8e459a98bca54.1565676440.git-series.knut.omang@oracle.com>

A few simple examples, and example of other test modules
to make it easier to get started with ktf.

Signed-off-by: Knut Omang <knut.omang@oracle.com>
---
 tools/testing/selftests/ktf/examples/Makefile | 17 ++++-
 tools/testing/selftests/ktf/examples/h2.c     | 45 +++++++++++-
 tools/testing/selftests/ktf/examples/h3.c     | 84 ++++++++++++++++++++-
 tools/testing/selftests/ktf/examples/h4.c     | 62 +++++++++++++++-
 tools/testing/selftests/ktf/examples/hello.c  | 38 +++++++++-
 tools/testing/selftests/ktf/examples/kgdemo.c | 61 +++++++++++++++-
 6 files changed, 307 insertions(+)
 create mode 100644 tools/testing/selftests/ktf/examples/Makefile
 create mode 100644 tools/testing/selftests/ktf/examples/h2.c
 create mode 100644 tools/testing/selftests/ktf/examples/h3.c
 create mode 100644 tools/testing/selftests/ktf/examples/h4.c
 create mode 100644 tools/testing/selftests/ktf/examples/hello.c
 create mode 100644 tools/testing/selftests/ktf/examples/kgdemo.c

diff --git a/tools/testing/selftests/ktf/examples/Makefile b/tools/testing/selftests/ktf/examples/Makefile
new file mode 100644
index 0000000..f3cfcc9
--- /dev/null
+++ b/tools/testing/selftests/ktf/examples/Makefile
@@ -0,0 +1,17 @@
+# Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
+#
+# SPDX-License-Identifier: GPL-2.0
+#
+# Kernel module implementing a few simple examples of KTF tests
+#
+
+include $(srctree)/$(src)/../scripts/ktf_syms.mk
+
+ccflags-y += -I$(srctree)/$(src)/../kernel -I$(src)
+
+obj-m := hello.o h2.o h3.o h4.o
+
+ifdef CONFIG_KGDB
+obj-m += kgdemo.o
+endif
+
diff --git a/tools/testing/selftests/ktf/examples/h2.c b/tools/testing/selftests/ktf/examples/h2.c
new file mode 100644
index 0000000..37a6fbb
--- /dev/null
+++ b/tools/testing/selftests/ktf/examples/h2.c
@@ -0,0 +1,45 @@
+#include <linux/module.h>
+#include "ktf.h"
+
+MODULE_LICENSE("GPL");
+
+KTF_INIT();
+
+#define MAX_CNT 3
+
+struct hello_ctx {
+	struct ktf_context k;
+	int value[MAX_CNT];
+};
+
+static struct hello_ctx myctx = { .value = { 0, 1, 4 } };
+
+TEST(examples, cmp)
+{
+	struct hello_ctx *hctx = KTF_CONTEXT_GET("value", struct hello_ctx);
+
+	EXPECT_INT_EQ(_i, hctx->value[_i]);
+}
+
+static void add_tests(void)
+{
+	ADD_LOOP_TEST(cmp, 0, MAX_CNT);
+}
+
+static int __init hello_init(void)
+{
+	KTF_CONTEXT_ADD(&myctx.k, "value");
+	add_tests();
+	return 0;
+}
+
+static void __exit hello_exit(void)
+{
+	struct ktf_context *kctx = KTF_CONTEXT_FIND("value");
+
+	KTF_CONTEXT_REMOVE(kctx);
+	KTF_CLEANUP();
+}
+
+module_init(hello_init);
+module_exit(hello_exit);
diff --git a/tools/testing/selftests/ktf/examples/h3.c b/tools/testing/selftests/ktf/examples/h3.c
new file mode 100644
index 0000000..a6aca98
--- /dev/null
+++ b/tools/testing/selftests/ktf/examples/h3.c
@@ -0,0 +1,84 @@
+#include <linux/module.h>
+#include "ktf.h"
+
+MODULE_LICENSE("GPL");
+
+KTF_INIT();
+
+DECLARE_F(hello_fixture)
+	struct list_head head;
+};
+
+struct my_element {
+	struct list_head list;
+	int value;
+};
+
+SETUP_F(hello_fixture, hello_setup)
+{
+	int i;
+
+	INIT_LIST_HEAD(&hello_fixture->head);
+	for (i = 0; i < 10; i++) {
+		struct my_element *e = kzalloc(sizeof(*e), GFP_KERNEL);
+
+		e->value = i;
+		list_add_tail(&e->list, &hello_fixture->head);
+	}
+	hello_fixture->ok = true;
+}
+
+TEARDOWN_F(hello_fixture, hello_teardown)
+{
+	struct list_head *p, *next_p;
+
+	/* Just cleanup whatever is left after the test */
+	list_for_each_safe(p, next_p, &hello_fixture->head) {
+		struct my_element *e = list_entry(p, struct my_element, list);
+
+		list_del(&e->list);
+		kfree(e);
+	}
+	EXPECT_TRUE(list_empty(&hello_fixture->head));
+}
+
+INIT_F(hello_fixture, hello_setup, hello_teardown);
+
+TEST_F(hello_fixture, examples, hello_del)
+{
+	int cnt = 0;
+	int cnt_ones = 0;
+	struct my_element *e = kzalloc(sizeof(*e), GFP_KERNEL);
+
+	e->value = 1;
+	list_add(&e->list, &ctx->head);
+
+	list_for_each_entry(e, &ctx->head, list) {
+		if (e->value == 1)
+			cnt_ones++;
+		cnt++;
+	}
+	EXPECT_INT_EQ(11, cnt);
+	EXPECT_INT_EQ(2, cnt_ones);
+}
+
+static void add_tests(void)
+{
+	ADD_TEST(hello_del);
+}
+
+static int __init hello_init(void)
+{
+	add_tests();
+	tlog(T_INFO, "hello: loaded");
+	return 0;
+}
+
+static void __exit hello_exit(void)
+{
+	KTF_CLEANUP();
+	tlog(T_INFO, "hello: unloaded");
+}
+
+module_init(hello_init);
+module_exit(hello_exit);
diff --git a/tools/testing/selftests/ktf/examples/h4.c b/tools/testing/selftests/ktf/examples/h4.c
new file mode 100644
index 0000000..3e85fef
--- /dev/null
+++ b/tools/testing/selftests/ktf/examples/h4.c
@@ -0,0 +1,62 @@
+#include <linux/module.h>
+#include "ktf.h"
+
+MODULE_LICENSE("GPL");
+
+KTF_INIT();
+
+static int count;
+static int ret;
+
+KTF_ENTRY_PROBE(printk, printkhandler)
+{
+	count++;
+
+	KTF_ENTRY_PROBE_RETURN(0);
+}
+
+TEST(examples, entrycheck)
+{
+	count = 0;
+	ASSERT_INT_EQ_GOTO(KTF_REGISTER_ENTRY_PROBE(printk, printkhandler),
+			   0, done);
+	printk(KERN_INFO "Testing kprobe entry...");
+	ASSERT_INT_GT_GOTO(count, 0, done);
+done:
+	KTF_UNREGISTER_ENTRY_PROBE(printk, printkhandler);
+}
+
+KTF_RETURN_PROBE(printk, printkrethandler)
+{
+	ret = KTF_RETURN_VALUE();
+
+	return 0;
+}
+
+TEST(examples, returncheck)
+{
+	char *teststr = "Testing kprobe return...";
+
+	ret = -1;
+	ASSERT_INT_EQ_GOTO(KTF_REGISTER_RETURN_PROBE(printk, printkrethandler),
+			   0, done);
+	printk(KERN_INFO "%s", teststr);
+	ASSERT_INT_EQ_GOTO(ret, strlen(teststr), done);
+done:
+	KTF_UNREGISTER_RETURN_PROBE(printk, printkrethandler);
+}
+
+static int __init hello_init(void)
+{
+	ADD_TEST(entrycheck);
+	ADD_TEST(returncheck);
+	return 0;
+}
+
+static void __exit hello_exit(void)
+{
+	KTF_CLEANUP();
+}
+
+module_init(hello_init);
+module_exit(hello_exit);
diff --git a/tools/testing/selftests/ktf/examples/hello.c b/tools/testing/selftests/ktf/examples/hello.c
new file mode 100644
index 0000000..9c4713f
--- /dev/null
+++ b/tools/testing/selftests/ktf/examples/hello.c
@@ -0,0 +1,38 @@
+#include <linux/module.h>
+#include "ktf.h"
+
+MODULE_LICENSE("GPL");
+
+KTF_INIT();
+
+TEST(examples, hello_ok)
+{
+	EXPECT_TRUE(true);
+}
+
+TEST(examples, hello_fail)
+{
+	EXPECT_TRUE(false);
+}
+
+static void add_tests(void)
+{
+	ADD_TEST(hello_ok);
+	ADD_TEST(hello_fail);
+}
+
+static int __init hello_init(void)
+{
+	add_tests();
+	tlog(T_INFO, "hello: loaded");
+	return 0;
+}
+
+static void __exit hello_exit(void)
+{
+	KTF_CLEANUP();
+	tlog(T_INFO, "hello: unloaded");
+}
+
+module_init(hello_init);
+module_exit(hello_exit);
diff --git a/tools/testing/selftests/ktf/examples/kgdemo.c b/tools/testing/selftests/ktf/examples/kgdemo.c
new file mode 100644
index 0000000..9ce19ff
--- /dev/null
+++ b/tools/testing/selftests/ktf/examples/kgdemo.c
@@ -0,0 +1,61 @@
+#include <linux/module.h>
+#include "ktf.h"
+
+/*
+ * A trivial and somewhat rough example used by the author
+ * for pedagogical purposes, to demonstrate
+ * interactive debugging with kgdb.
+ *
+ * Requires a kernel built with CONFIG_KGDB
+ *
+ * Note: these test breaks into kgdb and/or creates a NULL
+ *       pointer exception and corresponding stack dump, so
+ *       try out in a test environment only!
+ */
+
+MODULE_LICENSE("GPL");
+
+KTF_INIT();
+
+#define MAX_CNT 3
+#include <linux/kgdb.h>
+
+static int kgdemo_cnt;
+static int *bogus_ref;
+
+TEST(kgdb, breakpoint)
+{
+	kgdemo_cnt = 0;
+	printk(KERN_INFO "** Please set kgdemo_cnt = 1 **\n");
+	kgdb_breakpoint();
+	EXPECT_INT_EQ(1, kgdemo_cnt);
+}
+
+TEST(kgdb, nullpointer)
+{
+	int pre = kgdemo_cnt;
+
+	int b = *bogus_ref++;
+
+	EXPECT_INT_EQ(pre + 1, b);
+}
+
+static void add_tests(void)
+{
+	ADD_TEST(breakpoint);
+	ADD_TEST(nullpointer);
+}
+
+static int __init kgdemo_init(void)
+{
+	add_tests();
+	return 0;
+}
+
+static void __exit kgdemo_exit(void)
+{
+	KTF_CLEANUP();
+}
+
+module_init(kgdemo_init);
+module_exit(kgdemo_exit);
-- 
git-series 0.9.1

  parent reply	other threads:[~2019-08-13  6:12 UTC|newest]

Thread overview: 57+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-08-13  6:09 [RFC 00/19] Integration of Kernel Test Framework (KTF) into the kernel tree Knut Omang
2019-08-13  6:09 ` [RFC 01/19] kbuild: Fixes to rules for host-cshlib and host-cxxshlib Knut Omang
2019-08-13 14:01   ` Masahiro Yamada
2019-08-13 14:01     ` Masahiro Yamada
2019-08-13 16:19     ` Knut Omang
2019-08-13 16:19       ` Knut Omang
2019-08-14  2:02       ` Masahiro Yamada
2019-08-14  2:02         ` Masahiro Yamada
2019-08-14  5:50         ` Knut Omang
2019-08-14  5:50           ` Knut Omang
2019-08-14  5:52         ` Knut Omang
2019-08-14  5:52           ` Knut Omang
2019-08-14 12:52           ` Knut Omang
2019-08-14 12:52             ` Knut Omang
2019-08-21  1:47             ` Masahiro Yamada
2019-08-21  1:47               ` Masahiro Yamada
2019-08-21  4:03               ` Knut Omang
2019-08-21  4:03                 ` Knut Omang
2019-08-13  6:09 ` [RFC 02/19] ktf: Introduce the main part of the kernel side of ktf Knut Omang
2019-09-09  1:23   ` Brendan Higgins
2019-09-10  6:15     ` Knut Omang
2019-08-13  6:09 ` [RFC 03/19] ktf: Introduce a generic netlink protocol for test result communication Knut Omang
2019-09-09  1:28   ` Brendan Higgins
2019-09-10  6:30     ` Knut Omang
2019-08-13  6:09 ` [RFC 04/19] ktf: An implementation of a generic associative array container Knut Omang
2019-08-13  6:09 ` [RFC 05/19] ktf: Implementation of ktf support for overriding function entry and return Knut Omang
2019-08-13  6:09 ` [RFC 06/19] ktf: A simple debugfs interface to test results Knut Omang
2019-08-13  8:21   ` Greg Kroah-Hartman
2019-08-14 17:17     ` Knut Omang
2019-08-15  8:49       ` Greg Kroah-Hartman
2019-08-15 10:35         ` Knut Omang
2019-08-15 10:52           ` Greg Kroah-Hartman
2019-08-13  6:09 ` [RFC 07/19] ktf: Simple coverage support Knut Omang
2019-08-13  6:09 ` [RFC 08/19] ktf: Configurable context support for network info setup Knut Omang
2019-08-13  6:09 ` [RFC 09/19] ktf: resolve: A helper utility to aid in exposing private kernel symbols to KTF tests Knut Omang
2019-08-13  6:09 ` [RFC 10/19] ktf: Add documentation for Kernel Test Framework (KTF) Knut Omang
2019-08-13  6:09 ` [RFC 11/19] ktf: Add a small test suite with a few tests to test KTF itself Knut Omang
2019-08-13  6:09 ` [RFC 12/19] ktf: Main part of user land library for executing tests Knut Omang
2019-08-13  6:09 ` [RFC 13/19] ktf: Integration logic for running ktf tests from googletest Knut Omang
2019-08-13  6:09 ` [RFC 14/19] ktf: Internal debugging facilities Knut Omang
2019-08-13  6:09 ` Knut Omang [this message]
2019-08-13  6:09 ` [RFC 16/19] ktf: Some user applications to run tests Knut Omang
2019-08-13  6:09 ` [RFC 17/19] ktf: Toplevel ktf Makefile/makefile includes and scripts to run from kselftest Knut Omang
2019-08-13  6:09 ` [RFC 18/19] kselftests: Enable building ktf Knut Omang
2019-08-13  6:09 ` [RFC 19/19] Documentation/dev-tools: Add index entry for KTF documentation Knut Omang
2019-08-13  8:10 ` [RFC 00/19] Integration of Kernel Test Framework (KTF) into the kernel tree Brendan Higgins
2019-08-13  8:10   ` Brendan Higgins
2019-08-13  8:17 ` Brendan Higgins
2019-08-13  8:17   ` Brendan Higgins
2019-08-13 11:29   ` Knut Omang
2019-08-13 11:29     ` Knut Omang
2019-08-13 17:50     ` Brendan Higgins
2019-08-13 17:50       ` Brendan Higgins
2019-08-13  8:23 ` Greg Kroah-Hartman
2019-08-13  9:51   ` Knut Omang
2019-08-13 17:02     ` Brendan Higgins
2019-08-13 17:02       ` Brendan Higgins

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=930ceefe7f08e47c66cc43404d6e67b310cbec60.1565676440.git-series.knut.omang@oracle.com \
    --to=knut.omang@oracle.com \
    --cc=0xinfosect0r@gmail.com \
    --cc=Tim.Bird@sony.com \
    --cc=alan.maguire@oracle.com \
    --cc=brendanhiggins@google.com \
    --cc=corbet@lwn.net \
    --cc=daniel@ffwll.ch \
    --cc=frowand.list@gmail.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=hidenori.yamaji@sony.com \
    --cc=khilman@baylibre.com \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kbuild@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=mcgrof@kernel.org \
    --cc=michal.lkml@markovi.net \
    --cc=sboyd@kernel.org \
    --cc=shuah@kernel.org \
    --cc=tytso@mit.edu \
    --cc=yamada.masahiro@socionext.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.