| 213 | } |
| 214 | |
| 215 | static int fmap_find_test(struct fmap *fmap) |
| 216 | { |
| 217 | uint8_t *buf; |
| 218 | size_t total_size, offset; |
| 219 | |
| 220 | status = fail; |
| 221 | |
| 222 | /* |
| 223 | * Note: In these tests, we'll use fmap_find() and control usage of |
| 224 | * lsearch and bsearch by using a power-of-2 total_size. For lsearch, |
| 225 | * use total_size - 1. For bsearch, use total_size. |
| 226 | */ |
| 227 | |
| 228 | total_size = 0x100000; |
| 229 | buf = calloc(total_size, 1); |
| 230 | |
| 231 | /* test if image length is zero */ |
| 232 | if (fmap_find(buf, 0) >= 0) { |
| 233 | printf("FAILURE: failed to abort on zero-length image\n"); |
| 234 | goto fmap_find_test_exit; |
| 235 | } |
| 236 | |
| 237 | /* test if no fmap exists */ |
| 238 | if (fmap_find(buf, total_size - 1) >= 0) { |
| 239 | printf("FAILURE: lsearch returned false positive\n"); |
| 240 | goto fmap_find_test_exit; |
| 241 | } |
| 242 | if (fmap_find(buf, total_size) >= 0) { |
| 243 | printf("FAILURE: bsearch returned false positive\n"); |
| 244 | goto fmap_find_test_exit; |
| 245 | } |
| 246 | |
| 247 | /* simple test case: fmap at (total_size / 2) + 1 */ |
| 248 | offset = (total_size / 2) + 1; |
| 249 | memcpy(&buf[offset], fmap, fmap_size(fmap)); |
| 250 | |
| 251 | if ((unsigned)fmap_find(buf, total_size - 1) != offset) { |
| 252 | printf("FAILURE: lsearch failed to find fmap\n"); |
| 253 | goto fmap_find_test_exit; |
| 254 | } |
| 255 | if ((unsigned)fmap_find(buf, total_size) != offset) { |
| 256 | printf("FAILURE: bsearch failed to find fmap\n"); |
| 257 | goto fmap_find_test_exit; |
| 258 | } |
| 259 | |
| 260 | /* test bsearch if offset is at 0 */ |
| 261 | offset = 0; |
| 262 | memset(buf, 0, total_size); |
| 263 | memcpy(buf, fmap, fmap_size(fmap)); |
| 264 | if ((unsigned)fmap_find(buf, total_size) != offset) { |
| 265 | printf("FAILURE: bsearch failed to find fmap at offset 0\n"); |
| 266 | goto fmap_find_test_exit; |
| 267 | } |
| 268 | |
| 269 | /* test overrun detection */ |
| 270 | memset(buf, 0, total_size); |
| 271 | memcpy(&buf[total_size - fmap_size(fmap) + 1], |
| 272 | fmap, |