Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* vim:set ts=2 sw=2 sts=2 et: */ |
michael@0 | 2 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 3 | * http://creativecommons.org/publicdomain/zero/1.0/ |
michael@0 | 4 | */ |
michael@0 | 5 | |
michael@0 | 6 | /* Test ReadSysFile() */ |
michael@0 | 7 | |
michael@0 | 8 | #include <sys/types.h> |
michael@0 | 9 | #include <sys/stat.h> |
michael@0 | 10 | |
michael@0 | 11 | #include <errno.h> |
michael@0 | 12 | #include <fcntl.h> |
michael@0 | 13 | #include <stdio.h> |
michael@0 | 14 | #include <string.h> |
michael@0 | 15 | |
michael@0 | 16 | #include "FileUtils.h" |
michael@0 | 17 | |
michael@0 | 18 | #include "gtest/gtest.h" |
michael@0 | 19 | |
michael@0 | 20 | namespace mozilla { |
michael@0 | 21 | |
michael@0 | 22 | #ifdef ReadSysFile_PRESENT |
michael@0 | 23 | |
michael@0 | 24 | /** |
michael@0 | 25 | * Create a file with the specified contents. |
michael@0 | 26 | */ |
michael@0 | 27 | static bool |
michael@0 | 28 | WriteFile( |
michael@0 | 29 | const char* aFilename, |
michael@0 | 30 | const void* aContents, |
michael@0 | 31 | size_t aContentsLen) |
michael@0 | 32 | { |
michael@0 | 33 | int fd; |
michael@0 | 34 | ssize_t ret; |
michael@0 | 35 | size_t offt; |
michael@0 | 36 | |
michael@0 | 37 | fd = MOZ_TEMP_FAILURE_RETRY( |
michael@0 | 38 | open(aFilename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR)); |
michael@0 | 39 | if (fd == -1) { |
michael@0 | 40 | fprintf(stderr, "open(): %s: %s\n", aFilename, strerror(errno)); |
michael@0 | 41 | return false; |
michael@0 | 42 | } |
michael@0 | 43 | |
michael@0 | 44 | offt = 0; |
michael@0 | 45 | do { |
michael@0 | 46 | ret = MOZ_TEMP_FAILURE_RETRY( |
michael@0 | 47 | write(fd, (char*)aContents + offt, aContentsLen - offt)); |
michael@0 | 48 | if (ret == -1) { |
michael@0 | 49 | fprintf(stderr, "write(): %s: %s\n", aFilename, strerror(errno)); |
michael@0 | 50 | close(fd); |
michael@0 | 51 | return false; |
michael@0 | 52 | } |
michael@0 | 53 | offt += ret; |
michael@0 | 54 | } while (offt < aContentsLen); |
michael@0 | 55 | |
michael@0 | 56 | ret = MOZ_TEMP_FAILURE_RETRY(close(fd)); |
michael@0 | 57 | if (ret == -1) { |
michael@0 | 58 | fprintf(stderr, "close(): %s: %s\n", aFilename, strerror(errno)); |
michael@0 | 59 | return false; |
michael@0 | 60 | } |
michael@0 | 61 | return true; |
michael@0 | 62 | } |
michael@0 | 63 | |
michael@0 | 64 | TEST(ReadSysFile, Nonexistent) { |
michael@0 | 65 | bool ret; |
michael@0 | 66 | int errno_saved; |
michael@0 | 67 | |
michael@0 | 68 | ret = ReadSysFile("/nonexistent", nullptr, 0); |
michael@0 | 69 | errno_saved = errno; |
michael@0 | 70 | |
michael@0 | 71 | ASSERT_FALSE(ret); |
michael@0 | 72 | ASSERT_EQ(errno_saved, ENOENT); |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | TEST(ReadSysFile, Main) { |
michael@0 | 76 | /* Use a different file name for each test since different tests could be |
michael@0 | 77 | executed concurrently. */ |
michael@0 | 78 | static const char* fn = "TestReadSysFileMain"; |
michael@0 | 79 | /* If we have a file which contains "abcd" and we read it with ReadSysFile(), |
michael@0 | 80 | providing a buffer of size 10 bytes, we would expect 5 bytes to be written |
michael@0 | 81 | to that buffer: "abcd\0". */ |
michael@0 | 82 | struct { |
michael@0 | 83 | /* input (file contents), e.g. "abcd" */ |
michael@0 | 84 | const char* input; |
michael@0 | 85 | /* pretended output buffer size, e.g. 10; the actual buffer is larger |
michael@0 | 86 | and we check if anything was written past the end of the allowed length */ |
michael@0 | 87 | size_t output_size; |
michael@0 | 88 | /* expected number of bytes written to the output buffer, including the |
michael@0 | 89 | terminating '\0', e.g. 5 */ |
michael@0 | 90 | size_t output_len; |
michael@0 | 91 | /* expected output buffer contents, e.g. "abcd\0", the first output_len |
michael@0 | 92 | bytes of the output buffer should match the first 'output_len' bytes from |
michael@0 | 93 | 'output', the rest of the output buffer should be untouched. */ |
michael@0 | 94 | const char* output; |
michael@0 | 95 | } tests[] = { |
michael@0 | 96 | /* No new lines */ |
michael@0 | 97 | {"", 0, 0, ""}, |
michael@0 | 98 | {"", 1, 1, "\0"}, /* \0 is redundant, but we write it for clarity */ |
michael@0 | 99 | {"", 9, 1, "\0"}, |
michael@0 | 100 | |
michael@0 | 101 | {"a", 0, 0, ""}, |
michael@0 | 102 | {"a", 1, 1, "\0"}, |
michael@0 | 103 | {"a", 2, 2, "a\0"}, |
michael@0 | 104 | {"a", 9, 2, "a\0"}, |
michael@0 | 105 | |
michael@0 | 106 | {"abcd", 0, 0, ""}, |
michael@0 | 107 | {"abcd", 1, 1, "\0"}, |
michael@0 | 108 | {"abcd", 2, 2, "a\0"}, |
michael@0 | 109 | {"abcd", 3, 3, "ab\0"}, |
michael@0 | 110 | {"abcd", 4, 4, "abc\0"}, |
michael@0 | 111 | {"abcd", 5, 5, "abcd\0"}, |
michael@0 | 112 | {"abcd", 9, 5, "abcd\0"}, |
michael@0 | 113 | |
michael@0 | 114 | /* A single trailing new line */ |
michael@0 | 115 | {"\n", 0, 0, ""}, |
michael@0 | 116 | {"\n", 1, 1, "\0"}, |
michael@0 | 117 | {"\n", 2, 1, "\0"}, |
michael@0 | 118 | {"\n", 9, 1, "\0"}, |
michael@0 | 119 | |
michael@0 | 120 | {"a\n", 0, 0, ""}, |
michael@0 | 121 | {"a\n", 1, 1, "\0"}, |
michael@0 | 122 | {"a\n", 2, 2, "a\0"}, |
michael@0 | 123 | {"a\n", 3, 2, "a\0"}, |
michael@0 | 124 | {"a\n", 9, 2, "a\0"}, |
michael@0 | 125 | |
michael@0 | 126 | {"abcd\n", 0, 0, ""}, |
michael@0 | 127 | {"abcd\n", 1, 1, "\0"}, |
michael@0 | 128 | {"abcd\n", 2, 2, "a\0"}, |
michael@0 | 129 | {"abcd\n", 3, 3, "ab\0"}, |
michael@0 | 130 | {"abcd\n", 4, 4, "abc\0"}, |
michael@0 | 131 | {"abcd\n", 5, 5, "abcd\0"}, |
michael@0 | 132 | {"abcd\n", 6, 5, "abcd\0"}, |
michael@0 | 133 | {"abcd\n", 9, 5, "abcd\0"}, |
michael@0 | 134 | |
michael@0 | 135 | /* Multiple trailing new lines */ |
michael@0 | 136 | {"\n\n", 0, 0, ""}, |
michael@0 | 137 | {"\n\n", 1, 1, "\0"}, |
michael@0 | 138 | {"\n\n", 2, 2, "\n\0"}, |
michael@0 | 139 | {"\n\n", 3, 2, "\n\0"}, |
michael@0 | 140 | {"\n\n", 9, 2, "\n\0"}, |
michael@0 | 141 | |
michael@0 | 142 | {"a\n\n", 0, 0, ""}, |
michael@0 | 143 | {"a\n\n", 1, 1, "\0"}, |
michael@0 | 144 | {"a\n\n", 2, 2, "a\0"}, |
michael@0 | 145 | {"a\n\n", 3, 3, "a\n\0"}, |
michael@0 | 146 | {"a\n\n", 4, 3, "a\n\0"}, |
michael@0 | 147 | {"a\n\n", 9, 3, "a\n\0"}, |
michael@0 | 148 | |
michael@0 | 149 | {"abcd\n\n", 0, 0, ""}, |
michael@0 | 150 | {"abcd\n\n", 1, 1, "\0"}, |
michael@0 | 151 | {"abcd\n\n", 2, 2, "a\0"}, |
michael@0 | 152 | {"abcd\n\n", 3, 3, "ab\0"}, |
michael@0 | 153 | {"abcd\n\n", 4, 4, "abc\0"}, |
michael@0 | 154 | {"abcd\n\n", 5, 5, "abcd\0"}, |
michael@0 | 155 | {"abcd\n\n", 6, 6, "abcd\n\0"}, |
michael@0 | 156 | {"abcd\n\n", 7, 6, "abcd\n\0"}, |
michael@0 | 157 | {"abcd\n\n", 9, 6, "abcd\n\0"}, |
michael@0 | 158 | |
michael@0 | 159 | /* New line in the middle */ |
michael@0 | 160 | {"ab\ncd", 9, 6, "ab\ncd\0"}, |
michael@0 | 161 | }; |
michael@0 | 162 | |
michael@0 | 163 | for (size_t i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) { |
michael@0 | 164 | ASSERT_TRUE(WriteFile(fn, tests[i].input, strlen(tests[i].input))); |
michael@0 | 165 | /* Leave the file to exist if some of the assertions fail. */ |
michael@0 | 166 | |
michael@0 | 167 | char buf[128]; |
michael@0 | 168 | static const char unmodified = 'X'; |
michael@0 | 169 | |
michael@0 | 170 | memset(buf, unmodified, sizeof(buf)); |
michael@0 | 171 | |
michael@0 | 172 | ASSERT_TRUE(ReadSysFile(fn, buf, tests[i].output_size)); |
michael@0 | 173 | |
michael@0 | 174 | if (tests[i].output_size == 0) { |
michael@0 | 175 | /* The buffer must be unmodified. We check only the first byte. */ |
michael@0 | 176 | ASSERT_EQ(unmodified, buf[0]); |
michael@0 | 177 | } else { |
michael@0 | 178 | ASSERT_EQ(tests[i].output_len, strlen(buf) + 1); |
michael@0 | 179 | ASSERT_STREQ(tests[i].output, buf); |
michael@0 | 180 | /* Check that the first byte after the trailing '\0' has not been |
michael@0 | 181 | modified. */ |
michael@0 | 182 | ASSERT_EQ(unmodified, buf[tests[i].output_len]); |
michael@0 | 183 | } |
michael@0 | 184 | } |
michael@0 | 185 | |
michael@0 | 186 | unlink(fn); |
michael@0 | 187 | } |
michael@0 | 188 | |
michael@0 | 189 | TEST(ReadSysFile, Int) { |
michael@0 | 190 | static const char* fn = "TestReadSysFileInt"; |
michael@0 | 191 | struct { |
michael@0 | 192 | /* input (file contents), e.g. "5" */ |
michael@0 | 193 | const char* input; |
michael@0 | 194 | /* expected return value, if false, then the output is not checked */ |
michael@0 | 195 | bool ret; |
michael@0 | 196 | /* expected result */ |
michael@0 | 197 | int output; |
michael@0 | 198 | } tests[] = { |
michael@0 | 199 | {"0", true, 0}, |
michael@0 | 200 | {"00", true, 0}, |
michael@0 | 201 | {"1", true, 1}, |
michael@0 | 202 | {"5", true, 5}, |
michael@0 | 203 | {"55", true, 55}, |
michael@0 | 204 | |
michael@0 | 205 | {" 123", true, 123}, |
michael@0 | 206 | {"123 ", true, 123}, |
michael@0 | 207 | {" 123 ", true, 123}, |
michael@0 | 208 | {"123\n", true, 123}, |
michael@0 | 209 | |
michael@0 | 210 | {"", false, 0}, |
michael@0 | 211 | {" ", false, 0}, |
michael@0 | 212 | {"a", false, 0}, |
michael@0 | 213 | |
michael@0 | 214 | {"-1", true, -1}, |
michael@0 | 215 | {" -456 ", true, -456}, |
michael@0 | 216 | {" -78.9 ", true, -78}, |
michael@0 | 217 | }; |
michael@0 | 218 | |
michael@0 | 219 | for (size_t i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) { |
michael@0 | 220 | ASSERT_TRUE(WriteFile(fn, tests[i].input, strlen(tests[i].input))); |
michael@0 | 221 | /* Leave the file to exist if some of the assertions fail. */ |
michael@0 | 222 | |
michael@0 | 223 | bool ret; |
michael@0 | 224 | int output = 424242; |
michael@0 | 225 | |
michael@0 | 226 | ret = ReadSysFile(fn, &output); |
michael@0 | 227 | |
michael@0 | 228 | ASSERT_EQ(tests[i].ret, ret); |
michael@0 | 229 | |
michael@0 | 230 | if (ret) { |
michael@0 | 231 | ASSERT_EQ(tests[i].output, output); |
michael@0 | 232 | } |
michael@0 | 233 | } |
michael@0 | 234 | |
michael@0 | 235 | unlink(fn); |
michael@0 | 236 | } |
michael@0 | 237 | |
michael@0 | 238 | TEST(ReadSysFile, Bool) { |
michael@0 | 239 | static const char* fn = "TestReadSysFileBool"; |
michael@0 | 240 | struct { |
michael@0 | 241 | /* input (file contents), e.g. "1" */ |
michael@0 | 242 | const char* input; |
michael@0 | 243 | /* expected return value */ |
michael@0 | 244 | bool ret; |
michael@0 | 245 | /* expected result */ |
michael@0 | 246 | bool output; |
michael@0 | 247 | } tests[] = { |
michael@0 | 248 | {"0", true, false}, |
michael@0 | 249 | {"00", true, false}, |
michael@0 | 250 | {"1", true, true}, |
michael@0 | 251 | {"5", true, true}, |
michael@0 | 252 | {"23", true, true}, |
michael@0 | 253 | {"-1", true, true}, |
michael@0 | 254 | |
michael@0 | 255 | {"", false, true /* unused */}, |
michael@0 | 256 | }; |
michael@0 | 257 | |
michael@0 | 258 | for (size_t i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) { |
michael@0 | 259 | ASSERT_TRUE(WriteFile(fn, tests[i].input, strlen(tests[i].input))); |
michael@0 | 260 | /* Leave the file to exist if some of the assertions fail. */ |
michael@0 | 261 | |
michael@0 | 262 | bool ret; |
michael@0 | 263 | bool output; |
michael@0 | 264 | |
michael@0 | 265 | ret = ReadSysFile(fn, &output); |
michael@0 | 266 | |
michael@0 | 267 | ASSERT_EQ(tests[i].ret, ret); |
michael@0 | 268 | |
michael@0 | 269 | if (ret) { |
michael@0 | 270 | ASSERT_EQ(tests[i].output, output); |
michael@0 | 271 | } |
michael@0 | 272 | } |
michael@0 | 273 | |
michael@0 | 274 | unlink(fn); |
michael@0 | 275 | } |
michael@0 | 276 | |
michael@0 | 277 | #endif /* ReadSysFile_PRESENT */ |
michael@0 | 278 | |
michael@0 | 279 | } |