Repository navigation
Expand file tree
/
Copy pathtest_password.cpp
More file actions
69 lines (61 loc) · 3.14 KB
/
Copy pathtest_password.cpp
File metadata and controls
69 lines (61 loc) · 3.14 KB
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
// Transcription test for the level password system (0xB220 / 0xB2A7 / 0x97DC).
//
// The LCG (state*0x24A1 + 0x24DF) & 0x7FFF is a bijection on 15-bit words
// (odd multiplier), so every deity-rank position 0,5,..,0x1388 has a unique
// password word and a successful parse can only return the original rank.
// Some composed passwords do NOT re-parse: the parser's first-full-match
// syllable split is greedy in table order (e.g. "EA" is tried before "E", so
// s1="E" + s2="AL.." misparses) — the original binary has the exact same
// behaviour, so those must fail identically here, never succeed wrongly.
#include <cstdio>
#include <cstring>
#include "password.h"
static int fails = 0;
#define CHECK(cond, ...) do { if (!(cond)) { ++fails; \
std::printf("FAIL %s:%d: ", __FILE__, __LINE__); \
std::printf(__VA_ARGS__); std::printf("\n"); } } while (0)
int main() {
using namespace pop;
// LCG spot values (0x97DC: state*0x24A1+0x24DF, 15-bit)
CHECK(name_rng(0) == 0x24DF, "name_rng(0)=%04X", name_rng(0));
CHECK(name_rng(5) == 0x5C04, "name_rng(5)=%04X", name_rng(5));
// generator 0xB220: syllable split of the word, bits 0..4/5..9/10..14
CHECK(password_compose(0) == "RINGOUTHILL", "compose(0)=%s",
password_compose(0).c_str());
CHECK(password_compose(0x5C04) == "HURTOUTORD", "compose(5C04)=%s",
password_compose(0x5C04).c_str());
// battle names (0x10959): rank 0 is GENESIS, others one LCG step
CHECK(battle_name(0) == "GENESIS", "battle_name(0)=%s",
battle_name(0).c_str());
CHECK(battle_name(5) == "HURTOUTORD", "battle_name(5)=%s",
battle_name(5).c_str());
// parser error codes (0xB2E9/0xB331/0xB37F/0xB39E/0xB3CD)
CHECK(password_parse("GENESIS") == -1, "GENESIS is handled by the caller "
"(0xF3FB strcmp), the parser rejects it: %d",
password_parse("GENESIS"));
CHECK(password_parse("") == -1, "empty: %d", password_parse(""));
CHECK(password_parse("RINGXXX") == -2, "no s1: %d",
password_parse("RINGXXX"));
CHECK(password_parse("RINGOUTXXX") == -3, "no s2: %d",
password_parse("RINGOUTXXX"));
CHECK(password_parse("RINGOUTHILLX") == -4, "trailing junk: %d",
password_parse("RINGOUTHILLX"));
// round trip over every deity-rank position (0xB3B0 search domain)
int unparseable = 0;
for (int rank = 0; rank <= 0x1388; rank += 5) {
std::string pw = password_compose(name_rng((uint16_t)rank));
int back = password_parse(pw.c_str());
if (back >= 0)
CHECK(back == rank, "parse(%s) = %d, want %d", pw.c_str(), back, rank);
else
++unparseable; // greedy-split casualty, faithful to the original
}
// the syllable tables make the vast majority of positions reachable
CHECK(unparseable < 40, "%d of 1001 passwords unparseable", unparseable);
std::printf("password round-trip: %d/1001 positions re-parse, %d never do "
"(greedy split, as in the original)\n",
1001 - unparseable, unparseable);
if (fails) { std::printf("%d FAILURES\n", fails); return 1; }
std::printf("password: all checks passed\n");
return 0;
}