~aleteoryx/muditaos

ref: 4804015a7012e7bdf8d9b58d4b5ae2260efa34d1 muditaos/module-utils/rotator/tests/test_Rotator.cpp -rw-r--r-- 3.6 KiB
4804015a — Mateusz Piesta [MOS-496] Music player broken UI 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include <catch2/catch.hpp>

#include <rotator/Rotator.hpp>

#include <fstream>
#include <iostream>
#include <filesystem>

namespace
{
    constexpr std::size_t maxRotationFilesCount = 5;

    class HexRotator : public utils::Rotator<maxRotationFilesCount>
    {
      public:
        HexRotator() : utils::Rotator<maxRotationFilesCount>{".hex"}
        {}

        std::filesystem::path rotatedFilePath(const std::filesystem::path &source, int rotationCount)
        {
            return this->getRotatedFilePath(source, rotationCount);
        }
    };
} // namespace

TEST_CASE("Rotation of hex dump files")
{
    HexRotator rotator;
    constexpr auto fileName = "crashdump.hex";
    SECTION("Handle first dump")
    {
        REQUIRE(rotator.rotateFile(fileName));
    }
    SECTION("Handle first rotate")
    {
        constexpr auto firstRotationName = "crashdump.hex.1";
        std::ofstream file(fileName);
        REQUIRE(rotator.rotateFile(fileName));
        REQUIRE(std::filesystem::exists(firstRotationName));
        REQUIRE_FALSE(std::filesystem::exists(fileName));
        REQUIRE(std::filesystem::remove(firstRotationName));
    }
    SECTION("Handle max rotates")
    {
        for (std::size_t i = 1; i <= maxRotationFilesCount; i++) {
            const auto rotatedFileName = rotator.rotatedFilePath(fileName, i);
            std::ofstream file(fileName);
            REQUIRE(rotator.rotateFile(fileName));
            for (std::size_t j = i - 1; j > 0; j--) {
                REQUIRE(std::filesystem::exists(rotator.rotatedFilePath(fileName, j)));
            }
            if (i == maxRotationFilesCount) {
                REQUIRE_FALSE(std::filesystem::exists(rotator.rotatedFilePath(fileName, i)));
            }
            REQUIRE_FALSE(std::filesystem::exists(fileName));
        }
        for (std::size_t i = 1; i < maxRotationFilesCount; i++) {
            const auto rotatedFileName = rotator.rotatedFilePath(fileName, i);
            REQUIRE(std::filesystem::remove(rotatedFileName));
        }
    }

    SECTION("Handle more than max files")
    {
        for (std::size_t i = 1; i <= maxRotationFilesCount; i++) {
            const auto rotatedFileName = rotator.rotatedFilePath(fileName, i);
            std::ofstream file(fileName);
            REQUIRE(rotator.rotateFile(fileName));
            for (std::size_t j = i - 1; j > 0; j--) {
                REQUIRE(std::filesystem::exists(rotator.rotatedFilePath(fileName, j)));
            }
            if (i == maxRotationFilesCount) {
                REQUIRE_FALSE(std::filesystem::exists(rotator.rotatedFilePath(fileName, i)));
            }
            REQUIRE_FALSE(std::filesystem::exists(fileName));
        }
        std::ofstream file(fileName);
        REQUIRE(rotator.rotateFile(fileName));
        REQUIRE_FALSE(std::filesystem::exists(rotator.rotatedFilePath(fileName, maxRotationFilesCount + 1)));
        for (std::size_t i = 1; i < maxRotationFilesCount; i++) {
            const auto rotatedFileName = rotator.rotatedFilePath(fileName, i);
            REQUIRE(std::filesystem::exists(rotatedFileName));
        }
        REQUIRE_FALSE(std::filesystem::exists(rotator.rotatedFilePath(fileName, maxRotationFilesCount)));
        REQUIRE_FALSE(std::filesystem::exists(fileName));
        for (std::size_t i = 1; i < maxRotationFilesCount; i++) {
            const auto rotatedFileName = rotator.rotatedFilePath(fileName, i);
            REQUIRE(std::filesystem::remove(rotatedFileName));
        }
    }
}