Line data Source code
1 : /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
2 : * Copyright (C) 2014 Frediano Ziglio
3 : *
4 : * This library is free software; you can redistribute it and/or
5 : * modify it under the terms of the GNU Library General Public
6 : * License as published by the Free Software Foundation; either
7 : * version 2 of the License, or (at your option) any later version.
8 : *
9 : * This library is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 : * Library General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU Library General Public
15 : * License along with this library; if not, write to the
16 : * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 : * Boston, MA 02111-1307, USA.
18 : */
19 :
20 : #include <freetds/utils/test_base.h>
21 :
22 : #include <stdio.h>
23 :
24 : #if HAVE_STDLIB_H
25 : #include <stdlib.h>
26 : #endif /* HAVE_STDLIB_H */
27 :
28 : #include <assert.h>
29 :
30 : #include <freetds/replacements.h>
31 :
32 : /* If the system supplies these, we're going to simulate the situation
33 : * where it doesn't so we're always testing our own versions.
34 : */
35 : #if HAVE_STRLCPY
36 : size_t tds_strlcpy(char *dest, const char *src, size_t len);
37 : #include "../strlcpy.c"
38 : #endif
39 :
40 : #if HAVE_STRLCAT
41 : size_t tds_strlcat(char *dest, const char *src, size_t len);
42 : #include "../strlcat.c"
43 : #endif
44 :
45 10 : TEST_MAIN()
46 : {
47 10 : char *buf = (char *) malloc(10);
48 :
49 : /* test tds_strlcpy */
50 10 : memset(buf, 0xff, 10);
51 10 : assert(tds_strlcpy(buf, "test", 10) == 4);
52 10 : assert(strcmp(buf, "test") == 0);
53 :
54 10 : memset(buf, 0xff, 10);
55 10 : assert(tds_strlcpy(buf, "TESTTEST", 10) == 8);
56 10 : assert(strcmp(buf, "TESTTEST") == 0);
57 :
58 10 : memset(buf, 0xff, 10);
59 10 : assert(tds_strlcpy(buf, "abcdefghi", 10) == 9);
60 10 : assert(strcmp(buf, "abcdefghi") == 0);
61 :
62 10 : memset(buf, 0xff, 10);
63 10 : assert(tds_strlcpy(buf, "1234567890", 10) == 10);
64 10 : assert(strcmp(buf, "123456789") == 0);
65 :
66 10 : memset(buf, 0xff, 10);
67 10 : assert(tds_strlcpy(buf, "xyzabc1234567890", 10) == 16);
68 10 : assert(strcmp(buf, "xyzabc123") == 0);
69 :
70 : /* test tds_strlcat */
71 10 : strcpy(buf, "xyz");
72 10 : assert(tds_strlcat(buf, "test", 10) == 7);
73 10 : assert(strcmp(buf, "xyztest") == 0);
74 :
75 10 : strcpy(buf, "xyz");
76 10 : assert(tds_strlcat(buf, "TESTAB", 10) == 9);
77 10 : assert(strcmp(buf, "xyzTESTAB") == 0);
78 :
79 10 : strcpy(buf, "xyz");
80 10 : assert(tds_strlcat(buf, "TESTabc", 10) == 10);
81 10 : assert(strcmp(buf, "xyzTESTab") == 0);
82 :
83 10 : strcpy(buf, "xyz");
84 10 : assert(tds_strlcat(buf, "123456789012345", 10) == 18);
85 10 : assert(strcmp(buf, "xyz123456") == 0);
86 :
87 10 : strcpy(buf, "123456789");
88 10 : assert(tds_strlcat(buf, "test", 4) == 13);
89 10 : assert(strcmp(buf, "123456789") == 0);
90 :
91 : /* test length == 0 */
92 10 : assert(tds_strlcpy(buf + 10, "test", 0) == 4);
93 :
94 10 : strcpy(buf, "123");
95 10 : assert(tds_strlcat(buf, "456", 0) == 6);
96 10 : assert(strcmp(buf, "123") == 0);
97 :
98 10 : free(buf);
99 10 : return 0;
100 : }
101 :
|