~nabijaczleweli/klapki

ref: 0996052d7f8fc0c19dcb1b92025c179bdc9237e3 klapki/test/state_parse.cpp -rw-r--r-- 7.3 KiB
0996052dнаб Add screenshot and explanation to README 4 months 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// The MIT License (MIT)

// Copyright (c) 2020 наб <nabijaczleweli@nabijaczleweli.xyz>

// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


#include "state.hpp"
#include "test-util.hpp"
#include <catch.hpp>
#include <fmt/format.h>
#include <unordered_map>


static std::unordered_map<const char *, klapki::state::stated_config> states = {
    {"just-bootplace", klapki::state::stated_config{0x0a01, {}, {}}},
    {"just-kbase",
     klapki::state::stated_config{0x0b02,
                                  {},
                                  {{0x0102,
                                    {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13},
                                    "5.8.0-1-amd64",
                                    "",
                                    "/boot/",
                                    {0x13, 0x12, 0x11, 0x10, 0x0F, 0x0E, 0x0D, 0x0C, 0x0B, 0x0A, 0x09, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00},
                                    {}}}}},
    {"one-initrd",
     klapki::state::stated_config{
         0x0b02,
         {"test"},
         {{0x0102,
           {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13},
           "5.8.0-1-amd64",
           "debug",
           "/boot/",
           {0x13, 0x12, 0x11, 0x10, 0x0F, 0x0E, 0x0D, 0x0C, 0x0B, 0x0A, 0x09, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00},
           {{"/boot/initrds", {0x13, 0x12, 0x11, 0x10, 0x0F, 0x0E, 0x0D, 0x0C, 0x0B, 0x0A, 0x09, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00}}}}}}},
    {"one-initrd-copy+two-initrds",
     klapki::state::stated_config{
         0x0b02,
         {"test", "2st"},
         {{0x0102,
           {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13},
           "5.8.0-1-amd64",
           "loud",
           "/boot/",
           {0x13, 0x12, 0x11, 0x10, 0x0F, 0x0E, 0x0D, 0x0C, 0x0B, 0x0A, 0x09, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00},
           {{{}, {0x13, 0x12, 0x11, 0x10, 0x0F, 0x0E, 0x0D, 0x0C, 0x0B, 0x0A, 0x09, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00}}}},
          {0x0102,
           {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13},
           "5.8.0-1-amd64",
           "quiet",
           "/boot/",
           {0x13, 0x12, 0x11, 0x10, 0x0F, 0x0E, 0x0D, 0x0C, 0x0B, 0x0A, 0x09, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00},
           {
               {"/boot/initrds", {0x13, 0x12, 0x11, 0x10, 0x0F, 0x0E, 0x0D, 0x0C, 0x0B, 0x0A, 0x09, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00}},
               {"/boot", {0x13, 0x12, 0x11, 0x10, 0x0F, 0x0E, 0x0D, 0x0C, 0x0B, 0x0A, 0x09, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00}},
           }}}}},
    {"exempli-gratia",
     klapki::state::stated_config{
         0x0020,
         {"owo"},
         {{0x000B,
           {0x7C, 0x72, 0x2D, 0xF4, 0x1B, 0x78, 0xEF, 0xC7, 0xC4, 0x07, 0x20, 0xBA, 0x67, 0x54, 0xFC, 0x36, 0x6D, 0x8F, 0xE5, 0x39},
           "5.8.0-1-amd64",
           "",
           "/boot",
           {0x13, 0x12, 0x11, 0x10, 0x0F, 0x0E, 0x0D, 0x0C, 0x0B, 0x0A, 0x09, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00},
           {{{}, {0x13, 0x12, 0x11, 0x10, 0x0F, 0x0E, 0x0D, 0x0C, 0x0B, 0x0A, 0x09, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00}}}},
          {0x000D,
           {0xB0, 0x7C, 0x71, 0x35, 0x33, 0xEC, 0x0F, 0x2E, 0x21, 0x3C, 0xBF, 0xA2, 0x46, 0x1F, 0xD7, 0x25, 0xD5, 0x5D, 0xFE, 0xD4},
           "5.8.0-1-amd64",
           "owo",
           "/boot",
           {0x13, 0x12, 0x11, 0x10, 0x0F, 0x0E, 0x0D, 0x0C, 0x0B, 0x0A, 0x09, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00},
           {{{}, {0x13, 0x12, 0x11, 0x10, 0x0F, 0x0E, 0x0D, 0x0C, 0x0B, 0x0A, 0x09, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00}}}}}}},
};


TEST_CASE("klapki::state::stated_config::parse() ENODATA", "[klapki::state::stated_config::parse]") {
	klapki::state::stated_config clean{};
	clean.boot_position = __LINE__;
	auto cclean         = clean;

	klapki::state::stated_config::parse(clean, nullptr, 0);
	REQUIRE(clean == cclean);

	klapki::state::stated_config::parse(clean, nullptr, 1);
	REQUIRE(clean == cclean);
}

TEST_CASE("klapki::state::stated_config::parse() okay", "[klapki::state::stated_config::parse]") {
	for(auto && [name, cfg] : states)
		SECTION(name) {
			const auto indata = klapki::test::read_file(std::string{"test-data/state_parse/"} + name);

			klapki::state::stated_config incfg{};
			klapki::state::stated_config::parse(incfg, indata.data(), indata.size());
			// for(auto && ent : incfg.wanted_entries)
			// 	fmt::print(stderr,
			// 	           "\tbootnum_hint: {}\n"
			// 	           "\tload_option_sha: {}\n"
			// 	           "\tversion: {}\n"
			// 	           "\tkernel_dirname: {}\n"
			// 	           "\tinitrd_dirnames: [{}]\n",
			// 	           ent.bootnum_hint, std::string{(char *)ent.load_option_sha, sizeof(ent.load_option_sha)}, ent.version, ent.kernel_dirname,
			// 	           ent.initrd_dirnames.size());
			// REQUIRE(incfg.boot_position == cfg.boot_position);
			// REQUIRE(incfg.wanted_entries.size() == cfg.wanted_entries.size());
			// for(std::size_t i = 0; i < incfg.wanted_entries.size(); ++i) {
			// 	REQUIRE(incfg.wanted_entries[i].bootnum_hint == cfg.wanted_entries[i].bootnum_hint);
			// 	// REQUIRE(incfg.wanted_entries[i].load_option_sha == cfg.wanted_entries[i].load_option_sha);
			// 	REQUIRE(incfg.wanted_entries[i].version == cfg.wanted_entries[i].version);
			// 	REQUIRE(incfg.wanted_entries[i].kernel_dirname == cfg.wanted_entries[i].kernel_dirname);
			// 	REQUIRE(incfg.wanted_entries[i].initrd_dirnames.size() == cfg.wanted_entries[i].initrd_dirnames.size());
			// 	for(std::size_t n = 0; n < incfg.wanted_entries[i].initrd_dirnames.size(); ++n) {
			// 		fmt::print(stderr, "{}\n", n);
			// 		fmt::print(stderr, "{}\n", incfg.wanted_entries[i].initrd_dirnames[n].value_or("<blegh>"));
			// 		fmt::print(stderr, "{}\n", cfg.wanted_entries[i].initrd_dirnames[n].value_or("<blegh>"));
			// 		REQUIRE(incfg.wanted_entries[i].initrd_dirnames[n] == cfg.wanted_entries[i].initrd_dirnames[n]);
			// 	}
			// }
			// REQUIRE(incfg.wanted_entries == cfg.wanted_entries);
			REQUIRE(incfg == cfg);
		}
}