diff --git a/.github/scripts/levelization/results/ordering.txt b/.github/scripts/levelization/results/ordering.txt index d4d07eee94..747ea8b6a6 100644 --- a/.github/scripts/levelization/results/ordering.txt +++ b/.github/scripts/levelization/results/ordering.txt @@ -134,6 +134,7 @@ test.peerfinder > xrpld.core test.peerfinder > xrpld.peerfinder test.peerfinder > xrpl.protocol test.protocol > test.toplevel +test.protocol > test.unit_test test.protocol > xrpl.basics test.protocol > xrpl.json test.protocol > xrpl.protocol @@ -171,6 +172,7 @@ test.shamap > xrpl.shamap test.toplevel > test.csf test.toplevel > xrpl.json test.unit_test > xrpl.basics +test.unit_test > xrpl.protocol tests.libxrpl > xrpl.basics tests.libxrpl > xrpl.json tests.libxrpl > xrpl.net diff --git a/include/xrpl/protocol/SecretKey.h b/include/xrpl/protocol/SecretKey.h index a59b1150c1..550ccf75bc 100644 --- a/include/xrpl/protocol/SecretKey.h +++ b/include/xrpl/protocol/SecretKey.h @@ -16,8 +16,11 @@ namespace xrpl { /** A secret key. */ class SecretKey { +public: + static constexpr std::size_t size_ = 32; + private: - std::uint8_t buf_[32]; + std::uint8_t buf_[size_]; public: using const_iterator = std::uint8_t const*; @@ -27,9 +30,14 @@ public: SecretKey& operator=(SecretKey const&) = default; + bool + operator==(SecretKey const&) = delete; + bool + operator!=(SecretKey const&) = delete; + ~SecretKey(); - SecretKey(std::array const& data); + SecretKey(std::array const& data); SecretKey(Slice const& slice); std::uint8_t const* @@ -78,16 +86,10 @@ public: }; inline bool -operator==(SecretKey const& lhs, SecretKey const& rhs) -{ - return lhs.size() == rhs.size() && std::memcmp(lhs.data(), rhs.data(), rhs.size()) == 0; -} +operator==(SecretKey const& lhs, SecretKey const& rhs) = delete; inline bool -operator!=(SecretKey const& lhs, SecretKey const& rhs) -{ - return !(lhs == rhs); -} +operator!=(SecretKey const& lhs, SecretKey const& rhs) = delete; //------------------------------------------------------------------------------ diff --git a/src/test/app/Manifest_test.cpp b/src/test/app/Manifest_test.cpp index d9ecf89b26..45e5067403 100644 --- a/src/test/app/Manifest_test.cpp +++ b/src/test/app/Manifest_test.cpp @@ -1,4 +1,5 @@ #include +#include #include @@ -446,7 +447,7 @@ public: auto const token = loadValidatorToken(tokenBlob); BEAST_EXPECT(token); - BEAST_EXPECT(token->validationSecret == *valSecret); + BEAST_EXPECT(test::equal(token->validationSecret, *valSecret)); BEAST_EXPECT(token->manifest == manifest); } { diff --git a/src/test/app/ValidatorKeys_test.cpp b/src/test/app/ValidatorKeys_test.cpp index 925c02131b..8088577972 100644 --- a/src/test/app/ValidatorKeys_test.cpp +++ b/src/test/app/ValidatorKeys_test.cpp @@ -1,4 +1,5 @@ #include +#include #include #include @@ -92,7 +93,7 @@ public: if (BEAST_EXPECT(k.keys)) { BEAST_EXPECT(k.keys->publicKey == seedPublicKey); - BEAST_EXPECT(k.keys->secretKey == seedSecretKey); + BEAST_EXPECT(test::equal(k.keys->secretKey, seedSecretKey)); } BEAST_EXPECT(k.nodeID == seedNodeID); BEAST_EXPECT(k.manifest.empty()); @@ -119,7 +120,7 @@ public: if (BEAST_EXPECT(k.keys)) { BEAST_EXPECT(k.keys->publicKey == tokenPublicKey); - BEAST_EXPECT(k.keys->secretKey == tokenSecretKey); + BEAST_EXPECT(test::equal(k.keys->secretKey, tokenSecretKey)); } BEAST_EXPECT(k.nodeID == tokenNodeID); BEAST_EXPECT(k.manifest == tokenManifest); diff --git a/src/test/protocol/SecretKey_test.cpp b/src/test/protocol/SecretKey_test.cpp index de21c7d88b..86ce3bcbe9 100644 --- a/src/test/protocol/SecretKey_test.cpp +++ b/src/test/protocol/SecretKey_test.cpp @@ -1,3 +1,5 @@ +#include + #include #include #include @@ -183,7 +185,7 @@ public: TokenType::NodePrivate, "pnen77YEeUd4fFKG7iycBWcwKpTaeFRkW2WFostaATy1DSupwXe"); BEAST_EXPECT(sk2); - BEAST_EXPECT(sk1 == *sk2); + BEAST_EXPECT(test::equal(sk1, *sk2)); } { @@ -193,7 +195,7 @@ public: TokenType::NodePrivate, "paKv46LztLqK3GaKz1rG2nQGN6M4JLyRtxFBYFTw4wAVHtGys36"); BEAST_EXPECT(sk2); - BEAST_EXPECT(sk1 == *sk2); + BEAST_EXPECT(test::equal(sk1, *sk2)); } // Try converting short, long and malformed data @@ -261,20 +263,20 @@ public: BEAST_EXPECT(!si.empty()); auto const ski = parseBase58(TokenType::NodePrivate, si); - BEAST_EXPECT(ski && keys[i] == *ski); + BEAST_EXPECT(ski && test::equal(keys[i], *ski)); for (std::size_t j = i; j != keys.size(); ++j) { - BEAST_EXPECT((keys[i] == keys[j]) == (i == j)); + BEAST_EXPECT(test::equal(keys[i], keys[j]) == (i == j)); auto const sj = toBase58(TokenType::NodePrivate, keys[j]); BEAST_EXPECT((si == sj) == (i == j)); auto const skj = parseBase58(TokenType::NodePrivate, sj); - BEAST_EXPECT(skj && keys[j] == *skj); + BEAST_EXPECT(skj && test::equal(keys[j], *skj)); - BEAST_EXPECT((*ski == *skj) == (i == j)); + BEAST_EXPECT(test::equal(*ski, *skj) == (i == j)); } } } @@ -292,7 +294,7 @@ public: auto kp = generateKeyPair(KeyType::secp256k1, Seed{makeSlice(test.seed)}); BEAST_EXPECT(kp.first == PublicKey{makeSlice(test.pubkey)}); - BEAST_EXPECT(kp.second == SecretKey{makeSlice(test.seckey)}); + BEAST_EXPECT(test::equal(kp.second, SecretKey{makeSlice(test.seckey)})); BEAST_EXPECT(calcAccountID(kp.first) == *id); } } @@ -310,7 +312,7 @@ public: auto kp = generateKeyPair(KeyType::ed25519, Seed{makeSlice(test.seed)}); BEAST_EXPECT(kp.first == PublicKey{makeSlice(test.pubkey)}); - BEAST_EXPECT(kp.second == SecretKey{makeSlice(test.seckey)}); + BEAST_EXPECT(test::equal(kp.second, SecretKey{makeSlice(test.seckey)})); BEAST_EXPECT(calcAccountID(kp.first) == *id); } } diff --git a/src/test/unit_test/utils.h b/src/test/unit_test/utils.h new file mode 100644 index 0000000000..1f6ee58436 --- /dev/null +++ b/src/test/unit_test/utils.h @@ -0,0 +1,19 @@ +#include + +#include + +namespace xrpl { +namespace test { + +/// Compare two SecretKey objects for equality. +/// SecretKey::operator== is deleted, so a named function is used +/// to avoid member-function lookup shadowing free-function overloads. +inline bool +equal(SecretKey const& lhs, SecretKey const& rhs) +{ + return lhs.size() == SecretKey::size_ && rhs.size() == SecretKey::size_ && + std::memcmp(lhs.data(), rhs.data(), SecretKey::size_) == 0; +} + +} // namespace test +} // namespace xrpl