From patchwork Wed Mar 29 09:23:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Javier Martinez Canillas X-Patchwork-Id: 76489 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp284487vqo; Wed, 29 Mar 2023 02:37:19 -0700 (PDT) X-Google-Smtp-Source: AKy350Y5IFOk36U/QqzoORQ6nXJLCH6mvHvWrSVmdls5viL3+yNoOuLJmniPntbO/qHsr57TrPHr X-Received: by 2002:a17:903:6c4:b0:19e:61cc:6793 with SMTP id kj4-20020a17090306c400b0019e61cc6793mr14971813plb.48.1680082638852; Wed, 29 Mar 2023 02:37:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680082638; cv=none; d=google.com; s=arc-20160816; b=hku3w9BwirVpIoOllinQoQFwgsbyLofVlmRUaKs49313VZGnRJaXoiQobSkMx++WBg zor40BOervlppWbqsRpFA6ttD7D8DeDE/mOZ/1ijAFRTKPDvOHtfcfYxVokeTXV6f1DJ HXanVvuxz95BL+Mg4No9G+3W5NFP0YXG3DSUaThaIHZ50jOePUBZpmx/zdBSP+VQbJSw KjGdz1n+cmUQBVNLkXb+wWzIt8AIFauUcnVFQMUpBwExhZ804wmuHpVDDAq/T/fPLo6a JMemvXZCaLq7zUaz1RkGEHeR43+HyrPulArE74JsfZOH+Etern8iGhNo2QgEn++1LQy2 MXeg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=mYk5Vd+Cw2j906cJQiXMpCJj7sRVVRcz7eKjR8ZmdUk=; b=AE069g2GxJPh9R2Zyw2ro3AzEIuOOmz+zg9B59WEnAJWIUSp7Ql/Lo9ISxGwMYqxa6 WawCEY1c9h8zmMXC35iA/94P/68tPkSkEm7AV4Q8olz7L3Q9/DrJ1ksiaRbjfXtrxZKd EbwZAMtCHl0xOFnLGJ6CLztzodSXjvZLv6VdAkjClLP7Gsw4c0PZ2rmSgOnMrgpv/Wx3 PZ6TQv2d9P4ytzxP0U0lhIcbWmcVMfWvJnK5tvQNaCG75iGN4VVbzrzp5bFvfFjyk9s1 MiiDsO/+ZsafV4XYJwE/XF1xpnFKhwRk9w/hJh6w8iyRLpeui/fnRgYPlkXYsRKYRIvL hQIQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=fzNArQiz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i16-20020a63e450000000b004fb887b4733si32631475pgk.874.2023.03.29.02.37.06; Wed, 29 Mar 2023 02:37:18 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=fzNArQiz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229660AbjC2JYf (ORCPT + 99 others); Wed, 29 Mar 2023 05:24:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56824 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229549AbjC2JYd (ORCPT ); Wed, 29 Mar 2023 05:24:33 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A5056269E for ; Wed, 29 Mar 2023 02:23:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1680081825; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=mYk5Vd+Cw2j906cJQiXMpCJj7sRVVRcz7eKjR8ZmdUk=; b=fzNArQizBXnBqvEYCXTSHvjZmrYsBlOKOi+2e4edG18VRA2XvrKj4q5u0r18A9WFdsUXPp sEAR4bU5aTqXC1WtLCs9C92/vI5y2RcjDNG02DzY2EV+caIUbYIM8ruOF4Kar23K+9to5I 5ZFJzbmDAXh8jBtOQBhg3wnF/4rLNW8= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-331-qpnBuqA9MkWXxD_4wo8ZNg-1; Wed, 29 Mar 2023 05:23:44 -0400 X-MC-Unique: qpnBuqA9MkWXxD_4wo8ZNg-1 Received: by mail-wm1-f69.google.com with SMTP id ay37-20020a05600c1e2500b003ee69edec16so8544940wmb.5 for ; Wed, 29 Mar 2023 02:23:44 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680081823; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=mYk5Vd+Cw2j906cJQiXMpCJj7sRVVRcz7eKjR8ZmdUk=; b=fa2T+IM1BJfFmOquNjA6BkYOKMlp0vvexpJGN7uno0UpNWHmJzGg+y28eFJNUuHVBb 4hglPOoUIODfTmd1GeXZfAczQEpkRm09WQ/ZWTErhC4yPdUtobFB6jDZqp6gnRS7EqaW s6MAbuVTxUtO+LSfuTKPXtOeRIzM8i0ZTHHD4Zi6peaQBN5oe91kfJBzj8ckbX9Vfips yD7o8f91ywHl8Fe+XPD8l9hvEAg4QxkBP8nYLWe6Ue70JrMVMDyj3RZeyeSkJa21n6Qv 0d0O7vF9xwvXUbrFLMauNbOY7jfLiAGPlionNTt9gZ2WDHtEKIc4/M5ZC+gvxxe5Og55 4cbg== X-Gm-Message-State: AAQBX9e8oMI0J16OuJ9z5WQdl0FEpMcmtauXnyeeN4ee/magkCrGW9TP 8JriaFXYjGIXHoAas30fStr0rR1j7wQtkzDd+TRJS45DMs0OwVfwGRssJTlKzRNDzzWMSsUIX80 LnrDkFbdLRXB2eUQwUOb9c1+vPj1tNEalaa4bBK5IA+6GApYNzaS/z9jUUV7iK+gFgm9h8nncoo UAlZcEIqw= X-Received: by 2002:a1c:4c11:0:b0:3ef:6ea4:a675 with SMTP id z17-20020a1c4c11000000b003ef6ea4a675mr8271713wmf.36.1680081822790; Wed, 29 Mar 2023 02:23:42 -0700 (PDT) X-Received: by 2002:a1c:4c11:0:b0:3ef:6ea4:a675 with SMTP id z17-20020a1c4c11000000b003ef6ea4a675mr8271689wmf.36.1680081822369; Wed, 29 Mar 2023 02:23:42 -0700 (PDT) Received: from minerva.redhat.com (205.pool92-176-231.dynamic.orange.es. [92.176.231.205]) by smtp.gmail.com with ESMTPSA id l7-20020a05600c4f0700b003ef5deb4188sm1672219wmq.17.2023.03.29.02.23.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Mar 2023 02:23:42 -0700 (PDT) From: Javier Martinez Canillas To: linux-kernel@vger.kernel.org Cc: Brendan Higgins , Maxime Ripard , linux-kselftest@vger.kernel.org, Enric Balletbo i Serra , David Gow , kunit-dev@googlegroups.com, Javier Martinez Canillas , Dmitry Torokhov , linux-input@vger.kernel.org Subject: [PATCH] Input: Add KUnit tests for some of the input core helper functions Date: Wed, 29 Mar 2023 11:23:31 +0200 Message-Id: <20230329092332.2143623-1-javierm@redhat.com> X-Mailer: git-send-email 2.40.0 MIME-Version: 1.0 X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1761694333058833082?= X-GMAIL-MSGID: =?utf-8?q?1761694333058833082?= The input subsystem doesn't currently have any unit tests, let's add a CONFIG_INPUT_KUNIT_TEST option that builds a test suite to be executed with the KUnit test infrastructure. For now, only three tests were added for some of the input core helper functions that are trivial to test: * input_test_polling: set/get poll interval and set-up a poll handler. * input_test_timestamp: set/get input event timestamps. * input_test_match_device_id: match a device by bus, vendor, product and events that is capable of handling. But having the minimal KUnit support allows to add more tests and suites as follow-up changes. The tests can be run with the following command: $ ./tools/testing/kunit/kunit.py run \ --kunitconfig=drivers/input/tests/.kunitconfig Signed-off-by: Javier Martinez Canillas Tested-by: Enric Balletbo i Serra --- drivers/input/Kconfig | 12 +++ drivers/input/Makefile | 1 + drivers/input/tests/Makefile | 3 + drivers/input/tests/input_test.c | 144 +++++++++++++++++++++++++++++++ 4 files changed, 160 insertions(+) create mode 100644 drivers/input/tests/Makefile create mode 100644 drivers/input/tests/input_test.c base-commit: 3a93e40326c8f470e71d20b4c42d36767450f38f diff --git a/drivers/input/Kconfig b/drivers/input/Kconfig index e2752f7364bc..e094e5bbaa0c 100644 --- a/drivers/input/Kconfig +++ b/drivers/input/Kconfig @@ -166,6 +166,18 @@ config INPUT_EVBUG To compile this driver as a module, choose M here: the module will be called evbug. +config INPUT_KUNIT_TEST + tristate "KUnit tests for Input" if !KUNIT_ALL_TESTS + depends on INPUT && KUNIT=y + default KUNIT_ALL_TESTS + help + Say Y here if you want to build the KUnit tests for the input + subsystem. For more information about KUnit and unit tests in + general, please refer to the KUnit documentation in + Documentation/dev-tools/kunit/. + + If in doubt, say "N". + config INPUT_APMPOWER tristate "Input Power Event -> APM Bridge" if EXPERT depends on INPUT && APM_EMULATION diff --git a/drivers/input/Makefile b/drivers/input/Makefile index 2266c7d010ef..c78753274921 100644 --- a/drivers/input/Makefile +++ b/drivers/input/Makefile @@ -26,6 +26,7 @@ obj-$(CONFIG_INPUT_JOYSTICK) += joystick/ obj-$(CONFIG_INPUT_TABLET) += tablet/ obj-$(CONFIG_INPUT_TOUCHSCREEN) += touchscreen/ obj-$(CONFIG_INPUT_MISC) += misc/ +obj-$(CONFIG_INPUT_KUNIT_TEST) += tests/ obj-$(CONFIG_INPUT_APMPOWER) += apm-power.o diff --git a/drivers/input/tests/Makefile b/drivers/input/tests/Makefile new file mode 100644 index 000000000000..90cf954181bc --- /dev/null +++ b/drivers/input/tests/Makefile @@ -0,0 +1,3 @@ +# SPDX-License-Identifier: GPL-2.0 + +obj-$(CONFIG_INPUT_KUNIT_TEST) += input_test.o diff --git a/drivers/input/tests/input_test.c b/drivers/input/tests/input_test.c new file mode 100644 index 000000000000..25bbf51b5c87 --- /dev/null +++ b/drivers/input/tests/input_test.c @@ -0,0 +1,144 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * KUnit test for the input core. + * + * Copyright (c) 2023 Red Hat Inc + */ + +#include +#include + +#include + +#define POLL_INTERVAL 100 + +static int input_test_init(struct kunit *test) +{ + struct input_dev *input_dev; + int ret; + + input_dev = input_allocate_device(); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, input_dev); + + input_dev->name = "Test input device"; + input_dev->id.bustype = BUS_VIRTUAL; + input_dev->id.vendor = 1; + input_dev->id.product = 1; + input_dev->id.version = 1; + input_set_capability(input_dev, EV_KEY, BTN_LEFT); + input_set_capability(input_dev, EV_KEY, BTN_RIGHT); + + ret = input_register_device(input_dev); + KUNIT_ASSERT_EQ(test, ret, 0); + + test->priv = input_dev; + + return 0; +} + +static void input_test_exit(struct kunit *test) +{ + struct input_dev *input_dev = test->priv; + + input_unregister_device(input_dev); +} + +static void input_test_poll(struct input_dev *input) { } + +static void input_test_polling(struct kunit *test) +{ + struct input_dev *input_dev = test->priv; + int ret; + + ret = input_get_poll_interval(input_dev); + KUNIT_ASSERT_EQ(test, ret, -EINVAL); + + ret = input_setup_polling(input_dev, input_test_poll); + KUNIT_ASSERT_EQ(test, ret, 0); + + input_set_poll_interval(input_dev, POLL_INTERVAL); + + ret = input_get_poll_interval(input_dev); + KUNIT_ASSERT_EQ(test, ret, POLL_INTERVAL); +} + +static void input_test_timestamp(struct kunit *test) +{ + const ktime_t invalid_timestamp = ktime_set(0, 0); + struct input_dev *input_dev = test->priv; + ktime_t *timestamp, time; + int ret; + + timestamp = input_get_timestamp(input_dev); + time = timestamp[INPUT_CLK_MONO]; + + ret = ktime_compare(time, invalid_timestamp); + KUNIT_ASSERT_EQ(test, ret, 1); + + time = ktime_get(); + input_set_timestamp(input_dev, time); + + timestamp = input_get_timestamp(input_dev); + KUNIT_ASSERT_EQ(test, ktime_compare(timestamp[INPUT_CLK_MONO], + time), 0); +} + +static void input_test_match_device_id(struct kunit *test) +{ + struct input_dev *input_dev = test->priv; + struct input_device_id id; + + id.flags = INPUT_DEVICE_ID_MATCH_BUS; + id.bustype = BUS_VIRTUAL; + KUNIT_ASSERT_TRUE(test, input_match_device_id(input_dev, &id)); + + id.bustype = BUS_I2C; + KUNIT_ASSERT_FALSE(test, input_match_device_id(input_dev, &id)); + + id.flags = INPUT_DEVICE_ID_MATCH_VENDOR; + id.vendor = 1; + KUNIT_ASSERT_TRUE(test, input_match_device_id(input_dev, &id)); + + id.vendor = 2; + KUNIT_ASSERT_FALSE(test, input_match_device_id(input_dev, &id)); + + id.flags = INPUT_DEVICE_ID_MATCH_PRODUCT; + id.product = 1; + KUNIT_ASSERT_TRUE(test, input_match_device_id(input_dev, &id)); + + id.product = 2; + KUNIT_ASSERT_FALSE(test, input_match_device_id(input_dev, &id)); + + id.flags = INPUT_DEVICE_ID_MATCH_VERSION; + id.version = 1; + KUNIT_ASSERT_TRUE(test, input_match_device_id(input_dev, &id)); + + id.version = 2; + KUNIT_ASSERT_FALSE(test, input_match_device_id(input_dev, &id)); + + id.flags = INPUT_DEVICE_ID_MATCH_EVBIT; + __set_bit(EV_KEY, id.evbit); + KUNIT_ASSERT_TRUE(test, input_match_device_id(input_dev, &id)); + + __set_bit(EV_ABS, id.evbit); + KUNIT_ASSERT_FALSE(test, input_match_device_id(input_dev, &id)); +} + +static struct kunit_case input_tests[] = { + KUNIT_CASE(input_test_polling), + KUNIT_CASE(input_test_timestamp), + KUNIT_CASE(input_test_match_device_id), + { /* sentinel */ } +}; + +static struct kunit_suite input_test_suite = { + .name = "input_core", + .init = input_test_init, + .exit = input_test_exit, + .test_cases = input_tests, +}; + +kunit_test_suite(input_test_suite); + +MODULE_AUTHOR("Javier Martinez Canillas "); +MODULE_LICENSE("GPL");