2019-09-23 17:02:34 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
/*
|
|
|
|
* Assertion and expectation serialization API.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2019, Google LLC.
|
|
|
|
* Author: Brendan Higgins <brendanhiggins@google.com>
|
|
|
|
*/
|
|
|
|
#include <kunit/assert.h>
|
|
|
|
|
2020-01-07 06:28:18 +08:00
|
|
|
#include "string-stream.h"
|
|
|
|
|
2019-09-23 17:02:34 +08:00
|
|
|
void kunit_base_assert_format(const struct kunit_assert *assert,
|
|
|
|
struct string_stream *stream)
|
|
|
|
{
|
|
|
|
const char *expect_or_assert = NULL;
|
|
|
|
|
|
|
|
switch (assert->type) {
|
|
|
|
case KUNIT_EXPECTATION:
|
|
|
|
expect_or_assert = "EXPECTATION";
|
|
|
|
break;
|
|
|
|
case KUNIT_ASSERTION:
|
|
|
|
expect_or_assert = "ASSERTION";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
string_stream_add(stream, "%s FAILED at %s:%d\n",
|
|
|
|
expect_or_assert, assert->file, assert->line);
|
|
|
|
}
|
2020-01-07 06:28:20 +08:00
|
|
|
EXPORT_SYMBOL_GPL(kunit_base_assert_format);
|
2019-09-23 17:02:34 +08:00
|
|
|
|
|
|
|
void kunit_assert_print_msg(const struct kunit_assert *assert,
|
|
|
|
struct string_stream *stream)
|
|
|
|
{
|
|
|
|
if (assert->message.fmt)
|
|
|
|
string_stream_add(stream, "\n%pV", &assert->message);
|
|
|
|
}
|
2020-01-07 06:28:20 +08:00
|
|
|
EXPORT_SYMBOL_GPL(kunit_assert_print_msg);
|
2019-09-23 17:02:34 +08:00
|
|
|
|
|
|
|
void kunit_fail_assert_format(const struct kunit_assert *assert,
|
|
|
|
struct string_stream *stream)
|
|
|
|
{
|
|
|
|
kunit_base_assert_format(assert, stream);
|
|
|
|
string_stream_add(stream, "%pV", &assert->message);
|
|
|
|
}
|
2020-01-07 06:28:20 +08:00
|
|
|
EXPORT_SYMBOL_GPL(kunit_fail_assert_format);
|
2019-09-23 17:02:34 +08:00
|
|
|
|
|
|
|
void kunit_unary_assert_format(const struct kunit_assert *assert,
|
|
|
|
struct string_stream *stream)
|
|
|
|
{
|
|
|
|
struct kunit_unary_assert *unary_assert = container_of(
|
|
|
|
assert, struct kunit_unary_assert, assert);
|
|
|
|
|
|
|
|
kunit_base_assert_format(assert, stream);
|
|
|
|
if (unary_assert->expected_true)
|
|
|
|
string_stream_add(stream,
|
|
|
|
"\tExpected %s to be true, but is false\n",
|
|
|
|
unary_assert->condition);
|
|
|
|
else
|
|
|
|
string_stream_add(stream,
|
|
|
|
"\tExpected %s to be false, but is true\n",
|
|
|
|
unary_assert->condition);
|
|
|
|
kunit_assert_print_msg(assert, stream);
|
|
|
|
}
|
2020-01-07 06:28:20 +08:00
|
|
|
EXPORT_SYMBOL_GPL(kunit_unary_assert_format);
|
2019-09-23 17:02:34 +08:00
|
|
|
|
|
|
|
void kunit_ptr_not_err_assert_format(const struct kunit_assert *assert,
|
|
|
|
struct string_stream *stream)
|
|
|
|
{
|
|
|
|
struct kunit_ptr_not_err_assert *ptr_assert = container_of(
|
|
|
|
assert, struct kunit_ptr_not_err_assert, assert);
|
|
|
|
|
|
|
|
kunit_base_assert_format(assert, stream);
|
|
|
|
if (!ptr_assert->value) {
|
|
|
|
string_stream_add(stream,
|
|
|
|
"\tExpected %s is not null, but is\n",
|
|
|
|
ptr_assert->text);
|
|
|
|
} else if (IS_ERR(ptr_assert->value)) {
|
|
|
|
string_stream_add(stream,
|
|
|
|
"\tExpected %s is not error, but is: %ld\n",
|
|
|
|
ptr_assert->text,
|
|
|
|
PTR_ERR(ptr_assert->value));
|
|
|
|
}
|
|
|
|
kunit_assert_print_msg(assert, stream);
|
|
|
|
}
|
2020-01-07 06:28:20 +08:00
|
|
|
EXPORT_SYMBOL_GPL(kunit_ptr_not_err_assert_format);
|
2019-09-23 17:02:34 +08:00
|
|
|
|
|
|
|
void kunit_binary_assert_format(const struct kunit_assert *assert,
|
|
|
|
struct string_stream *stream)
|
|
|
|
{
|
|
|
|
struct kunit_binary_assert *binary_assert = container_of(
|
|
|
|
assert, struct kunit_binary_assert, assert);
|
|
|
|
|
|
|
|
kunit_base_assert_format(assert, stream);
|
|
|
|
string_stream_add(stream,
|
|
|
|
"\tExpected %s %s %s, but\n",
|
|
|
|
binary_assert->left_text,
|
|
|
|
binary_assert->operation,
|
|
|
|
binary_assert->right_text);
|
|
|
|
string_stream_add(stream, "\t\t%s == %lld\n",
|
|
|
|
binary_assert->left_text,
|
|
|
|
binary_assert->left_value);
|
|
|
|
string_stream_add(stream, "\t\t%s == %lld",
|
|
|
|
binary_assert->right_text,
|
|
|
|
binary_assert->right_value);
|
|
|
|
kunit_assert_print_msg(assert, stream);
|
|
|
|
}
|
2020-01-07 06:28:20 +08:00
|
|
|
EXPORT_SYMBOL_GPL(kunit_binary_assert_format);
|
2019-09-23 17:02:34 +08:00
|
|
|
|
|
|
|
void kunit_binary_ptr_assert_format(const struct kunit_assert *assert,
|
|
|
|
struct string_stream *stream)
|
|
|
|
{
|
|
|
|
struct kunit_binary_ptr_assert *binary_assert = container_of(
|
|
|
|
assert, struct kunit_binary_ptr_assert, assert);
|
|
|
|
|
|
|
|
kunit_base_assert_format(assert, stream);
|
|
|
|
string_stream_add(stream,
|
|
|
|
"\tExpected %s %s %s, but\n",
|
|
|
|
binary_assert->left_text,
|
|
|
|
binary_assert->operation,
|
|
|
|
binary_assert->right_text);
|
kunit: Always print actual pointer values in asserts
KUnit assertions and expectations will print the values being tested. If
these are pointers (e.g., KUNIT_EXPECT_PTR_EQ(test, a, b)), these
pointers are currently printed with the %pK format specifier, which -- to
prevent information leaks which may compromise, e.g., ASLR -- are often
either hashed or replaced with ____ptrval____ or similar, making debugging
tests difficult.
By replacing %pK with %px as Documentation/core-api/printk-formats.rst
suggests, we disable this security feature for KUnit assertions and
expectations, allowing the actual pointer values to be printed. Given
that KUnit is not intended for use in production kernels, and the
pointers are only printed on failing tests, this seems like a worthwhile
tradeoff.
Signed-off-by: David Gow <davidgow@google.com>
Reviewed-by: Brendan Higgins <brendanhiggins@google.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
2019-11-22 07:50:58 +08:00
|
|
|
string_stream_add(stream, "\t\t%s == %px\n",
|
2019-09-23 17:02:34 +08:00
|
|
|
binary_assert->left_text,
|
|
|
|
binary_assert->left_value);
|
kunit: Always print actual pointer values in asserts
KUnit assertions and expectations will print the values being tested. If
these are pointers (e.g., KUNIT_EXPECT_PTR_EQ(test, a, b)), these
pointers are currently printed with the %pK format specifier, which -- to
prevent information leaks which may compromise, e.g., ASLR -- are often
either hashed or replaced with ____ptrval____ or similar, making debugging
tests difficult.
By replacing %pK with %px as Documentation/core-api/printk-formats.rst
suggests, we disable this security feature for KUnit assertions and
expectations, allowing the actual pointer values to be printed. Given
that KUnit is not intended for use in production kernels, and the
pointers are only printed on failing tests, this seems like a worthwhile
tradeoff.
Signed-off-by: David Gow <davidgow@google.com>
Reviewed-by: Brendan Higgins <brendanhiggins@google.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
2019-11-22 07:50:58 +08:00
|
|
|
string_stream_add(stream, "\t\t%s == %px",
|
2019-09-23 17:02:34 +08:00
|
|
|
binary_assert->right_text,
|
|
|
|
binary_assert->right_value);
|
|
|
|
kunit_assert_print_msg(assert, stream);
|
|
|
|
}
|
2020-01-07 06:28:20 +08:00
|
|
|
EXPORT_SYMBOL_GPL(kunit_binary_ptr_assert_format);
|
2019-09-23 17:02:34 +08:00
|
|
|
|
|
|
|
void kunit_binary_str_assert_format(const struct kunit_assert *assert,
|
|
|
|
struct string_stream *stream)
|
|
|
|
{
|
|
|
|
struct kunit_binary_str_assert *binary_assert = container_of(
|
|
|
|
assert, struct kunit_binary_str_assert, assert);
|
|
|
|
|
|
|
|
kunit_base_assert_format(assert, stream);
|
|
|
|
string_stream_add(stream,
|
|
|
|
"\tExpected %s %s %s, but\n",
|
|
|
|
binary_assert->left_text,
|
|
|
|
binary_assert->operation,
|
|
|
|
binary_assert->right_text);
|
|
|
|
string_stream_add(stream, "\t\t%s == %s\n",
|
|
|
|
binary_assert->left_text,
|
|
|
|
binary_assert->left_value);
|
|
|
|
string_stream_add(stream, "\t\t%s == %s",
|
|
|
|
binary_assert->right_text,
|
|
|
|
binary_assert->right_value);
|
|
|
|
kunit_assert_print_msg(assert, stream);
|
|
|
|
}
|
2020-01-07 06:28:20 +08:00
|
|
|
EXPORT_SYMBOL_GPL(kunit_binary_str_assert_format);
|