aboutsummaryrefslogtreecommitdiffstats
path: root/tests/src/test_strl.c
blob: a2caa226b0d75c3925ab39591d4d68e374c4ec19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include "libft.h"
#include "test_utils.h"
#include <bsd/string.h>

_S_CRASH (ft_strlcpy_null_dst, ft_strlcpy (NULL, "abc", 10))
_S_CRASH (libc_strlcpy_null_dst, strlcpy (NULL, "abc", 10))
_S_CRASH_BUF (ft_strlcpy_null_src, 10, _s_sink = ft_strlcpy (buf, NULL, 10))
_S_CRASH_BUF (libc_strlcpy_null_src, 10, _s_sink = strlcpy (buf, NULL, 10))
_S_CRASH (ft_strlcat_null_dst, ft_strlcat (NULL, "abc", 10))
_S_CRASH (libc_strlcat_null_dst, strlcat (NULL, "abc", 10))

/* strlcat NULL src needs an initialized buffer — keep manual */
static void
_s_crash_ft_strlcat_null_src (void)
{
  char buf[10] = "hi";
  _s_sink = ft_strlcat (buf, NULL, 10);
}
static void
_s_crash_libc_strlcat_null_src (void)
{
  char buf[10] = "hi";
  _s_sink = strlcat (buf, NULL, 10);
}

/* ======================================
 *  strlcpy
 * ====================================== */

static void
_s_test_strlcpy (void)
{
  char ft_dst[64];
  char libc_dst[64];
  size_t ft_ret;
  size_t libc_ret;
  int i;
  size_t size;
  char src[64];
  char label[64];

  _s_section ("ft_strlcpy");

  /* NULL */
  _s_check_both_crash ("strlcpy NULL dst", _s_crash_ft_strlcpy_null_dst,
                       _s_crash_libc_strlcpy_null_dst);
  _s_check_both_crash ("strlcpy NULL src", _s_crash_ft_strlcpy_null_src,
                       _s_crash_libc_strlcpy_null_src);

  /* basic copy */
  ft_ret = ft_strlcpy (ft_dst, "hello", 64);
  libc_ret = strlcpy (libc_dst, "hello", 64);
  _s_check ("basic copy ret", ft_ret == libc_ret);
  _s_check ("basic copy buf", strcmp (ft_dst, libc_dst) == 0);

  /* size=0 */
  memset (ft_dst, 'X', 64);
  memset (libc_dst, 'X', 64);
  ft_ret = ft_strlcpy (ft_dst, "hello", 0);
  libc_ret = strlcpy (libc_dst, "hello", 0);
  _s_check ("size=0 ret", ft_ret == libc_ret);
  _s_check ("size=0 buf unchanged", ft_dst[0] == 'X');

  /* truncation */
  ft_ret = ft_strlcpy (ft_dst, "hello world", 6);
  libc_ret = strlcpy (libc_dst, "hello world", 6);
  _s_check ("truncate ret", ft_ret == libc_ret);
  _s_check ("truncate buf", strcmp (ft_dst, libc_dst) == 0);
  _s_check ("truncate null term", ft_dst[5] == '\0');

  /* exact fit */
  ft_ret = ft_strlcpy (ft_dst, "abc", 4);
  libc_ret = strlcpy (libc_dst, "abc", 4);
  _s_check ("exact fit ret", ft_ret == libc_ret);
  _s_check ("exact fit buf", strcmp (ft_dst, libc_dst) == 0);

  /* empty src */
  ft_ret = ft_strlcpy (ft_dst, "", 64);
  libc_ret = strlcpy (libc_dst, "", 64);
  _s_check ("empty src ret", ft_ret == libc_ret);
  _s_check ("empty src buf", strcmp (ft_dst, libc_dst) == 0);

  /* size=1 */
  ft_ret = ft_strlcpy (ft_dst, "hello", 1);
  libc_ret = strlcpy (libc_dst, "hello", 1);
  _s_check ("size=1 ret", ft_ret == libc_ret);
  _s_check ("size=1 buf", ft_dst[0] == '\0');

  /* randomized */
  for (i = 0; i < _S_RAND_ITERS; i++)
    {
      int len = rand () % 60 + 1;
      int j;
      for (j = 0; j < len; j++)
        src[j] = 'A' + rand () % 26;
      src[len] = '\0';
      size = rand () % 64;
      memset (ft_dst, 'Z', 64);
      memset (libc_dst, 'Z', 64);
      ft_ret = ft_strlcpy (ft_dst, src, size);
      libc_ret = strlcpy (libc_dst, src, size);
      snprintf (label, sizeof (label), "random len=%d size=%zu", len, size);
      _s_check (label,
                ft_ret == libc_ret && memcmp (ft_dst, libc_dst, 64) == 0);
    }
}

/* ======================================
 *  strlcat
 * ====================================== */

