^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Assertion and expectation serialization API.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2019, Google LLC.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Author: Brendan Higgins <brendanhiggins@google.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <kunit/assert.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <kunit/test.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include "string-stream.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) void kunit_base_assert_format(const struct kunit_assert *assert,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) struct string_stream *stream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) const char *expect_or_assert = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) switch (assert->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) case KUNIT_EXPECTATION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) expect_or_assert = "EXPECTATION";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) case KUNIT_ASSERTION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) expect_or_assert = "ASSERTION";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) string_stream_add(stream, "%s FAILED at %s:%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) expect_or_assert, assert->file, assert->line);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) EXPORT_SYMBOL_GPL(kunit_base_assert_format);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) void kunit_assert_print_msg(const struct kunit_assert *assert,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) struct string_stream *stream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) if (assert->message.fmt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) string_stream_add(stream, "\n%pV", &assert->message);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) EXPORT_SYMBOL_GPL(kunit_assert_print_msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) void kunit_fail_assert_format(const struct kunit_assert *assert,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) struct string_stream *stream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) kunit_base_assert_format(assert, stream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) string_stream_add(stream, "%pV", &assert->message);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) EXPORT_SYMBOL_GPL(kunit_fail_assert_format);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) void kunit_unary_assert_format(const struct kunit_assert *assert,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) struct string_stream *stream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) struct kunit_unary_assert *unary_assert = container_of(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) assert, struct kunit_unary_assert, assert);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) kunit_base_assert_format(assert, stream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) if (unary_assert->expected_true)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) string_stream_add(stream,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) KUNIT_SUBTEST_INDENT "Expected %s to be true, but is false\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) unary_assert->condition);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) string_stream_add(stream,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) KUNIT_SUBTEST_INDENT "Expected %s to be false, but is true\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) unary_assert->condition);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) kunit_assert_print_msg(assert, stream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) EXPORT_SYMBOL_GPL(kunit_unary_assert_format);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) void kunit_ptr_not_err_assert_format(const struct kunit_assert *assert,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) struct string_stream *stream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) struct kunit_ptr_not_err_assert *ptr_assert = container_of(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) assert, struct kunit_ptr_not_err_assert, assert);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) kunit_base_assert_format(assert, stream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) if (!ptr_assert->value) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) string_stream_add(stream,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) KUNIT_SUBTEST_INDENT "Expected %s is not null, but is\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) ptr_assert->text);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) } else if (IS_ERR(ptr_assert->value)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) string_stream_add(stream,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) KUNIT_SUBTEST_INDENT "Expected %s is not error, but is: %ld\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) ptr_assert->text,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) PTR_ERR(ptr_assert->value));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) kunit_assert_print_msg(assert, stream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) EXPORT_SYMBOL_GPL(kunit_ptr_not_err_assert_format);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) void kunit_binary_assert_format(const struct kunit_assert *assert,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) struct string_stream *stream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) struct kunit_binary_assert *binary_assert = container_of(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) assert, struct kunit_binary_assert, assert);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) kunit_base_assert_format(assert, stream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) string_stream_add(stream,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) KUNIT_SUBTEST_INDENT "Expected %s %s %s, but\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) binary_assert->left_text,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) binary_assert->operation,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) binary_assert->right_text);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %lld\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) binary_assert->left_text,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) binary_assert->left_value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %lld",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) binary_assert->right_text,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) binary_assert->right_value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) kunit_assert_print_msg(assert, stream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) EXPORT_SYMBOL_GPL(kunit_binary_assert_format);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) void kunit_binary_ptr_assert_format(const struct kunit_assert *assert,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) struct string_stream *stream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) struct kunit_binary_ptr_assert *binary_assert = container_of(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) assert, struct kunit_binary_ptr_assert, assert);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) kunit_base_assert_format(assert, stream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) string_stream_add(stream,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) KUNIT_SUBTEST_INDENT "Expected %s %s %s, but\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) binary_assert->left_text,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) binary_assert->operation,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) binary_assert->right_text);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %px\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) binary_assert->left_text,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) binary_assert->left_value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %px",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) binary_assert->right_text,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) binary_assert->right_value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) kunit_assert_print_msg(assert, stream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) EXPORT_SYMBOL_GPL(kunit_binary_ptr_assert_format);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) void kunit_binary_str_assert_format(const struct kunit_assert *assert,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) struct string_stream *stream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) struct kunit_binary_str_assert *binary_assert = container_of(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) assert, struct kunit_binary_str_assert, assert);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) kunit_base_assert_format(assert, stream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) string_stream_add(stream,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) KUNIT_SUBTEST_INDENT "Expected %s %s %s, but\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) binary_assert->left_text,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) binary_assert->operation,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) binary_assert->right_text);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) binary_assert->left_text,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) binary_assert->left_value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) binary_assert->right_text,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) binary_assert->right_value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) kunit_assert_print_msg(assert, stream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) EXPORT_SYMBOL_GPL(kunit_binary_str_assert_format);