From patchwork Tue Oct 16 23:51:14 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brendan Higgins X-Patchwork-Id: 985063 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:e::133; helo=bombadil.infradead.org; envelope-from=linux-um-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="KrTFzp6M"; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=google.com header.i=@google.com header.b="SkamGQ8F"; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 42ZXJ23lQqz9sB5 for ; Wed, 17 Oct 2018 10:55:46 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:To:From:Subject:References:Mime-Version :Message-Id:In-Reply-To:Date:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=txCuQnKaMkmHJJgBshWdvvqU9NFqBapsUrQofV3pJcI=; b=KrTFzp6Md76zxE uhnRVvfUvIkiUAtLhErG4N7fTd2pJwqkItC4QwWPmY5PHmh9jAa5KgEROoEYz/27/3P7bhpdeX+rB yD/1c/WDApmOJ0hNo6Pv4KG2ygk+Us6bRc1XSFeI1Pk33AwTiVptxEnZ6jU8GgdJLCOq5xm8tNFj2 FsZdK1uk8V2D8Sm8W2lJ9J8Ic3VDcNnOd2BOYoIgON+wy8/RqgyjVZ0YGSHj2xnRnxgp04/bqXAH/ QOd9MM5uDVxGaVQGulmVO0ZBLYTMlSjybKtOp8KwW+aiMUWNMWwRBKocKZpFajNXeFcTUbsm1LQRw xrFwpN64EJSrXRyBZAKA==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1gCZBY-00067W-8P; Tue, 16 Oct 2018 23:55:36 +0000 Received: from mail-vk1-xa4a.google.com ([2607:f8b0:4864:20::a4a]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1gCZAx-0004Xj-R5 for linux-um@lists.infradead.org; Tue, 16 Oct 2018 23:55:26 +0000 Received: by mail-vk1-xa4a.google.com with SMTP id y199-v6so2823231vkd.20 for ; Tue, 16 Oct 2018 16:54:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=v7Vdu6qIuJDj39fxhnwZmKq9fD4gFEoXGX/BaHbf6WY=; b=SkamGQ8FhTSHorjRljiJ6OFjD1m25IP36W7aMKtTvCYBTsCiHMQ+g6OFZ5CrW0Upqi 7mPFJKchLsgfdjOW+QOJbhaJfZoUjNBWn9pxuTZBLRFB/G3iYschPGYmqGTRLXnWXe56 E+Ku6Hy4CgqEyeNB+DFizdSn/TCDkQ73T+lJA0qmOY+SQIGxS8zbc7Yu8SDw01YaZYbQ 3c7PRNsuK6UycYb0zxh8yXy4612Ntg8n3d0GWgXfEUqF0cnDN9FCfxwow5Au5BXO1w+l 5vR1oqiR6EH4rgQ6FYCUHMk9hG+r+Vwcq9dCw+Vig44hEnQNg09IjV8bDhM4LIW0YOUH z6+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=v7Vdu6qIuJDj39fxhnwZmKq9fD4gFEoXGX/BaHbf6WY=; b=VfOMwDlMgbrl/UnPZ3wNvxZmG0vsMdaPOmqteXpsQOczntrNowd3Iaoo1NC7jTqSac yXPLKRDfePHJij9N6wICZbRhPMQHgLzowU0cJQ3H4iYZqVXDnSfGFe/Xy9v3nmfqHF7M L5CyJDsHwLuiZaPbMfzV/AUuLI8c45IAQwfPrDd6858TSveqv9qbt+6LxgJUK6CftABn TeRucXcDqv8OiRkYTX5Sh+ZEd0Dq/0tjzswWHxc3CN8WKCO1zm5YU3vB6WR78efurLPp tFq0zy8uhyrS7q7TC5u2UZqtwm5NtGdp+rUcZP/R5Y2XYAgeDIUXhiGSDwLz4GD7ovkI WO1Q== X-Gm-Message-State: ABuFfogwmrHU8aH58pi6U0i3yTBucwf7Rmebi9rZscsDCAmpKH5Gol/G vw6nsbtvze61Ryh+4CcnA0UZx5ULQ3qwtarQ8voPEw== X-Google-Smtp-Source: ACcGV624pVPucriQYprUzO/GVEaPpNRkvZ/3eMhUsnZvHlDsB/kz5iwwgeHbsp39vDToO03LzP5qRgwLVX6LGUat1s5cMA== X-Received: by 2002:a67:7ec5:: with SMTP id z188mr18417863vsc.30.1539734087634; Tue, 16 Oct 2018 16:54:47 -0700 (PDT) Date: Tue, 16 Oct 2018 16:51:14 -0700 In-Reply-To: <20181016235120.138227-1-brendanhiggins@google.com> Message-Id: <20181016235120.138227-26-brendanhiggins@google.com> Mime-Version: 1.0 References: <20181016235120.138227-1-brendanhiggins@google.com> X-Mailer: git-send-email 2.19.1.331.ge82ca0e54c-goog Subject: [RFC v1 25/31] kunit: added concept of platform mocking From: Brendan Higgins To: gregkh@linuxfoundation.org, keescook@google.com, mcgrof@kernel.org, shuah@kernel.org X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20181016_165459_906365_C98EE9C2 X-CRM114-Status: GOOD ( 11.92 ) X-Spam-Score: -7.6 (-------) X-Spam-Report: SpamAssassin version 3.4.1 on bombadil.infradead.org summary: Content analysis details: (-7.6 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at http://www.dnswl.org/, no trust [2607:f8b0:4864:20:0:0:0:a4a listed in] [list.dnswl.org] -7.5 USER_IN_DEF_DKIM_WL From: address is in the default DKIM white-list -0.0 SPF_PASS SPF: sender matches SPF record -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.0 DKIMWL_WL_MED DKIMwl.org - Whitelisted Medium sender X-BeenThere: linux-um@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: brakmo@fb.com, richard@nod.at, mpe@ellerman.id.au, Tim.Bird@sony.com, linux-um@lists.infradead.org, linux-kernel@vger.kernel.org, rostedt@goodmis.org, julia.lawall@lip6.fr, joel@jms.id.au, linux-kselftest@vger.kernel.org, khilman@baylibre.com, joe@perches.com, jdike@addtoit.com, Brendan Higgins , kunit-dev@googlegroups.com Sender: "linux-um" Errors-To: linux-um-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org Platform mocking is the mocking of all platform specific functions that interact directly with hardware. In effect, this provides the ability to mock any hardware behavior. Signed-off-by: Brendan Higgins --- drivers/base/Makefile | 1 + drivers/base/platform-mock.c | 65 ++++++++++++++++++++++++++++ include/linux/platform_device_mock.h | 64 +++++++++++++++++++++++++++ 3 files changed, 130 insertions(+) create mode 100644 drivers/base/platform-mock.c create mode 100644 include/linux/platform_device_mock.h diff --git a/drivers/base/Makefile b/drivers/base/Makefile index 704f442958103..77cc599daa020 100644 --- a/drivers/base/Makefile +++ b/drivers/base/Makefile @@ -23,6 +23,7 @@ obj-$(CONFIG_PINCTRL) += pinctrl.o obj-$(CONFIG_DEV_COREDUMP) += devcoredump.o obj-$(CONFIG_GENERIC_MSI_IRQ_DOMAIN) += platform-msi.o obj-$(CONFIG_GENERIC_ARCH_TOPOLOGY) += arch_topology.o +obj-$(CONFIG_PLATFORM_MOCK) += platform-mock.o obj-y += test/ diff --git a/drivers/base/platform-mock.c b/drivers/base/platform-mock.c new file mode 100644 index 0000000000000..3df9f1b0bb50f --- /dev/null +++ b/drivers/base/platform-mock.c @@ -0,0 +1,65 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Fake platform device API for unit testing platform drivers. + * + * Copyright (C) 2018, Google LLC. + * Author: Brendan Higgins + */ + +#include +#include + +struct device_node *of_fake_node(struct test *test, const char *name) +{ + struct device_node *node; + + node = test_kzalloc(test, sizeof(*node), GFP_KERNEL); + if (!node) + return NULL; + + of_node_init(node); + + return node; +} + +struct platform_device * +of_fake_probe_platform(struct test *test, + const struct platform_driver *driver, + const char *node_name) +{ + struct platform_device *pdev; + struct device_node *of_node; + int ret; + + of_node = of_fake_node(test, node_name); + if (!of_node) + return ERR_PTR(-ENOMEM); + + test_info(test, "Creating device"); + pdev = of_platform_device_create(of_node, node_name, NULL); + if (!pdev) + return ERR_PTR(-ENODEV); + + test_info(test, "Probing"); + ret = driver->probe(pdev); + if (ret) + return ERR_PTR(ret); + + return pdev; +} + +struct platform_device *of_fake_probe_platform_by_name(struct test *test, + const char *driver_name, + const char *node_name) +{ + const struct device_driver *driver; + + test_info(test, "Locating driver by name"); + driver = driver_find(driver_name, &platform_bus_type); + if (!driver) + return ERR_PTR(-ENODEV); + + return of_fake_probe_platform(test, + to_platform_driver(driver), + node_name); +} diff --git a/include/linux/platform_device_mock.h b/include/linux/platform_device_mock.h new file mode 100644 index 0000000000000..898539d166f66 --- /dev/null +++ b/include/linux/platform_device_mock.h @@ -0,0 +1,64 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Fake platform device API for unit testing platform drivers. + * + * Copyright (C) 2018, Google LLC. + * Author: Brendan Higgins + */ + +#include +#include + +static inline struct platform_driver *platform_driver_find(const char *name) +{ + struct device_driver *driver; + + driver = driver_find(name, &platform_bus_type); + if (!driver) + return NULL; + + return to_platform_driver(driver); +} + +/** + * of_fake_node() + * @test: the test to associate node with + * @name: name of the node + * + * The &struct device_node returned is allocated as a root node with the given + * name and otherwise behaves as a real &struct device_node. + * + * Returns: the faked &struct device_node + */ +struct device_node *of_fake_node(struct test *test, const char *name); + +/** + * of_fake_probe_platform() + * @test: the test to associate the fake platform device with + * @driver: driver to probe + * @node_name: name of the device node created + * + * Creates a &struct platform_device and an associated &struct device_node, + * probes the provided &struct platform_driver with the &struct platform_device. + * + * Returns: the &struct platform_device that was created + */ +struct platform_device * +of_fake_probe_platform(struct test *test, + const struct platform_driver *driver, + const char *node_name); + +/** + * of_fake_probe_platform_by_name() + * @test: the test to associate the fake platform device with + * @driver_name: name of the driver to probe + * @node_name: name of the device node created + * + * Same as of_fake_probe_platform() but looks up the &struct platform_driver by + * the provided name. + * + * Returns: the &struct platform_device that was created + */ +struct platform_device *of_fake_probe_platform_by_name(struct test *test, + const char *driver_name, + const char *node_name);