Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 17 | #include "io/FileStream.h" |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 18 | |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 19 | #include "android-base/file.h" |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 20 | #include "android-base/macros.h" |
| 21 | #include "android-base/test_utils.h" |
| 22 | |
| 23 | #include "test/Test.h" |
| 24 | |
| 25 | using ::android::StringPiece; |
| 26 | using ::testing::Eq; |
| 27 | using ::testing::NotNull; |
| 28 | using ::testing::StrEq; |
| 29 | |
| 30 | namespace aapt { |
| 31 | namespace io { |
| 32 | |
| 33 | TEST(FileInputStreamTest, NextAndBackup) { |
| 34 | std::string input = "this is a cool string"; |
| 35 | TemporaryFile file; |
| 36 | ASSERT_THAT(TEMP_FAILURE_RETRY(write(file.fd, input.c_str(), input.size())), Eq(21)); |
| 37 | lseek64(file.fd, 0, SEEK_SET); |
| 38 | |
| 39 | // Use a small buffer size so that we can call Next() a few times. |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 40 | FileInputStream in(file.release(), 10u); |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 41 | ASSERT_FALSE(in.HadError()); |
| 42 | EXPECT_THAT(in.ByteCount(), Eq(0u)); |
| 43 | |
Ryan Mitchell | 8f37734 | 2018-09-26 10:26:53 -0700 | [diff] [blame] | 44 | const void* buffer; |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 45 | size_t size; |
Ryan Mitchell | 8f37734 | 2018-09-26 10:26:53 -0700 | [diff] [blame] | 46 | ASSERT_TRUE(in.Next(&buffer, &size)) << in.GetError(); |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 47 | ASSERT_THAT(size, Eq(10u)); |
| 48 | ASSERT_THAT(buffer, NotNull()); |
| 49 | EXPECT_THAT(in.ByteCount(), Eq(10u)); |
Ryan Mitchell | 8f37734 | 2018-09-26 10:26:53 -0700 | [diff] [blame] | 50 | EXPECT_THAT(StringPiece(reinterpret_cast<const char*>(buffer), size), Eq("this is a ")); |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 51 | |
Ryan Mitchell | 8f37734 | 2018-09-26 10:26:53 -0700 | [diff] [blame] | 52 | ASSERT_TRUE(in.Next(&buffer, &size)); |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 53 | ASSERT_THAT(size, Eq(10u)); |
| 54 | ASSERT_THAT(buffer, NotNull()); |
| 55 | EXPECT_THAT(in.ByteCount(), Eq(20u)); |
Ryan Mitchell | 8f37734 | 2018-09-26 10:26:53 -0700 | [diff] [blame] | 56 | EXPECT_THAT(StringPiece(reinterpret_cast<const char*>(buffer), size), Eq("cool strin")); |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 57 | |
| 58 | in.BackUp(5u); |
| 59 | EXPECT_THAT(in.ByteCount(), Eq(15u)); |
| 60 | |
Ryan Mitchell | 8f37734 | 2018-09-26 10:26:53 -0700 | [diff] [blame] | 61 | ASSERT_TRUE(in.Next(&buffer, &size)); |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 62 | ASSERT_THAT(size, Eq(5u)); |
| 63 | ASSERT_THAT(buffer, NotNull()); |
| 64 | ASSERT_THAT(in.ByteCount(), Eq(20u)); |
Ryan Mitchell | 8f37734 | 2018-09-26 10:26:53 -0700 | [diff] [blame] | 65 | EXPECT_THAT(StringPiece(reinterpret_cast<const char*>(buffer), size), Eq("strin")); |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 66 | |
| 67 | // Backup 1 more than possible. Should clamp. |
| 68 | in.BackUp(11u); |
| 69 | EXPECT_THAT(in.ByteCount(), Eq(10u)); |
| 70 | |
Ryan Mitchell | 8f37734 | 2018-09-26 10:26:53 -0700 | [diff] [blame] | 71 | ASSERT_TRUE(in.Next(&buffer, &size)); |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 72 | ASSERT_THAT(size, Eq(10u)); |
| 73 | ASSERT_THAT(buffer, NotNull()); |
| 74 | ASSERT_THAT(in.ByteCount(), Eq(20u)); |
Ryan Mitchell | 8f37734 | 2018-09-26 10:26:53 -0700 | [diff] [blame] | 75 | EXPECT_THAT(StringPiece(reinterpret_cast<const char*>(buffer), size), Eq("cool strin")); |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 76 | |
Ryan Mitchell | 8f37734 | 2018-09-26 10:26:53 -0700 | [diff] [blame] | 77 | ASSERT_TRUE(in.Next(&buffer, &size)); |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 78 | ASSERT_THAT(size, Eq(1u)); |
| 79 | ASSERT_THAT(buffer, NotNull()); |
| 80 | ASSERT_THAT(in.ByteCount(), Eq(21u)); |
Ryan Mitchell | 8f37734 | 2018-09-26 10:26:53 -0700 | [diff] [blame] | 81 | EXPECT_THAT(StringPiece(reinterpret_cast<const char*>(buffer), size), Eq("g")); |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 82 | |
Ryan Mitchell | 8f37734 | 2018-09-26 10:26:53 -0700 | [diff] [blame] | 83 | EXPECT_FALSE(in.Next(&buffer, &size)); |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 84 | EXPECT_FALSE(in.HadError()); |
| 85 | } |
| 86 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 87 | TEST(FileOutputStreamTest, NextAndBackup) { |
| 88 | const std::string input = "this is a cool string"; |
| 89 | |
| 90 | TemporaryFile file; |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 91 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 92 | FileOutputStream out(file.fd, 10u); |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 93 | ASSERT_FALSE(out.HadError()); |
| 94 | EXPECT_THAT(out.ByteCount(), Eq(0u)); |
| 95 | |
Ryan Mitchell | 8f37734 | 2018-09-26 10:26:53 -0700 | [diff] [blame] | 96 | void* buffer; |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 97 | size_t size; |
Ryan Mitchell | 8f37734 | 2018-09-26 10:26:53 -0700 | [diff] [blame] | 98 | ASSERT_TRUE(out.Next(&buffer, &size)); |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 99 | ASSERT_THAT(size, Eq(10u)); |
| 100 | ASSERT_THAT(buffer, NotNull()); |
| 101 | EXPECT_THAT(out.ByteCount(), Eq(10u)); |
Ryan Mitchell | 8f37734 | 2018-09-26 10:26:53 -0700 | [diff] [blame] | 102 | memcpy(reinterpret_cast<char*>(buffer), input.c_str(), size); |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 103 | |
Ryan Mitchell | 8f37734 | 2018-09-26 10:26:53 -0700 | [diff] [blame] | 104 | ASSERT_TRUE(out.Next(&buffer, &size)); |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 105 | ASSERT_THAT(size, Eq(10u)); |
| 106 | ASSERT_THAT(buffer, NotNull()); |
| 107 | EXPECT_THAT(out.ByteCount(), Eq(20u)); |
Ryan Mitchell | 8f37734 | 2018-09-26 10:26:53 -0700 | [diff] [blame] | 108 | memcpy(reinterpret_cast<char*>(buffer), input.c_str() + 10u, size); |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 109 | |
Ryan Mitchell | 8f37734 | 2018-09-26 10:26:53 -0700 | [diff] [blame] | 110 | ASSERT_TRUE(out.Next(&buffer, &size)); |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 111 | ASSERT_THAT(size, Eq(10u)); |
| 112 | ASSERT_THAT(buffer, NotNull()); |
| 113 | EXPECT_THAT(out.ByteCount(), Eq(30u)); |
Ryan Mitchell | 8f37734 | 2018-09-26 10:26:53 -0700 | [diff] [blame] | 114 | reinterpret_cast<char*>(buffer)[0] = input[20u]; |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 115 | out.BackUp(size - 1); |
| 116 | EXPECT_THAT(out.ByteCount(), Eq(21u)); |
| 117 | |
| 118 | ASSERT_TRUE(out.Flush()); |
| 119 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 120 | lseek64(file.fd, 0, SEEK_SET); |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 121 | |
| 122 | std::string actual; |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 123 | ASSERT_TRUE(android::base::ReadFdToString(file.fd, &actual)); |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 124 | EXPECT_THAT(actual, StrEq(input)); |
| 125 | } |
| 126 | |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 127 | } // namespace io |
| 128 | } // namespace aapt |