blob: f93ea7a7094ca279efc93c0e2d3047f17e97c6fe [file] [log] [blame]
Brian Carlstromcd60ac72013-01-20 17:09:51 -08001/*
2 * Copyright (C) 2013 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
Brian Carlstromcd60ac72013-01-20 17:09:51 -080017#include "file_output_stream.h"
18#include "vector_output_stream.h"
19
Andreas Gampe57943812017-12-06 21:39:13 -080020#include <android-base/logging.h>
21
22#include "base/macros.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070023#include "base/unix_file/fd_file.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080024#include "buffered_output_stream.h"
25#include "common_runtime_test.h"
26
Brian Carlstromcd60ac72013-01-20 17:09:51 -080027namespace art {
Vladimir Marko74527972016-11-29 15:57:32 +000028namespace linker {
Brian Carlstromcd60ac72013-01-20 17:09:51 -080029
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080030class OutputStreamTest : public CommonRuntimeTest {
Brian Carlstromcd60ac72013-01-20 17:09:51 -080031 protected:
32 void CheckOffset(off_t expected) {
Brian Carlstrom49a0f152013-01-22 17:17:36 -080033 off_t actual = output_stream_->Seek(0, kSeekCurrent);
Ian Rogers8d3a1172013-06-04 01:13:28 -070034 EXPECT_EQ(expected, actual);
Brian Carlstromcd60ac72013-01-20 17:09:51 -080035 }
36
37 void SetOutputStream(OutputStream& output_stream) {
38 output_stream_ = &output_stream;
39 }
40
41 void GenerateTestOutput() {
Ian Rogers8d3a1172013-06-04 01:13:28 -070042 EXPECT_EQ(3, output_stream_->Seek(3, kSeekCurrent));
Brian Carlstromcd60ac72013-01-20 17:09:51 -080043 CheckOffset(3);
Ian Rogers8d3a1172013-06-04 01:13:28 -070044 EXPECT_EQ(2, output_stream_->Seek(2, kSeekSet));
Brian Carlstromcd60ac72013-01-20 17:09:51 -080045 CheckOffset(2);
46 uint8_t buf[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
Ian Rogers8d3a1172013-06-04 01:13:28 -070047 EXPECT_TRUE(output_stream_->WriteFully(buf, 2));
Brian Carlstromcd60ac72013-01-20 17:09:51 -080048 CheckOffset(4);
Ian Rogers8d3a1172013-06-04 01:13:28 -070049 EXPECT_EQ(6, output_stream_->Seek(2, kSeekEnd));
Brian Carlstromcd60ac72013-01-20 17:09:51 -080050 CheckOffset(6);
Ian Rogers8d3a1172013-06-04 01:13:28 -070051 EXPECT_TRUE(output_stream_->WriteFully(buf, 4));
Brian Carlstromcd60ac72013-01-20 17:09:51 -080052 CheckOffset(10);
Zheng Xu98088c42015-06-19 14:12:45 +080053 EXPECT_TRUE(output_stream_->WriteFully(buf, 6));
Vladimir Marko10c13562015-11-25 14:33:36 +000054 EXPECT_TRUE(output_stream_->Flush());
Brian Carlstromcd60ac72013-01-20 17:09:51 -080055 }
56
57 void CheckTestOutput(const std::vector<uint8_t>& actual) {
58 uint8_t expected[] = {
Zheng Xu98088c42015-06-19 14:12:45 +080059 0, 0, 1, 2, 0, 0, 1, 2, 3, 4, 1, 2, 3, 4, 5, 6
Brian Carlstromcd60ac72013-01-20 17:09:51 -080060 };
Ian Rogers8d3a1172013-06-04 01:13:28 -070061 EXPECT_EQ(sizeof(expected), actual.size());
62 EXPECT_EQ(0, memcmp(expected, &actual[0], actual.size()));
Brian Carlstromcd60ac72013-01-20 17:09:51 -080063 }
64
65 OutputStream* output_stream_;
66};
67
68TEST_F(OutputStreamTest, File) {
69 ScratchFile tmp;
70 FileOutputStream output_stream(tmp.GetFile());
71 SetOutputStream(output_stream);
72 GenerateTestOutput();
Ian Rogers700a4022014-05-19 16:49:03 -070073 std::unique_ptr<File> in(OS::OpenFileForReading(tmp.GetFilename().c_str()));
Mathieu Chartier2cebb242015-04-21 16:50:40 -070074 EXPECT_TRUE(in.get() != nullptr);
Brian Carlstromcd60ac72013-01-20 17:09:51 -080075 std::vector<uint8_t> actual(in->GetLength());
76 bool readSuccess = in->ReadFully(&actual[0], actual.size());
Ian Rogers8d3a1172013-06-04 01:13:28 -070077 EXPECT_TRUE(readSuccess);
Brian Carlstromcd60ac72013-01-20 17:09:51 -080078 CheckTestOutput(actual);
79}
80
Brian Carlstromc6dfdac2013-08-26 18:57:31 -070081TEST_F(OutputStreamTest, Buffered) {
82 ScratchFile tmp;
Zheng Xu98088c42015-06-19 14:12:45 +080083 {
Andreas Gampe8bdda5a2017-06-08 15:30:36 -070084 BufferedOutputStream buffered_output_stream(std::make_unique<FileOutputStream>(tmp.GetFile()));
Zheng Xu98088c42015-06-19 14:12:45 +080085 SetOutputStream(buffered_output_stream);
86 GenerateTestOutput();
87 }
Ian Rogers700a4022014-05-19 16:49:03 -070088 std::unique_ptr<File> in(OS::OpenFileForReading(tmp.GetFilename().c_str()));
Mathieu Chartier2cebb242015-04-21 16:50:40 -070089 EXPECT_TRUE(in.get() != nullptr);
Brian Carlstromc6dfdac2013-08-26 18:57:31 -070090 std::vector<uint8_t> actual(in->GetLength());
91 bool readSuccess = in->ReadFully(&actual[0], actual.size());
92 EXPECT_TRUE(readSuccess);
93 CheckTestOutput(actual);
94}
95
Brian Carlstromcd60ac72013-01-20 17:09:51 -080096TEST_F(OutputStreamTest, Vector) {
97 std::vector<uint8_t> output;
Ian Rogers0279ebb2014-10-08 17:27:48 -070098 VectorOutputStream output_stream("test vector output", &output);
Brian Carlstromcd60ac72013-01-20 17:09:51 -080099 SetOutputStream(output_stream);
100 GenerateTestOutput();
101 CheckTestOutput(output);
102}
103
Vladimir Marko10c13562015-11-25 14:33:36 +0000104TEST_F(OutputStreamTest, BufferedFlush) {
105 struct CheckingOutputStream : OutputStream {
106 CheckingOutputStream()
107 : OutputStream("dummy"),
108 flush_called(false) { }
109 ~CheckingOutputStream() OVERRIDE {}
110
111 bool WriteFully(const void* buffer ATTRIBUTE_UNUSED,
112 size_t byte_count ATTRIBUTE_UNUSED) OVERRIDE {
113 LOG(FATAL) << "UNREACHABLE";
114 UNREACHABLE();
115 }
116
117 off_t Seek(off_t offset ATTRIBUTE_UNUSED, Whence whence ATTRIBUTE_UNUSED) OVERRIDE {
118 LOG(FATAL) << "UNREACHABLE";
119 UNREACHABLE();
120 }
121
122 bool Flush() OVERRIDE {
123 flush_called = true;
124 return true;
125 }
126
127 bool flush_called;
128 };
129
Andreas Gampe8bdda5a2017-06-08 15:30:36 -0700130 std::unique_ptr<CheckingOutputStream> cos = std::make_unique<CheckingOutputStream>();
Vladimir Marko10c13562015-11-25 14:33:36 +0000131 CheckingOutputStream* checking_output_stream = cos.get();
132 BufferedOutputStream buffered(std::move(cos));
133 ASSERT_FALSE(checking_output_stream->flush_called);
134 bool flush_result = buffered.Flush();
135 ASSERT_TRUE(flush_result);
136 ASSERT_TRUE(checking_output_stream->flush_called);
137}
138
Vladimir Marko74527972016-11-29 15:57:32 +0000139} // namespace linker
Mathieu Chartier02e25112013-08-14 16:14:24 -0700140} // namespace art