blob: 98eb374c18f937c238a11ddad311a23c8562c2f2 [file] [log] [blame]
John Edward Broadbent7f2ab642021-11-11 21:00:38 -08001#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
John Edward Broadbent69786762022-01-21 14:16:23 -080011#include <fstream>
John Edward Broadbent7f2ab642021-11-11 21:00:38 -080012#include <system_error>
13
14#include <gmock/gmock-matchers.h>
15#include <gmock/gmock.h>
16#include <gtest/gtest.h>
17
John Edward Broadbentd3bfa7b2022-01-13 17:41:32 -080018namespace estoraged_test
19{
20
21using estoraged::Pattern;
John Edward Broadbent7f2ab642021-11-11 21:00:38 -080022using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
John Edward Broadbent7f2ab642021-11-11 21:00:38 -080023
24TEST(pattern, patternPass)
25{
John Edward Broadbent69786762022-01-21 14:16:23 -080026 std::string testFileName = "patternPass";
John Edward Broadbent7f2ab642021-11-11 21:00:38 -080027 uint64_t size = 4096;
John Edward Broadbent69786762022-01-21 14:16:23 -080028 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));
John Edward Broadbent7f2ab642021-11-11 21:00:38 -080036}
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 */
John Edward Broadbent69786762022-01-21 14:16:23 -080041TEST(pattern, patternNotDivisible)
John Edward Broadbent7f2ab642021-11-11 21:00:38 -080042{
John Edward Broadbent69786762022-01-21 14:16:23 -080043 std::string testFileName = "notDivisible";
John Edward Broadbent7f2ab642021-11-11 21:00:38 -080044 uint64_t size = 4097;
45 // 4097 is not divisible by the block size, and we expect no errors
John Edward Broadbent69786762022-01-21 14:16:23 -080046 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));
John Edward Broadbent7f2ab642021-11-11 21:00:38 -080054}
55
56TEST(pattern, patternsDontMatch)
57{
John Edward Broadbent69786762022-01-21 14:16:23 -080058 std::string testFileName = "patternsDontMatch";
John Edward Broadbent7f2ab642021-11-11 21:00:38 -080059 uint64_t size = 4096;
John Edward Broadbent69786762022-01-21 14:16:23 -080060 std::ofstream testFile;
61
62 Pattern pass(testFileName);
63
John Edward Broadbent7f2ab642021-11-11 21:00:38 -080064 int dummyValue = 88;
John Edward Broadbent69786762022-01-21 14:16:23 -080065 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);
John Edward Broadbent7f2ab642021-11-11 21:00:38 -080071}
John Edward Broadbentd3bfa7b2022-01-13 17:41:32 -080072
73} // namespace estoraged_test