Linux-kselftest Archive on lore.kernel.org
 help / color / 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 16/19] ktf: Some user applications to run tests
Date: Tue, 13 Aug 2019 08:09:31 +0200
Message-ID: <c8c6df6d7b068c2103475f28056ab9a6df3fda4c.1565676440.git-series.knut.omang@oracle.com> (raw)
In-Reply-To: <cover.92d76bb4f6dcedc971d0b72a49e8e459a98bca54.1565676440.git-series.knut.omang@oracle.com>

Some minimal user land executables to run tests and to enable/disable
coverage:

ktfrun:		Simple generic test runner for generic ktf tests
ktftest:	A test runner for the ktf selftests. Contains code to
		configure specific selftest context objects.
ktfcov:		A utility to selectively enable coverage support for
		a kernel module. Coverage support can also be enabled
		in code by tests, if desired.
hybrun:		A test that implements a hybrid test runner.

hybrid.cc:      User mode part of the hybrid_self
ktfrun.cc:      Generic user level application to run kernel tests

Signed-off-by: Knut Omang <knut.omang@oracle.com>
---
 tools/testing/selftests/ktf/user/Makefile   | 26 ++++++++-
 tools/testing/selftests/ktf/user/hybrid.cc  | 39 +++++++++++++-
 tools/testing/selftests/ktf/user/ktfcov.cc  | 68 ++++++++++++++++++++++-
 tools/testing/selftests/ktf/user/ktfrun.cc  | 20 ++++++-
 tools/testing/selftests/ktf/user/ktftest.cc | 46 +++++++++++++++-
 5 files changed, 199 insertions(+)
 create mode 100644 tools/testing/selftests/ktf/user/Makefile
 create mode 100644 tools/testing/selftests/ktf/user/hybrid.cc
 create mode 100644 tools/testing/selftests/ktf/user/ktfcov.cc
 create mode 100644 tools/testing/selftests/ktf/user/ktfrun.cc
 create mode 100644 tools/testing/selftests/ktf/user/ktftest.cc

