]> git.saurik.com Git - apple/libc.git/blame_incremental - tests/strlcat.c
Libc-1081.1.3.tar.gz
[apple/libc.git] / tests / strlcat.c
... / ...
CommitLineData
1#include <bsdtests.h>
2#include <mach/mach_types.h>
3#include <sys/mman.h>
4#include <string.h>
5
6static const char* qbf = "The quick brown fox jumps over the lazy dog";
7static const char* lynx = "Lynx c.q. vos prikt bh: dag zwemjuf!";
8
9int
10main(void)
11{
12 test_start("strlcat");
13
14 void *ptr = mmap(NULL, PAGE_SIZE*2, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
15 test_ptr_not("mmap", ptr, MAP_FAILED);
16
17 test_errno("mprotect", mprotect(ptr+PAGE_SIZE, PAGE_SIZE, PROT_READ), 0);
18
19 off_t offset = strlen(qbf)+strlen(lynx)+1;
20 char *dst = (ptr+PAGE_SIZE)-offset;
21 strcpy(dst, qbf);
22
23 size_t res = strlcat(dst, lynx, offset);
24 test_long("strlcat", res, offset-1);
25 test_long("memcmp", memcmp(dst, qbf, strlen(qbf)), 0);
26 test_long("memcmp", memcmp(dst+strlen(qbf), lynx, strlen(lynx)), 0);
27 test_long("null-term", dst[offset], 0);
28
29 memset(ptr, '\0', PAGE_SIZE);
30
31 offset = strlen(qbf)+(strlen(lynx)/2)+1;
32 dst = (ptr+PAGE_SIZE)-offset;
33 strcpy(dst, qbf);
34
35 res = strlcat(dst, lynx, offset);
36 test_long("strlcat", res, strlen(qbf)+strlen(lynx));
37 test_long("memcmp", memcmp(dst, qbf, strlen(qbf)), 0);
38 test_long("memcmp", memcmp(dst+strlen(qbf), lynx, offset-strlen(qbf)-1), 0);
39 test_long("overrun", *(char*)(ptr+PAGE_SIZE), 0);
40 test_long("null-term", dst[offset], 0);
41
42 memset(ptr, '\0', PAGE_SIZE);
43
44 offset = strlen(qbf)-4;
45 dst = (ptr+PAGE_SIZE)-offset;
46 strncpy(dst, qbf, offset);
47
48 res = strlcat(dst, lynx, offset);
49 test_long("strlcat", res, offset+strlen(lynx));
50 test_long("memcmp", memcmp(dst, qbf, offset), 0);
51 test_long("overrun", *(char*)(ptr+PAGE_SIZE), 0);
52 test_long("null-term", dst[offset], 0);
53
54 test_stop();
55 return EXIT_SUCCESS;
56}