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