static void
_s_test_strlcat (void)
{
  char ft_dst[64];
  char libc_dst[64];
  size_t ft_ret;
  size_t libc_ret;
  int i;
  size_t size;
  char label[64];

  _s_section ("ft_strlcat");

  /* NULL */
  _s_check_both_crash ("strlcat NULL dst", _s_crash_ft_strlcat_null_dst,
                       _s_crash_libc_strlcat_null_dst);
  _s_check_both_crash ("strlcat NULL src", _s_crash_ft_strlcat_null_src,
                       _s_crash_libc_strlcat_null_src);

  /* basic concat */
  strcpy (ft_dst, "hello");
  strcpy (libc_dst, "hello");
  ft_ret = ft_strlcat (ft_dst, " world", 64);
  libc_ret = strlcat (libc_dst, " world", 64);
  _s_check ("basic ret", ft_ret == libc_ret);
  _s_check ("basic buf", strcmp (ft_dst, libc_dst) == 0);

  /* size=0 */
  strcpy (ft_dst, "hi");
  strcpy (libc_dst, "hi");
  ft_ret = ft_strlcat (ft_dst, "abc", 0);
  libc_ret = strlcat (libc_dst, "abc", 0);
  _s_check ("size=0 ret", ft_ret == libc_ret);

  /* size == dstlen (no room to append) */
  strcpy (ft_dst, "hello");
  strcpy (libc_dst, "hello");
  ft_ret = ft_strlcat (ft_dst, "abc", 5);
  libc_ret = strlcat (libc_dst, "abc", 5);
  _s_check ("size == dstlen ret", ft_ret == libc_ret);
  _s_check ("size == dstlen buf", strcmp (ft_dst, libc_dst) == 0);

  /* size less than dst len */
  strcpy (ft_dst, "hello");
  strcpy (libc_dst, "hello");
  ft_ret = ft_strlcat (ft_dst, "abc", 3);
  libc_ret = strlcat (libc_dst, "abc", 3);
  _s_check ("size < dstlen ret", ft_ret == libc_ret);
  _s_check ("size < dstlen buf", strcmp (ft_dst, libc_dst) == 0);

  /* truncation */
  strcpy (ft_dst, "hello");
  strcpy (libc_dst, "hello");
  ft_ret = ft_strlcat (ft_dst, " world", 8);
  libc_ret = strlcat (libc_dst, " world", 8);
  _s_check ("truncate ret", ft_ret == libc_ret);
  _s_check ("truncate buf", strcmp (ft_dst, libc_dst) == 0);

  /* exact fit */
  strcpy (ft_dst, "abc");
  strcpy (libc_dst, "abc");
  ft_ret = ft_strlcat (ft_dst, "de", 6);
  libc_ret = strlcat (libc_dst, "de", 6);
  _s_check ("exact fit ret", ft_ret == libc_ret);
  _s_check ("exact fit buf", strcmp (ft_dst, libc_dst) == 0);

  /* empty src */
  strcpy (ft_dst, "hello");
  strcpy (libc_dst, "hello");
  ft_ret = ft_strlcat (ft_dst, "", 64);
  libc_ret = strlcat (libc_dst, "", 64);
  _s_check ("empty src ret", ft_ret == libc_ret);
  _s_check ("empty src buf", strcmp (ft_dst, libc_dst) == 0);

  /* empty dst */
  ft_dst[0] = '\0';
  libc_dst[0] = '\0';
  ft_ret = ft_strlcat (ft_dst, "hello", 64);
  libc_ret = strlcat (libc_dst, "hello", 64);
  _s_check ("empty dst ret", ft_ret == libc_ret);
  _s_check ("empty dst buf", strcmp (ft_dst, libc_dst) == 0);

  /* randomized */
  for (i = 0; i < _S_RAND_ITERS; i++)
    {
      int dlen = rand () % 30;
      int slen = rand () % 30 + 1;
      int j;
      for (j = 0; j < dlen; j++)
        ft_dst[j] = libc_dst[j] = 'A' + rand () % 26;
      ft_dst[dlen] = libc_dst[dlen] = '\0';
      char src[64];
      for (j = 0; j < slen; j++)
        src[j] = 'a' + rand () % 26;
      src[slen] = '\0';
      size = rand () % 64;
      memset (ft_dst + dlen + 1, 'Z', 63 - dlen);
      memset (libc_dst + dlen + 1, 'Z', 63 - dlen);
      ft_ret = ft_strlcat (ft_dst, src, size);
      libc_ret = strlcat (libc_dst, src, size);
      snprintf (label, sizeof (label), "random dlen=%d slen=%d size=%zu", dlen,
                slen, size);
      _s_check (label,
                ft_ret == libc_ret && memcmp (ft_dst, libc_dst, 64) == 0);
    }
}

int
main (void)
{
  srand (time (NULL));
  _s_test_strlcpy ();
  _s_test_strlcat ();
  _s_print_results ();
  return (_s_fail != 0);
}