From patchwork Thu Feb 14 21:37:22 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brendan Higgins X-Patchwork-Id: 1042450 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="tNVVHaXw"; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=google.com header.i=@google.com header.b="wR5UZ8Mn"; 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 440qXX44C8z9sDL for ; Fri, 15 Feb 2019 08:39:08 +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=eNwSg22XVZ9MU4vMbG5xsUvd7i2a2CxDsG9cP8UTBJQ=; b=tNVVHaXwuvys4k GF5wj+ABJFJk+Mp7/c/PfdwEkk5A9971DU3Wy7yocluD0KrT2MCddi35ayMs8yIrQEowvj0kXeURR lucMkvapxKBjUk7Jrh+23B2NWnl1+O3KqAZeGPJEHQdBxkhkdhAr9sYZIiiv2fjcFK8iNoWVYraVX LbS+P935k0GHiNStT6dYTctoQd57k5mjaevHFxi3FdqZhYjMTITFtmHaTdDNZAtlTxvHuNBf8f2zf KTsTm8o+dpqOSR6soHDubnw4DGCnG33flEZf6YkoeLHFrN3S9wklv6R/C8JX7TRF1uQ5HPFXoCM7t Qzkz9KSdVTEratA2mb7A==; 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 1guOii-0002Dh-Cn; Thu, 14 Feb 2019 21:39:00 +0000 Received: from mail-vk1-xa49.google.com ([2607:f8b0:4864:20::a49]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1guOif-0002Cl-H5 for linux-um@lists.infradead.org; Thu, 14 Feb 2019 21:38:58 +0000 Received: by mail-vk1-xa49.google.com with SMTP id y196so3129247vkd.0 for ; Thu, 14 Feb 2019 13:38:56 -0800 (PST) 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=lFB3ckD9wxM7bLXj4FIGXk/8wLU/9JJW8w6AYqQ9grk=; b=wR5UZ8MnBfmgt0mrk9OI2zfDUhZY2FSpJFlc0H37jwgc96pBztb7fGKWTkoOFM28Ga QdMIu5N3UkjMmm7021TzRYk/cV1BnPbW4MtjUKPwt51uaE+fMNPFoimjbpRGCDHRC4qG rF/9HGEJTgXVmjERaYbSkHQNurtVfrADHvm+4f2etjUc19ab4p6e7BsAxhXpCzh2DVbh 1qIqHUMYZmAhWwLj0wStuzKrlp2f5ArmCo8Pw4WHmaV0ubR/6cHsjshHUAlvy2ad4Zc7 fT6+TNOikOpDCk2iZY9GB/1MB0KgGDOgjPxVqJZHudZCO4TukRT9wX0iqh4to7EcadBk jKQg== 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=lFB3ckD9wxM7bLXj4FIGXk/8wLU/9JJW8w6AYqQ9grk=; b=FAGOoVXs4rO5PCs35uMArex6wGXpyJphcP6E4e5Hh5TfZPkJRTShOLqE1aCWignKau J+Am7HUWB0lEdQ5MNVFYRvbgoyLj3x0wvllvz/VNwyB7/HhbWOdQ0/3v/S+/TJDte9ZV 8kqCKjuva/5TD9ubXEjVIe2RyRIs7O0U6pu0ot9B7uQOwz96RYvH4N8NEfQ1pTFMv3Co JlFP+EYZ2Jha3JA7OFkQ8uXpuCUa8cKfdvBtgitnWkubtmzbhbBW4ycC0oheVdGFGCjs ThKGBPBdCAw5Ww6mnwAFwrtAuLTjs2KHnI6PlsDAuilIAqAvE6Eqztu3oitUcWTk7fk4 rgFw== X-Gm-Message-State: AHQUAuYgLhIblL5RdeoSB0STxjkLlU1jkHFZIuNX+XRddLeyfYoLHtlC vjw3wNmxi53OuFMonSpjmPFH7B2PsY/FUvt3EhkrYg== X-Google-Smtp-Source: AHgI3IZSFaDNk0CEH4L9GvdAED7vCeS8gXXQOioZtPpSgMCEqNw/swX6iIzcGwBJejpW5Xow4Gqbz47ePA5Svka1+dm8fw== X-Received: by 2002:ac5:c203:: with SMTP id m3mr3934061vkk.19.1550180335945; Thu, 14 Feb 2019 13:38:55 -0800 (PST) Date: Thu, 14 Feb 2019 13:37:22 -0800 In-Reply-To: <20190214213729.21702-1-brendanhiggins@google.com> Message-Id: <20190214213729.21702-11-brendanhiggins@google.com> Mime-Version: 1.0 References: <20190214213729.21702-1-brendanhiggins@google.com> X-Mailer: git-send-email 2.21.0.rc0.258.g878e2cd30e-goog Subject: [RFC v4 10/17] kunit: test: add test managed resource tests From: Brendan Higgins To: keescook@google.com, mcgrof@kernel.org, shuah@kernel.org, robh@kernel.org, kieran.bingham@ideasonboard.com, frowand.list@gmail.com X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20190214_133857_561277_5B96BD8F X-CRM114-Status: UNSURE ( 8.75 ) X-CRM114-Notice: Please train this message. X-Spam-Score: -7.7 (-------) X-Spam-Report: SpamAssassin version 3.4.2 on bombadil.infradead.org summary: Content analysis details: (-7.7 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:a49 listed in] [list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record -7.5 USER_IN_DEF_DKIM_WL From: address is in the default DKIM white-list -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain -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, pmladek@suse.com, amir73il@gmail.com, Brendan Higgins , dri-devel@lists.freedesktop.org, Alexander.Levin@microsoft.com, linux-kselftest@vger.kernel.org, linux-nvdimm@lists.01.org, richard@nod.at, knut.omang@oracle.com, wfg@linux.intel.com, joel@jms.id.au, jdike@addtoit.com, dan.carpenter@oracle.com, devicetree@vger.kernel.org, Tim.Bird@sony.com, linux-um@lists.infradead.org, rostedt@goodmis.org, julia.lawall@lip6.fr, dan.j.williams@intel.com, kunit-dev@googlegroups.com, gregkh@linuxfoundation.org, linux-kernel@vger.kernel.org, Avinash Kondareddy , daniel@ffwll.ch, mpe@ellerman.id.au, joe@perches.com, khilman@baylibre.com Sender: "linux-um" Errors-To: linux-um-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org Tests how tests interact with test managed resources in their lifetime. Signed-off-by: Avinash Kondareddy Signed-off-by: Brendan Higgins --- kunit/test-test.c | 121 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) diff --git a/kunit/test-test.c b/kunit/test-test.c index 0b4ad6690310d..bb34431398526 100644 --- a/kunit/test-test.c +++ b/kunit/test-test.c @@ -127,3 +127,124 @@ static struct kunit_module kunit_try_catch_test_module = { .test_cases = kunit_try_catch_test_cases, }; module_test(kunit_try_catch_test_module); + +/* + * Context for testing test managed resources + * is_resource_initialized is used to test arbitrary resources + */ +struct kunit_test_resource_context { + struct kunit test; + bool is_resource_initialized; +}; + +static int fake_resource_init(struct kunit_resource *res, void *context) +{ + struct kunit_test_resource_context *ctx = context; + + res->allocation = &ctx->is_resource_initialized; + ctx->is_resource_initialized = true; + return 0; +} + +static void fake_resource_free(struct kunit_resource *res) +{ + bool *is_resource_initialized = res->allocation; + + *is_resource_initialized = false; +} + +static void kunit_resource_test_init_resources(struct kunit *test) +{ + struct kunit_test_resource_context *ctx = test->priv; + + kunit_init_test(&ctx->test, "testing_test_init_test"); + + KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources)); +} + +static void kunit_resource_test_alloc_resource(struct kunit *test) +{ + struct kunit_test_resource_context *ctx = test->priv; + struct kunit_resource *res; + kunit_resource_free_t free = fake_resource_free; + + res = kunit_alloc_resource(&ctx->test, + fake_resource_init, + fake_resource_free, + ctx); + + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, res); + KUNIT_EXPECT_EQ(test, &ctx->is_resource_initialized, res->allocation); + KUNIT_EXPECT_TRUE(test, list_is_last(&res->node, &ctx->test.resources)); + KUNIT_EXPECT_EQ(test, free, res->free); +} + +static void kunit_resource_test_free_resource(struct kunit *test) +{ + struct kunit_test_resource_context *ctx = test->priv; + struct kunit_resource *res = kunit_alloc_resource(&ctx->test, + fake_resource_init, + fake_resource_free, + ctx); + + kunit_free_resource(&ctx->test, res); + + KUNIT_EXPECT_EQ(test, false, ctx->is_resource_initialized); + KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources)); +} + +#define KUNIT_RESOURCE_NUM 5 +static void kunit_resource_test_cleanup_resources(struct kunit *test) +{ + int i; + struct kunit_test_resource_context *ctx = test->priv; + struct kunit_resource *resources[KUNIT_RESOURCE_NUM]; + + for (i = 0; i < KUNIT_RESOURCE_NUM; i++) { + resources[i] = kunit_alloc_resource(&ctx->test, + fake_resource_init, + fake_resource_free, + ctx); + } + + kunit_cleanup(&ctx->test); + + KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources)); +} + +static int kunit_resource_test_init(struct kunit *test) +{ + struct kunit_test_resource_context *ctx = + kzalloc(sizeof(*ctx), GFP_KERNEL); + + if (!ctx) + return -ENOMEM; + test->priv = ctx; + + kunit_init_test(&ctx->test, "test_test_context"); + return 0; +} + +static void kunit_resource_test_exit(struct kunit *test) +{ + struct kunit_test_resource_context *ctx = test->priv; + + kunit_cleanup(&ctx->test); + kfree(ctx); +} + +static struct kunit_case kunit_resource_test_cases[] = { + KUNIT_CASE(kunit_resource_test_init_resources), + KUNIT_CASE(kunit_resource_test_alloc_resource), + KUNIT_CASE(kunit_resource_test_free_resource), + KUNIT_CASE(kunit_resource_test_cleanup_resources), + {}, +}; + +static struct kunit_module kunit_resource_test_module = { + .name = "kunit-resource-test", + .init = kunit_resource_test_init, + .exit = kunit_resource_test_exit, + .test_cases = kunit_resource_test_cases, +}; +module_test(kunit_resource_test_module);