2019-09-23 17:02:40 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
/*
|
|
|
|
* KUnit test for core test infrastructure.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2019, Google LLC.
|
|
|
|
* Author: Brendan Higgins <brendanhiggins@google.com>
|
|
|
|
*/
|
|
|
|
#include <kunit/test.h>
|
|
|
|
|
2020-01-07 06:28:19 +08:00
|
|
|
#include "try-catch-impl.h"
|
|
|
|
|
2019-09-23 17:02:40 +08:00
|
|
|
struct kunit_try_catch_test_context {
|
|
|
|
struct kunit_try_catch *try_catch;
|
|
|
|
bool function_called;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void kunit_test_successful_try(void *data)
|
|
|
|
{
|
|
|
|
struct kunit *test = data;
|
|
|
|
struct kunit_try_catch_test_context *ctx = test->priv;
|
|
|
|
|
|
|
|
ctx->function_called = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
static 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");
|
|
|
|
}
|
|
|
|
|
|
|
|
static 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);
|
2019-09-23 17:02:41 +08:00
|
|
|
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
|
2019-09-23 17:02:40 +08:00
|
|
|
test->priv = ctx;
|
|
|
|
|
|
|
|
ctx->try_catch = kunit_kmalloc(test,
|
|
|
|
sizeof(*ctx->try_catch),
|
|
|
|
GFP_KERNEL);
|
2019-09-23 17:02:41 +08:00
|
|
|
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->try_catch);
|
2019-09-23 17:02:40 +08:00
|
|
|
|
|
|
|
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_suite kunit_try_catch_test_suite = {
|
|
|
|
.name = "kunit-try-catch-test",
|
|
|
|
.init = kunit_try_catch_test_init,
|
|
|
|
.test_cases = kunit_try_catch_test_cases,
|
|
|
|
};
|
2019-09-23 17:02:42 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
int allocate_order[2];
|
|
|
|
int free_order[2];
|
|
|
|
};
|
|
|
|
|
|
|
|
static int fake_resource_init(struct kunit_resource *res, void *context)
|
|
|
|
{
|
|
|
|
struct kunit_test_resource_context *ctx = context;
|
|
|
|
|
2020-05-30 05:46:20 +08:00
|
|
|
res->data = &ctx->is_resource_initialized;
|
2019-09-23 17:02:42 +08:00
|
|
|
ctx->is_resource_initialized = true;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void fake_resource_free(struct kunit_resource *res)
|
|
|
|
{
|
2020-05-30 05:46:20 +08:00
|
|
|
bool *is_resource_initialized = res->data;
|
2019-09-23 17:02:42 +08:00
|
|
|
|
|
|
|
*is_resource_initialized = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void kunit_resource_test_init_resources(struct kunit *test)
|
|
|
|
{
|
|
|
|
struct kunit_test_resource_context *ctx = test->priv;
|
|
|
|
|
2020-03-26 22:25:07 +08:00
|
|
|
kunit_init_test(&ctx->test, "testing_test_init_test", NULL);
|
2019-09-23 17:02:42 +08:00
|
|
|
|
|
|
|
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_and_get_resource(&ctx->test,
|
|
|
|
fake_resource_init,
|
|
|
|
fake_resource_free,
|
|
|
|
GFP_KERNEL,
|
|
|
|
ctx);
|
|
|
|
|
|
|
|
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, res);
|
|
|
|
KUNIT_EXPECT_PTR_EQ(test,
|
|
|
|
&ctx->is_resource_initialized,
|
2020-05-30 05:46:20 +08:00
|
|
|
(bool *)res->data);
|
2019-09-23 17:02:42 +08:00
|
|
|
KUNIT_EXPECT_TRUE(test, list_is_last(&res->node, &ctx->test.resources));
|
|
|
|
KUNIT_EXPECT_PTR_EQ(test, free, res->free);
|
2020-05-30 05:46:20 +08:00
|
|
|
|
|
|
|
kunit_put_resource(res);
|
2019-09-23 17:02:42 +08:00
|
|
|
}
|
|
|
|
|
2020-05-30 05:46:20 +08:00
|
|
|
/*
|
|
|
|
* Note: tests below use kunit_alloc_and_get_resource(), so as a consequence
|
|
|
|
* they have a reference to the associated resource that they must release
|
|
|
|
* via kunit_put_resource(). In normal operation, users will only
|
|
|
|
* have to do this for cases where they use kunit_find_resource(), and the
|
|
|
|
* kunit_alloc_resource() function will be used (which does not take a
|
|
|
|
* resource reference).
|
|
|
|
*/
|
2019-09-23 17:02:42 +08:00
|
|
|
static void kunit_resource_test_destroy_resource(struct kunit *test)
|
|
|
|
{
|
|
|
|
struct kunit_test_resource_context *ctx = test->priv;
|
|
|
|
struct kunit_resource *res = kunit_alloc_and_get_resource(
|
|
|
|
&ctx->test,
|
|
|
|
fake_resource_init,
|
|
|
|
fake_resource_free,
|
|
|
|
GFP_KERNEL,
|
|
|
|
ctx);
|
|
|
|
|
2020-05-30 05:46:20 +08:00
|
|
|
kunit_put_resource(res);
|
|
|
|
|
2019-09-23 17:02:42 +08:00
|
|
|
KUNIT_ASSERT_FALSE(test,
|
2020-05-30 05:46:20 +08:00
|
|
|
kunit_destroy_resource(&ctx->test,
|
2019-09-23 17:02:42 +08:00
|
|
|
kunit_resource_instance_match,
|
2020-05-30 05:46:20 +08:00
|
|
|
res->data));
|
2019-09-23 17:02:42 +08:00
|
|
|
|
|
|
|
KUNIT_EXPECT_FALSE(test, ctx->is_resource_initialized);
|
|
|
|
KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
|
|
|
|
}
|
|
|
|
|
2022-04-02 12:35:29 +08:00
|
|
|
static void kunit_resource_test_remove_resource(struct kunit *test)
|
|
|
|
{
|
|
|
|
struct kunit_test_resource_context *ctx = test->priv;
|
|
|
|
struct kunit_resource *res = kunit_alloc_and_get_resource(
|
|
|
|
&ctx->test,
|
|
|
|
fake_resource_init,
|
|
|
|
fake_resource_free,
|
|
|
|
GFP_KERNEL,
|
|
|
|
ctx);
|
|
|
|
|
|
|
|
/* The resource is in the list */
|
|
|
|
KUNIT_EXPECT_FALSE(test, list_empty(&ctx->test.resources));
|
|
|
|
|
|
|
|
/* Remove the resource. The pointer is still valid, but it can't be
|
|
|
|
* found.
|
|
|
|
*/
|
|
|
|
kunit_remove_resource(test, res);
|
|
|
|
KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
|
|
|
|
/* We haven't been freed yet. */
|
|
|
|
KUNIT_EXPECT_TRUE(test, ctx->is_resource_initialized);
|
|
|
|
|
|
|
|
/* Removing the resource multiple times is valid. */
|
|
|
|
kunit_remove_resource(test, res);
|
|
|
|
KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
|
|
|
|
/* Despite having been removed twice (from only one reference), the
|
|
|
|
* resource still has not been freed.
|
|
|
|
*/
|
|
|
|
KUNIT_EXPECT_TRUE(test, ctx->is_resource_initialized);
|
|
|
|
|
|
|
|
/* Free the resource. */
|
|
|
|
kunit_put_resource(res);
|
|
|
|
KUNIT_EXPECT_FALSE(test, ctx->is_resource_initialized);
|
|
|
|
}
|
|
|
|
|
2019-09-23 17:02:42 +08:00
|
|
|
static void kunit_resource_test_cleanup_resources(struct kunit *test)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct kunit_test_resource_context *ctx = test->priv;
|
|
|
|
struct kunit_resource *resources[5];
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(resources); i++) {
|
|
|
|
resources[i] = kunit_alloc_and_get_resource(&ctx->test,
|
|
|
|
fake_resource_init,
|
|
|
|
fake_resource_free,
|
|
|
|
GFP_KERNEL,
|
|
|
|
ctx);
|
2020-05-30 05:46:20 +08:00
|
|
|
kunit_put_resource(resources[i]);
|
2019-09-23 17:02:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
kunit_cleanup(&ctx->test);
|
|
|
|
|
|
|
|
KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void kunit_resource_test_mark_order(int order_array[],
|
|
|
|
size_t order_size,
|
|
|
|
int key)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < order_size && order_array[i]; i++)
|
|
|
|
;
|
|
|
|
|
|
|
|
order_array[i] = key;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, order_field, key) \
|
|
|
|
kunit_resource_test_mark_order(ctx->order_field, \
|
|
|
|
ARRAY_SIZE(ctx->order_field), \
|
|
|
|
key)
|
|
|
|
|
|
|
|
static int fake_resource_2_init(struct kunit_resource *res, void *context)
|
|
|
|
{
|
|
|
|
struct kunit_test_resource_context *ctx = context;
|
|
|
|
|
|
|
|
KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, allocate_order, 2);
|
|
|
|
|
2020-05-30 05:46:20 +08:00
|
|
|
res->data = ctx;
|
2019-09-23 17:02:42 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void fake_resource_2_free(struct kunit_resource *res)
|
|
|
|
{
|
2020-05-30 05:46:20 +08:00
|
|
|
struct kunit_test_resource_context *ctx = res->data;
|
2019-09-23 17:02:42 +08:00
|
|
|
|
|
|
|
KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, free_order, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int fake_resource_1_init(struct kunit_resource *res, void *context)
|
|
|
|
{
|
|
|
|
struct kunit_test_resource_context *ctx = context;
|
2020-05-30 05:46:20 +08:00
|
|
|
struct kunit_resource *res2;
|
2019-09-23 17:02:42 +08:00
|
|
|
|
2020-05-30 05:46:20 +08:00
|
|
|
res2 = kunit_alloc_and_get_resource(&ctx->test,
|
|
|
|
fake_resource_2_init,
|
|
|
|
fake_resource_2_free,
|
|
|
|
GFP_KERNEL,
|
|
|
|
ctx);
|
2019-09-23 17:02:42 +08:00
|
|
|
|
|
|
|
KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, allocate_order, 1);
|
|
|
|
|
2020-05-30 05:46:20 +08:00
|
|
|
res->data = ctx;
|
|
|
|
|
|
|
|
kunit_put_resource(res2);
|
2019-09-23 17:02:42 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void fake_resource_1_free(struct kunit_resource *res)
|
|
|
|
{
|
2020-05-30 05:46:20 +08:00
|
|
|
struct kunit_test_resource_context *ctx = res->data;
|
2019-09-23 17:02:42 +08:00
|
|
|
|
|
|
|
KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, free_order, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* TODO(brendanhiggins@google.com): replace the arrays that keep track of the
|
|
|
|
* order of allocation and freeing with strict mocks using the IN_SEQUENCE macro
|
|
|
|
* to assert allocation and freeing order when the feature becomes available.
|
|
|
|
*/
|
|
|
|
static void kunit_resource_test_proper_free_ordering(struct kunit *test)
|
|
|
|
{
|
|
|
|
struct kunit_test_resource_context *ctx = test->priv;
|
2020-05-30 05:46:20 +08:00
|
|
|
struct kunit_resource *res;
|
2019-09-23 17:02:42 +08:00
|
|
|
|
|
|
|
/* fake_resource_1 allocates a fake_resource_2 in its init. */
|
2020-05-30 05:46:20 +08:00
|
|
|
res = kunit_alloc_and_get_resource(&ctx->test,
|
|
|
|
fake_resource_1_init,
|
|
|
|
fake_resource_1_free,
|
|
|
|
GFP_KERNEL,
|
|
|
|
ctx);
|
2019-09-23 17:02:42 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Since fake_resource_2_init calls KUNIT_RESOURCE_TEST_MARK_ORDER
|
|
|
|
* before returning to fake_resource_1_init, it should be the first to
|
|
|
|
* put its key in the allocate_order array.
|
|
|
|
*/
|
|
|
|
KUNIT_EXPECT_EQ(test, ctx->allocate_order[0], 2);
|
|
|
|
KUNIT_EXPECT_EQ(test, ctx->allocate_order[1], 1);
|
|
|
|
|
2020-05-30 05:46:20 +08:00
|
|
|
kunit_put_resource(res);
|
|
|
|
|
2019-09-23 17:02:42 +08:00
|
|
|
kunit_cleanup(&ctx->test);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Because fake_resource_2 finishes allocation before fake_resource_1,
|
|
|
|
* fake_resource_1 should be freed first since it could depend on
|
|
|
|
* fake_resource_2.
|
|
|
|
*/
|
|
|
|
KUNIT_EXPECT_EQ(test, ctx->free_order[0], 1);
|
|
|
|
KUNIT_EXPECT_EQ(test, ctx->free_order[1], 2);
|
|
|
|
}
|
|
|
|
|
2020-05-30 05:46:20 +08:00
|
|
|
static void kunit_resource_test_static(struct kunit *test)
|
|
|
|
{
|
|
|
|
struct kunit_test_resource_context ctx;
|
|
|
|
struct kunit_resource res;
|
|
|
|
|
|
|
|
KUNIT_EXPECT_EQ(test, kunit_add_resource(test, NULL, NULL, &res, &ctx),
|
|
|
|
0);
|
|
|
|
|
|
|
|
KUNIT_EXPECT_PTR_EQ(test, res.data, (void *)&ctx);
|
|
|
|
|
|
|
|
kunit_cleanup(test);
|
|
|
|
|
|
|
|
KUNIT_EXPECT_TRUE(test, list_empty(&test->resources));
|
|
|
|
}
|
|
|
|
|
2020-05-30 05:46:21 +08:00
|
|
|
static void kunit_resource_test_named(struct kunit *test)
|
|
|
|
{
|
|
|
|
struct kunit_resource res1, res2, *found = NULL;
|
|
|
|
struct kunit_test_resource_context ctx;
|
|
|
|
|
|
|
|
KUNIT_EXPECT_EQ(test,
|
|
|
|
kunit_add_named_resource(test, NULL, NULL, &res1,
|
|
|
|
"resource_1", &ctx),
|
|
|
|
0);
|
|
|
|
KUNIT_EXPECT_PTR_EQ(test, res1.data, (void *)&ctx);
|
|
|
|
|
|
|
|
KUNIT_EXPECT_EQ(test,
|
|
|
|
kunit_add_named_resource(test, NULL, NULL, &res1,
|
|
|
|
"resource_1", &ctx),
|
|
|
|
-EEXIST);
|
|
|
|
|
|
|
|
KUNIT_EXPECT_EQ(test,
|
|
|
|
kunit_add_named_resource(test, NULL, NULL, &res2,
|
|
|
|
"resource_2", &ctx),
|
|
|
|
0);
|
|
|
|
|
|
|
|
found = kunit_find_named_resource(test, "resource_1");
|
|
|
|
|
|
|
|
KUNIT_EXPECT_PTR_EQ(test, found, &res1);
|
|
|
|
|
|
|
|
if (found)
|
|
|
|
kunit_put_resource(&res1);
|
|
|
|
|
|
|
|
KUNIT_EXPECT_EQ(test, kunit_destroy_named_resource(test, "resource_2"),
|
|
|
|
0);
|
|
|
|
|
|
|
|
kunit_cleanup(test);
|
|
|
|
|
|
|
|
KUNIT_EXPECT_TRUE(test, list_empty(&test->resources));
|
|
|
|
}
|
|
|
|
|
2019-09-23 17:02:42 +08:00
|
|
|
static int kunit_resource_test_init(struct kunit *test)
|
|
|
|
{
|
|
|
|
struct kunit_test_resource_context *ctx =
|
|
|
|
kzalloc(sizeof(*ctx), GFP_KERNEL);
|
|
|
|
|
|
|
|
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
|
|
|
|
|
|
|
|
test->priv = ctx;
|
|
|
|
|
2020-03-26 22:25:07 +08:00
|
|
|
kunit_init_test(&ctx->test, "test_test_context", NULL);
|
2019-09-23 17:02:42 +08:00
|
|
|
|
|
|
|
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_destroy_resource),
|
2022-04-02 12:35:29 +08:00
|
|
|
KUNIT_CASE(kunit_resource_test_remove_resource),
|
2019-09-23 17:02:42 +08:00
|
|
|
KUNIT_CASE(kunit_resource_test_cleanup_resources),
|
|
|
|
KUNIT_CASE(kunit_resource_test_proper_free_ordering),
|
2020-05-30 05:46:20 +08:00
|
|
|
KUNIT_CASE(kunit_resource_test_static),
|
2020-05-30 05:46:21 +08:00
|
|
|
KUNIT_CASE(kunit_resource_test_named),
|
2019-09-23 17:02:42 +08:00
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct kunit_suite kunit_resource_test_suite = {
|
|
|
|
.name = "kunit-resource-test",
|
|
|
|
.init = kunit_resource_test_init,
|
|
|
|
.exit = kunit_resource_test_exit,
|
|
|
|
.test_cases = kunit_resource_test_cases,
|
|
|
|
};
|
2020-03-26 22:25:08 +08:00
|
|
|
|
|
|
|
static void kunit_log_test(struct kunit *test);
|
|
|
|
|
|
|
|
static struct kunit_case kunit_log_test_cases[] = {
|
|
|
|
KUNIT_CASE(kunit_log_test),
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct kunit_suite kunit_log_test_suite = {
|
|
|
|
.name = "kunit-log-test",
|
|
|
|
.test_cases = kunit_log_test_cases,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void kunit_log_test(struct kunit *test)
|
|
|
|
{
|
kunit: drop assumption in kunit-log-test about current suite
This test assumes that the declared kunit_suite object is the exact one
which is being executed, which KUnit will not guarantee [1].
Specifically, `suite->log` is not initialized until a suite object is
executed. So if KUnit makes a copy of the suite and runs that instead,
this test dereferences an invalid pointer and (hopefully) segfaults.
N.B. since we no longer assume this, we can no longer verify that
`suite->log` is *not* allocated during normal execution.
An alternative to this patch that would allow us to test that would
require exposing an API for the current test to get its current suite.
Exposing that for one internal kunit test seems like overkill, and
grants users more footguns (e.g. reusing a test case in multiple suites
and changing behavior based on the suite name, dynamically modifying the
setup/cleanup funcs, storing/reading stuff out of the suite->log, etc.).
[1] In a subsequent patch, KUnit will allow running subsets of test
cases within a suite by making a copy of the suite w/ the filtered test
list. But there are other reasons KUnit might execute a copy, e.g. if it
ever wants to support parallel execution of different suites, recovering
from errors and restarting suites
Signed-off-by: Daniel Latypov <dlatypov@google.com>
Reviewed-by: Brendan Higgins <brendanhiggins@google.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
2021-09-15 05:03:47 +08:00
|
|
|
struct kunit_suite suite;
|
|
|
|
|
|
|
|
suite.log = kunit_kzalloc(test, KUNIT_LOG_SIZE, GFP_KERNEL);
|
|
|
|
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, suite.log);
|
2020-03-26 22:25:08 +08:00
|
|
|
|
|
|
|
kunit_log(KERN_INFO, test, "put this in log.");
|
|
|
|
kunit_log(KERN_INFO, test, "this too.");
|
kunit: drop assumption in kunit-log-test about current suite
This test assumes that the declared kunit_suite object is the exact one
which is being executed, which KUnit will not guarantee [1].
Specifically, `suite->log` is not initialized until a suite object is
executed. So if KUnit makes a copy of the suite and runs that instead,
this test dereferences an invalid pointer and (hopefully) segfaults.
N.B. since we no longer assume this, we can no longer verify that
`suite->log` is *not* allocated during normal execution.
An alternative to this patch that would allow us to test that would
require exposing an API for the current test to get its current suite.
Exposing that for one internal kunit test seems like overkill, and
grants users more footguns (e.g. reusing a test case in multiple suites
and changing behavior based on the suite name, dynamically modifying the
setup/cleanup funcs, storing/reading stuff out of the suite->log, etc.).
[1] In a subsequent patch, KUnit will allow running subsets of test
cases within a suite by making a copy of the suite w/ the filtered test
list. But there are other reasons KUnit might execute a copy, e.g. if it
ever wants to support parallel execution of different suites, recovering
from errors and restarting suites
Signed-off-by: Daniel Latypov <dlatypov@google.com>
Reviewed-by: Brendan Higgins <brendanhiggins@google.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
2021-09-15 05:03:47 +08:00
|
|
|
kunit_log(KERN_INFO, &suite, "add to suite log.");
|
|
|
|
kunit_log(KERN_INFO, &suite, "along with this.");
|
2020-03-26 22:25:08 +08:00
|
|
|
|
|
|
|
#ifdef CONFIG_KUNIT_DEBUGFS
|
|
|
|
KUNIT_EXPECT_NOT_ERR_OR_NULL(test,
|
|
|
|
strstr(test->log, "put this in log."));
|
|
|
|
KUNIT_EXPECT_NOT_ERR_OR_NULL(test,
|
|
|
|
strstr(test->log, "this too."));
|
|
|
|
KUNIT_EXPECT_NOT_ERR_OR_NULL(test,
|
kunit: drop assumption in kunit-log-test about current suite
This test assumes that the declared kunit_suite object is the exact one
which is being executed, which KUnit will not guarantee [1].
Specifically, `suite->log` is not initialized until a suite object is
executed. So if KUnit makes a copy of the suite and runs that instead,
this test dereferences an invalid pointer and (hopefully) segfaults.
N.B. since we no longer assume this, we can no longer verify that
`suite->log` is *not* allocated during normal execution.
An alternative to this patch that would allow us to test that would
require exposing an API for the current test to get its current suite.
Exposing that for one internal kunit test seems like overkill, and
grants users more footguns (e.g. reusing a test case in multiple suites
and changing behavior based on the suite name, dynamically modifying the
setup/cleanup funcs, storing/reading stuff out of the suite->log, etc.).
[1] In a subsequent patch, KUnit will allow running subsets of test
cases within a suite by making a copy of the suite w/ the filtered test
list. But there are other reasons KUnit might execute a copy, e.g. if it
ever wants to support parallel execution of different suites, recovering
from errors and restarting suites
Signed-off-by: Daniel Latypov <dlatypov@google.com>
Reviewed-by: Brendan Higgins <brendanhiggins@google.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
2021-09-15 05:03:47 +08:00
|
|
|
strstr(suite.log, "add to suite log."));
|
2020-03-26 22:25:08 +08:00
|
|
|
KUNIT_EXPECT_NOT_ERR_OR_NULL(test,
|
kunit: drop assumption in kunit-log-test about current suite
This test assumes that the declared kunit_suite object is the exact one
which is being executed, which KUnit will not guarantee [1].
Specifically, `suite->log` is not initialized until a suite object is
executed. So if KUnit makes a copy of the suite and runs that instead,
this test dereferences an invalid pointer and (hopefully) segfaults.
N.B. since we no longer assume this, we can no longer verify that
`suite->log` is *not* allocated during normal execution.
An alternative to this patch that would allow us to test that would
require exposing an API for the current test to get its current suite.
Exposing that for one internal kunit test seems like overkill, and
grants users more footguns (e.g. reusing a test case in multiple suites
and changing behavior based on the suite name, dynamically modifying the
setup/cleanup funcs, storing/reading stuff out of the suite->log, etc.).
[1] In a subsequent patch, KUnit will allow running subsets of test
cases within a suite by making a copy of the suite w/ the filtered test
list. But there are other reasons KUnit might execute a copy, e.g. if it
ever wants to support parallel execution of different suites, recovering
from errors and restarting suites
Signed-off-by: Daniel Latypov <dlatypov@google.com>
Reviewed-by: Brendan Higgins <brendanhiggins@google.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
2021-09-15 05:03:47 +08:00
|
|
|
strstr(suite.log, "along with this."));
|
2020-03-26 22:25:08 +08:00
|
|
|
#else
|
2022-02-12 00:42:42 +08:00
|
|
|
KUNIT_EXPECT_NULL(test, test->log);
|
2020-03-26 22:25:08 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-06-25 14:58:12 +08:00
|
|
|
static void kunit_status_set_failure_test(struct kunit *test)
|
|
|
|
{
|
|
|
|
struct kunit fake;
|
|
|
|
|
|
|
|
kunit_init_test(&fake, "fake test", NULL);
|
|
|
|
|
|
|
|
KUNIT_EXPECT_EQ(test, fake.status, (enum kunit_status)KUNIT_SUCCESS);
|
|
|
|
kunit_set_failure(&fake);
|
|
|
|
KUNIT_EXPECT_EQ(test, fake.status, (enum kunit_status)KUNIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void kunit_status_mark_skipped_test(struct kunit *test)
|
|
|
|
{
|
|
|
|
struct kunit fake;
|
|
|
|
|
|
|
|
kunit_init_test(&fake, "fake test", NULL);
|
|
|
|
|
|
|
|
/* Before: Should be SUCCESS with no comment. */
|
|
|
|
KUNIT_EXPECT_EQ(test, fake.status, KUNIT_SUCCESS);
|
|
|
|
KUNIT_EXPECT_STREQ(test, fake.status_comment, "");
|
|
|
|
|
|
|
|
/* Mark the test as skipped. */
|
|
|
|
kunit_mark_skipped(&fake, "Accepts format string: %s", "YES");
|
|
|
|
|
|
|
|
/* After: Should be SKIPPED with our comment. */
|
|
|
|
KUNIT_EXPECT_EQ(test, fake.status, (enum kunit_status)KUNIT_SKIPPED);
|
|
|
|
KUNIT_EXPECT_STREQ(test, fake.status_comment, "Accepts format string: YES");
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct kunit_case kunit_status_test_cases[] = {
|
|
|
|
KUNIT_CASE(kunit_status_set_failure_test),
|
|
|
|
KUNIT_CASE(kunit_status_mark_skipped_test),
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct kunit_suite kunit_status_test_suite = {
|
|
|
|
.name = "kunit_status",
|
|
|
|
.test_cases = kunit_status_test_cases,
|
|
|
|
};
|
|
|
|
|
2020-03-26 22:25:08 +08:00
|
|
|
kunit_test_suites(&kunit_try_catch_test_suite, &kunit_resource_test_suite,
|
2021-06-25 14:58:12 +08:00
|
|
|
&kunit_log_test_suite, &kunit_status_test_suite);
|
2020-01-07 06:28:20 +08:00
|
|
|
|
|
|
|
MODULE_LICENSE("GPL v2");
|