diff options
| author | Enzo BILLIS <enzo.billis@gmail.com> | 2026-11-23 17:00:38 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-11-23 10:00:38 -0600 |
| commit | a637671a940f9c66f296ce3a4ea12d614f894c88 (patch) | |
| tree | 5dbe9eb59f0e76d595d4d736d3ad4e6c496e0c1d /runtime/test/mods_test.cpp | |
| parent | b66e184ebe9e6c12f6fa60faf2cf32e042d8cd2a (diff) | |
mods: a mod.json with a UTF-8 byte-order mark loads; UTF-16 is named; contexts are bounded before the cast (#6724)
Squash-merged after diff review of PR #6724 - mods: a mod.json with a UTF-8 byte-order mark loads; UTF-16 is named; contexts are bounded before the cast
Diffstat (limited to 'runtime/test/mods_test.cpp')
| -rw-r--r-- | runtime/test/mods_test.cpp | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/runtime/test/mods_test.cpp b/runtime/test/mods_test.cpp index c61182c3c..cfbc6e694 100644 --- a/runtime/test/mods_test.cpp +++ b/runtime/test/mods_test.cpp | |||
| @@ -56,6 +56,36 @@ int main() { | |||
| 56 | check(!parse_mod("[1,2]", "m", mod, error), "a manifest that is not an object is refused"); | 56 | check(!parse_mod("[1,2]", "m", mod, error), "a manifest that is not an object is refused"); |
| 57 | check(parse_mod("{}", "folder_name", mod, error) && mod.name == "folder_name", "the name defaults to the folder"); | 57 | check(parse_mod("{}", "folder_name", mod, error) && mod.name == "folder_name", "the name defaults to the folder"); |
| 58 | 58 | ||
| 59 | // The encodings Windows editors and shells write. A UTF-8 byte-order mark is skipped; UTF-16 is named. | ||
| 60 | const std::string manifest = | ||
| 61 | R"({"name":"Mandy","archetype_swaps":[{"from":"player_story000_00","to":"cs_mandy"}]})"; | ||
| 62 | const std::string bom = "\xEF\xBB\xBF"; | ||
| 63 | check(parse_mod(bom + manifest, "m", mod, error) && mod.name == "Mandy" && mod.archetype_swaps.size() == 1 && | ||
| 64 | mod.archetype_swaps[0].to == 0xD7C0CDFFu, | ||
| 65 | "a manifest saved as UTF-8 with a byte-order mark parses"); | ||
| 66 | check(parse_mod(bom + "{\r\n \"name\": \"crlf\"\r\n}\r\n", "m", mod, error) && mod.name == "crlf", | ||
| 67 | "a byte-order mark before CRLF text parses"); | ||
| 68 | check(!parse_mod(bom + "[1,2]", "m", mod, error) && error == "mod.json is not a JSON object", | ||
| 69 | "a byte-order mark does not make a list a manifest"); | ||
| 70 | check(!parse_mod(bom, "m", mod, error) && !parse_mod(bom + bom + manifest, "m", mod, error), | ||
| 71 | "a byte-order mark alone, or two of them, is still refused"); | ||
| 72 | const auto utf16 = [&manifest](bool little_endian, bool mark) { | ||
| 73 | std::string out; | ||
| 74 | if (mark) out = little_endian ? std::string("\xFF\xFE", 2) : std::string("\xFE\xFF", 2); | ||
| 75 | for (const char c : manifest) { | ||
| 76 | if (!little_endian) out.push_back('\0'); | ||
| 77 | out.push_back(c); | ||
| 78 | if (little_endian) out.push_back('\0'); | ||
| 79 | } | ||
| 80 | return out; | ||
| 81 | }; | ||
| 82 | for (const std::string& text : {utf16(true, true), utf16(false, true), utf16(true, false), utf16(false, false)}) | ||
| 83 | check(!parse_mod(text, "m", mod, error) && error == "mod.json is UTF-16; save it as UTF-8", | ||
| 84 | "a UTF-16 manifest is refused by name, with or without a byte-order mark"); | ||
| 85 | check(!parse_mod("", "m", mod, error) && !parse_mod("{", "m", mod, error) && | ||
| 86 | error == "mod.json is not a JSON object", | ||
| 87 | "an empty or one-byte manifest is not mistaken for UTF-16"); | ||
| 88 | |||
| 59 | // "camera": first-person view (docs/modding.md, "First-person view") | 89 | // "camera": first-person view (docs/modding.md, "First-person view") |
| 60 | check(parse_mod(R"({"camera":{"view_mode":"first_person","first_person_value":4,"contexts":[0]}})", "fp", mod, | 90 | check(parse_mod(R"({"camera":{"view_mode":"first_person","first_person_value":4,"contexts":[0]}})", "fp", mod, |
| 61 | error) && mod.camera.set && mod.camera.first_person && mod.camera.first_person_value == 4 && | 91 | error) && mod.camera.set && mod.camera.first_person && mod.camera.first_person_value == 4 && |
| @@ -69,6 +99,25 @@ int main() { | |||
| 69 | "a fractional enum value is refused"); | 99 | "a fractional enum value is refused"); |
| 70 | check(!parse_mod(R"({"camera":{"view_mode":"first_person","contexts":"all"}})", "x", mod, error), | 100 | check(!parse_mod(R"({"camera":{"view_mode":"first_person","contexts":"all"}})", "x", mod, error), |
| 71 | "contexts must be a list"); | 101 | "contexts must be a list"); |
| 102 | // A context is a uint32: every whole value up to 4294967295 is taken, and nothing is cast before it is bounded. | ||
| 103 | check(parse_mod(R"({"camera":{"view_mode":"first_person","contexts":[2147483647,2147483648,4294967295]}})", "x", | ||
| 104 | mod, error) && mod.camera.contexts.size() == 3 && mod.camera.contexts[0] == 2147483647u && | ||
| 105 | mod.camera.contexts[1] == 2147483648u && mod.camera.contexts[2] == 4294967295u, | ||
| 106 | "contexts past INT_MAX are whole numbers too"); | ||
| 107 | for (const char* value : {"4294967296", "1e300", "-1", "1.5", "-0.5"}) { | ||
| 108 | const std::string text = std::string(R"({"camera":{"view_mode":"first_person","contexts":[)") + value + "]}}"; | ||
| 109 | check(!parse_mod(text, "x", mod, error) && | ||
| 110 | error.find("whole numbers from 0 to 4294967295") != std::string::npos, | ||
| 111 | "a context that is not a whole uint32 is refused"); | ||
| 112 | } | ||
| 113 | for (const char* value : {"256", "-1", "1e300", "4.5"}) { | ||
| 114 | const std::string text = | ||
| 115 | std::string(R"({"camera":{"view_mode":"first_person","first_person_value":)") + value + "}}"; | ||
| 116 | check(!parse_mod(text, "x", mod, error), "a first_person_value outside 0-255 is refused"); | ||
| 117 | } | ||
| 118 | check(parse_mod(R"({"camera":{"view_mode":"first_person","first_person_value":255}})", "x", mod, error) && | ||
| 119 | mod.camera.first_person_value == 255, | ||
| 120 | "255 is the largest first_person_value"); | ||
| 72 | 121 | ||
| 73 | Mod fp_on_foot, fp_everywhere, off; | 122 | Mod fp_on_foot, fp_everywhere, off; |
| 74 | check(parse_mod(R"({"camera":{"view_mode":"first_person","contexts":[0]}})", "a", fp_on_foot, error) && | 123 | check(parse_mod(R"({"camera":{"view_mode":"first_person","contexts":[0]}})", "a", fp_on_foot, error) && |