blob: cc9cd2808a0c986f72ecd17b5994bd051596a04b [file] [log] [blame]
Adam Lesinskiefeb7af2017-08-02 14:57:43 -07001/*
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 Lesinski00451162017-10-03 07:44:08 -070017#include "io/FileStream.h"
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070018
Adam Lesinskia693c4a2017-11-09 11:29:39 -080019#include "android-base/file.h"
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070020#include "android-base/macros.h"
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070021
22#include "test/Test.h"
23
24using ::android::StringPiece;
25using ::testing::Eq;
26using ::testing::NotNull;
27using ::testing::StrEq;
28
29namespace aapt {
30namespace io {
31
32TEST(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 Lesinski00451162017-10-03 07:44:08 -070039 FileInputStream in(file.release(), 10u);
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070040 ASSERT_FALSE(in.HadError());
41 EXPECT_THAT(in.ByteCount(), Eq(0u));
42
Ryan Mitchell8f377342018-09-26 10:26:53 -070043 const void* buffer;
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070044 size_t size;
Ryan Mitchell8f377342018-09-26 10:26:53 -070045 ASSERT_TRUE(in.Next(&buffer, &size)) << in.GetError();
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070046 ASSERT_THAT(size, Eq(10u));
47 ASSERT_THAT(buffer, NotNull());
48 EXPECT_THAT(in.ByteCount(), Eq(10u));
Ryan Mitchell8f377342018-09-26 10:26:53 -070049 EXPECT_THAT(StringPiece(reinterpret_cast<const char*>(buffer), size), Eq("this is a "));
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070050
Ryan Mitchell8f377342018-09-26 10:26:53 -070051 ASSERT_TRUE(in.Next(&buffer, &size));
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070052 ASSERT_THAT(size, Eq(10u));
53 ASSERT_THAT(buffer, NotNull());
54 EXPECT_THAT(in.ByteCount(), Eq(20u));
Ryan Mitchell8f377342018-09-26 10:26:53 -070055 EXPECT_THAT(StringPiece(reinterpret_cast<const char*>(buffer), size), Eq("cool strin"));
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070056
57 in.BackUp(5u);
58 EXPECT_THAT(in.ByteCount(), Eq(15u));
59
Ryan Mitchell8f377342018-09-26 10:26:53 -070060 ASSERT_TRUE(in.Next(&buffer, &size));
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070061 ASSERT_THAT(size, Eq(5u));
62 ASSERT_THAT(buffer, NotNull());
63 ASSERT_THAT(in.ByteCount(), Eq(20u));
Ryan Mitchell8f377342018-09-26 10:26:53 -070064 EXPECT_THAT(StringPiece(reinterpret_cast<const char*>(buffer), size), Eq("strin"));
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070065
66 // Backup 1 more than possible. Should clamp.
67 in.BackUp(11u);
68 EXPECT_THAT(in.ByteCount(), Eq(10u));
69
Ryan Mitchell8f377342018-09-26 10:26:53 -070070 ASSERT_TRUE(in.Next(&buffer, &size));
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070071 ASSERT_THAT(size, Eq(10u));
72 ASSERT_THAT(buffer, NotNull());
73 ASSERT_THAT(in.ByteCount(), Eq(20u));
Ryan Mitchell8f377342018-09-26 10:26:53 -070074 EXPECT_THAT(StringPiece(reinterpret_cast<const char*>(buffer), size), Eq("cool strin"));
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070075
Ryan Mitchell8f377342018-09-26 10:26:53 -070076 ASSERT_TRUE(in.Next(&buffer, &size));
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070077 ASSERT_THAT(size, Eq(1u));
78 ASSERT_THAT(buffer, NotNull());
79 ASSERT_THAT(in.ByteCount(), Eq(21u));
Ryan Mitchell8f377342018-09-26 10:26:53 -070080 EXPECT_THAT(StringPiece(reinterpret_cast<const char*>(buffer), size), Eq("g"));
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070081
Ryan Mitchell8f377342018-09-26 10:26:53 -070082 EXPECT_FALSE(in.Next(&buffer, &size));
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070083 EXPECT_FALSE(in.HadError());
84}
85
Adam Lesinski00451162017-10-03 07:44:08 -070086TEST(FileOutputStreamTest, NextAndBackup) {
87 const std::string input = "this is a cool string";
88
89 TemporaryFile file;
Adam Lesinski00451162017-10-03 07:44:08 -070090
Adam Lesinski93190b72017-11-03 15:20:17 -070091 FileOutputStream out(file.fd, 10u);
Adam Lesinski00451162017-10-03 07:44:08 -070092 ASSERT_FALSE(out.HadError());
93 EXPECT_THAT(out.ByteCount(), Eq(0u));
94
Ryan Mitchell8f377342018-09-26 10:26:53 -070095 void* buffer;
Adam Lesinski00451162017-10-03 07:44:08 -070096 size_t size;
Ryan Mitchell8f377342018-09-26 10:26:53 -070097 ASSERT_TRUE(out.Next(&buffer, &size));
Adam Lesinski00451162017-10-03 07:44:08 -070098 ASSERT_THAT(size, Eq(10u));
99 ASSERT_THAT(buffer, NotNull());
100 EXPECT_THAT(out.ByteCount(), Eq(10u));
Ryan Mitchell8f377342018-09-26 10:26:53 -0700101 memcpy(reinterpret_cast<char*>(buffer), input.c_str(), size);
Adam Lesinski00451162017-10-03 07:44:08 -0700102
Ryan Mitchell8f377342018-09-26 10:26:53 -0700103 ASSERT_TRUE(out.Next(&buffer, &size));
Adam Lesinski00451162017-10-03 07:44:08 -0700104 ASSERT_THAT(size, Eq(10u));
105 ASSERT_THAT(buffer, NotNull());
106 EXPECT_THAT(out.ByteCount(), Eq(20u));
Ryan Mitchell8f377342018-09-26 10:26:53 -0700107 memcpy(reinterpret_cast<char*>(buffer), input.c_str() + 10u, size);
Adam Lesinski00451162017-10-03 07:44:08 -0700108
Ryan Mitchell8f377342018-09-26 10:26:53 -0700109 ASSERT_TRUE(out.Next(&buffer, &size));
Adam Lesinski00451162017-10-03 07:44:08 -0700110 ASSERT_THAT(size, Eq(10u));
111 ASSERT_THAT(buffer, NotNull());
112 EXPECT_THAT(out.ByteCount(), Eq(30u));
Ryan Mitchell8f377342018-09-26 10:26:53 -0700113 reinterpret_cast<char*>(buffer)[0] = input[20u];
Adam Lesinski00451162017-10-03 07:44:08 -0700114 out.BackUp(size - 1);
115 EXPECT_THAT(out.ByteCount(), Eq(21u));
116
117 ASSERT_TRUE(out.Flush());
118
Adam Lesinski93190b72017-11-03 15:20:17 -0700119 lseek64(file.fd, 0, SEEK_SET);
Adam Lesinski00451162017-10-03 07:44:08 -0700120
121 std::string actual;
Adam Lesinski93190b72017-11-03 15:20:17 -0700122 ASSERT_TRUE(android::base::ReadFdToString(file.fd, &actual));
Adam Lesinski00451162017-10-03 07:44:08 -0700123 EXPECT_THAT(actual, StrEq(input));
124}
125
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700126} // namespace io
127} // namespace aapt