Skip to content

Commit c4979f7

Browse files
committed
Add boost tests for bech32 error detection
1 parent 02a7bde commit c4979f7

File tree

1 file changed

+53
-1
lines changed

1 file changed

+53
-1
lines changed

src/test/bech32_tests.cpp

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,36 @@ BOOST_AUTO_TEST_CASE(bech32_testvectors_invalid)
6868
"1qzzfhee",
6969
"a12UEL5L",
7070
"A12uEL5L",
71+
"abcdef1qpzrz9x8gf2tvdw0s3jn54khce6mua7lmqqqxw",
7172
};
73+
static const std::pair<std::string, int> ERRORS[] = {
74+
{"Invalid character or mixed case", 0},
75+
{"Invalid character or mixed case", 0},
76+
{"Invalid character or mixed case", 0},
77+
{"Bech32 string too long", 90},
78+
{"Missing separator", -1},
79+
{"Invalid separator position", 0},
80+
{"Invalid Base 32 character", 2},
81+
{"Invalid separator position", 2},
82+
{"Invalid character or mixed case", 8},
83+
{"Invalid checksum", -1}, // The checksum is calculated using the uppercase form so the entire string is invalid, not just a few characters
84+
{"Invalid separator position", 0},
85+
{"Invalid separator position", 0},
86+
{"Invalid character or mixed case", 3},
87+
{"Invalid character or mixed case", 3},
88+
{"Invalid checksum", 11}
89+
};
90+
int i = 0;
7291
for (const std::string& str : CASES) {
92+
const auto& err = ERRORS[i];
7393
const auto dec = bech32::Decode(str);
7494
BOOST_CHECK(dec.encoding == bech32::Encoding::INVALID);
95+
std::vector<int> error_locations;
96+
std::string error = bech32::LocateErrors(str, error_locations);
97+
BOOST_CHECK_EQUAL(err.first, error);
98+
if (err.second == -1) BOOST_CHECK(error_locations.empty());
99+
else BOOST_CHECK_EQUAL(err.second, error_locations[0]);
100+
i++;
75101
}
76102
}
77103

@@ -91,11 +117,37 @@ BOOST_AUTO_TEST_CASE(bech32m_testvectors_invalid)
91117
"au1s5cgom",
92118
"M1VUXWEZ",
93119
"16plkw9",
94-
"1p2gdwpf"
120+
"1p2gdwpf",
121+
"abcdef1l7aum6echk45nj2s0wdvt2fg8x9yrzpqzd3ryx",
122+
};
123+
static const std::pair<std::string, int> ERRORS[] = {
124+
{"Invalid character or mixed case", 0},
125+
{"Invalid character or mixed case", 0},
126+
{"Invalid character or mixed case", 0},
127+
{"Bech32 string too long", 90},
128+
{"Missing separator", -1},
129+
{"Invalid separator position", 0},
130+
{"Invalid Base 32 character", 2},
131+
{"Invalid Base 32 character", 3},
132+
{"Invalid separator position", 2},
133+
{"Invalid Base 32 character", 8},
134+
{"Invalid Base 32 character", 7},
135+
{"Invalid checksum", -1},
136+
{"Invalid separator position", 0},
137+
{"Invalid separator position", 0},
138+
{"Invalid checksum", 21},
95139
};
140+
int i = 0;
96141
for (const std::string& str : CASES) {
142+
const auto& err = ERRORS[i];
97143
const auto dec = bech32::Decode(str);
98144
BOOST_CHECK(dec.encoding == bech32::Encoding::INVALID);
145+
std::vector<int> error_locations;
146+
std::string error = bech32::LocateErrors(str, error_locations);
147+
BOOST_CHECK_EQUAL(err.first, error);
148+
if (err.second == -1) BOOST_CHECK(error_locations.empty());
149+
else BOOST_CHECK_EQUAL(err.second, error_locations[0]);
150+
i++;
99151
}
100152
}
101153

0 commit comments

Comments
 (0)