1 // Copyright 2009 Google LLC
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are
5 // met:
6 //
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above
10 // copyright notice, this list of conditions and the following disclaimer
11 // in the documentation and/or other materials provided with the
12 // distribution.
13 // * Neither the name of Google LLC nor the names of its
14 // contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 #ifdef HAVE_CONFIG_H
30 #include <config.h> // Must come first
31 #endif
32
33 #include "breakpad_googletest_includes.h"
34 #include "common/linux/linux_libc_support.h"
35
36 namespace {
37 typedef testing::Test LinuxLibcSupportTest;
38 }
39
TEST(LinuxLibcSupportTest,strlen)40 TEST(LinuxLibcSupportTest, strlen) {
41 static const char* test_data[] = { "", "a", "aa", "aaa", "aabc", NULL };
42 for (unsigned i = 0; ; ++i) {
43 if (!test_data[i])
44 break;
45 ASSERT_EQ(strlen(test_data[i]), my_strlen(test_data[i]));
46 }
47 }
48
TEST(LinuxLibcSupportTest,strcmp)49 TEST(LinuxLibcSupportTest, strcmp) {
50 static const char* test_data[] = {
51 "", "",
52 "a", "",
53 "", "a",
54 "a", "b",
55 "a", "a",
56 "ab", "aa",
57 "abc", "ab",
58 "abc", "abc",
59 NULL,
60 };
61
62 for (unsigned i = 0; ; ++i) {
63 if (!test_data[i*2])
64 break;
65 int libc_result = strcmp(test_data[i*2], test_data[i*2 + 1]);
66 if (libc_result > 1)
67 libc_result = 1;
68 else if (libc_result < -1)
69 libc_result = -1;
70 ASSERT_EQ(my_strcmp(test_data[i*2], test_data[i*2 + 1]), libc_result);
71 }
72 }
73
TEST(LinuxLibcSupportTest,strtoui)74 TEST(LinuxLibcSupportTest, strtoui) {
75 int result;
76
77 ASSERT_FALSE(my_strtoui(&result, ""));
78 ASSERT_FALSE(my_strtoui(&result, "-1"));
79 ASSERT_FALSE(my_strtoui(&result, "-"));
80 ASSERT_FALSE(my_strtoui(&result, "a"));
81 ASSERT_FALSE(my_strtoui(&result, "23472893472938472987987398472398"));
82
83 ASSERT_TRUE(my_strtoui(&result, "0"));
84 ASSERT_EQ(result, 0);
85 ASSERT_TRUE(my_strtoui(&result, "1"));
86 ASSERT_EQ(result, 1);
87 ASSERT_TRUE(my_strtoui(&result, "12"));
88 ASSERT_EQ(result, 12);
89 ASSERT_TRUE(my_strtoui(&result, "123"));
90 ASSERT_EQ(result, 123);
91 ASSERT_TRUE(my_strtoui(&result, "0123"));
92 ASSERT_EQ(result, 123);
93 }
94
TEST(LinuxLibcSupportTest,uint_len)95 TEST(LinuxLibcSupportTest, uint_len) {
96 ASSERT_EQ(my_uint_len(0), 1U);
97 ASSERT_EQ(my_uint_len(2), 1U);
98 ASSERT_EQ(my_uint_len(5), 1U);
99 ASSERT_EQ(my_uint_len(9), 1U);
100 ASSERT_EQ(my_uint_len(10), 2U);
101 ASSERT_EQ(my_uint_len(99), 2U);
102 ASSERT_EQ(my_uint_len(100), 3U);
103 ASSERT_EQ(my_uint_len(101), 3U);
104 ASSERT_EQ(my_uint_len(1000), 4U);
105 // 0xFFFFFFFFFFFFFFFF
106 ASSERT_EQ(my_uint_len(18446744073709551615LLU), 20U);
107 }
108
TEST(LinuxLibcSupportTest,uitos)109 TEST(LinuxLibcSupportTest, uitos) {
110 char buf[32];
111
112 my_uitos(buf, 0, 1);
113 ASSERT_EQ(0, memcmp(buf, "0", 1));
114
115 my_uitos(buf, 1, 1);
116 ASSERT_EQ(0, memcmp(buf, "1", 1));
117
118 my_uitos(buf, 10, 2);
119 ASSERT_EQ(0, memcmp(buf, "10", 2));
120
121 my_uitos(buf, 63, 2);
122 ASSERT_EQ(0, memcmp(buf, "63", 2));
123
124 my_uitos(buf, 101, 3);
125 ASSERT_EQ(0, memcmp(buf, "101", 2));
126
127 // 0xFFFFFFFFFFFFFFFF
128 my_uitos(buf, 18446744073709551615LLU, 20);
129 ASSERT_EQ(0, memcmp(buf, "18446744073709551615", 20));
130 }
131
TEST(LinuxLibcSupportTest,strchr)132 TEST(LinuxLibcSupportTest, strchr) {
133 ASSERT_EQ(NULL, my_strchr("abc", 'd'));
134 ASSERT_EQ(NULL, my_strchr("", 'd'));
135 ASSERT_EQ(NULL, my_strchr("efghi", 'd'));
136
137 ASSERT_TRUE(my_strchr("a", 'a'));
138 ASSERT_TRUE(my_strchr("abc", 'a'));
139 ASSERT_TRUE(my_strchr("bcda", 'a'));
140 ASSERT_TRUE(my_strchr("sdfasdf", 'a'));
141
142 static const char abc3[] = "abcabcabc";
143 ASSERT_EQ(abc3, my_strchr(abc3, 'a'));
144 }
145
TEST(LinuxLibcSupportTest,strrchr)146 TEST(LinuxLibcSupportTest, strrchr) {
147 ASSERT_EQ(NULL, my_strrchr("abc", 'd'));
148 ASSERT_EQ(NULL, my_strrchr("", 'd'));
149 ASSERT_EQ(NULL, my_strrchr("efghi", 'd'));
150
151 ASSERT_TRUE(my_strrchr("a", 'a'));
152 ASSERT_TRUE(my_strrchr("abc", 'a'));
153 ASSERT_TRUE(my_strrchr("bcda", 'a'));
154 ASSERT_TRUE(my_strrchr("sdfasdf", 'a'));
155
156 static const char abc3[] = "abcabcabc";
157 ASSERT_EQ(abc3 + 6, my_strrchr(abc3, 'a'));
158 }
159
TEST(LinuxLibcSupportTest,memchr)160 TEST(LinuxLibcSupportTest, memchr) {
161 ASSERT_EQ(NULL, my_memchr("abc", 'd', 3));
162 ASSERT_EQ(NULL, my_memchr("abcd", 'd', 3));
163 ASSERT_EQ(NULL, my_memchr("a", 'a', 0));
164
165 static const char abc3[] = "abcabcabc";
166 ASSERT_EQ(abc3, my_memchr(abc3, 'a', 3));
167 ASSERT_EQ(abc3, my_memchr(abc3, 'a', 9));
168 ASSERT_EQ(abc3+1, my_memchr(abc3, 'b', 9));
169 ASSERT_EQ(abc3+2, my_memchr(abc3, 'c', 9));
170 }
171
TEST(LinuxLibcSupportTest,read_hex_ptr)172 TEST(LinuxLibcSupportTest, read_hex_ptr) {
173 uintptr_t result;
174 const char* last;
175
176 last = my_read_hex_ptr(&result, "");
177 ASSERT_EQ(result, 0U);
178 ASSERT_EQ(*last, 0);
179
180 last = my_read_hex_ptr(&result, "0");
181 ASSERT_EQ(result, 0U);
182 ASSERT_EQ(*last, 0);
183
184 last = my_read_hex_ptr(&result, "0123");
185 ASSERT_EQ(result, 0x123U);
186 ASSERT_EQ(*last, 0);
187
188 last = my_read_hex_ptr(&result, "0123a");
189 ASSERT_EQ(result, 0x123aU);
190 ASSERT_EQ(*last, 0);
191
192 last = my_read_hex_ptr(&result, "0123a-");
193 ASSERT_EQ(result, 0x123aU);
194 ASSERT_EQ(*last, '-');
195 }
196
TEST(LinuxLibcSupportTest,read_decimal_ptr)197 TEST(LinuxLibcSupportTest, read_decimal_ptr) {
198 uintptr_t result;
199 const char* last;
200
201 last = my_read_decimal_ptr(&result, "0");
202 ASSERT_EQ(result, 0U);
203 ASSERT_EQ(*last, 0);
204
205 last = my_read_decimal_ptr(&result, "0123");
206 ASSERT_EQ(result, 123U);
207 ASSERT_EQ(*last, 0);
208
209 last = my_read_decimal_ptr(&result, "1234");
210 ASSERT_EQ(result, 1234U);
211 ASSERT_EQ(*last, 0);
212
213 last = my_read_decimal_ptr(&result, "01234-");
214 ASSERT_EQ(result, 1234U);
215 ASSERT_EQ(*last, '-');
216 }
217