Brendan Higgins | 6b229e5 | 2019-09-23 02:02:34 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Assertion and expectation serialization API. |
| 4 | * |
| 5 | * Copyright (C) 2019, Google LLC. |
| 6 | * Author: Brendan Higgins <brendanhiggins@google.com> |
| 7 | */ |
| 8 | #include <kunit/assert.h> |
Alan Maguire | c3bba69 | 2020-03-26 14:25:09 +0000 | [diff] [blame] | 9 | #include <kunit/test.h> |
Brendan Higgins | 6b229e5 | 2019-09-23 02:02:34 -0700 | [diff] [blame] | 10 | |
Alan Maguire | 109fb06 | 2020-01-06 22:28:18 +0000 | [diff] [blame] | 11 | #include "string-stream.h" |
| 12 | |
Brendan Higgins | 6b229e5 | 2019-09-23 02:02:34 -0700 | [diff] [blame] | 13 | void kunit_base_assert_format(const struct kunit_assert *assert, |
| 14 | struct string_stream *stream) |
| 15 | { |
| 16 | const char *expect_or_assert = NULL; |
| 17 | |
| 18 | switch (assert->type) { |
| 19 | case KUNIT_EXPECTATION: |
| 20 | expect_or_assert = "EXPECTATION"; |
| 21 | break; |
| 22 | case KUNIT_ASSERTION: |
| 23 | expect_or_assert = "ASSERTION"; |
| 24 | break; |
| 25 | } |
| 26 | |
| 27 | string_stream_add(stream, "%s FAILED at %s:%d\n", |
Lucas Stankus | 2f9f21c | 2021-03-12 16:11:26 -0300 | [diff] [blame] | 28 | expect_or_assert, assert->file, assert->line); |
Brendan Higgins | 6b229e5 | 2019-09-23 02:02:34 -0700 | [diff] [blame] | 29 | } |
Alan Maguire | c475c77 | 2020-01-06 22:28:20 +0000 | [diff] [blame] | 30 | EXPORT_SYMBOL_GPL(kunit_base_assert_format); |
Brendan Higgins | 6b229e5 | 2019-09-23 02:02:34 -0700 | [diff] [blame] | 31 | |
| 32 | void kunit_assert_print_msg(const struct kunit_assert *assert, |
| 33 | struct string_stream *stream) |
| 34 | { |
| 35 | if (assert->message.fmt) |
| 36 | string_stream_add(stream, "\n%pV", &assert->message); |
| 37 | } |
Alan Maguire | c475c77 | 2020-01-06 22:28:20 +0000 | [diff] [blame] | 38 | EXPORT_SYMBOL_GPL(kunit_assert_print_msg); |
Brendan Higgins | 6b229e5 | 2019-09-23 02:02:34 -0700 | [diff] [blame] | 39 | |
| 40 | void kunit_fail_assert_format(const struct kunit_assert *assert, |
| 41 | struct string_stream *stream) |
| 42 | { |
| 43 | kunit_base_assert_format(assert, stream); |
| 44 | string_stream_add(stream, "%pV", &assert->message); |
| 45 | } |
Alan Maguire | c475c77 | 2020-01-06 22:28:20 +0000 | [diff] [blame] | 46 | EXPORT_SYMBOL_GPL(kunit_fail_assert_format); |
Brendan Higgins | 6b229e5 | 2019-09-23 02:02:34 -0700 | [diff] [blame] | 47 | |
| 48 | void kunit_unary_assert_format(const struct kunit_assert *assert, |
| 49 | struct string_stream *stream) |
| 50 | { |
Lucas Stankus | 2f9f21c | 2021-03-12 16:11:26 -0300 | [diff] [blame] | 51 | struct kunit_unary_assert *unary_assert; |
| 52 | |
| 53 | unary_assert = container_of(assert, struct kunit_unary_assert, assert); |
Brendan Higgins | 6b229e5 | 2019-09-23 02:02:34 -0700 | [diff] [blame] | 54 | |
| 55 | kunit_base_assert_format(assert, stream); |
| 56 | if (unary_assert->expected_true) |
| 57 | string_stream_add(stream, |
Alan Maguire | c3bba69 | 2020-03-26 14:25:09 +0000 | [diff] [blame] | 58 | KUNIT_SUBTEST_INDENT "Expected %s to be true, but is false\n", |
| 59 | unary_assert->condition); |
Brendan Higgins | 6b229e5 | 2019-09-23 02:02:34 -0700 | [diff] [blame] | 60 | else |
| 61 | string_stream_add(stream, |
Alan Maguire | c3bba69 | 2020-03-26 14:25:09 +0000 | [diff] [blame] | 62 | KUNIT_SUBTEST_INDENT "Expected %s to be false, but is true\n", |
| 63 | unary_assert->condition); |
Brendan Higgins | 6b229e5 | 2019-09-23 02:02:34 -0700 | [diff] [blame] | 64 | kunit_assert_print_msg(assert, stream); |
| 65 | } |
Alan Maguire | c475c77 | 2020-01-06 22:28:20 +0000 | [diff] [blame] | 66 | EXPORT_SYMBOL_GPL(kunit_unary_assert_format); |
Brendan Higgins | 6b229e5 | 2019-09-23 02:02:34 -0700 | [diff] [blame] | 67 | |
| 68 | void kunit_ptr_not_err_assert_format(const struct kunit_assert *assert, |
| 69 | struct string_stream *stream) |
| 70 | { |
Lucas Stankus | 2f9f21c | 2021-03-12 16:11:26 -0300 | [diff] [blame] | 71 | struct kunit_ptr_not_err_assert *ptr_assert; |
| 72 | |
| 73 | ptr_assert = container_of(assert, struct kunit_ptr_not_err_assert, |
| 74 | assert); |
Brendan Higgins | 6b229e5 | 2019-09-23 02:02:34 -0700 | [diff] [blame] | 75 | |
| 76 | kunit_base_assert_format(assert, stream); |
| 77 | if (!ptr_assert->value) { |
| 78 | string_stream_add(stream, |
Alan Maguire | c3bba69 | 2020-03-26 14:25:09 +0000 | [diff] [blame] | 79 | KUNIT_SUBTEST_INDENT "Expected %s is not null, but is\n", |
| 80 | ptr_assert->text); |
Brendan Higgins | 6b229e5 | 2019-09-23 02:02:34 -0700 | [diff] [blame] | 81 | } else if (IS_ERR(ptr_assert->value)) { |
| 82 | string_stream_add(stream, |
Alan Maguire | c3bba69 | 2020-03-26 14:25:09 +0000 | [diff] [blame] | 83 | KUNIT_SUBTEST_INDENT "Expected %s is not error, but is: %ld\n", |
| 84 | ptr_assert->text, |
| 85 | PTR_ERR(ptr_assert->value)); |
Brendan Higgins | 6b229e5 | 2019-09-23 02:02:34 -0700 | [diff] [blame] | 86 | } |
| 87 | kunit_assert_print_msg(assert, stream); |
| 88 | } |
Alan Maguire | c475c77 | 2020-01-06 22:28:20 +0000 | [diff] [blame] | 89 | EXPORT_SYMBOL_GPL(kunit_ptr_not_err_assert_format); |
Brendan Higgins | 6b229e5 | 2019-09-23 02:02:34 -0700 | [diff] [blame] | 90 | |
Daniel Latypov | 65af9b9 | 2021-02-05 14:14:09 -0800 | [diff] [blame] | 91 | /* Checks if `text` is a literal representing `value`, e.g. "5" and 5 */ |
| 92 | static bool is_literal(struct kunit *test, const char *text, long long value, |
| 93 | gfp_t gfp) |
| 94 | { |
| 95 | char *buffer; |
| 96 | int len; |
| 97 | bool ret; |
| 98 | |
| 99 | len = snprintf(NULL, 0, "%lld", value); |
| 100 | if (strlen(text) != len) |
| 101 | return false; |
| 102 | |
| 103 | buffer = kunit_kmalloc(test, len+1, gfp); |
| 104 | if (!buffer) |
| 105 | return false; |
| 106 | |
| 107 | snprintf(buffer, len+1, "%lld", value); |
| 108 | ret = strncmp(buffer, text, len) == 0; |
| 109 | |
| 110 | kunit_kfree(test, buffer); |
| 111 | return ret; |
| 112 | } |
| 113 | |
Brendan Higgins | 6b229e5 | 2019-09-23 02:02:34 -0700 | [diff] [blame] | 114 | void kunit_binary_assert_format(const struct kunit_assert *assert, |
| 115 | struct string_stream *stream) |
| 116 | { |
Lucas Stankus | 2f9f21c | 2021-03-12 16:11:26 -0300 | [diff] [blame] | 117 | struct kunit_binary_assert *binary_assert; |
| 118 | |
| 119 | binary_assert = container_of(assert, struct kunit_binary_assert, |
| 120 | assert); |
Brendan Higgins | 6b229e5 | 2019-09-23 02:02:34 -0700 | [diff] [blame] | 121 | |
| 122 | kunit_base_assert_format(assert, stream); |
| 123 | string_stream_add(stream, |
Alan Maguire | c3bba69 | 2020-03-26 14:25:09 +0000 | [diff] [blame] | 124 | KUNIT_SUBTEST_INDENT "Expected %s %s %s, but\n", |
| 125 | binary_assert->left_text, |
| 126 | binary_assert->operation, |
| 127 | binary_assert->right_text); |
Daniel Latypov | 65af9b9 | 2021-02-05 14:14:09 -0800 | [diff] [blame] | 128 | if (!is_literal(stream->test, binary_assert->left_text, |
| 129 | binary_assert->left_value, stream->gfp)) |
| 130 | string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %lld\n", |
| 131 | binary_assert->left_text, |
| 132 | binary_assert->left_value); |
| 133 | if (!is_literal(stream->test, binary_assert->right_text, |
| 134 | binary_assert->right_value, stream->gfp)) |
| 135 | string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %lld", |
| 136 | binary_assert->right_text, |
| 137 | binary_assert->right_value); |
Brendan Higgins | 6b229e5 | 2019-09-23 02:02:34 -0700 | [diff] [blame] | 138 | kunit_assert_print_msg(assert, stream); |
| 139 | } |
Alan Maguire | c475c77 | 2020-01-06 22:28:20 +0000 | [diff] [blame] | 140 | EXPORT_SYMBOL_GPL(kunit_binary_assert_format); |
Brendan Higgins | 6b229e5 | 2019-09-23 02:02:34 -0700 | [diff] [blame] | 141 | |
| 142 | void kunit_binary_ptr_assert_format(const struct kunit_assert *assert, |
| 143 | struct string_stream *stream) |
| 144 | { |
Lucas Stankus | 2f9f21c | 2021-03-12 16:11:26 -0300 | [diff] [blame] | 145 | struct kunit_binary_ptr_assert *binary_assert; |
| 146 | |
| 147 | binary_assert = container_of(assert, struct kunit_binary_ptr_assert, |
| 148 | assert); |
Brendan Higgins | 6b229e5 | 2019-09-23 02:02:34 -0700 | [diff] [blame] | 149 | |
| 150 | kunit_base_assert_format(assert, stream); |
| 151 | string_stream_add(stream, |
Alan Maguire | c3bba69 | 2020-03-26 14:25:09 +0000 | [diff] [blame] | 152 | KUNIT_SUBTEST_INDENT "Expected %s %s %s, but\n", |
| 153 | binary_assert->left_text, |
| 154 | binary_assert->operation, |
| 155 | binary_assert->right_text); |
| 156 | string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %px\n", |
| 157 | binary_assert->left_text, |
| 158 | binary_assert->left_value); |
| 159 | string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %px", |
| 160 | binary_assert->right_text, |
| 161 | binary_assert->right_value); |
Brendan Higgins | 6b229e5 | 2019-09-23 02:02:34 -0700 | [diff] [blame] | 162 | kunit_assert_print_msg(assert, stream); |
| 163 | } |
Alan Maguire | c475c77 | 2020-01-06 22:28:20 +0000 | [diff] [blame] | 164 | EXPORT_SYMBOL_GPL(kunit_binary_ptr_assert_format); |
Brendan Higgins | 6b229e5 | 2019-09-23 02:02:34 -0700 | [diff] [blame] | 165 | |
Daniel Latypov | acd9762 | 2021-04-02 12:33:57 -0700 | [diff] [blame] | 166 | /* Checks if KUNIT_EXPECT_STREQ() args were string literals. |
| 167 | * Note: `text` will have ""s where as `value` will not. |
| 168 | */ |
| 169 | static bool is_str_literal(const char *text, const char *value) |
| 170 | { |
| 171 | int len; |
| 172 | |
| 173 | len = strlen(text); |
| 174 | if (len < 2) |
| 175 | return false; |
| 176 | if (text[0] != '\"' || text[len - 1] != '\"') |
| 177 | return false; |
| 178 | |
| 179 | return strncmp(text + 1, value, len - 2) == 0; |
| 180 | } |
| 181 | |
Brendan Higgins | 6b229e5 | 2019-09-23 02:02:34 -0700 | [diff] [blame] | 182 | void kunit_binary_str_assert_format(const struct kunit_assert *assert, |
| 183 | struct string_stream *stream) |
| 184 | { |
Lucas Stankus | 2f9f21c | 2021-03-12 16:11:26 -0300 | [diff] [blame] | 185 | struct kunit_binary_str_assert *binary_assert; |
| 186 | |
| 187 | binary_assert = container_of(assert, struct kunit_binary_str_assert, |
| 188 | assert); |
Brendan Higgins | 6b229e5 | 2019-09-23 02:02:34 -0700 | [diff] [blame] | 189 | |
| 190 | kunit_base_assert_format(assert, stream); |
| 191 | string_stream_add(stream, |
Alan Maguire | c3bba69 | 2020-03-26 14:25:09 +0000 | [diff] [blame] | 192 | KUNIT_SUBTEST_INDENT "Expected %s %s %s, but\n", |
| 193 | binary_assert->left_text, |
| 194 | binary_assert->operation, |
| 195 | binary_assert->right_text); |
Daniel Latypov | acd9762 | 2021-04-02 12:33:57 -0700 | [diff] [blame] | 196 | if (!is_str_literal(binary_assert->left_text, binary_assert->left_value)) |
| 197 | string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == \"%s\"\n", |
| 198 | binary_assert->left_text, |
| 199 | binary_assert->left_value); |
| 200 | if (!is_str_literal(binary_assert->right_text, binary_assert->right_value)) |
| 201 | string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == \"%s\"", |
| 202 | binary_assert->right_text, |
| 203 | binary_assert->right_value); |
Brendan Higgins | 6b229e5 | 2019-09-23 02:02:34 -0700 | [diff] [blame] | 204 | kunit_assert_print_msg(assert, stream); |
| 205 | } |
Alan Maguire | c475c77 | 2020-01-06 22:28:20 +0000 | [diff] [blame] | 206 | EXPORT_SYMBOL_GPL(kunit_binary_str_assert_format); |