Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 1 | /* |
| 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 Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 17 | #include <android-base/logging.h> |
| 18 | |
| 19 | #include "base/macros.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 20 | #include "base/unix_file/fd_file.h" |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 21 | #include "common_runtime_test.h" |
David Srbecky | 2faab00 | 2019-02-12 16:35:48 +0000 | [diff] [blame] | 22 | #include "stream/buffered_output_stream.h" |
| 23 | #include "stream/file_output_stream.h" |
| 24 | #include "stream/vector_output_stream.h" |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 25 | |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 26 | namespace art { |
Vladimir Marko | 7452797 | 2016-11-29 15:57:32 +0000 | [diff] [blame] | 27 | namespace linker { |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 28 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 29 | class OutputStreamTest : public CommonRuntimeTest { |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 30 | protected: |
| 31 | void CheckOffset(off_t expected) { |
Brian Carlstrom | 49a0f15 | 2013-01-22 17:17:36 -0800 | [diff] [blame] | 32 | off_t actual = output_stream_->Seek(0, kSeekCurrent); |
Ian Rogers | 8d3a117 | 2013-06-04 01:13:28 -0700 | [diff] [blame] | 33 | EXPECT_EQ(expected, actual); |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | void SetOutputStream(OutputStream& output_stream) { |
| 37 | output_stream_ = &output_stream; |
| 38 | } |
| 39 | |
| 40 | void GenerateTestOutput() { |
Ian Rogers | 8d3a117 | 2013-06-04 01:13:28 -0700 | [diff] [blame] | 41 | EXPECT_EQ(3, output_stream_->Seek(3, kSeekCurrent)); |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 42 | CheckOffset(3); |
Ian Rogers | 8d3a117 | 2013-06-04 01:13:28 -0700 | [diff] [blame] | 43 | EXPECT_EQ(2, output_stream_->Seek(2, kSeekSet)); |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 44 | CheckOffset(2); |
| 45 | uint8_t buf[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; |
Ian Rogers | 8d3a117 | 2013-06-04 01:13:28 -0700 | [diff] [blame] | 46 | EXPECT_TRUE(output_stream_->WriteFully(buf, 2)); |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 47 | CheckOffset(4); |
Ian Rogers | 8d3a117 | 2013-06-04 01:13:28 -0700 | [diff] [blame] | 48 | EXPECT_EQ(6, output_stream_->Seek(2, kSeekEnd)); |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 49 | CheckOffset(6); |
Ian Rogers | 8d3a117 | 2013-06-04 01:13:28 -0700 | [diff] [blame] | 50 | EXPECT_TRUE(output_stream_->WriteFully(buf, 4)); |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 51 | CheckOffset(10); |
Zheng Xu | 98088c4 | 2015-06-19 14:12:45 +0800 | [diff] [blame] | 52 | EXPECT_TRUE(output_stream_->WriteFully(buf, 6)); |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 53 | EXPECT_TRUE(output_stream_->Flush()); |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | void CheckTestOutput(const std::vector<uint8_t>& actual) { |
| 57 | uint8_t expected[] = { |
Zheng Xu | 98088c4 | 2015-06-19 14:12:45 +0800 | [diff] [blame] | 58 | 0, 0, 1, 2, 0, 0, 1, 2, 3, 4, 1, 2, 3, 4, 5, 6 |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 59 | }; |
Ian Rogers | 8d3a117 | 2013-06-04 01:13:28 -0700 | [diff] [blame] | 60 | EXPECT_EQ(sizeof(expected), actual.size()); |
| 61 | EXPECT_EQ(0, memcmp(expected, &actual[0], actual.size())); |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | OutputStream* output_stream_; |
| 65 | }; |
| 66 | |
| 67 | TEST_F(OutputStreamTest, File) { |
| 68 | ScratchFile tmp; |
| 69 | FileOutputStream output_stream(tmp.GetFile()); |
| 70 | SetOutputStream(output_stream); |
| 71 | GenerateTestOutput(); |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 72 | std::unique_ptr<File> in(OS::OpenFileForReading(tmp.GetFilename().c_str())); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 73 | EXPECT_TRUE(in.get() != nullptr); |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 74 | std::vector<uint8_t> actual(in->GetLength()); |
| 75 | bool readSuccess = in->ReadFully(&actual[0], actual.size()); |
Ian Rogers | 8d3a117 | 2013-06-04 01:13:28 -0700 | [diff] [blame] | 76 | EXPECT_TRUE(readSuccess); |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 77 | CheckTestOutput(actual); |
| 78 | } |
| 79 | |
Brian Carlstrom | c6dfdac | 2013-08-26 18:57:31 -0700 | [diff] [blame] | 80 | TEST_F(OutputStreamTest, Buffered) { |
| 81 | ScratchFile tmp; |
Zheng Xu | 98088c4 | 2015-06-19 14:12:45 +0800 | [diff] [blame] | 82 | { |
Andreas Gampe | 8bdda5a | 2017-06-08 15:30:36 -0700 | [diff] [blame] | 83 | BufferedOutputStream buffered_output_stream(std::make_unique<FileOutputStream>(tmp.GetFile())); |
Zheng Xu | 98088c4 | 2015-06-19 14:12:45 +0800 | [diff] [blame] | 84 | SetOutputStream(buffered_output_stream); |
| 85 | GenerateTestOutput(); |
| 86 | } |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 87 | std::unique_ptr<File> in(OS::OpenFileForReading(tmp.GetFilename().c_str())); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 88 | EXPECT_TRUE(in.get() != nullptr); |
Brian Carlstrom | c6dfdac | 2013-08-26 18:57:31 -0700 | [diff] [blame] | 89 | 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 Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 95 | TEST_F(OutputStreamTest, Vector) { |
| 96 | std::vector<uint8_t> output; |
Ian Rogers | 0279ebb | 2014-10-08 17:27:48 -0700 | [diff] [blame] | 97 | VectorOutputStream output_stream("test vector output", &output); |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 98 | SetOutputStream(output_stream); |
| 99 | GenerateTestOutput(); |
| 100 | CheckTestOutput(output); |
| 101 | } |
| 102 | |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 103 | TEST_F(OutputStreamTest, BufferedFlush) { |
| 104 | struct CheckingOutputStream : OutputStream { |
| 105 | CheckingOutputStream() |
| 106 | : OutputStream("dummy"), |
| 107 | flush_called(false) { } |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 108 | ~CheckingOutputStream() override {} |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 109 | |
| 110 | bool WriteFully(const void* buffer ATTRIBUTE_UNUSED, |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 111 | size_t byte_count ATTRIBUTE_UNUSED) override { |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 112 | LOG(FATAL) << "UNREACHABLE"; |
| 113 | UNREACHABLE(); |
| 114 | } |
| 115 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 116 | off_t Seek(off_t offset ATTRIBUTE_UNUSED, Whence whence ATTRIBUTE_UNUSED) override { |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 117 | LOG(FATAL) << "UNREACHABLE"; |
| 118 | UNREACHABLE(); |
| 119 | } |
| 120 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 121 | bool Flush() override { |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 122 | flush_called = true; |
| 123 | return true; |
| 124 | } |
| 125 | |
| 126 | bool flush_called; |
| 127 | }; |
| 128 | |
Andreas Gampe | 8bdda5a | 2017-06-08 15:30:36 -0700 | [diff] [blame] | 129 | std::unique_ptr<CheckingOutputStream> cos = std::make_unique<CheckingOutputStream>(); |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 130 | 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 Marko | 7452797 | 2016-11-29 15:57:32 +0000 | [diff] [blame] | 138 | } // namespace linker |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 139 | } // namespace art |