void unit_test_string(void) { static const char test_path[] = "/path/to/file"; const char *ret; char buf[MAXIMUM_PATH]; unsigned long num; print_file(STDERR, "testing string\n"); /* strchr */ ret = strchr(identity(test_path), '/'); EXPECT(ret == test_path, true); ret = strchr(identity(test_path), '\0'); EXPECT(ret != NULL, true); EXPECT(*ret, '\0'); /* strrchr */ ret = strrchr(identity(test_path), '/'); EXPECT(strcmp(ret, "/file"), 0); ret = strrchr(identity(test_path), '\0'); EXPECT(ret != NULL, true); EXPECT(*ret, '\0'); /* strncpy, strncat */ strncpy(buf, test_path, sizeof(buf)); EXPECT(is_region_memset_to_char((byte *) buf + strlen(test_path), sizeof(buf) - strlen(test_path), '\0'), true); strncat(buf, "/foo_wont_copy", 4); EXPECT(strcmp(buf, "/path/to/file/foo"), 0); /* strtoul */ num = strtoul(identity("-10"), NULL, 0); EXPECT((long)num, -10); /* negative */ num = strtoul(identity("0777"), NULL, 0); EXPECT(num, 0777); /* octal */ num = strtoul(identity("0xdeadBEEF"), NULL, 0); EXPECT(num, 0xdeadbeef); /* hex */ num = strtoul(identity("deadBEEF next"), (char **) &ret, 16); EXPECT(num, 0xdeadbeef); /* non-0x prefixed hex */ EXPECT(strcmp(ret, " next"), 0); /* end */ num = strtoul(identity("1001a"), NULL, 2); EXPECT(num, 9); /* binary */ num = strtoul(identity("1aZ"), NULL, 36); EXPECT(num, 1 * 36 * 36 + 10 * 36 + 35); /* weird base */ num = strtoul(identity("1aZ"), (char **) &ret, 37); EXPECT(num, ULONG_MAX); /* invalid base */ EXPECT(ret == NULL, true); /* memmove */ strncpy(buf, test_path, sizeof(buf)); memmove(buf + 4, buf, strlen(buf) + 1); strncpy(buf, "/foo", 4); EXPECT(strcmp(buf, "/foo/path/to/file"), 0); print_file(STDERR, "done testing string\n"); }
static void test_memset_offset_size(int val, int start_offs, int end_offs) { byte buf[512]; int i; int end = sizeof(buf) - start_offs - end_offs; /* Zero without memset. */ for (i = 0; i < sizeof(buf); i++) buf[i] = 0; memset(buf + start_offs, val, end); EXPECT(is_region_memset_to_char(buf + start_offs, end, val), 1); if (start_offs > 0) EXPECT(buf[start_offs-1], 0); if (end_offs > 0) EXPECT(buf[sizeof(buf)-end_offs], 0); }