OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Crashpad Authors. All rights reserved. |
| 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at |
| 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. |
| 14 |
| 15 #include "util/file/string_file.h" |
| 16 |
| 17 #include <string.h> |
| 18 |
| 19 #include <algorithm> |
| 20 #include <limits> |
| 21 |
| 22 #include "gtest/gtest.h" |
| 23 |
| 24 namespace crashpad { |
| 25 namespace test { |
| 26 namespace { |
| 27 |
| 28 TEST(StringFile, EmptyFile) { |
| 29 StringFile string_file; |
| 30 EXPECT_TRUE(string_file.string().empty()); |
| 31 EXPECT_EQ(0, string_file.Seek(0, SEEK_CUR)); |
| 32 EXPECT_TRUE(string_file.Write("", 0)); |
| 33 EXPECT_TRUE(string_file.string().empty()); |
| 34 EXPECT_EQ(0, string_file.Seek(0, SEEK_CUR)); |
| 35 |
| 36 char c = '6'; |
| 37 EXPECT_EQ(0, string_file.Read(&c, 1)); |
| 38 EXPECT_EQ('6', c); |
| 39 EXPECT_EQ(0, string_file.Seek(0, SEEK_CUR)); |
| 40 |
| 41 EXPECT_TRUE(string_file.string().empty()); |
| 42 } |
| 43 |
| 44 TEST(StringFile, OneByteFile) { |
| 45 StringFile string_file; |
| 46 |
| 47 EXPECT_TRUE(string_file.Write("a", 1)); |
| 48 EXPECT_EQ(1u, string_file.string().size()); |
| 49 EXPECT_EQ("a", string_file.string()); |
| 50 EXPECT_EQ(1, string_file.Seek(0, SEEK_CUR)); |
| 51 EXPECT_EQ(0, string_file.Seek(0, SEEK_SET)); |
| 52 char c = '6'; |
| 53 EXPECT_EQ(1, string_file.Read(&c, 1)); |
| 54 EXPECT_EQ('a', c); |
| 55 EXPECT_EQ(1, string_file.Seek(0, SEEK_CUR)); |
| 56 EXPECT_EQ(0, string_file.Read(&c, 1)); |
| 57 EXPECT_EQ('a', c); |
| 58 EXPECT_EQ(1, string_file.Seek(0, SEEK_CUR)); |
| 59 EXPECT_EQ("a", string_file.string()); |
| 60 |
| 61 EXPECT_EQ(0, string_file.Seek(0, SEEK_SET)); |
| 62 EXPECT_TRUE(string_file.Write("b", 1)); |
| 63 EXPECT_EQ(1u, string_file.string().size()); |
| 64 EXPECT_EQ("b", string_file.string()); |
| 65 EXPECT_EQ(1, string_file.Seek(0, SEEK_CUR)); |
| 66 EXPECT_EQ(0, string_file.Seek(0, SEEK_SET)); |
| 67 EXPECT_EQ(1, string_file.Read(&c, 1)); |
| 68 EXPECT_EQ('b', c); |
| 69 EXPECT_EQ(1, string_file.Seek(0, SEEK_CUR)); |
| 70 EXPECT_EQ(0, string_file.Read(&c, 1)); |
| 71 EXPECT_EQ('b', c); |
| 72 EXPECT_EQ(1, string_file.Seek(0, SEEK_CUR)); |
| 73 EXPECT_EQ("b", string_file.string()); |
| 74 |
| 75 EXPECT_EQ(0, string_file.Seek(0, SEEK_SET)); |
| 76 EXPECT_TRUE(string_file.Write("\0", 1)); |
| 77 EXPECT_EQ(1u, string_file.string().size()); |
| 78 EXPECT_EQ('\0', string_file.string()[0]); |
| 79 EXPECT_EQ(1, string_file.Seek(0, SEEK_CUR)); |
| 80 EXPECT_EQ(1u, string_file.string().size()); |
| 81 EXPECT_EQ('\0', string_file.string()[0]); |
| 82 } |
| 83 |
| 84 TEST(StringFile, SetString) { |
| 85 char kString1[] = "Four score"; |
| 86 StringFile string_file; |
| 87 string_file.SetString(kString1); |
| 88 EXPECT_EQ(0, string_file.Seek(0, SEEK_SET)); |
| 89 char buf[5] = "****"; |
| 90 EXPECT_EQ(4, string_file.Read(buf, 4)); |
| 91 EXPECT_STREQ("Four", buf); |
| 92 EXPECT_EQ(4, string_file.Seek(0, SEEK_CUR)); |
| 93 EXPECT_EQ(static_cast<FileOffset>(strlen(kString1)), |
| 94 string_file.Seek(0, SEEK_END)); |
| 95 EXPECT_EQ(kString1, string_file.string()); |
| 96 |
| 97 char kString2[] = "and seven years ago"; |
| 98 EXPECT_EQ(4, string_file.Seek(4, SEEK_SET)); |
| 99 EXPECT_EQ(4, string_file.Seek(0, SEEK_CUR)); |
| 100 string_file.SetString(kString2); |
| 101 EXPECT_EQ(0, string_file.Seek(0, SEEK_CUR)); |
| 102 EXPECT_EQ(4, string_file.Read(buf, 4)); |
| 103 EXPECT_STREQ("and ", buf); |
| 104 EXPECT_EQ(static_cast<FileOffset>(strlen(kString2)), |
| 105 string_file.Seek(0, SEEK_END)); |
| 106 EXPECT_EQ(kString2, string_file.string()); |
| 107 |
| 108 char kString3[] = "our fathers"; |
| 109 EXPECT_EQ(4, string_file.Seek(4, SEEK_SET)); |
| 110 EXPECT_EQ(4, string_file.Seek(0, SEEK_CUR)); |
| 111 string_file.SetString(kString3); |
| 112 EXPECT_EQ(0, string_file.Seek(0, SEEK_CUR)); |
| 113 EXPECT_EQ(4, string_file.Read(buf, 4)); |
| 114 EXPECT_STREQ("our ", buf); |
| 115 EXPECT_EQ(static_cast<FileOffset>(strlen(kString3)), |
| 116 string_file.Seek(0, SEEK_END)); |
| 117 EXPECT_EQ(kString3, string_file.string()); |
| 118 } |
| 119 |
| 120 TEST(StringFile, Reset) { |
| 121 StringFile string_file; |
| 122 |
| 123 EXPECT_TRUE(string_file.Write("abc", 3)); |
| 124 EXPECT_EQ(3u, string_file.string().size()); |
| 125 EXPECT_EQ("abc", string_file.string()); |
| 126 EXPECT_EQ(3, string_file.Seek(0, SEEK_CUR)); |
| 127 char buf[10] = "*********"; |
| 128 EXPECT_EQ(0, string_file.Seek(0, SEEK_SET)); |
| 129 EXPECT_EQ(3, string_file.Read(&buf, 10)); |
| 130 EXPECT_STREQ("abc******", buf); |
| 131 EXPECT_EQ(3, string_file.Seek(0, SEEK_CUR)); |
| 132 EXPECT_FALSE(string_file.string().empty()); |
| 133 |
| 134 string_file.Reset(); |
| 135 EXPECT_TRUE(string_file.string().empty()); |
| 136 EXPECT_EQ(0, string_file.Seek(0, SEEK_CUR)); |
| 137 |
| 138 EXPECT_TRUE(string_file.Write("de", 2)); |
| 139 EXPECT_EQ(2u, string_file.string().size()); |
| 140 EXPECT_EQ("de", string_file.string()); |
| 141 EXPECT_EQ(2, string_file.Seek(0, SEEK_CUR)); |
| 142 EXPECT_EQ(0, string_file.Seek(0, SEEK_SET)); |
| 143 EXPECT_EQ(2, string_file.Read(&buf, 10)); |
| 144 EXPECT_STREQ("dec******", buf); |
| 145 EXPECT_EQ(2, string_file.Seek(0, SEEK_CUR)); |
| 146 EXPECT_FALSE(string_file.string().empty()); |
| 147 |
| 148 string_file.Reset(); |
| 149 EXPECT_TRUE(string_file.string().empty()); |
| 150 EXPECT_EQ(0, string_file.Seek(0, SEEK_CUR)); |
| 151 |
| 152 EXPECT_TRUE(string_file.Write("fghi", 4)); |
| 153 EXPECT_EQ(4u, string_file.string().size()); |
| 154 EXPECT_EQ("fghi", string_file.string()); |
| 155 EXPECT_EQ(4, string_file.Seek(0, SEEK_CUR)); |
| 156 EXPECT_EQ(0, string_file.Seek(0, SEEK_SET)); |
| 157 EXPECT_EQ(2, string_file.Read(&buf, 2)); |
| 158 EXPECT_STREQ("fgc******", buf); |
| 159 EXPECT_EQ(2, string_file.Seek(0, SEEK_CUR)); |
| 160 EXPECT_EQ(2, string_file.Read(&buf, 2)); |
| 161 EXPECT_STREQ("hic******", buf); |
| 162 EXPECT_EQ(4, string_file.Seek(0, SEEK_CUR)); |
| 163 EXPECT_FALSE(string_file.string().empty()); |
| 164 |
| 165 string_file.Reset(); |
| 166 EXPECT_TRUE(string_file.string().empty()); |
| 167 EXPECT_EQ(0, string_file.Seek(0, SEEK_CUR)); |
| 168 |
| 169 // Test resetting after a sparse write. |
| 170 EXPECT_EQ(1, string_file.Seek(1, SEEK_SET)); |
| 171 EXPECT_TRUE(string_file.Write("j", 1)); |
| 172 EXPECT_EQ(2u, string_file.string().size()); |
| 173 EXPECT_EQ(std::string("\0j", 2), string_file.string()); |
| 174 EXPECT_EQ(2, string_file.Seek(0, SEEK_CUR)); |
| 175 EXPECT_FALSE(string_file.string().empty()); |
| 176 |
| 177 string_file.Reset(); |
| 178 EXPECT_TRUE(string_file.string().empty()); |
| 179 EXPECT_EQ(0, string_file.Seek(0, SEEK_CUR)); |
| 180 } |
| 181 |
| 182 TEST(StringFile, WriteInvalid) { |
| 183 StringFile string_file; |
| 184 |
| 185 EXPECT_EQ(0, string_file.Seek(0, SEEK_CUR)); |
| 186 |
| 187 EXPECT_FALSE(string_file.Write( |
| 188 "", implicit_cast<size_t>(std::numeric_limits<ssize_t>::max()) + 1)); |
| 189 EXPECT_TRUE(string_file.string().empty()); |
| 190 EXPECT_EQ(0, string_file.Seek(0, SEEK_CUR)); |
| 191 |
| 192 EXPECT_TRUE(string_file.Write("a", 1)); |
| 193 EXPECT_FALSE(string_file.Write( |
| 194 "", implicit_cast<size_t>(std::numeric_limits<ssize_t>::max()))); |
| 195 EXPECT_EQ(1u, string_file.string().size()); |
| 196 EXPECT_EQ("a", string_file.string()); |
| 197 EXPECT_EQ(1, string_file.Seek(0, SEEK_CUR)); |
| 198 } |
| 199 |
| 200 TEST(StringFile, WriteIoVec) { |
| 201 StringFile string_file; |
| 202 |
| 203 std::vector<WritableIoVec> iovecs; |
| 204 WritableIoVec iov; |
| 205 iov.iov_base = ""; |
| 206 iov.iov_len = 0; |
| 207 iovecs.push_back(iov); |
| 208 EXPECT_TRUE(string_file.WriteIoVec(&iovecs)); |
| 209 EXPECT_TRUE(string_file.string().empty()); |
| 210 EXPECT_EQ(0, string_file.Seek(0, SEEK_CUR)); |
| 211 |
| 212 iovecs.clear(); |
| 213 iov.iov_base = "a"; |
| 214 iov.iov_len = 1; |
| 215 iovecs.push_back(iov); |
| 216 EXPECT_TRUE(string_file.WriteIoVec(&iovecs)); |
| 217 EXPECT_EQ(1u, string_file.string().size()); |
| 218 EXPECT_EQ("a", string_file.string()); |
| 219 EXPECT_EQ(1, string_file.Seek(0, SEEK_CUR)); |
| 220 |
| 221 iovecs.clear(); |
| 222 iovecs.push_back(iov); |
| 223 EXPECT_TRUE(string_file.WriteIoVec(&iovecs)); |
| 224 EXPECT_EQ(2u, string_file.string().size()); |
| 225 EXPECT_EQ("aa", string_file.string()); |
| 226 EXPECT_EQ(2, string_file.Seek(0, SEEK_CUR)); |
| 227 |
| 228 iovecs.clear(); |
| 229 iovecs.push_back(iov); |
| 230 iov.iov_base = "bc"; |
| 231 iov.iov_len = 2; |
| 232 iovecs.push_back(iov); |
| 233 EXPECT_TRUE(string_file.WriteIoVec(&iovecs)); |
| 234 EXPECT_EQ(5u, string_file.string().size()); |
| 235 EXPECT_EQ("aaabc", string_file.string()); |
| 236 EXPECT_EQ(5, string_file.Seek(0, SEEK_CUR)); |
| 237 |
| 238 EXPECT_TRUE(string_file.Write("def", 3)); |
| 239 EXPECT_EQ(8u, string_file.string().size()); |
| 240 EXPECT_EQ("aaabcdef", string_file.string()); |
| 241 EXPECT_EQ(8, string_file.Seek(0, SEEK_CUR)); |
| 242 |
| 243 iovecs.clear(); |
| 244 iov.iov_base = "ghij"; |
| 245 iov.iov_len = 4; |
| 246 iovecs.push_back(iov); |
| 247 iov.iov_base = "klmno"; |
| 248 iov.iov_len = 5; |
| 249 iovecs.push_back(iov); |
| 250 EXPECT_TRUE(string_file.WriteIoVec(&iovecs)); |
| 251 EXPECT_EQ(17u, string_file.string().size()); |
| 252 EXPECT_EQ("aaabcdefghijklmno", string_file.string()); |
| 253 EXPECT_EQ(17, string_file.Seek(0, SEEK_CUR)); |
| 254 |
| 255 string_file.Reset(); |
| 256 EXPECT_TRUE(string_file.string().empty()); |
| 257 EXPECT_EQ(0, string_file.Seek(0, SEEK_CUR)); |
| 258 |
| 259 iovecs.clear(); |
| 260 iov.iov_base = "abcd"; |
| 261 iov.iov_len = 4; |
| 262 iovecs.resize(16, iov); |
| 263 EXPECT_TRUE(string_file.WriteIoVec(&iovecs)); |
| 264 EXPECT_EQ(64u, string_file.string().size()); |
| 265 EXPECT_EQ("abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd", |
| 266 string_file.string()); |
| 267 EXPECT_EQ(64, string_file.Seek(0, SEEK_CUR)); |
| 268 } |
| 269 |
| 270 TEST(StringFile, WriteIoVecInvalid) { |
| 271 StringFile string_file; |
| 272 |
| 273 std::vector<WritableIoVec> iovecs; |
| 274 EXPECT_FALSE(string_file.WriteIoVec(&iovecs)); |
| 275 EXPECT_TRUE(string_file.string().empty()); |
| 276 EXPECT_EQ(0, string_file.Seek(0, SEEK_CUR)); |
| 277 |
| 278 WritableIoVec iov; |
| 279 EXPECT_EQ(1, string_file.Seek(1, SEEK_CUR)); |
| 280 iov.iov_base = "a"; |
| 281 iov.iov_len = std::numeric_limits<ssize_t>::max(); |
| 282 iovecs.push_back(iov); |
| 283 EXPECT_FALSE(string_file.WriteIoVec(&iovecs)); |
| 284 EXPECT_TRUE(string_file.string().empty()); |
| 285 EXPECT_EQ(1, string_file.Seek(0, SEEK_CUR)); |
| 286 |
| 287 iovecs.clear(); |
| 288 iov.iov_base = "a"; |
| 289 iov.iov_len = 1; |
| 290 iovecs.push_back(iov); |
| 291 iov.iov_len = std::numeric_limits<ssize_t>::max() - 1; |
| 292 iovecs.push_back(iov); |
| 293 EXPECT_FALSE(string_file.WriteIoVec(&iovecs)); |
| 294 EXPECT_TRUE(string_file.string().empty()); |
| 295 EXPECT_EQ(1, string_file.Seek(0, SEEK_CUR)); |
| 296 } |
| 297 |
| 298 TEST(StringFile, Seek) { |
| 299 StringFile string_file; |
| 300 |
| 301 EXPECT_TRUE(string_file.Write("abcd", 4)); |
| 302 EXPECT_EQ(4u, string_file.string().size()); |
| 303 EXPECT_EQ("abcd", string_file.string()); |
| 304 EXPECT_EQ(4, string_file.Seek(0, SEEK_CUR)); |
| 305 |
| 306 EXPECT_EQ(0, string_file.Seek(0, SEEK_SET)); |
| 307 EXPECT_TRUE(string_file.Write("efgh", 4)); |
| 308 EXPECT_EQ(4u, string_file.string().size()); |
| 309 EXPECT_EQ("efgh", string_file.string()); |
| 310 EXPECT_EQ(4, string_file.Seek(0, SEEK_CUR)); |
| 311 |
| 312 EXPECT_EQ(0, string_file.Seek(0, SEEK_SET)); |
| 313 EXPECT_TRUE(string_file.Write("ijk", 3)); |
| 314 EXPECT_EQ(4u, string_file.string().size()); |
| 315 EXPECT_EQ("ijkh", string_file.string()); |
| 316 EXPECT_EQ(3, string_file.Seek(0, SEEK_CUR)); |
| 317 |
| 318 EXPECT_EQ(0, string_file.Seek(0, SEEK_SET)); |
| 319 EXPECT_TRUE(string_file.Write("lmnop", 5)); |
| 320 EXPECT_EQ(5u, string_file.string().size()); |
| 321 EXPECT_EQ("lmnop", string_file.string()); |
| 322 EXPECT_EQ(5, string_file.Seek(0, SEEK_CUR)); |
| 323 |
| 324 EXPECT_EQ(1, string_file.Seek(1, SEEK_SET)); |
| 325 EXPECT_TRUE(string_file.Write("q", 1)); |
| 326 EXPECT_EQ(5u, string_file.string().size()); |
| 327 EXPECT_EQ("lqnop", string_file.string()); |
| 328 EXPECT_EQ(2, string_file.Seek(0, SEEK_CUR)); |
| 329 |
| 330 EXPECT_EQ(1, string_file.Seek(-1, SEEK_CUR)); |
| 331 EXPECT_TRUE(string_file.Write("r", 1)); |
| 332 EXPECT_EQ(5u, string_file.string().size()); |
| 333 EXPECT_EQ("lrnop", string_file.string()); |
| 334 EXPECT_EQ(2, string_file.Seek(0, SEEK_CUR)); |
| 335 |
| 336 EXPECT_TRUE(string_file.Write("s", 1)); |
| 337 EXPECT_EQ(5u, string_file.string().size()); |
| 338 EXPECT_EQ("lrsop", string_file.string()); |
| 339 EXPECT_EQ(3, string_file.Seek(0, SEEK_CUR)); |
| 340 |
| 341 EXPECT_EQ(2, string_file.Seek(-1, SEEK_CUR)); |
| 342 EXPECT_TRUE(string_file.Write("t", 1)); |
| 343 EXPECT_EQ(5u, string_file.string().size()); |
| 344 EXPECT_EQ("lrtop", string_file.string()); |
| 345 EXPECT_EQ(3, string_file.Seek(0, SEEK_CUR)); |
| 346 |
| 347 EXPECT_EQ(4, string_file.Seek(-1, SEEK_END)); |
| 348 EXPECT_TRUE(string_file.Write("u", 1)); |
| 349 EXPECT_EQ(5u, string_file.string().size()); |
| 350 EXPECT_EQ("lrtou", string_file.string()); |
| 351 EXPECT_EQ(5, string_file.Seek(0, SEEK_CUR)); |
| 352 |
| 353 EXPECT_EQ(0, string_file.Seek(-5, SEEK_END)); |
| 354 EXPECT_TRUE(string_file.Write("v", 1)); |
| 355 EXPECT_EQ(5u, string_file.string().size()); |
| 356 EXPECT_EQ("vrtou", string_file.string()); |
| 357 EXPECT_EQ(1, string_file.Seek(0, SEEK_CUR)); |
| 358 |
| 359 EXPECT_EQ(5, string_file.Seek(0, SEEK_END)); |
| 360 EXPECT_TRUE(string_file.Write("w", 1)); |
| 361 EXPECT_EQ(6u, string_file.string().size()); |
| 362 EXPECT_EQ("vrtouw", string_file.string()); |
| 363 EXPECT_EQ(6, string_file.Seek(0, SEEK_CUR)); |
| 364 |
| 365 EXPECT_EQ(8, string_file.Seek(2, SEEK_END)); |
| 366 EXPECT_EQ(6u, string_file.string().size()); |
| 367 EXPECT_EQ("vrtouw", string_file.string()); |
| 368 |
| 369 EXPECT_EQ(6, string_file.Seek(0, SEEK_END)); |
| 370 EXPECT_TRUE(string_file.Write("x", 1)); |
| 371 EXPECT_EQ(7u, string_file.string().size()); |
| 372 EXPECT_EQ("vrtouwx", string_file.string()); |
| 373 EXPECT_EQ(7, string_file.Seek(0, SEEK_CUR)); |
| 374 } |
| 375 |
| 376 TEST(StringFile, SeekSparse) { |
| 377 StringFile string_file; |
| 378 |
| 379 EXPECT_EQ(3, string_file.Seek(3, SEEK_SET)); |
| 380 EXPECT_TRUE(string_file.string().empty()); |
| 381 EXPECT_EQ(3, string_file.Seek(0, SEEK_CUR)); |
| 382 |
| 383 EXPECT_TRUE(string_file.Write("abc", 3)); |
| 384 EXPECT_EQ(6u, string_file.string().size()); |
| 385 EXPECT_EQ(std::string("\0\0\0abc", 6), string_file.string()); |
| 386 EXPECT_EQ(6, string_file.Seek(0, SEEK_CUR)); |
| 387 |
| 388 EXPECT_EQ(9, string_file.Seek(3, SEEK_END)); |
| 389 EXPECT_EQ(6u, string_file.string().size()); |
| 390 EXPECT_EQ(9, string_file.Seek(0, SEEK_CUR)); |
| 391 char c; |
| 392 EXPECT_EQ(0, string_file.Read(&c, 1)); |
| 393 EXPECT_EQ(9, string_file.Seek(0, SEEK_CUR)); |
| 394 EXPECT_EQ(6u, string_file.string().size()); |
| 395 EXPECT_TRUE(string_file.Write("def", 3)); |
| 396 EXPECT_EQ(12u, string_file.string().size()); |
| 397 EXPECT_EQ(std::string("\0\0\0abc\0\0\0def", 12), string_file.string()); |
| 398 EXPECT_EQ(12, string_file.Seek(0, SEEK_CUR)); |
| 399 |
| 400 EXPECT_EQ(7, string_file.Seek(-5, SEEK_END)); |
| 401 EXPECT_EQ(12u, string_file.string().size()); |
| 402 EXPECT_EQ(7, string_file.Seek(0, SEEK_CUR)); |
| 403 EXPECT_TRUE(string_file.Write("g", 1)); |
| 404 EXPECT_EQ(12u, string_file.string().size()); |
| 405 EXPECT_EQ(std::string("\0\0\0abc\0g\0def", 12), string_file.string()); |
| 406 EXPECT_EQ(8, string_file.Seek(0, SEEK_CUR)); |
| 407 |
| 408 EXPECT_EQ(15, string_file.Seek(7, SEEK_CUR)); |
| 409 EXPECT_EQ(12u, string_file.string().size()); |
| 410 EXPECT_EQ(15, string_file.Seek(0, SEEK_CUR)); |
| 411 EXPECT_TRUE(string_file.Write("hij", 3)); |
| 412 EXPECT_EQ(18u, string_file.string().size()); |
| 413 EXPECT_EQ(std::string("\0\0\0abc\0g\0def\0\0\0hij", 18), |
| 414 string_file.string()); |
| 415 EXPECT_EQ(18, string_file.Seek(0, SEEK_CUR)); |
| 416 |
| 417 EXPECT_EQ(1, string_file.Seek(-17, SEEK_CUR)); |
| 418 EXPECT_EQ(18u, string_file.string().size()); |
| 419 EXPECT_EQ(1, string_file.Seek(0, SEEK_CUR)); |
| 420 EXPECT_TRUE(string_file.Write("k", 1)); |
| 421 EXPECT_EQ(18u, string_file.string().size()); |
| 422 EXPECT_EQ(std::string("\0k\0abc\0g\0def\0\0\0hij", 18), string_file.string()); |
| 423 EXPECT_EQ(2, string_file.Seek(0, SEEK_CUR)); |
| 424 |
| 425 EXPECT_TRUE(string_file.Write("l", 1)); |
| 426 EXPECT_TRUE(string_file.Write("mnop", 4)); |
| 427 EXPECT_EQ(18u, string_file.string().size()); |
| 428 EXPECT_EQ(std::string("\0klmnopg\0def\0\0\0hij", 18), string_file.string()); |
| 429 EXPECT_EQ(7, string_file.Seek(0, SEEK_CUR)); |
| 430 } |
| 431 |
| 432 TEST(StringFile, SeekInvalid) { |
| 433 StringFile string_file; |
| 434 |
| 435 EXPECT_EQ(0, string_file.Seek(0, SEEK_CUR)); |
| 436 EXPECT_EQ(1, string_file.Seek(1, SEEK_SET)); |
| 437 EXPECT_EQ(1, string_file.Seek(0, SEEK_CUR)); |
| 438 EXPECT_LT(string_file.Seek(-1, SEEK_SET), 0); |
| 439 EXPECT_EQ(1, string_file.Seek(0, SEEK_CUR)); |
| 440 EXPECT_LT(string_file.Seek(std::numeric_limits<ssize_t>::min(), SEEK_SET), 0); |
| 441 EXPECT_EQ(1, string_file.Seek(0, SEEK_CUR)); |
| 442 EXPECT_LT(string_file.Seek(std::numeric_limits<FileOffset>::min(), SEEK_SET), |
| 443 0); |
| 444 EXPECT_EQ(1, string_file.Seek(0, SEEK_CUR)); |
| 445 EXPECT_TRUE(string_file.string().empty()); |
| 446 |
| 447 static_assert(SEEK_SET != 3 && SEEK_CUR != 3 && SEEK_END != 3, |
| 448 "3 must be invalid for whence"); |
| 449 EXPECT_LT(string_file.Seek(0, 3), 0); |
| 450 |
| 451 string_file.Reset(); |
| 452 EXPECT_EQ(0, string_file.Seek(0, SEEK_CUR)); |
| 453 EXPECT_TRUE(string_file.string().empty()); |
| 454 |
| 455 const FileOffset kMaxOffset = static_cast<FileOffset>( |
| 456 std::min(implicit_cast<uint64_t>(std::numeric_limits<FileOffset>::max()), |
| 457 implicit_cast<uint64_t>(std::numeric_limits<size_t>::max()))); |
| 458 |
| 459 EXPECT_EQ(kMaxOffset, string_file.Seek(kMaxOffset, SEEK_SET)); |
| 460 EXPECT_EQ(kMaxOffset, string_file.Seek(0, SEEK_CUR)); |
| 461 EXPECT_LT(string_file.Seek(1, SEEK_CUR), 0); |
| 462 |
| 463 EXPECT_EQ(1, string_file.Seek(1, SEEK_SET)); |
| 464 EXPECT_EQ(1, string_file.Seek(0, SEEK_CUR)); |
| 465 EXPECT_LT(string_file.Seek(kMaxOffset, SEEK_CUR), 0); |
| 466 } |
| 467 |
| 468 } // namespace |
| 469 } // namespace test |
| 470 } // namespace crashpad |
OLD | NEW |