Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp6865130yba; Tue, 14 May 2019 15:21:17 -0700 (PDT) X-Google-Smtp-Source: APXvYqze8Lfo897+a6sSwzccVBUB56sDpYo6Iq9+Xd4tezU4w4LdHci47abcl8qyu3N1BypgReoC X-Received: by 2002:a17:902:5a47:: with SMTP id f7mr15137516plm.321.1557872477129; Tue, 14 May 2019 15:21:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1557872477; cv=none; d=google.com; s=arc-20160816; b=WN52Wwy5SExcsKPGFk87UYii4G2mULPuMU0/y7i0wJjY5zMGioonX8OOVKmoDZ3rMG 50520aNG6j/5qTfTGTqU8bTt+DQNbFvU5bVoTBqcjcBe+AN1/4BW36JQiXPUyMF3w8OL gjvHrbJHjMQSErF7282bw252hT8Zy8KM9TpjI70wuz0XWBvU2nDpyhhVA5kaTVj9riEA chn+tmL+eFI26SpsKiJYAAn/YOY8kPpD+RtM4fP+hegnC+/eHC9RtBk1VbPytNeMFYS9 URTZyK7+PwjuuY8/DVOho0ineafiUqW0N4C/h7TLNeiuI5wdkkqjeZs5u+gEAL1l7WIS IVYQ== 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=bLsKx2EZGy7Z5lmF6P8U1p14LxEyvmeV2AI/h0+08ao=; b=YYoN9Nlu1JblCuph+9J40/4O5Xm1vASCJ+d7FiwKqMbk1ZQ5MSo5N2q6gpQyZft2fT DbFSo3llPdReQXxAES801aAKLHAiy6xxQVMi3cBar/4A6X9WmO+izhVQNfm2YIICWKyM diO/REXuliqnuDOh9Y1GBUy6I25rSQ2mPrqVGqIYzjGtJsV6hUPnvNTXfbVK8rJy6KnV kUa/t2Tv+81SdYnKNqMfU32sAPnpmfNq2ixGEPHSPM/6ZH/HojhsT9NZU1ZkA7fFXtYw t6SC3ZCRhfOiU3WXcXHG2ZYB+H8YBRIlD6OdwUm1oLez7x7IT//N2X38Hm0ciW+BwY4P 9Wig== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=nXpFW2eC; 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 h93si50822plb.256.2019.05.14.15.21.01; Tue, 14 May 2019 15:21:17 -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=nXpFW2eC; 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 S1726906AbfENWTP (ORCPT + 99 others); Tue, 14 May 2019 18:19:15 -0400 Received: from mail-qk1-f202.google.com ([209.85.222.202]:49797 "EHLO mail-qk1-f202.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726887AbfENWTO (ORCPT ); Tue, 14 May 2019 18:19:14 -0400 Received: by mail-qk1-f202.google.com with SMTP id o64so626075qka.16 for ; Tue, 14 May 2019 15:19:13 -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=bLsKx2EZGy7Z5lmF6P8U1p14LxEyvmeV2AI/h0+08ao=; b=nXpFW2eCo8X3CQb6h8VPPaz/nmyRZIiaCq3jooONYXc01LAdzHCG40XWqjgKfCFObO OEGVaBDvDJABKQVffkfT2y7fMrQ2buUGVY+aInZrCiehyv21nC+k5Hcydi4gm8nqbYB8 Nw2dP8K+uH9inmhXE0KTpVdjpxIcOri3fBqRN4lneUFaD+4Fo+s1UEAvh84rFwXVx+Rz anLK3Us/r4rV/nc3wZjSMrluqgZ4nw/g1EQhMXNcDgNrmg7yUitw6vUluK5k4eio3IFB 5xRcwISWlDNshdQu7GX6Mk0GgSA8AhHvgE2sBTQYZsaPmjhsCZn1eEkb4eJlnaVjNvN0 Xv3A== 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=bLsKx2EZGy7Z5lmF6P8U1p14LxEyvmeV2AI/h0+08ao=; b=IWWYE144PfVHSAUKvK7ga6Lt1mAur7DWd6dn2pcH/vS9dZLpoKDFlPxDD3JvEfzm+f mNihrQPlcl8h/P/YDyHBvkPvjorAMtEGifLk18hxrxVlM39qCVuJhX4G+FJkWqZEpXIN Ra6FwFrAep7C3YwUDdBuNJ2xJ74XdrVczrrfUfZZ0ONr8DgMl7UcIJv+qCBgISWcIllT 7heTgU7XVCo+wNEZSB1zG7aox2jym4MBQeFJdXVfFKmeMQpKf6Z3cZHCiEMio0mPV9jC nHec8GgMbBVrFVXI2GaQ8l4tXuv09t6gLMasWcz/LIY032xvDgSIjwAbeHwluG15B60G Cimg== X-Gm-Message-State: APjAAAVHfeoese9KRgLMac/pHoJZHi8fJy0EqlTxc3g1hnj4y2B/DN3a Jlx1AIn2+RDsxiv278PbvqBHumOO6eJZd/NjriS5Iw== X-Received: by 2002:a0c:fe65:: with SMTP id b5mr30921774qvv.106.1557872352515; Tue, 14 May 2019 15:19:12 -0700 (PDT) Date: Tue, 14 May 2019 15:17:03 -0700 In-Reply-To: <20190514221711.248228-1-brendanhiggins@google.com> Message-Id: <20190514221711.248228-11-brendanhiggins@google.com> Mime-Version: 1.0 References: <20190514221711.248228-1-brendanhiggins@google.com> X-Mailer: git-send-email 2.21.0.1020.gf2820cf01a-goog Subject: [PATCH v4 10/18] kunit: test: add tests for kunit test abort From: Brendan Higgins To: frowand.list@gmail.com, gregkh@linuxfoundation.org, jpoimboe@redhat.com, keescook@google.com, kieran.bingham@ideasonboard.com, mcgrof@kernel.org, peterz@infradead.org, robh@kernel.org, sboyd@kernel.org, shuah@kernel.org, tytso@mit.edu, yamada.masahiro@socionext.com 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, 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, rdunlap@infradead.org, 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 Reviewed-by: Greg Kroah-Hartman Reviewed-by: Logan Gunthorpe --- kunit/Makefile | 3 +- kunit/test-test.c | 101 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 103 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..577df5a8d309e --- /dev/null +++ b/kunit/test-test.c @@ -0,0 +1,101 @@ +// 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 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), + {}, +}; + +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.1020.gf2820cf01a-goog