1 | /* |
---|
2 | * strlcat test suite. |
---|
3 | * |
---|
4 | * Written by Russ Allbery <rra@stanford.edu> |
---|
5 | * Copyright 2009 Board of Trustees, Leland Stanford Jr. University |
---|
6 | * Copyright (c) 2004, 2005, 2006 |
---|
7 | * by Internet Systems Consortium, Inc. ("ISC") |
---|
8 | * Copyright (c) 1991, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, |
---|
9 | * 2002, 2003 by The Internet Software Consortium and Rich Salz |
---|
10 | * |
---|
11 | * See LICENSE for licensing terms. |
---|
12 | */ |
---|
13 | |
---|
14 | #include <config.h> |
---|
15 | #include <portable/system.h> |
---|
16 | |
---|
17 | #include <tests/tap/basic.h> |
---|
18 | |
---|
19 | size_t test_strlcat(char *, const char *, size_t); |
---|
20 | |
---|
21 | |
---|
22 | int |
---|
23 | main(void) |
---|
24 | { |
---|
25 | char buffer[10] = ""; |
---|
26 | |
---|
27 | plan(27); |
---|
28 | |
---|
29 | is_int(3, test_strlcat(buffer, "foo", sizeof(buffer)), |
---|
30 | "strlcat into empty buffer"); |
---|
31 | is_string("foo", buffer, "...with right output"); |
---|
32 | is_int(7, test_strlcat(buffer, " bar", sizeof(buffer)), |
---|
33 | "...and append more"); |
---|
34 | is_string("foo bar", buffer, "...and output is still correct"); |
---|
35 | is_int(9, test_strlcat(buffer, "!!", sizeof(buffer)), |
---|
36 | "...and append to buffer limit"); |
---|
37 | is_string("foo bar!!", buffer, "...output is still correct"); |
---|
38 | is_int(10, test_strlcat(buffer, "!", sizeof(buffer)), |
---|
39 | "...append one more character"); |
---|
40 | is_string("foo bar!!", buffer, "...and output didn't change"); |
---|
41 | ok(buffer[9] == '\0', "...buffer still nul-terminated"); |
---|
42 | buffer[0] = '\0'; |
---|
43 | is_int(11, test_strlcat(buffer, "hello world", sizeof(buffer)), |
---|
44 | "append single long string"); |
---|
45 | is_string("hello wor", buffer, "...string truncates properly"); |
---|
46 | ok(buffer[9] == '\0', "...buffer still nul-terminated"); |
---|
47 | buffer[0] = '\0'; |
---|
48 | is_int(7, test_strlcat(buffer, "sausage", 5), "lie about buffer length"); |
---|
49 | is_string("saus", buffer, "...contents are correct"); |
---|
50 | is_int(14, test_strlcat(buffer, "bacon eggs", sizeof(buffer)), |
---|
51 | "...add more up to real size"); |
---|
52 | is_string("sausbacon", buffer, "...and result is correct"); |
---|
53 | |
---|
54 | /* Make sure that with a size of 0, the destination isn't changed. */ |
---|
55 | is_int(11, test_strlcat(buffer, "!!", 0), "no change with size of 0"); |
---|
56 | is_string("sausbacon", buffer, "...and content is the same"); |
---|
57 | |
---|
58 | /* Now play with empty strings. */ |
---|
59 | is_int(9, test_strlcat(buffer, "", 0), |
---|
60 | "correct count when appending empty string"); |
---|
61 | is_string("sausbacon", buffer, "...and contents are unchanged"); |
---|
62 | buffer[0] = '\0'; |
---|
63 | is_int(0, test_strlcat(buffer, "", sizeof(buffer)), |
---|
64 | "correct count when appending empty string to empty buffer"); |
---|
65 | is_string("", buffer, "...and buffer content is correct"); |
---|
66 | is_int(3, test_strlcat(buffer, "foo", 2), "append to length 2 buffer"); |
---|
67 | is_string("f", buffer, "...and got only a single character"); |
---|
68 | ok(buffer[1] == '\0', "...and buffer is still nul-terminated"); |
---|
69 | is_int(1, test_strlcat(buffer, "", sizeof(buffer)), |
---|
70 | "append an empty string"); |
---|
71 | ok(buffer[1] == '\0', "...and buffer is still nul-terminated"); |
---|
72 | |
---|
73 | return 0; |
---|
74 | } |
---|