Skip to content

Commit 92f0caf

Browse files
committed
Improve Bech32 boost tests
1 parent bb4d3e9 commit 92f0caf

File tree

1 file changed

+42
-36
lines changed

1 file changed

+42
-36
lines changed

src/test/bech32_tests.cpp

Lines changed: 42 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -70,24 +70,28 @@ BOOST_AUTO_TEST_CASE(bech32_testvectors_invalid)
7070
"a12UEL5L",
7171
"A12uEL5L",
7272
"abcdef1qpzrz9x8gf2tvdw0s3jn54khce6mua7lmqqqxw",
73+
"test1zg69w7y6hn0aqy352euf40x77qddq3dc",
7374
};
74-
static const std::pair<std::string, int> ERRORS[] = {
75-
{"Invalid character or mixed case", 0},
76-
{"Invalid character or mixed case", 0},
77-
{"Invalid character or mixed case", 0},
78-
{"Bech32 string too long", 90},
79-
{"Missing separator", -1},
80-
{"Invalid separator position", 0},
81-
{"Invalid Base 32 character", 2},
82-
{"Invalid separator position", 2},
83-
{"Invalid character or mixed case", 8},
84-
{"Invalid checksum", -1}, // The checksum is calculated using the uppercase form so the entire string is invalid, not just a few characters
85-
{"Invalid separator position", 0},
86-
{"Invalid separator position", 0},
87-
{"Invalid character or mixed case", 3},
88-
{"Invalid character or mixed case", 3},
89-
{"Invalid checksum", 11}
75+
static const std::pair<std::string, std::vector<int>> ERRORS[] = {
76+
{"Invalid character or mixed case", {0}},
77+
{"Invalid character or mixed case", {0}},
78+
{"Invalid character or mixed case", {0}},
79+
{"Bech32 string too long", {90}},
80+
{"Missing separator", {}},
81+
{"Invalid separator position", {0}},
82+
{"Invalid Base 32 character", {2}},
83+
{"Invalid separator position", {2}},
84+
{"Invalid character or mixed case", {8}},
85+
{"Invalid checksum", {}}, // The checksum is calculated using the uppercase form so the entire string is invalid, not just a few characters
86+
{"Invalid separator position", {0}},
87+
{"Invalid separator position", {0}},
88+
{"Invalid character or mixed case", {3, 4, 5, 7}},
89+
{"Invalid character or mixed case", {3}},
90+
{"Invalid checksum", {11}},
91+
{"Invalid checksum", {9, 16}},
9092
};
93+
static_assert(std::size(CASES) == std::size(ERRORS), "Bech32 CASES and ERRORS should have the same length");
94+
9195
int i = 0;
9296
for (const std::string& str : CASES) {
9397
const auto& err = ERRORS[i];
@@ -96,8 +100,7 @@ BOOST_AUTO_TEST_CASE(bech32_testvectors_invalid)
96100
std::vector<int> error_locations;
97101
std::string error = bech32::LocateErrors(str, error_locations);
98102
BOOST_CHECK_EQUAL(err.first, error);
99-
if (err.second == -1) BOOST_CHECK(error_locations.empty());
100-
else BOOST_CHECK_EQUAL(err.second, error_locations[0]);
103+
BOOST_CHECK(err.second == error_locations);
101104
i++;
102105
}
103106
}
@@ -120,24 +123,28 @@ BOOST_AUTO_TEST_CASE(bech32m_testvectors_invalid)
120123
"16plkw9",
121124
"1p2gdwpf",
122125
"abcdef1l7aum6echk45nj2s0wdvt2fg8x9yrzpqzd3ryx",
126+
"test1zg69v7y60n00qy352euf40x77qcusag6",
123127
};
124-
static const std::pair<std::string, int> ERRORS[] = {
125-
{"Invalid character or mixed case", 0},
126-
{"Invalid character or mixed case", 0},
127-
{"Invalid character or mixed case", 0},
128-
{"Bech32 string too long", 90},
129-
{"Missing separator", -1},
130-
{"Invalid separator position", 0},
131-
{"Invalid Base 32 character", 2},
132-
{"Invalid Base 32 character", 3},
133-
{"Invalid separator position", 2},
134-
{"Invalid Base 32 character", 8},
135-
{"Invalid Base 32 character", 7},
136-
{"Invalid checksum", -1},
137-
{"Invalid separator position", 0},
138-
{"Invalid separator position", 0},
139-
{"Invalid checksum", 21},
128+
static const std::pair<std::string, std::vector<int>> ERRORS[] = {
129+
{"Invalid character or mixed case", {0}},
130+
{"Invalid character or mixed case", {0}},
131+
{"Invalid character or mixed case", {0}},
132+
{"Bech32 string too long", {90}},
133+
{"Missing separator", {}},
134+
{"Invalid separator position", {0}},
135+
{"Invalid Base 32 character", {2}},
136+
{"Invalid Base 32 character", {3}},
137+
{"Invalid separator position", {2}},
138+
{"Invalid Base 32 character", {8}},
139+
{"Invalid Base 32 character", {7}},
140+
{"Invalid checksum", {}},
141+
{"Invalid separator position", {0}},
142+
{"Invalid separator position", {0}},
143+
{"Invalid checksum", {21}},
144+
{"Invalid checksum", {13, 32}},
140145
};
146+
static_assert(std::size(CASES) == std::size(ERRORS), "Bech32m CASES and ERRORS should have the same length");
147+
141148
int i = 0;
142149
for (const std::string& str : CASES) {
143150
const auto& err = ERRORS[i];
@@ -146,8 +153,7 @@ BOOST_AUTO_TEST_CASE(bech32m_testvectors_invalid)
146153
std::vector<int> error_locations;
147154
std::string error = bech32::LocateErrors(str, error_locations);
148155
BOOST_CHECK_EQUAL(err.first, error);
149-
if (err.second == -1) BOOST_CHECK(error_locations.empty());
150-
else BOOST_CHECK_EQUAL(err.second, error_locations[0]);
156+
BOOST_CHECK(err.second == error_locations);
151157
i++;
152158
}
153159
}

0 commit comments

Comments
 (0)