Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp6603904yba; Wed, 1 May 2019 16:05:54 -0700 (PDT) X-Google-Smtp-Source: APXvYqxYtOGz3/sX1hnmmCYsdSc5a90UNEPu7NKG2st84G68Fpy8girz1q+mYrZ3TZpj5eIuuIuN X-Received: by 2002:a17:902:bd4a:: with SMTP id b10mr231894plx.75.1556751954531; Wed, 01 May 2019 16:05:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1556751954; cv=none; d=google.com; s=arc-20160816; b=m6mmuY6U/Ig3rJAxM1p78RDhbeYHEHu+vQEMkW6DFhBJlmqJUsKYWLr9i7iNdbYNtR B5cWhhR1H1P9uDT2epaZSKJ++iZ7uInQ/g1PezBQ/9cYIrAaXkZXvwKUL0Jc0pu/5wQF pDwoBkYlCEBroyMjEetZGvSFyhkm6yMdovm9AJFg9fjnFG8oz8T+MQgxCqo/nMi4Cqq6 gYRiBYCrycd2wjQezkOtTYiE4/PZW5ZklF4QDKracV+otfjNJdZsgdnbG7jiwtqVe0q4 63l+0Psbx3+32iE0/wyw0bY5cU2ZRQOlhrioaC7jt9DKOgeYRrpOSMij87oAe959fN0k WL+g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:from:subject:references :mime-version:message-id:in-reply-to:date:dkim-signature; bh=OoB1Q1B1OKTnVM2sKsSTjgsrzyoIYKMiukBZ0PgDZ9o=; b=Ipg3B5hw8sZcYIOBjMCF1XXU7NVWiVj3ZH5xMMzJHqRoexgyjXEONmMDewEkM8Kj6C 8vVrEmb1KD0XzFRcIJr6UglEuooJNox+coJmbXEnmBLSWPZEQrLPZ6ndWaJrCe93Iquf podGDntWnXBTgPtbXZDWminlo7jO2EtcwlT/omg6yR0H9LM/snzmQFQKC/28HrGmvbaW 3twDuMBADGljjB6ikm+dFW3mil7ch01VBbjTpDrwpfqJAbjKmqcZ/A3R2GjZ1txzCFUv /z5DB2eNrNGp8W6Qlw8Tr8XQPrgwv6MpWVQGDFkB/dnRpYlHyzehdYFbqsur3WTyVTFq WI4A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b="awoM/T0A"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id n3si39370251pgq.202.2019.05.01.16.05.39; Wed, 01 May 2019 16:05:54 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b="awoM/T0A"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726407AbfEAXEV (ORCPT + 99 others); Wed, 1 May 2019 19:04:21 -0400 Received: from mail-ot1-f73.google.com ([209.85.210.73]:43240 "EHLO mail-ot1-f73.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726480AbfEAXEU (ORCPT ); Wed, 1 May 2019 19:04:20 -0400 Received: by mail-ot1-f73.google.com with SMTP id q23so295522otk.10 for ; Wed, 01 May 2019 16:04:19 -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=OoB1Q1B1OKTnVM2sKsSTjgsrzyoIYKMiukBZ0PgDZ9o=; b=awoM/T0AoqG0wYtJorCPvm2AZEqZUzUs5qq1R8YR0UNkjXyRX24zF53GZ1UD6KiV+8 WoKSipt9phdWDdeo/zDMvjArUywT8O+1qVccX5uPZnOzbzBfB+8h4jIyjzJGGBdg4n7N imrAzBCt4voZvz3O+nOIMytkIln4VOZ1LvzQprZkOv9VhoaC6AXS8ZkpWrt4ZerM7ljw vSmK9aGxj4/37leOcxAYhpXvhHLCXqqaZL5x06G9q7/18UfD7rqvqJjZVKB+iD2XROqO KzY2suaK26r7WlIWBRrvFuLTnnO37Kovql58dhGKMkYPGEiPzC9D5ieJu5MBGxBF7KnL 22pw== 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=OoB1Q1B1OKTnVM2sKsSTjgsrzyoIYKMiukBZ0PgDZ9o=; b=JUOt6JcliEdp1KtRsPgFdcPYnWR99mhPOeRkmJZzDlQAp9oXDfXw+PSpYELdEBXVMp a3N/s+N7dUVUw1nfPZCed+FGGZt1ynZjfgTG+3A0IwoxarPK1dJZhAfjeZNnmLh3AfaC Jgesm8HRnYcqhwQpEaSU37qg2YB3KG8gNu9YvqYiAW2k2ItZSf6aWfJanigqv413Hmq5 /X6Ajw/tQLbokl73jzj2UJWfPce/4aJ/FhR5YREMdKueX/8ir27CB9K/jGa1Anu00XDl 1S+0dIwUzyjDMMebTrGW+wX44fDGS70XjJfgs56ZvSHgjGczqLFlE6IdxQD1Fs+2hwc8 Jfdg== X-Gm-Message-State: APjAAAXrrDr3iTQauLbRztVKs3tt97i6G/kVN1VhwCYhPu4UKa5uBFc8 TfSUCGTZwO7LTB29xuP8UP86r5aok7ec4Un9KgJTtA== X-Received: by 2002:a9d:6b93:: with SMTP id b19mr372226otq.313.1556751859218; Wed, 01 May 2019 16:04:19 -0700 (PDT) Date: Wed, 1 May 2019 16:01:18 -0700 In-Reply-To: <20190501230126.229218-1-brendanhiggins@google.com> Message-Id: <20190501230126.229218-10-brendanhiggins@google.com> Mime-Version: 1.0 References: <20190501230126.229218-1-brendanhiggins@google.com> X-Mailer: git-send-email 2.21.0.593.g511ec345e18-goog Subject: [PATCH v2 09/17] kunit: test: add tests for kunit test abort From: Brendan Higgins To: frowand.list@gmail.com, gregkh@linuxfoundation.org, keescook@google.com, kieran.bingham@ideasonboard.com, mcgrof@kernel.org, robh@kernel.org, sboyd@kernel.org, shuah@kernel.org Cc: devicetree@vger.kernel.org, dri-devel@lists.freedesktop.org, kunit-dev@googlegroups.com, linux-doc@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-nvdimm@lists.01.org, linux-um@lists.infradead.org, Alexander.Levin@microsoft.com, Tim.Bird@sony.com, amir73il@gmail.com, dan.carpenter@oracle.com, dan.j.williams@intel.com, daniel@ffwll.ch, jdike@addtoit.com, joel@jms.id.au, julia.lawall@lip6.fr, khilman@baylibre.com, knut.omang@oracle.com, logang@deltatee.com, mpe@ellerman.id.au, pmladek@suse.com, richard@nod.at, rientjes@google.com, rostedt@goodmis.org, wfg@linux.intel.com, Brendan Higgins Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add KUnit tests for the KUnit test abort mechanism (see preceding commit). Add tests both for general try catch mechanism as well as non-architecture specific mechanism. Signed-off-by: Brendan Higgins --- kunit/Makefile | 3 +- kunit/test-test.c | 135 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 137 insertions(+), 1 deletion(-) create mode 100644 kunit/test-test.c diff --git a/kunit/Makefile b/kunit/Makefile index 1f7680cfa11ad..533355867abd2 100644 --- a/kunit/Makefile +++ b/kunit/Makefile @@ -3,6 +3,7 @@ obj-$(CONFIG_KUNIT) += test.o \ kunit-stream.o \ try-catch.o -obj-$(CONFIG_KUNIT_TEST) += string-stream-test.o +obj-$(CONFIG_KUNIT_TEST) += test-test.o \ + string-stream-test.o obj-$(CONFIG_KUNIT_EXAMPLE_TEST) += example-test.o diff --git a/kunit/test-test.c b/kunit/test-test.c new file mode 100644 index 0000000000000..c81ae6efb959f --- /dev/null +++ b/kunit/test-test.c @@ -0,0 +1,135 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * KUnit test for core test infrastructure. + * + * Copyright (C) 2019, Google LLC. + * Author: Brendan Higgins + */ +#include + +struct kunit_try_catch_test_context { + struct kunit_try_catch *try_catch; + bool function_called; +}; + +void kunit_test_successful_try(void *data) +{ + struct kunit *test = data; + struct kunit_try_catch_test_context *ctx = test->priv; + + ctx->function_called = true; +} + +void kunit_test_no_catch(void *data) +{ + struct kunit *test = data; + + KUNIT_FAIL(test, "Catch should not be called.\n"); +} + +static void kunit_test_try_catch_successful_try_no_catch(struct kunit *test) +{ + struct kunit_try_catch_test_context *ctx = test->priv; + struct kunit_try_catch *try_catch = ctx->try_catch; + + kunit_try_catch_init(try_catch, + test, + kunit_test_successful_try, + kunit_test_no_catch); + kunit_try_catch_run(try_catch, test); + + KUNIT_EXPECT_TRUE(test, ctx->function_called); +} + +void kunit_test_unsuccessful_try(void *data) +{ + struct kunit *test = data; + struct kunit_try_catch_test_context *ctx = test->priv; + struct kunit_try_catch *try_catch = ctx->try_catch; + + kunit_try_catch_throw(try_catch); + KUNIT_FAIL(test, "This line should never be reached.\n"); +} + +void kunit_test_catch(void *data) +{ + struct kunit *test = data; + struct kunit_try_catch_test_context *ctx = test->priv; + + ctx->function_called = true; +} + +static void kunit_test_try_catch_unsuccessful_try_does_catch(struct kunit *test) +{ + struct kunit_try_catch_test_context *ctx = test->priv; + struct kunit_try_catch *try_catch = ctx->try_catch; + + kunit_try_catch_init(try_catch, + test, + kunit_test_unsuccessful_try, + kunit_test_catch); + kunit_try_catch_run(try_catch, test); + + KUNIT_EXPECT_TRUE(test, ctx->function_called); +} + +static void kunit_test_generic_try_catch_successful_try_no_catch( + struct kunit *test) +{ + struct kunit_try_catch_test_context *ctx = test->priv; + struct kunit_try_catch *try_catch = ctx->try_catch; + + try_catch->test = test; + kunit_generic_try_catch_init(try_catch); + try_catch->try = kunit_test_successful_try; + try_catch->catch = kunit_test_no_catch; + + kunit_try_catch_run(try_catch, test); + + KUNIT_EXPECT_TRUE(test, ctx->function_called); +} + +static void kunit_test_generic_try_catch_unsuccessful_try_does_catch( + struct kunit *test) +{ + struct kunit_try_catch_test_context *ctx = test->priv; + struct kunit_try_catch *try_catch = ctx->try_catch; + + try_catch->test = test; + kunit_generic_try_catch_init(try_catch); + try_catch->try = kunit_test_unsuccessful_try; + try_catch->catch = kunit_test_catch; + + kunit_try_catch_run(try_catch, test); + + KUNIT_EXPECT_TRUE(test, ctx->function_called); +} + +static int kunit_try_catch_test_init(struct kunit *test) +{ + struct kunit_try_catch_test_context *ctx; + + ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); + test->priv = ctx; + + ctx->try_catch = kunit_kmalloc(test, + sizeof(*ctx->try_catch), + GFP_KERNEL); + + return 0; +} + +static struct kunit_case kunit_try_catch_test_cases[] = { + KUNIT_CASE(kunit_test_try_catch_successful_try_no_catch), + KUNIT_CASE(kunit_test_try_catch_unsuccessful_try_does_catch), + KUNIT_CASE(kunit_test_generic_try_catch_successful_try_no_catch), + KUNIT_CASE(kunit_test_generic_try_catch_unsuccessful_try_does_catch), + {}, +}; + +static struct kunit_module kunit_try_catch_test_module = { + .name = "kunit-try-catch-test", + .init = kunit_try_catch_test_init, + .test_cases = kunit_try_catch_test_cases, +}; +module_test(kunit_try_catch_test_module); -- 2.21.0.593.g511ec345e18-goog