blob: 00231b1240e1076ade6c050ccca6e4f66dcdd3d6 [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
Andreas Gampe57943812017-12-06 21:39:13 -080017#include <android-base/logging.h>
18
19#include "base/macros.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070020#include "base/unix_file/fd_file.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080021#include "common_runtime_test.h"
David Srbecky2faab002019-02-12 16:35:48 +000022#include "stream/buffered_output_stream.h"
23#include "stream/file_output_stream.h"
24#include "stream/vector_output_stream.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080025
Brian Carlstromcd60ac72013-01-20 17:09:51 -080026namespace art {
Vladimir Marko74527972016-11-29 15:57:32 +000027namespace linker {
Brian Carlstromcd60ac72013-01-20 17:09:51 -080028
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080029class OutputStreamTest : public CommonRuntimeTest {
Brian Carlstromcd60ac72013-01-20 17:09:51 -080030 protected:
31 void CheckOffset(off_t expected) {
Brian Carlstrom49a0f152013-01-22 17:17:36 -080032 off_t actual = output_stream_->Seek(0, kSeekCurrent);
Ian Rogers8d3a1172013-06-04 01:13:28 -070033 EXPECT_EQ(expected, actual);
Brian Carlstromcd60ac72013-01-20 17:09:51 -080034 }
35
36 void SetOutputStream(OutputStream& output_stream) {
37 output_stream_ = &output_stream;
38 }
39
40 void GenerateTestOutput() {
Ian Rogers8d3a1172013-06-04 01:13:28 -070041 EXPECT_EQ(3, output_stream_->Seek(3, kSeekCurrent));
Brian Carlstromcd60ac72013-01-20 17:09:51 -080042 CheckOffset(3);
Ian Rogers8d3a1172013-06-04 01:13:28 -070043 EXPECT_EQ(2, output_stream_->Seek(2, kSeekSet));
Brian Carlstromcd60ac72013-01-20 17:09:51 -080044 CheckOffset(2);
45 uint8_t buf[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
Ian Rogers8d3a1172013-06-04 01:13:28 -070046 EXPECT_TRUE(output_stream_->WriteFully(buf, 2));
Brian Carlstromcd60ac72013-01-20 17:09:51 -080047 CheckOffset(4);
Ian Rogers8d3a1172013-06-04 01:13:28 -070048 EXPECT_EQ(6, output_stream_->Seek(2, kSeekEnd));
Brian Carlstromcd60ac72013-01-20 17:09:51 -080049 CheckOffset(6);
Ian Rogers8d3a1172013-06-04 01:13:28 -070050 EXPECT_TRUE(output_stream_->WriteFully(buf, 4));
Brian Carlstromcd60ac72013-01-20 17:09:51 -080051 CheckOffset(10);
Zheng Xu98088c42015-06-19 14:12:45 +080052 EXPECT_TRUE(output_stream_->WriteFully(buf, 6));
Vladimir Marko10c13562015-11-25 14:33:36 +000053 EXPECT_TRUE(output_stream_->Flush());
Brian Carlstromcd60ac72013-01-20 17:09:51 -080054 }
55
56 void CheckTestOutput(const std::vector<uint8_t>& actual) {
57 uint8_t expected[] = {
Zheng Xu98088c42015-06-19 14:12:45 +080058 0, 0, 1, 2, 0, 0, 1, 2, 3, 4, 1, 2, 3, 4, 5, 6
Brian Carlstromcd60ac72013-01-20 17:09:51 -080059 };
Ian Rogers8d3a1172013-06-04 01:13:28 -070060 EXPECT_EQ(sizeof(expected), actual.size());
61 EXPECT_EQ(0, memcmp(expected, &actual[0], actual.size()));
Brian Carlstromcd60ac72013-01-20 17:09:51 -080062 }
63
64 OutputStream* output_stream_;
65};
66
67TEST_F(OutputStreamTest, File) {
68 ScratchFile tmp;
69 FileOutputStream output_stream(tmp.GetFile());
70 SetOutputStream(output_stream);
71 GenerateTestOutput();
Ian Rogers700a4022014-05-19 16:49:03 -070072 std::unique_ptr<File> in(OS::OpenFileForReading(tmp.GetFilename().c_str()));
Mathieu Chartier2cebb242015-04-21 16:50:40 -070073 EXPECT_TRUE(in.get() != nullptr);
Brian Carlstromcd60ac72013-01-20 17:09:51 -080074 std::vector<uint8_t> actual(in->GetLength());
75 bool readSuccess = in->ReadFully(&actual[0], actual.size());
Ian Rogers8d3a1172013-06-04 01:13:28 -070076 EXPECT_TRUE(readSuccess);
Brian Carlstromcd60ac72013-01-20 17:09:51 -080077 CheckTestOutput(actual);
78}
79
Brian Carlstromc6dfdac2013-08-26 18:57:31 -070080TEST_F(OutputStreamTest, Buffered) {
81 ScratchFile tmp;
Zheng Xu98088c42015-06-19 14:12:45 +080082 {
Andreas Gampe8bdda5a2017-06-08 15:30:36 -070083 BufferedOutputStream buffered_output_stream(std::make_unique<FileOutputStream>(tmp.GetFile()));
Zheng Xu98088c42015-06-19 14:12:45 +080084 SetOutputStream(buffered_output_stream);
85 GenerateTestOutput();
86 }
Ian Rogers700a4022014-05-19 16:49:03 -070087 std::unique_ptr<File> in(OS::OpenFileForReading(tmp.GetFilename().c_str()));
Mathieu Chartier2cebb242015-04-21 16:50:40 -070088 EXPECT_TRUE(in.get() != nullptr);
Brian Carlstromc6dfdac2013-08-26 18:57:31 -070089 std::vector<uint8_t> actual(in->GetLength());
90 bool readSuccess = in->ReadFully(&actual[0], actual.size());
91 EXPECT_TRUE(readSuccess);
92 CheckTestOutput(actual);
93}
94
Brian Carlstromcd60ac72013-01-20 17:09:51 -080095TEST_F(OutputStreamTest, Vector) {
96 std::vector<uint8_t> output;
Ian Rogers0279ebb2014-10-08 17:27:48 -070097 VectorOutputStream output_stream("test vector output", &output);
Brian Carlstromcd60ac72013-01-20 17:09:51 -080098 SetOutputStream(output_stream);
99 GenerateTestOutput();
100 CheckTestOutput(output);
101}
102
Vladimir Marko10c13562015-11-25 14:33:36 +0000103TEST_F(OutputStreamTest, BufferedFlush) {
104 struct CheckingOutputStream : OutputStream {
105 CheckingOutputStream()
106 : OutputStream("dummy"),
107 flush_called(false) { }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100108 ~CheckingOutputStream() override {}
Vladimir Marko10c13562015-11-25 14:33:36 +0000109
110 bool WriteFully(const void* buffer ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100111 size_t byte_count ATTRIBUTE_UNUSED) override {
Vladimir Marko10c13562015-11-25 14:33:36 +0000112 LOG(FATAL) << "UNREACHABLE";
113 UNREACHABLE();
114 }
115
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100116 off_t Seek(off_t offset ATTRIBUTE_UNUSED, Whence whence ATTRIBUTE_UNUSED) override {
Vladimir Marko10c13562015-11-25 14:33:36 +0000117 LOG(FATAL) << "UNREACHABLE";
118 UNREACHABLE();
119 }
120
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100121 bool Flush() override {
Vladimir Marko10c13562015-11-25 14:33:36 +0000122 flush_called = true;
123 return true;
124 }
125
126 bool flush_called;
127 };
128
Andreas Gampe8bdda5a2017-06-08 15:30:36 -0700129 std::unique_ptr<CheckingOutputStream> cos = std::make_unique<CheckingOutputStream>();
Vladimir Marko10c13562015-11-25 14:33:36 +0000130 CheckingOutputStream* checking_output_stream = cos.get();
131 BufferedOutputStream buffered(std::move(cos));
132 ASSERT_FALSE(checking_output_stream->flush_called);
133 bool flush_result = buffered.Flush();
134 ASSERT_TRUE(flush_result);
135 ASSERT_TRUE(checking_output_stream->flush_called);
136}
137
Vladimir Marko74527972016-11-29 15:57:32 +0000138} // namespace linker
Mathieu Chartier02e25112013-08-14 16:14:24 -0700139} // namespace art