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 
17 #include "file_output_stream.h"
18 #include "vector_output_stream.h"
19 
20 #include <android-base/logging.h>
21 
22 #include "base/macros.h"
23 #include "base/unix_file/fd_file.h"
24 #include "buffered_output_stream.h"
25 #include "common_runtime_test.h"
26 
27 namespace art {
28 namespace linker {
29 
30 class OutputStreamTest : public CommonRuntimeTest {
31  protected:
CheckOffset(off_t expected)32   void CheckOffset(off_t expected) {
33     off_t actual = output_stream_->Seek(0, kSeekCurrent);
34     EXPECT_EQ(expected, actual);
35   }
36 
SetOutputStream(OutputStream & output_stream)37   void SetOutputStream(OutputStream& output_stream) {
38     output_stream_ = &output_stream;
39   }
40 
GenerateTestOutput()41   void GenerateTestOutput() {
42     EXPECT_EQ(3, output_stream_->Seek(3, kSeekCurrent));
43     CheckOffset(3);
44     EXPECT_EQ(2, output_stream_->Seek(2, kSeekSet));
45     CheckOffset(2);
46     uint8_t buf[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
47     EXPECT_TRUE(output_stream_->WriteFully(buf, 2));
48     CheckOffset(4);
49     EXPECT_EQ(6, output_stream_->Seek(2, kSeekEnd));
50     CheckOffset(6);
51     EXPECT_TRUE(output_stream_->WriteFully(buf, 4));
52     CheckOffset(10);
53     EXPECT_TRUE(output_stream_->WriteFully(buf, 6));
54     EXPECT_TRUE(output_stream_->Flush());
55   }
56 
CheckTestOutput(const std::vector<uint8_t> & actual)57   void CheckTestOutput(const std::vector<uint8_t>& actual) {
58     uint8_t expected[] = {
59         0, 0, 1, 2, 0, 0, 1, 2, 3, 4, 1, 2, 3, 4, 5, 6
60     };
61     EXPECT_EQ(sizeof(expected), actual.size());
62     EXPECT_EQ(0, memcmp(expected, &actual[0], actual.size()));
63   }
64 
65   OutputStream* output_stream_;
66 };
67 
TEST_F(OutputStreamTest,File)68 TEST_F(OutputStreamTest, File) {
69   ScratchFile tmp;
70   FileOutputStream output_stream(tmp.GetFile());
71   SetOutputStream(output_stream);
72   GenerateTestOutput();
73   std::unique_ptr<File> in(OS::OpenFileForReading(tmp.GetFilename().c_str()));
74   EXPECT_TRUE(in.get() != nullptr);
75   std::vector<uint8_t> actual(in->GetLength());
76   bool readSuccess = in->ReadFully(&actual[0], actual.size());
77   EXPECT_TRUE(readSuccess);
78   CheckTestOutput(actual);
79 }
80 
TEST_F(OutputStreamTest,Buffered)81 TEST_F(OutputStreamTest, Buffered) {
82   ScratchFile tmp;
83   {
84     BufferedOutputStream buffered_output_stream(std::make_unique<FileOutputStream>(tmp.GetFile()));
85     SetOutputStream(buffered_output_stream);
86     GenerateTestOutput();
87   }
88   std::unique_ptr<File> in(OS::OpenFileForReading(tmp.GetFilename().c_str()));
89   EXPECT_TRUE(in.get() != nullptr);
90   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 
TEST_F(OutputStreamTest,Vector)96 TEST_F(OutputStreamTest, Vector) {
97   std::vector<uint8_t> output;
98   VectorOutputStream output_stream("test vector output", &output);
99   SetOutputStream(output_stream);
100   GenerateTestOutput();
101   CheckTestOutput(output);
102 }
103 
TEST_F(OutputStreamTest,BufferedFlush)104 TEST_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 
130   std::unique_ptr<CheckingOutputStream> cos = std::make_unique<CheckingOutputStream>();
131   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 
139 }  // namespace linker
140 }  // namespace art
141