okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Léon Avic Simmonsexpert profile: refuse a header version other than 1, naming both numbers and the file536db01af

main
3.4 KiB73 linesraw
1// read_expert_profile refuses a profile whose header version is not 1, naming the version it found, the one it
2// reads, and the file - before the geometry check, since the fields after the version are only known to be a
3// version 1 layout.  A version 1 file still loads.  CPU only: no device is touched.
4#include "strata/core/expert_cache.hpp"
5
6#include <cstdint>
7#include <cstdio>
8#include <filesystem>
9#include <string>
10#include <utility>
11#include <vector>
12
13namespace {
14int fails = 0;
15void check(bool ok, const char* what) {
16    if (!ok) {
17        std::fprintf(stderr, "FAIL: %s\n", what);
18        ++fails;
19    }
20}
21// overwrite the little-endian uint32 `version` field at byte 4 (after "STRP") in place
22bool set_version(const std::string& path, uint32_t v) {
23    std::FILE* f = std::fopen(path.c_str(), "r+b");
24    if (f == nullptr) return false;
25    uint8_t le[4];
26    for (int b = 0; b < 4; ++b) le[b] = (uint8_t) (v >> (8 * b));
27    const bool ok = std::fseek(f, 4, SEEK_SET) == 0 && std::fwrite(le, 1, 4, f) == 4;
28    return (std::fclose(f) == 0) && ok;
29}
30bool has(const std::string& s, const std::string& what) { return s.find(what) != std::string::npos; }
31}  // namespace
32
33int main() {
34    using Pair = std::pair<int32_t, int32_t>;
35    const int64_t L = 2, E = 3;
36    const std::vector<Pair> ranked = {{1, 2}, {0, 0}, {1, 0}};
37    const std::filesystem::path dir = std::filesystem::temp_directory_path() / "strata_profile_version_test";
38    std::filesystem::create_directories(dir);
39    const std::string path = (dir / "profile.bin").string();
40    std::string err;
41    std::vector<Pair> back;
42    int64_t slots = 0;
43
44    // version 1, as --expert-profile-save and tools/make_profile.py write it: loads
45    check(strata::core::write_expert_profile(path, L, E, ranked, err), "written");
46    check(strata::core::read_expert_profile(path, L, E, back, slots, err) && back == ranked && slots == 3,
47          "a version 1 profile loads");
48
49    // the same bytes with the version field at 2: refused, naming 2, 1 and the file
50    check(set_version(path, 2), "version field rewritten to 2");
51    err.clear();
52    check(!strata::core::read_expert_profile(path, L, E, back, slots, err), "a version 2 profile is refused");
53    check(has(err, "read_expert_profile: ") && has(err, path), "the refusal names the file");
54    check(has(err, "version 2") && has(err, "version 1"), "the refusal names the version found and the one read");
55
56    // version 0 (a zeroed header) is not version 1 either
57    check(set_version(path, 0), "version field rewritten to 0");
58    check(!strata::core::read_expert_profile(path, L, E, back, slots, err) && has(err, "version 0"),
59          "a version 0 profile is refused by its number");
60
61    // the version is checked before the geometry: a version 2 file for another model is refused as version 2
62    check(set_version(path, 2), "version field rewritten to 2 again");
63    check(!strata::core::read_expert_profile(path, L + 1, E, back, slots, err) && has(err, "version 2"),
64          "a version 2 profile of another geometry is refused by its version, not its geometry");
65
66    // and back to 1: it loads again, so the refusal was the version field and nothing else
67    check(set_version(path, 1), "version field rewritten to 1");
68    check(strata::core::read_expert_profile(path, L, E, back, slots, err) && back == ranked, "version 1 loads again");
69
70    std::filesystem::remove_all(dir);
71    if (fails == 0) std::puts("expert_profile_version_test: OK");
72    return fails == 0 ? 0 : 1;
73}