| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | #include <linux/module.h> |
|---|
| 2 | 3 | #include <linux/printk.h> |
|---|
| 3 | 4 | #include <linux/slab.h> |
|---|
| .. | .. |
|---|
| 111 | 112 | return 0; |
|---|
| 112 | 113 | } |
|---|
| 113 | 114 | |
|---|
| 115 | +static __init int strchr_selftest(void) |
|---|
| 116 | +{ |
|---|
| 117 | + const char *test_string = "abcdefghijkl"; |
|---|
| 118 | + const char *empty_string = ""; |
|---|
| 119 | + char *result; |
|---|
| 120 | + int i; |
|---|
| 121 | + |
|---|
| 122 | + for (i = 0; i < strlen(test_string) + 1; i++) { |
|---|
| 123 | + result = strchr(test_string, test_string[i]); |
|---|
| 124 | + if (result - test_string != i) |
|---|
| 125 | + return i + 'a'; |
|---|
| 126 | + } |
|---|
| 127 | + |
|---|
| 128 | + result = strchr(empty_string, '\0'); |
|---|
| 129 | + if (result != empty_string) |
|---|
| 130 | + return 0x101; |
|---|
| 131 | + |
|---|
| 132 | + result = strchr(empty_string, 'a'); |
|---|
| 133 | + if (result) |
|---|
| 134 | + return 0x102; |
|---|
| 135 | + |
|---|
| 136 | + result = strchr(test_string, 'z'); |
|---|
| 137 | + if (result) |
|---|
| 138 | + return 0x103; |
|---|
| 139 | + |
|---|
| 140 | + return 0; |
|---|
| 141 | +} |
|---|
| 142 | + |
|---|
| 143 | +static __init int strnchr_selftest(void) |
|---|
| 144 | +{ |
|---|
| 145 | + const char *test_string = "abcdefghijkl"; |
|---|
| 146 | + const char *empty_string = ""; |
|---|
| 147 | + char *result; |
|---|
| 148 | + int i, j; |
|---|
| 149 | + |
|---|
| 150 | + for (i = 0; i < strlen(test_string) + 1; i++) { |
|---|
| 151 | + for (j = 0; j < strlen(test_string) + 2; j++) { |
|---|
| 152 | + result = strnchr(test_string, j, test_string[i]); |
|---|
| 153 | + if (j <= i) { |
|---|
| 154 | + if (!result) |
|---|
| 155 | + continue; |
|---|
| 156 | + return ((i + 'a') << 8) | j; |
|---|
| 157 | + } |
|---|
| 158 | + if (result - test_string != i) |
|---|
| 159 | + return ((i + 'a') << 8) | j; |
|---|
| 160 | + } |
|---|
| 161 | + } |
|---|
| 162 | + |
|---|
| 163 | + result = strnchr(empty_string, 0, '\0'); |
|---|
| 164 | + if (result) |
|---|
| 165 | + return 0x10001; |
|---|
| 166 | + |
|---|
| 167 | + result = strnchr(empty_string, 1, '\0'); |
|---|
| 168 | + if (result != empty_string) |
|---|
| 169 | + return 0x10002; |
|---|
| 170 | + |
|---|
| 171 | + result = strnchr(empty_string, 1, 'a'); |
|---|
| 172 | + if (result) |
|---|
| 173 | + return 0x10003; |
|---|
| 174 | + |
|---|
| 175 | + result = strnchr(NULL, 0, '\0'); |
|---|
| 176 | + if (result) |
|---|
| 177 | + return 0x10004; |
|---|
| 178 | + |
|---|
| 179 | + return 0; |
|---|
| 180 | +} |
|---|
| 181 | + |
|---|
| 114 | 182 | static __init int string_selftest_init(void) |
|---|
| 115 | 183 | { |
|---|
| 116 | 184 | int test, subtest; |
|---|
| .. | .. |
|---|
| 130 | 198 | if (subtest) |
|---|
| 131 | 199 | goto fail; |
|---|
| 132 | 200 | |
|---|
| 201 | + test = 4; |
|---|
| 202 | + subtest = strchr_selftest(); |
|---|
| 203 | + if (subtest) |
|---|
| 204 | + goto fail; |
|---|
| 205 | + |
|---|
| 206 | + test = 5; |
|---|
| 207 | + subtest = strnchr_selftest(); |
|---|
| 208 | + if (subtest) |
|---|
| 209 | + goto fail; |
|---|
| 210 | + |
|---|
| 133 | 211 | pr_info("String selftests succeeded\n"); |
|---|
| 134 | 212 | return 0; |
|---|
| 135 | 213 | fail: |
|---|