2 * Copyright 2020 Google Inc.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met: redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer;
8 * redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution;
11 * neither the name of the copyright holders nor the names of its
12 * contributors may be used to endorse or promote products derived from
13 * this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <gmock/gmock.h>
29 #include <gtest/gtest.h>
41 #include "dispatch_table.hh"
43 uint64_t test_total_written
;
44 std::string test_host_file_name
;
46 std::vector
<uint8_t> test_written_data
;
47 uint64_t test_max_buf_size
;
50 test_m5_write_file(void *buffer
, uint64_t len
, uint64_t offset
,
53 if (test_max_buf_size
&& len
> test_max_buf_size
)
54 len
= test_max_buf_size
;
56 test_total_written
+= len
;
58 if (test_host_file_name
== "")
59 test_host_file_name
= filename
;
61 EXPECT_EQ(test_host_file_name
, filename
);
64 test_written_data
.clear();
66 size_t required_size
= offset
+ len
;
67 if (test_written_data
.size() < required_size
)
68 test_written_data
.resize(required_size
);
70 memcpy(test_written_data
.data() + offset
, buffer
, len
);
75 DispatchTable dt
= { .m5_write_file
= &test_m5_write_file
};
77 std::string cout_output
;
80 run(std::initializer_list
<std::string
> arg_args
)
82 test_total_written
= 0;
83 test_host_file_name
= "";
84 test_written_data
.clear();
88 // Redirect cout into a stringstream.
89 std::stringstream buffer
;
90 std::streambuf
*orig
= std::cout
.rdbuf(buffer
.rdbuf());
92 bool res
= Command::run(dt
, args
);
94 // Capture the contents of the stringstream and restore cout.
95 cout_output
= buffer
.str();
96 std::cout
.rdbuf(orig
);
110 TempFile(size_t _size
) : _size(_size
)
112 // Generate a temporary filename.
113 char *tmp_name
= strdup("/tmp/writefile.test.XXXXXXXX");
114 fd
= mkstemp(tmp_name
);
118 // Make the file the appropriate length.
119 assert(!ftruncate(fd
, _size
));
122 _buf
= mmap(nullptr, _size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
125 // Fill it with an incrementing 32 bit integers.
127 int chunk_size
= sizeof(uint32_t);
128 size_t num_chunks
= _size
/ chunk_size
;
129 int leftovers
= _size
% chunk_size
;
131 uint32_t *buf32
= (uint32_t *)_buf
;
133 for (size_t i
= 0; i
< num_chunks
; i
++)
136 memcpy(buf32
, &val
, leftovers
);
138 // Make sure our new contents are out there.
139 msync(_buf
, _size
, MS_SYNC
| MS_INVALIDATE
);
144 unlink(path().c_str());
148 size_t size() const { return _size
; }
149 const std::string
&path() const { return _path
; }
150 const void *buf() const { return _buf
; }
159 verify(const std::string
&expected_path
)
161 EXPECT_EQ(test_written_data
.size(), size());
162 EXPECT_EQ(memcmp(test_written_data
.data(), buf(), size()), 0);
163 EXPECT_EQ(test_host_file_name
, expected_path
);
165 std::ostringstream os
;
166 os
<< "Opening \"" << path() << "\".";
167 EXPECT_THAT(cout_output
, ::testing::HasSubstr(os
.str()));
169 os
<< "Wrote " << size() << " bytes.";
170 EXPECT_THAT(cout_output
, ::testing::HasSubstr(os
.str()));
174 TEST(Writefile
, NoArguments
)
176 // Call with no arguments.
177 EXPECT_FALSE(run({"writefile"}));
178 EXPECT_EQ(test_total_written
, 0);
181 TEST(Writefile
, ThreeArguments
)
183 // Call with no arguments.
184 EXPECT_FALSE(run({"writefile", "1", "2", "3"}));
185 EXPECT_EQ(test_total_written
, 0);
188 TEST(Writefile
, SmallFile
)
190 // Write a small file.
192 test_max_buf_size
= 0;
193 EXPECT_TRUE(run({"writefile", tmp
.path()}));
197 TEST(Writefile
, SmallFileHostName
)
199 // Write a small file with a different host file name.
201 test_max_buf_size
= 0;
202 std::string host_path
= "/different/host/path";
203 EXPECT_TRUE(run({"writefile", tmp
.path(), host_path
}));
204 tmp
.verify(host_path
);
207 TEST(Writefile
, MultipleChunks
)
209 // Write a file which will need to be split into multiple whole chunks.
210 TempFile
tmp(256 * 1024 * 4);
211 test_max_buf_size
= 0;
212 EXPECT_TRUE(run({"writefile", tmp
.path()}));
216 TEST(Writefile
, MultipleAndPartialChunks
)
218 // Write a file which will be split into some whole and one partial chunk.
219 TempFile
tmp(256 * 1024 * 2 + 256);
220 test_max_buf_size
= 0;
221 EXPECT_TRUE(run({"writefile", tmp
.path()}));
225 TEST(Writefile
, OddSizedChunks
)
227 // Write a file in chunks that aren't nicely aligned.
228 TempFile
tmp(256 * 1024);
229 test_max_buf_size
= 13;
230 EXPECT_TRUE(run({"writefile", tmp
.path()}));
234 TEST(Writefile
, CappedWriteSize
)
236 // Write a file, accepting less than the requested amount of data.
237 TempFile
tmp(256 * 1024 * 2 + 256);
238 test_max_buf_size
= 256;
239 EXPECT_TRUE(run({"writefile", tmp
.path()}));
243 TEST(WritefileDeathTest
, BadFile
)
245 EXPECT_EXIT(run({"writefile", "this is not a valid path#$#$://\\\\"}),
246 ::testing::ExitedWithCode(2), "Error opening ");