1 #include "estoraged_conf.hpp"
2 #include "pattern.hpp"
3 
4 #include <fcntl.h>
5 #include <unistd.h>
6 
7 #include <stdplus/fd/create.hpp>
8 #include <stdplus/fd/managed.hpp>
9 #include <xyz/openbmc_project/Common/error.hpp>
10 
11 #include <fstream>
12 #include <system_error>
13 
14 #include <gmock/gmock-matchers.h>
15 #include <gmock/gmock.h>
16 #include <gtest/gtest.h>
17 
18 namespace estoraged_test
19 {
20 
21 using estoraged::Pattern;
22 using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
23 
24 TEST(pattern, patternPass)
25 {
26     std::string testFileName = "patternPass";
27     uint64_t size = 4096;
28     std::ofstream testFile;
29     testFile.open(testFileName,
30                   std::ios::out | std::ios::binary | std::ios::trunc);
31     testFile.close();
32 
33     Pattern pass(testFileName);
34     EXPECT_NO_THROW(pass.writePattern(size));
35     EXPECT_NO_THROW(pass.verifyPattern(size));
36 }
37 
38 /* This test that pattern writes the correct number of bytes even if
39  * size of the drive is not divisable by the block size
40  */
41 TEST(pattern, patternNotDivisible)
42 {
43     std::string testFileName = "notDivisible";
44     uint64_t size = 4097;
45     // 4097 is not divisible by the block size, and we expect no errors
46     std::ofstream testFile;
47     testFile.open(testFileName,
48                   std::ios::out | std::ios::binary | std::ios::trunc);
49     testFile.close();
50 
51     Pattern pass(testFileName);
52     EXPECT_NO_THROW(pass.writePattern(size));
53     EXPECT_NO_THROW(pass.verifyPattern(size));
54 }
55 
56 TEST(pattern, patternsDontMatch)
57 {
58     std::string testFileName = "patternsDontMatch";
59     uint64_t size = 4096;
60     std::ofstream testFile;
61 
62     Pattern pass(testFileName);
63 
64     int dummyValue = 88;
65     testFile.open(testFileName, std::ios::binary | std::ios::out);
66     testFile.write((const char*)&dummyValue, sizeof(dummyValue));
67     testFile.close();
68 
69     EXPECT_NO_THROW(pass.writePattern(size - sizeof(dummyValue)));
70     EXPECT_THROW(pass.verifyPattern(size), InternalFailure);
71 }
72 
73 } // namespace estoraged_test
74