blob: 7872738320c3da1f5c9c4a00ebf4dc3b909b9071 [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"
21#include "android-base/test_utils.h"
22
23#include "test/Test.h"
24
25using ::android::StringPiece;
26using ::testing::Eq;
27using ::testing::NotNull;
28using ::testing::StrEq;
29
30namespace aapt {
31namespace io {
32
33TEST(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 Lesinski00451162017-10-03 07:44:08 -070040 FileInputStream in(file.release(), 10u);
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070041 ASSERT_FALSE(in.HadError());
42 EXPECT_THAT(in.ByteCount(), Eq(0u));
43
Ryan Mitchell8f377342018-09-26 10:26:53 -070044 const void* buffer;
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070045 size_t size;
Ryan Mitchell8f377342018-09-26 10:26:53 -070046 ASSERT_TRUE(in.Next(&buffer, &size)) << in.GetError();
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070047 ASSERT_THAT(size, Eq(10u));
48 ASSERT_THAT(buffer, NotNull());
49 EXPECT_THAT(in.ByteCount(), Eq(10u));
Ryan Mitchell8f377342018-09-26 10:26:53 -070050 EXPECT_THAT(StringPiece(reinterpret_cast<const char*>(buffer), size), Eq("this is a "));
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070051
Ryan Mitchell8f377342018-09-26 10:26:53 -070052 ASSERT_TRUE(in.Next(&buffer, &size));
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070053 ASSERT_THAT(size, Eq(10u));
54 ASSERT_THAT(buffer, NotNull());
55 EXPECT_THAT(in.ByteCount(), Eq(20u));
Ryan Mitchell8f377342018-09-26 10:26:53 -070056 EXPECT_THAT(StringPiece(reinterpret_cast<const char*>(buffer), size), Eq("cool strin"));
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070057
58 in.BackUp(5u);
59 EXPECT_THAT(in.ByteCount(), Eq(15u));
60
Ryan Mitchell8f377342018-09-26 10:26:53 -070061 ASSERT_TRUE(in.Next(&buffer, &size));
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070062 ASSERT_THAT(size, Eq(5u));
63 ASSERT_THAT(buffer, NotNull());
64 ASSERT_THAT(in.ByteCount(), Eq(20u));
Ryan Mitchell8f377342018-09-26 10:26:53 -070065 EXPECT_THAT(StringPiece(reinterpret_cast<const char*>(buffer), size), Eq("strin"));
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070066
67 // Backup 1 more than possible. Should clamp.
68 in.BackUp(11u);
69 EXPECT_THAT(in.ByteCount(), Eq(10u));
70
Ryan Mitchell8f377342018-09-26 10:26:53 -070071 ASSERT_TRUE(in.Next(&buffer, &size));
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070072 ASSERT_THAT(size, Eq(10u));
73 ASSERT_THAT(buffer, NotNull());
74 ASSERT_THAT(in.ByteCount(), Eq(20u));
Ryan Mitchell8f377342018-09-26 10:26:53 -070075 EXPECT_THAT(StringPiece(reinterpret_cast<const char*>(buffer), size), Eq("cool strin"));
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070076
Ryan Mitchell8f377342018-09-26 10:26:53 -070077 ASSERT_TRUE(in.Next(&buffer, &size));
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070078 ASSERT_THAT(size, Eq(1u));
79 ASSERT_THAT(buffer, NotNull());
80 ASSERT_THAT(in.ByteCount(), Eq(21u));
Ryan Mitchell8f377342018-09-26 10:26:53 -070081 EXPECT_THAT(StringPiece(reinterpret_cast<const char*>(buffer), size), Eq("g"));
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070082
Ryan Mitchell8f377342018-09-26 10:26:53 -070083 EXPECT_FALSE(in.Next(&buffer, &size));
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070084 EXPECT_FALSE(in.HadError());
85}
86
Adam Lesinski00451162017-10-03 07:44:08 -070087TEST(FileOutputStreamTest, NextAndBackup) {
88 const std::string input = "this is a cool string";
89
90 TemporaryFile file;
Adam Lesinski00451162017-10-03 07:44:08 -070091
Adam Lesinski93190b72017-11-03 15:20:17 -070092 FileOutputStream out(file.fd, 10u);
Adam Lesinski00451162017-10-03 07:44:08 -070093 ASSERT_FALSE(out.HadError());
94 EXPECT_THAT(out.ByteCount(), Eq(0u));
95
Ryan Mitchell8f377342018-09-26 10:26:53 -070096 void* buffer;
Adam Lesinski00451162017-10-03 07:44:08 -070097 size_t size;
Ryan Mitchell8f377342018-09-26 10:26:53 -070098 ASSERT_TRUE(out.Next(&buffer, &size));
Adam Lesinski00451162017-10-03 07:44:08 -070099 ASSERT_THAT(size, Eq(10u));
100 ASSERT_THAT(buffer, NotNull());
101 EXPECT_THAT(out.ByteCount(), Eq(10u));
Ryan Mitchell8f377342018-09-26 10:26:53 -0700102 memcpy(reinterpret_cast<char*>(buffer), input.c_str(), size);
Adam Lesinski00451162017-10-03 07:44:08 -0700103
Ryan Mitchell8f377342018-09-26 10:26:53 -0700104 ASSERT_TRUE(out.Next(&buffer, &size));
Adam Lesinski00451162017-10-03 07:44:08 -0700105 ASSERT_THAT(size, Eq(10u));
106 ASSERT_THAT(buffer, NotNull());
107 EXPECT_THAT(out.ByteCount(), Eq(20u));
Ryan Mitchell8f377342018-09-26 10:26:53 -0700108 memcpy(reinterpret_cast<char*>(buffer), input.c_str() + 10u, size);
Adam Lesinski00451162017-10-03 07:44:08 -0700109
Ryan Mitchell8f377342018-09-26 10:26:53 -0700110 ASSERT_TRUE(out.Next(&buffer, &size));
Adam Lesinski00451162017-10-03 07:44:08 -0700111 ASSERT_THAT(size, Eq(10u));
112 ASSERT_THAT(buffer, NotNull());
113 EXPECT_THAT(out.ByteCount(), Eq(30u));
Ryan Mitchell8f377342018-09-26 10:26:53 -0700114 reinterpret_cast<char*>(buffer)[0] = input[20u];
Adam Lesinski00451162017-10-03 07:44:08 -0700115 out.BackUp(size - 1);
116 EXPECT_THAT(out.ByteCount(), Eq(21u));
117
118 ASSERT_TRUE(out.Flush());
119
Adam Lesinski93190b72017-11-03 15:20:17 -0700120 lseek64(file.fd, 0, SEEK_SET);
Adam Lesinski00451162017-10-03 07:44:08 -0700121
122 std::string actual;
Adam Lesinski93190b72017-11-03 15:20:17 -0700123 ASSERT_TRUE(android::base::ReadFdToString(file.fd, &actual));
Adam Lesinski00451162017-10-03 07:44:08 -0700124 EXPECT_THAT(actual, StrEq(input));
125}
126
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700127} // namespace io
128} // namespace aapt