blob: 68c3cb1f4156539debfe6154951d21510949afd4 [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
19#include "android-base/macros.h"
20#include "android-base/test_utils.h"
21
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
43 const char* buffer;
44 size_t size;
45 ASSERT_TRUE(in.Next(reinterpret_cast<const void**>(&buffer), &size)) << in.GetError();
46 ASSERT_THAT(size, Eq(10u));
47 ASSERT_THAT(buffer, NotNull());
48 EXPECT_THAT(in.ByteCount(), Eq(10u));
49 EXPECT_THAT(StringPiece(buffer, size), Eq("this is a "));
50
51 ASSERT_TRUE(in.Next(reinterpret_cast<const void**>(&buffer), &size));
52 ASSERT_THAT(size, Eq(10u));
53 ASSERT_THAT(buffer, NotNull());
54 EXPECT_THAT(in.ByteCount(), Eq(20u));
55 EXPECT_THAT(StringPiece(buffer, size), Eq("cool strin"));
56
57 in.BackUp(5u);
58 EXPECT_THAT(in.ByteCount(), Eq(15u));
59
60 ASSERT_TRUE(in.Next(reinterpret_cast<const void**>(&buffer), &size));
61 ASSERT_THAT(size, Eq(5u));
62 ASSERT_THAT(buffer, NotNull());
63 ASSERT_THAT(in.ByteCount(), Eq(20u));
64 EXPECT_THAT(StringPiece(buffer, size), Eq("strin"));
65
66 // Backup 1 more than possible. Should clamp.
67 in.BackUp(11u);
68 EXPECT_THAT(in.ByteCount(), Eq(10u));
69
70 ASSERT_TRUE(in.Next(reinterpret_cast<const void**>(&buffer), &size));
71 ASSERT_THAT(size, Eq(10u));
72 ASSERT_THAT(buffer, NotNull());
73 ASSERT_THAT(in.ByteCount(), Eq(20u));
74 EXPECT_THAT(StringPiece(buffer, size), Eq("cool strin"));
75
76 ASSERT_TRUE(in.Next(reinterpret_cast<const void**>(&buffer), &size));
77 ASSERT_THAT(size, Eq(1u));
78 ASSERT_THAT(buffer, NotNull());
79 ASSERT_THAT(in.ByteCount(), Eq(21u));
80 EXPECT_THAT(StringPiece(buffer, size), Eq("g"));
81
82 EXPECT_FALSE(in.Next(reinterpret_cast<const void**>(&buffer), &size));
83 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;
90 int fd = file.release();
91
92 // FileOutputStream takes ownership.
93 FileOutputStream out(fd, 10u);
94 ASSERT_FALSE(out.HadError());
95 EXPECT_THAT(out.ByteCount(), Eq(0u));
96
97 char* buffer;
98 size_t size;
99 ASSERT_TRUE(out.Next(reinterpret_cast<void**>(&buffer), &size));
100 ASSERT_THAT(size, Eq(10u));
101 ASSERT_THAT(buffer, NotNull());
102 EXPECT_THAT(out.ByteCount(), Eq(10u));
103 memcpy(buffer, input.c_str(), size);
104
105 ASSERT_TRUE(out.Next(reinterpret_cast<void**>(&buffer), &size));
106 ASSERT_THAT(size, Eq(10u));
107 ASSERT_THAT(buffer, NotNull());
108 EXPECT_THAT(out.ByteCount(), Eq(20u));
109 memcpy(buffer, input.c_str() + 10u, size);
110
111 ASSERT_TRUE(out.Next(reinterpret_cast<void**>(&buffer), &size));
112 ASSERT_THAT(size, Eq(10u));
113 ASSERT_THAT(buffer, NotNull());
114 EXPECT_THAT(out.ByteCount(), Eq(30u));
115 buffer[0] = input[20u];
116 out.BackUp(size - 1);
117 EXPECT_THAT(out.ByteCount(), Eq(21u));
118
119 ASSERT_TRUE(out.Flush());
120
121 lseek64(fd, 0, SEEK_SET);
122
123 std::string actual;
124 ASSERT_TRUE(android::base::ReadFdToString(fd, &actual));
125 EXPECT_THAT(actual, StrEq(input));
126}
127
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700128} // namespace io
129} // namespace aapt