Patrick Venture | 7753d94 | 2018-11-15 13:15:36 -0800 | [diff] [blame^] | 1 | #include "file_handler.hpp" |
Patrick Venture | a17cf44 | 2018-11-15 09:31:51 -0800 | [diff] [blame] | 2 | |
| 3 | #include <cstdint> |
| 4 | #include <cstdio> |
| 5 | #include <fstream> |
| 6 | #include <vector> |
| 7 | |
| 8 | #include <gtest/gtest.h> |
| 9 | |
| 10 | namespace blobs |
| 11 | { |
| 12 | |
| 13 | static constexpr auto TESTPATH = "test.output"; |
| 14 | |
Patrick Venture | 7753d94 | 2018-11-15 13:15:36 -0800 | [diff] [blame^] | 15 | class FileHandlerOpenTest : public ::testing::Test |
Patrick Venture | a17cf44 | 2018-11-15 09:31:51 -0800 | [diff] [blame] | 16 | { |
| 17 | protected: |
| 18 | void TearDown() override |
| 19 | { |
| 20 | (void)std::remove(TESTPATH); |
| 21 | } |
| 22 | }; |
| 23 | |
Patrick Venture | 7753d94 | 2018-11-15 13:15:36 -0800 | [diff] [blame^] | 24 | TEST_F(FileHandlerOpenTest, VerifyItIsHappy) |
Patrick Venture | a17cf44 | 2018-11-15 09:31:51 -0800 | [diff] [blame] | 25 | { |
| 26 | /* Opening a fail may create it? */ |
| 27 | |
Patrick Venture | 7753d94 | 2018-11-15 13:15:36 -0800 | [diff] [blame^] | 28 | FileHandler handler(TESTPATH); |
Patrick Venture | a17cf44 | 2018-11-15 09:31:51 -0800 | [diff] [blame] | 29 | EXPECT_TRUE(handler.open("")); |
| 30 | |
| 31 | /* Calling open twice fails the second time. */ |
| 32 | EXPECT_FALSE(handler.open("")); |
| 33 | } |
| 34 | |
Patrick Venture | 7753d94 | 2018-11-15 13:15:36 -0800 | [diff] [blame^] | 35 | TEST_F(FileHandlerOpenTest, VerifyWriteDataWrites) |
Patrick Venture | a17cf44 | 2018-11-15 09:31:51 -0800 | [diff] [blame] | 36 | { |
| 37 | /* Verify writing bytes writes them... flushing data can be an issue here, |
| 38 | * so we close first. |
| 39 | */ |
Patrick Venture | 7753d94 | 2018-11-15 13:15:36 -0800 | [diff] [blame^] | 40 | FileHandler handler(TESTPATH); |
Patrick Venture | a17cf44 | 2018-11-15 09:31:51 -0800 | [diff] [blame] | 41 | EXPECT_TRUE(handler.open("")); |
| 42 | |
| 43 | std::vector<std::uint8_t> bytes = {0x01, 0x02}; |
| 44 | std::uint32_t offset = 0; |
| 45 | |
| 46 | EXPECT_TRUE(handler.write(offset, bytes)); |
| 47 | handler.close(); |
| 48 | |
| 49 | std::ifstream data; |
| 50 | data.open(TESTPATH, std::ios::binary); |
| 51 | char expectedBytes[2]; |
| 52 | data.read(&expectedBytes[0], sizeof(expectedBytes)); |
| 53 | EXPECT_EQ(expectedBytes[0], bytes[0]); |
| 54 | EXPECT_EQ(expectedBytes[1], bytes[1]); |
| 55 | /* annoyingly the memcmp was failing... but it's the same data. */ |
| 56 | } |
| 57 | |
| 58 | } // namespace blobs |