Heinrich Schuchardt | f77a635 | 2019-01-30 07:53:31 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (c) 2019 Heinrich Schuchardt <xypron.glpk@gmx.de> |
| 4 | * |
| 5 | * Unit tests for memory functions |
| 6 | * |
| 7 | * The architecture dependent implementations run through different lines of |
| 8 | * code depending on the alignment and length of memory regions copied or set. |
| 9 | * This has to be considered in testing. |
| 10 | */ |
| 11 | |
Heinrich Schuchardt | f77a635 | 2019-01-30 07:53:31 +0100 | [diff] [blame] | 12 | #include <command.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 13 | #include <log.h> |
Heinrich Schuchardt | f77a635 | 2019-01-30 07:53:31 +0100 | [diff] [blame] | 14 | #include <test/lib.h> |
| 15 | #include <test/test.h> |
| 16 | #include <test/ut.h> |
| 17 | |
| 18 | /* Xor mask used for marking memory regions */ |
| 19 | #define MASK 0xA5 |
| 20 | /* Number of different alignment values */ |
| 21 | #define SWEEP 16 |
| 22 | /* Allow for copying up to 32 bytes */ |
| 23 | #define BUFLEN (SWEEP + 33) |
| 24 | |
Simon Glass | 4b2a18b | 2021-09-25 07:03:06 -0600 | [diff] [blame] | 25 | #define TEST_STR "hello" |
| 26 | |
Heinrich Schuchardt | f77a635 | 2019-01-30 07:53:31 +0100 | [diff] [blame] | 27 | /** |
| 28 | * init_buffer() - initialize buffer |
| 29 | * |
| 30 | * The buffer is filled with incrementing values xor'ed with the mask. |
| 31 | * |
| 32 | * @buf: buffer |
| 33 | * @mask: xor mask |
| 34 | */ |
| 35 | static void init_buffer(u8 buf[], u8 mask) |
| 36 | { |
| 37 | int i; |
| 38 | |
| 39 | for (i = 0; i < BUFLEN; ++i) |
| 40 | buf[i] = i ^ mask; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * test_memset() - test result of memset() |
| 45 | * |
| 46 | * @uts: unit test state |
| 47 | * @buf: buffer |
| 48 | * @mask: value set by memset() |
| 49 | * @offset: relative start of region changed by memset() in buffer |
| 50 | * @len: length of region changed by memset() |
| 51 | * Return: 0 = success, 1 = failure |
| 52 | */ |
| 53 | static int test_memset(struct unit_test_state *uts, u8 buf[], u8 mask, |
| 54 | int offset, int len) |
| 55 | { |
| 56 | int i; |
| 57 | |
| 58 | for (i = 0; i < BUFLEN; ++i) { |
| 59 | if (i < offset || i >= offset + len) { |
| 60 | ut_asserteq(i, buf[i]); |
| 61 | } else { |
| 62 | ut_asserteq(mask, buf[i]); |
| 63 | } |
| 64 | } |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * lib_memset() - unit test for memset() |
| 70 | * |
| 71 | * Test memset() with varied alignment and length of the changed buffer. |
| 72 | * |
| 73 | * @uts: unit test state |
| 74 | * Return: 0 = success, 1 = failure |
| 75 | */ |
| 76 | static int lib_memset(struct unit_test_state *uts) |
| 77 | { |
| 78 | u8 buf[BUFLEN]; |
| 79 | int offset, len; |
| 80 | void *ptr; |
| 81 | |
| 82 | for (offset = 0; offset <= SWEEP; ++offset) { |
| 83 | for (len = 1; len < BUFLEN - SWEEP; ++len) { |
| 84 | init_buffer(buf, 0); |
| 85 | ptr = memset(buf + offset, MASK, len); |
| 86 | ut_asserteq_ptr(buf + offset, (u8 *)ptr); |
| 87 | if (test_memset(uts, buf, MASK, offset, len)) { |
| 88 | debug("%s: failure %d, %d\n", |
| 89 | __func__, offset, len); |
| 90 | return CMD_RET_FAILURE; |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | LIB_TEST(lib_memset, 0); |
| 98 | |
| 99 | /** |
| 100 | * test_memmove() - test result of memcpy() or memmove() |
| 101 | * |
| 102 | * @uts: unit test state |
| 103 | * @buf: buffer |
| 104 | * @mask: xor mask used to initialize source buffer |
| 105 | * @offset1: relative start of copied region in source buffer |
| 106 | * @offset2: relative start of copied region in destination buffer |
| 107 | * @len: length of region changed by memset() |
| 108 | * Return: 0 = success, 1 = failure |
| 109 | */ |
| 110 | static int test_memmove(struct unit_test_state *uts, u8 buf[], u8 mask, |
| 111 | int offset1, int offset2, int len) |
| 112 | { |
| 113 | int i; |
| 114 | |
| 115 | for (i = 0; i < BUFLEN; ++i) { |
| 116 | if (i < offset2 || i >= offset2 + len) { |
| 117 | ut_asserteq(i, buf[i]); |
| 118 | } else { |
| 119 | ut_asserteq((i + offset1 - offset2) ^ mask, buf[i]); |
| 120 | } |
| 121 | } |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * lib_memcpy() - unit test for memcpy() |
| 127 | * |
| 128 | * Test memcpy() with varied alignment and length of the copied buffer. |
| 129 | * |
| 130 | * @uts: unit test state |
| 131 | * Return: 0 = success, 1 = failure |
| 132 | */ |
| 133 | static int lib_memcpy(struct unit_test_state *uts) |
| 134 | { |
| 135 | u8 buf1[BUFLEN]; |
| 136 | u8 buf2[BUFLEN]; |
| 137 | int offset1, offset2, len; |
| 138 | void *ptr; |
| 139 | |
| 140 | init_buffer(buf1, MASK); |
| 141 | |
| 142 | for (offset1 = 0; offset1 <= SWEEP; ++offset1) { |
| 143 | for (offset2 = 0; offset2 <= SWEEP; ++offset2) { |
| 144 | for (len = 1; len < BUFLEN - SWEEP; ++len) { |
| 145 | init_buffer(buf2, 0); |
| 146 | ptr = memcpy(buf2 + offset2, buf1 + offset1, |
| 147 | len); |
| 148 | ut_asserteq_ptr(buf2 + offset2, (u8 *)ptr); |
| 149 | if (test_memmove(uts, buf2, MASK, offset1, |
| 150 | offset2, len)) { |
| 151 | debug("%s: failure %d, %d, %d\n", |
| 152 | __func__, offset1, offset2, len); |
| 153 | return CMD_RET_FAILURE; |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | LIB_TEST(lib_memcpy, 0); |
| 162 | |
| 163 | /** |
| 164 | * lib_memmove() - unit test for memmove() |
| 165 | * |
| 166 | * Test memmove() with varied alignment and length of the copied buffer. |
| 167 | * |
| 168 | * @uts: unit test state |
| 169 | * Return: 0 = success, 1 = failure |
| 170 | */ |
| 171 | static int lib_memmove(struct unit_test_state *uts) |
| 172 | { |
| 173 | u8 buf[BUFLEN]; |
| 174 | int offset1, offset2, len; |
| 175 | void *ptr; |
| 176 | |
| 177 | for (offset1 = 0; offset1 <= SWEEP; ++offset1) { |
| 178 | for (offset2 = 0; offset2 <= SWEEP; ++offset2) { |
| 179 | for (len = 1; len < BUFLEN - SWEEP; ++len) { |
| 180 | init_buffer(buf, 0); |
| 181 | ptr = memmove(buf + offset2, buf + offset1, |
| 182 | len); |
| 183 | ut_asserteq_ptr(buf + offset2, (u8 *)ptr); |
| 184 | if (test_memmove(uts, buf, 0, offset1, offset2, |
| 185 | len)) { |
| 186 | debug("%s: failure %d, %d, %d\n", |
| 187 | __func__, offset1, offset2, len); |
| 188 | return CMD_RET_FAILURE; |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | LIB_TEST(lib_memmove, 0); |
Simon Glass | 4b2a18b | 2021-09-25 07:03:06 -0600 | [diff] [blame] | 197 | |
| 198 | /** lib_memdup() - unit test for memdup() */ |
| 199 | static int lib_memdup(struct unit_test_state *uts) |
| 200 | { |
| 201 | char buf[BUFLEN]; |
| 202 | size_t len; |
| 203 | char *p, *q; |
| 204 | |
| 205 | /* Zero size should do nothing */ |
| 206 | p = memdup(NULL, 0); |
| 207 | ut_assertnonnull(p); |
| 208 | free(p); |
| 209 | |
| 210 | p = memdup(buf, 0); |
| 211 | ut_assertnonnull(p); |
| 212 | free(p); |
| 213 | |
| 214 | strcpy(buf, TEST_STR); |
| 215 | len = sizeof(TEST_STR); |
| 216 | p = memdup(buf, len); |
| 217 | ut_asserteq_mem(p, buf, len); |
| 218 | |
| 219 | q = memdup(p, len); |
| 220 | ut_asserteq_mem(q, buf, len); |
| 221 | free(q); |
| 222 | free(p); |
| 223 | |
| 224 | return 0; |
| 225 | } |
| 226 | LIB_TEST(lib_memdup, 0); |