]> eyrie.org Git - kerberos/krb5-strength.git/blob - tests/portable/strndup-t.c
Change CrackLib tests for system CrackLib
[kerberos/krb5-strength.git] / tests / portable / strndup-t.c
1 /*
2  * strndup test suite.
3  *
4  * The canonical version of this file is maintained in the rra-c-util package,
5  * which can be found at <https://www.eyrie.org/~eagle/software/rra-c-util/>.
6  *
7  * Written by Russ Allbery <eagle@eyrie.org>
8  *
9  * The authors hereby relinquish any claim to any copyright that they may have
10  * in this work, whether granted under contract or by operation of law or
11  * international treaty, and hereby commit to the public, at large, that they
12  * shall not, at any time in the future, seek to enforce any copyright in this
13  * work against any person or entity, or prevent any person or entity from
14  * copying, publishing, distributing or creating derivative works of this
15  * work.
16  */
17
18 #include <config.h>
19 #include <portable/system.h>
20
21 #include <errno.h>
22
23 #include <tests/tap/basic.h>
24
25 char *test_strndup(const char *, size_t);
26
27
28 int
29 main(void)
30 {
31     char buffer[3];
32     char *result = NULL;
33
34     plan(7);
35
36     result = test_strndup("foo", 8);
37     is_string("foo", result, "strndup longer than string");
38     free(result);
39     result = test_strndup("foo", 2);
40     is_string("fo", result, "strndup shorter than string");
41     free(result);
42     result = test_strndup("foo", 3);
43     is_string("foo", result, "strndup same size as string");
44     free(result);
45     result = test_strndup("foo", 0);
46     is_string("", result, "strndup of size 0");
47     free(result);
48     memcpy(buffer, "foo", 3);
49     result = test_strndup(buffer, 3);
50     is_string("foo", result, "strndup of non-nul-terminated string");
51     free(result);
52     errno = 0;
53     result = test_strndup(NULL, 0);
54     is_string(NULL, result, "strndup of NULL");
55     is_int(errno, EINVAL, "...and returns EINVAL");
56
57     return 0;
58 }