diff --git a/tools/testing/selftests/ktf/user/Makefile b/tools/testing/selftests/ktf/user/Makefile
new file mode 100644
index 0000000..04c8e7e
--- /dev/null
+++ b/tools/testing/selftests/ktf/user/Makefile
@@ -0,0 +1,26 @@
+
+GTEST_CFLAGS ?= -I$(GTEST_PATH)/include -DGTEST_HAS_PTHREAD=1 -lpthread
+GTEST_LIBS ?= -L$(GTEST_PATH)/lib64 -lgtest -lpthread
+NETLINK_CFLAGS ?= $(shell pkgconf --cflags libnl-genl-3.0)
+HOST_EXTRACFLAGS = -I$(srctree)/$(src)/../lib $(NETLINK_CFLAGS) $(GTEST_CFLAGS) \
+		-Wall -Werror \
+		-Wno-packed-bitfield-compat -D_GNU_SOURCE
+
+HOST_EXTRACXXFLAGS = -I$(srctree)/$(src)/../lib $(NETLINK_CFLAGS) $(GTEST_CFLAGS) \
+		-Wall \
+		-Wno-packed-bitfield-compat \
+		-Wno-pointer-arith -Werror \
+		-D__FILENAME__=\"`basename $<`\"
+NETLINK_LIBS ?= $(shell pkgconf --libs libnl-genl-3.0)
+KBUILD_HOSTLDLIBS =	-L$(obj)/../lib -lktf $(NETLINK_LIBS) $(GTEST_LIBS)
+
+hostprogs-y := ktfrun ktfcov ktftest
+
+__build: $(addprefix $(obj)/,$(hostprogs-y))
+
+## Simple kernel test runner sample program:
+ktfrun-cxxobjs = ktfrun.o
+ktfcov-cxxobjs = ktfcov.o
+
+## Configure and run the KTF selftests:
+ktftest-cxxobjs = ktftest.o hybrid.o
diff --git a/tools/testing/selftests/ktf/user/hybrid.cc b/tools/testing/selftests/ktf/user/hybrid.cc
new file mode 100644
index 0000000..6aa5ad2
--- /dev/null
+++ b/tools/testing/selftests/ktf/user/hybrid.cc
@@ -0,0 +1,39 @@
+/* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ *    Author: Knut Omang <knut.omang@oracle.com>
+ *
+ * SPDX-License-Identifier: GPL-2.0
+ *
+ * hybrid.cpp: User mode part of the hybrid_self
+ * test in selftests
+ */
+
+#include "ktf.h"
+#include <string.h>
+
+extern "C" {
+#include "../selftest/hybrid_self.h"
+}
+
+/* User side of a simple hybrid test that just sends an out-of-band message
+ * to the kernel side - the kernel implementation picks it up and verifies
+ * that it is the expected string and integer values.
+ *
+ * This form of test allows the mixing of normal gtest user land assertions
+ * with one or more calls to the kernel side to run tests there:
+ */
+
+HTEST(selftest, msg)
+{
+  KTF_USERDATA(self, hybrid_self_params, data);
+
+  strcpy(data->text_val, HYBRID_MSG);
+  data->val = HYBRID_MSG_VAL;
+
+  /* assertions can be specified here: */
+  EXPECT_TRUE(true);
+
+  ktf::run(self);
+
+  /* and here.. */
+  EXPECT_TRUE(true);
+}
diff --git a/tools/testing/selftests/ktf/user/ktfcov.cc b/tools/testing/selftests/ktf/user/ktfcov.cc
new file mode 100644
index 0000000..d5a9ef4
--- /dev/null
+++ b/tools/testing/selftests/ktf/user/ktfcov.cc
@@ -0,0 +1,68 @@
+/*
+ * Copyright (c) 2011, 2017, Oracle and/or its affiliates. All rights reserved.
+ *    Author: Alan Maguire <alan.maguire@oracle.com>
+ *
+ * SPDX-License-Identifier: GPL-2.0
+ *
+ * ktfcov.cpp:
+ *   User level application to enable/disable coverage of kernel modules.
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include "ktf.h"
+#include "../kernel/ktf_unlproto.h"
+
+using namespace std;
+
+void
+usage(char *progname)
+{
+	cerr << "Usage: " << progname << " [-e module[-m]] [-d module]\n";
+}
+
+int main (int argc, char** argv)
+{
+  int opt, nopts = 0;
+  unsigned int cov_opts = 0;
+  std::string modname = std::string();
+  bool enable = false;
+
+  ktf::setup();
+  testing::InitGoogleTest(&argc,argv);
+
+  if (argc < 3) {
+	usage(argv[0]);
+	return -1;
+  }
+
+  while ((opt = getopt(argc, argv, "e:d:m")) != -1) {
+	switch (opt) {
+	case 'e':
+		nopts++;
+		enable = true;
+		modname = optarg;
+		break;
+	case 'd':
+		nopts++;
+		enable = false;
+		modname = optarg;
+		break;
+	case 'm':
+		cov_opts |= KTF_COV_OPT_MEM;
+		break;
+	default:
+		cerr << "Unknown option '" << char(optopt) << "'";
+		return -1;
+	}
+  }
+  /* Either enable or disable must be specified, and -m is only valid
+   * for enable.
+   */
+  if (modname.size() == 0 || nopts != 1 || (cov_opts && !enable)) {
+	usage(argv[0]);
+	return -1;
+  }
+  return ktf::set_coverage(modname, cov_opts, enable);
+}
diff --git a/tools/testing/selftests/ktf/user/ktfrun.cc b/tools/testing/selftests/ktf/user/ktfrun.cc
new file mode 100644
index 0000000..9229b21
--- /dev/null
+++ b/tools/testing/selftests/ktf/user/ktfrun.cc
@@ -0,0 +1,20 @@
+/*
+ * Copyright (c) 2011, 2017, Oracle and/or its affiliates. All rights reserved.
+ *    Author: Knut Omang <knut.omang@oracle.com>
+ *
+ * SPDX-License-Identifier: GPL-2.0
+ *
+ * ktfrun.cpp: Generic user level application to run kernel tests
+ *   provided by modules subscribing to ktf services.
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <ktf.h>
+
+int main (int argc, char** argv)
+{
+  ktf::setup();
+  testing::InitGoogleTest(&argc,argv);
+
+  return RUN_ALL_TESTS();
+}
diff --git a/tools/testing/selftests/ktf/user/ktftest.cc b/tools/testing/selftests/ktf/user/ktftest.cc
new file mode 100644
index 0000000..fda625d
--- /dev/null
+++ b/tools/testing/selftests/ktf/user/ktftest.cc
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2011, 2017, Oracle and/or its affiliates. All rights reserved.
+ *    Author: Knut Omang <knut.omang@oracle.com>
+ *
+ * SPDX-License-Identifier: GPL-2.0
+ *
+ * ktfrun.cpp: Generic user level application to run kernel tests
+ *   provided by modules subscribing to ktf services.
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <ktf.h>
+
+extern "C" {
+#include "../selftest/context_self.h"
+}
+
+void selftest_configure()
+{
+  struct test_parameter_block p;
+  memset(&p, 0, sizeof(p));
+  strcpy(p.s, CONTEXT_MSG);
+
+  /* First configure two contexts provided by the kernel part: */
+  p.magic = CONTEXT_MAGIC1;
+  KTF_CONTEXT_CFG("context1", "context_type_1", test_parameter_block, &p);
+  p.magic = CONTEXT_MAGIC2;
+  KTF_CONTEXT_CFG("context2", "context_type_2", test_parameter_block, &p);
+
+  /* Configure a 3rd, dynamically created context, using CONTEXT3_TYPE_ID
+   * which the kernel part has enabled for dynamic creation of contexts
+   * from user space (see kernel/context.c: add_context_tests()
+   * for details of setup)
+   */
+  p.magic = CONTEXT_MAGIC3;
+  KTF_CONTEXT_CFG("context3", "context_type_3", test_parameter_block, &p);
+}
+
+
+int main (int argc, char** argv)
+{
+  ktf::setup(selftest_configure);
+  testing::InitGoogleTest(&argc,argv);
+
+  return RUN_ALL_TESTS();
+}
-- 
git-series 0.9.1

  parent reply index

Thread overview: 44+ 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 16:19     ` Knut Omang
2019-08-14  2:02       ` Masahiro Yamada
2019-08-14  5:50         ` Knut Omang
2019-08-14  5:52         ` Knut Omang
2019-08-14 12:52           ` Knut Omang
2019-08-21  1:47             ` Masahiro Yamada
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 ` [RFC 15/19] ktf: Some simple examples Knut Omang
2019-08-13  6:09 ` Knut Omang [this message]
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:17 ` Brendan Higgins
2019-08-13 11:29   ` Knut Omang
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

Reply instructions:

You may reply publically 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=c8c6df6d7b068c2103475f28056ab9a6df3fda4c.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

Linux-kselftest Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-kselftest/0 linux-kselftest/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-kselftest linux-kselftest/ https://lore.kernel.org/linux-kselftest \
		linux-kselftest@vger.kernel.org linux-kselftest@archiver.kernel.org
	public-inbox-index linux-kselftest


Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-kselftest


AGPL code for this site: git clone https://public-inbox.org/ public-inbox