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 using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
19 
20 TEST(pattern, patternPass)
21 {
22     std::string testFileName = "patternPass";
23     uint64_t size = 4096;
24     std::ofstream testFile;
25     testFile.open(testFileName,
26                   std::ios::out | std::ios::binary | std::ios::trunc);
27     testFile.close();
28 
29     Pattern pass(testFileName);
30     EXPECT_NO_THROW(pass.writePattern(size));
31     EXPECT_NO_THROW(pass.verifyPattern(size));
32 }
33 
34 /* This test that pattern writes the correct number of bytes even if
35  * size of the drive is not divisable by the block size
36  */
37 TEST(pattern, patternNotDivisible)
38 {
39     std::string testFileName = "notDivisible";
40     uint64_t size = 4097;
41     // 4097 is not divisible by the block size, and we expect no errors
42     std::ofstream testFile;
43     testFile.open(testFileName,
44                   std::ios::out | std::ios::binary | std::ios::trunc);
45     testFile.close();
46 
47     Pattern pass(testFileName);
48     EXPECT_NO_THROW(pass.writePattern(size));
49     EXPECT_NO_THROW(pass.verifyPattern(size));
50 }
51 
52 TEST(pattern, patternsDontMatch)
53 {
54     std::string testFileName = "patternsDontMatch";
55     uint64_t size = 4096;
56     std::ofstream testFile;
57 
58     Pattern pass(testFileName);
59 
60     int dummyValue = 88;
61     testFile.open(testFileName, std::ios::binary | std::ios::out);
62     testFile.write((const char*)&dummyValue, sizeof(dummyValue));
63     testFile.close();
64 
65     EXPECT_NO_THROW(pass.writePattern(size - sizeof(dummyValue)));
66     EXPECT_THROW(pass.verifyPattern(size), InternalFailure);
67 }
68