• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "base/unix_file/fd_file.h"
21 #include "base/logging.h"
22 #include "buffered_output_stream.h"
23 #include "common_runtime_test.h"
24 
25 namespace art {
26 
27 class OutputStreamTest : public CommonRuntimeTest {
28  protected:
CheckOffset(off_t expected)29   void CheckOffset(off_t expected) {
30     off_t actual = output_stream_->Seek(0, kSeekCurrent);
31     EXPECT_EQ(expected, actual);
32   }
33 
SetOutputStream(OutputStream & output_stream)34   void SetOutputStream(OutputStream& output_stream) {
35     output_stream_ = &output_stream;
36   }
37 
GenerateTestOutput()38   void GenerateTestOutput() {
39     EXPECT_EQ(3, output_stream_->Seek(3, kSeekCurrent));
40     CheckOffset(3);
41     EXPECT_EQ(2, output_stream_->Seek(2, kSeekSet));
42     CheckOffset(2);
43     uint8_t buf[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
44     EXPECT_TRUE(output_stream_->WriteFully(buf, 2));
45     CheckOffset(4);
46     EXPECT_EQ(6, output_stream_->Seek(2, kSeekEnd));
47     CheckOffset(6);
48     EXPECT_TRUE(output_stream_->WriteFully(buf, 4));
49     CheckOffset(10);
50     EXPECT_TRUE(output_stream_->WriteFully(buf, 6));
51     EXPECT_TRUE(output_stream_->Flush());
52   }
53 
CheckTestOutput(const std::vector<uint8_t> & actual)54   void CheckTestOutput(const std::vector<uint8_t>& actual) {
55     uint8_t expected[] = {
56         0, 0, 1, 2, 0, 0, 1, 2, 3, 4, 1, 2, 3, 4, 5, 6
57     };
58     EXPECT_EQ(sizeof(expected), actual.size());
59     EXPECT_EQ(0, memcmp(expected, &actual[0], actual.size()));
60   }
61 
62   OutputStream* output_stream_;
63 };
64 
TEST_F(OutputStreamTest,File)65 TEST_F(OutputStreamTest, File) {
66   ScratchFile tmp;
67   FileOutputStream output_stream(tmp.GetFile());
68   SetOutputStream(output_stream);
69   GenerateTestOutput();
70   std::unique_ptr<File> in(OS::OpenFileForReading(tmp.GetFilename().c_str()));
71   EXPECT_TRUE(in.get() != nullptr);
72   std::vector<uint8_t> actual(in->GetLength());
73   bool readSuccess = in->ReadFully(&actual[0], actual.size());
74   EXPECT_TRUE(readSuccess);
75   CheckTestOutput(actual);
76 }
77 
TEST_F(OutputStreamTest,Buffered)78 TEST_F(OutputStreamTest, Buffered) {
79   ScratchFile tmp;
80   {
81     BufferedOutputStream buffered_output_stream(std::make_unique<FileOutputStream>(tmp.GetFile()));
82     SetOutputStream(buffered_output_stream);
83     GenerateTestOutput();
84   }
85   std::unique_ptr<File> in(OS::OpenFileForReading(tmp.GetFilename().c_str()));
86   EXPECT_TRUE(in.get() != nullptr);
87   std::vector<uint8_t> actual(in->GetLength());
88   bool readSuccess = in->ReadFully(&actual[0], actual.size());
89   EXPECT_TRUE(readSuccess);
90   CheckTestOutput(actual);
91 }
92 
TEST_F(OutputStreamTest,Vector)93 TEST_F(OutputStreamTest, Vector) {
94   std::vector<uint8_t> output;
95   VectorOutputStream output_stream("test vector output", &output);
96   SetOutputStream(output_stream);
97   GenerateTestOutput();
98   CheckTestOutput(output);
99 }
100 
TEST_F(OutputStreamTest,BufferedFlush)101 TEST_F(OutputStreamTest, BufferedFlush) {
102   struct CheckingOutputStream : OutputStream {
103     CheckingOutputStream()
104         : OutputStream("dummy"),
105           flush_called(false) { }
106     ~CheckingOutputStream() OVERRIDE {}
107 
108     bool WriteFully(const void* buffer ATTRIBUTE_UNUSED,
109                     size_t byte_count ATTRIBUTE_UNUSED) OVERRIDE {
110       LOG(FATAL) << "UNREACHABLE";
111       UNREACHABLE();
112     }
113 
114     off_t Seek(off_t offset ATTRIBUTE_UNUSED, Whence whence ATTRIBUTE_UNUSED) OVERRIDE {
115       LOG(FATAL) << "UNREACHABLE";
116       UNREACHABLE();
117     }
118 
119     bool Flush() OVERRIDE {
120       flush_called = true;
121       return true;
122     }
123 
124     bool flush_called;
125   };
126 
127   std::unique_ptr<CheckingOutputStream> cos = std::make_unique<CheckingOutputStream>();
128   CheckingOutputStream* checking_output_stream = cos.get();
129   BufferedOutputStream buffered(std::move(cos));
130   ASSERT_FALSE(checking_output_stream->flush_called);
131   bool flush_result = buffered.Flush();
132   ASSERT_TRUE(flush_result);
133   ASSERT_TRUE(checking_output_stream->flush_called);
134 }
135 
136 }  // namespace art
137