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" |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 21 | |
| 22 | #include "test/Test.h" |
| 23 | |
| 24 | using ::android::StringPiece; |
| 25 | using ::testing::Eq; |
| 26 | using ::testing::NotNull; |
| 27 | using ::testing::StrEq; |
| 28 | |
| 29 | namespace aapt { |
| 30 | namespace io { |
| 31 | |
| 32 | TEST(FileInputStreamTest, NextAndBackup) { |
| 33 | std::string input = "this is a cool string"; |
| 34 | TemporaryFile file; |
| 35 | ASSERT_THAT(TEMP_FAILURE_RETRY(write(file.fd, input.c_str(), input.size())), Eq(21)); |
| 36 | lseek64(file.fd, 0, SEEK_SET); |
| 37 | |
| 38 | // 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] | 39 | FileInputStream in(file.release(), 10u); |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 40 | ASSERT_FALSE(in.HadError()); |
| 41 | EXPECT_THAT(in.ByteCount(), Eq(0u)); |
| 42 | |
Ryan Mitchell | 8f37734 | 2018-09-26 10:26:53 -0700 | [diff] [blame] | 43 | const void* buffer; |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 44 | size_t size; |
Ryan Mitchell | 8f37734 | 2018-09-26 10:26:53 -0700 | [diff] [blame] | 45 | ASSERT_TRUE(in.Next(&buffer, &size)) << in.GetError(); |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 46 | ASSERT_THAT(size, Eq(10u)); |
| 47 | ASSERT_THAT(buffer, NotNull()); |
| 48 | EXPECT_THAT(in.ByteCount(), Eq(10u)); |
Ryan Mitchell | 8f37734 | 2018-09-26 10:26:53 -0700 | [diff] [blame] | 49 | 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] | 50 | |
Ryan Mitchell | 8f37734 | 2018-09-26 10:26:53 -0700 | [diff] [blame] | 51 | ASSERT_TRUE(in.Next(&buffer, &size)); |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 52 | ASSERT_THAT(size, Eq(10u)); |
| 53 | ASSERT_THAT(buffer, NotNull()); |
| 54 | EXPECT_THAT(in.ByteCount(), Eq(20u)); |
Ryan Mitchell | 8f37734 | 2018-09-26 10:26:53 -0700 | [diff] [blame] | 55 | EXPECT_THAT(StringPiece(reinterpret_cast<const char*>(buffer), size), Eq("cool strin")); |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 56 | |
| 57 | in.BackUp(5u); |
| 58 | EXPECT_THAT(in.ByteCount(), Eq(15u)); |
| 59 | |
Ryan Mitchell | 8f37734 | 2018-09-26 10:26:53 -0700 | [diff] [blame] | 60 | ASSERT_TRUE(in.Next(&buffer, &size)); |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 61 | ASSERT_THAT(size, Eq(5u)); |
| 62 | ASSERT_THAT(buffer, NotNull()); |
| 63 | ASSERT_THAT(in.ByteCount(), Eq(20u)); |
Ryan Mitchell | 8f37734 | 2018-09-26 10:26:53 -0700 | [diff] [blame] | 64 | EXPECT_THAT(StringPiece(reinterpret_cast<const char*>(buffer), size), Eq("strin")); |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 65 | |
| 66 | // Backup 1 more than possible. Should clamp. |
| 67 | in.BackUp(11u); |
| 68 | EXPECT_THAT(in.ByteCount(), Eq(10u)); |
| 69 | |
Ryan Mitchell | 8f37734 | 2018-09-26 10:26:53 -0700 | [diff] [blame] | 70 | ASSERT_TRUE(in.Next(&buffer, &size)); |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 71 | ASSERT_THAT(size, Eq(10u)); |
| 72 | ASSERT_THAT(buffer, NotNull()); |
| 73 | ASSERT_THAT(in.ByteCount(), Eq(20u)); |
Ryan Mitchell | 8f37734 | 2018-09-26 10:26:53 -0700 | [diff] [blame] | 74 | EXPECT_THAT(StringPiece(reinterpret_cast<const char*>(buffer), size), Eq("cool strin")); |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 75 | |
Ryan Mitchell | 8f37734 | 2018-09-26 10:26:53 -0700 | [diff] [blame] | 76 | ASSERT_TRUE(in.Next(&buffer, &size)); |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 77 | ASSERT_THAT(size, Eq(1u)); |
| 78 | ASSERT_THAT(buffer, NotNull()); |
| 79 | ASSERT_THAT(in.ByteCount(), Eq(21u)); |
Ryan Mitchell | 8f37734 | 2018-09-26 10:26:53 -0700 | [diff] [blame] | 80 | EXPECT_THAT(StringPiece(reinterpret_cast<const char*>(buffer), size), Eq("g")); |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 81 | |
Ryan Mitchell | 8f37734 | 2018-09-26 10:26:53 -0700 | [diff] [blame] | 82 | EXPECT_FALSE(in.Next(&buffer, &size)); |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 83 | EXPECT_FALSE(in.HadError()); |
| 84 | } |
| 85 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 86 | TEST(FileOutputStreamTest, NextAndBackup) { |
| 87 | const std::string input = "this is a cool string"; |
| 88 | |
| 89 | TemporaryFile file; |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 90 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 91 | FileOutputStream out(file.fd, 10u); |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 92 | ASSERT_FALSE(out.HadError()); |
| 93 | EXPECT_THAT(out.ByteCount(), Eq(0u)); |
| 94 | |
Ryan Mitchell | 8f37734 | 2018-09-26 10:26:53 -0700 | [diff] [blame] | 95 | void* buffer; |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 96 | size_t size; |
Ryan Mitchell | 8f37734 | 2018-09-26 10:26:53 -0700 | [diff] [blame] | 97 | ASSERT_TRUE(out.Next(&buffer, &size)); |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 98 | ASSERT_THAT(size, Eq(10u)); |
| 99 | ASSERT_THAT(buffer, NotNull()); |
| 100 | EXPECT_THAT(out.ByteCount(), Eq(10u)); |
Ryan Mitchell | 8f37734 | 2018-09-26 10:26:53 -0700 | [diff] [blame] | 101 | memcpy(reinterpret_cast<char*>(buffer), input.c_str(), size); |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 102 | |
Ryan Mitchell | 8f37734 | 2018-09-26 10:26:53 -0700 | [diff] [blame] | 103 | ASSERT_TRUE(out.Next(&buffer, &size)); |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 104 | ASSERT_THAT(size, Eq(10u)); |
| 105 | ASSERT_THAT(buffer, NotNull()); |
| 106 | EXPECT_THAT(out.ByteCount(), Eq(20u)); |
Ryan Mitchell | 8f37734 | 2018-09-26 10:26:53 -0700 | [diff] [blame] | 107 | memcpy(reinterpret_cast<char*>(buffer), input.c_str() + 10u, size); |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 108 | |
Ryan Mitchell | 8f37734 | 2018-09-26 10:26:53 -0700 | [diff] [blame] | 109 | ASSERT_TRUE(out.Next(&buffer, &size)); |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 110 | ASSERT_THAT(size, Eq(10u)); |
| 111 | ASSERT_THAT(buffer, NotNull()); |
| 112 | EXPECT_THAT(out.ByteCount(), Eq(30u)); |
Ryan Mitchell | 8f37734 | 2018-09-26 10:26:53 -0700 | [diff] [blame] | 113 | reinterpret_cast<char*>(buffer)[0] = input[20u]; |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 114 | out.BackUp(size - 1); |
| 115 | EXPECT_THAT(out.ByteCount(), Eq(21u)); |
| 116 | |
| 117 | ASSERT_TRUE(out.Flush()); |
| 118 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 119 | lseek64(file.fd, 0, SEEK_SET); |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 120 | |
| 121 | std::string actual; |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 122 | ASSERT_TRUE(android::base::ReadFdToString(file.fd, &actual)); |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 123 | EXPECT_THAT(actual, StrEq(input)); |
| 124 | } |
| 125 | |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 126 | } // namespace io |
| 127 | } // namespace aapt |