summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/tools
diff options
context:
space:
mode:
authorMarc Mutz <marc.mutz@qt.io>2023-02-22 08:27:55 +0100
committerMarc Mutz <marc.mutz@qt.io>2023-02-25 16:50:03 +0100
commit0411d98192ede84b115618ece876579fd79252f8 (patch)
tree915f241c2517d26dfd753f1b0b7cafae3c36074c /tests/auto/corelib/tools
parent10e9687159f9d1fa1eebe3fffd844b506d986c64 (diff)
QCryptographicHash: auto-calculate MaxHashLength
Add Algorithm::NumAlgorithms and use it to iterate over all statically-available algorithms, querying their hashLengthInternal(). This avoids having to statically_assert(<= MaxHashLength) everywhere, and auto-adjusts the buffer size in SHA1_ONLY builds. Yes, the extra case labels for NumAlgorithms are a nuisance, but at least the compiler will remind us when we forget, unlike a missing static_cast(<= MaxHashLength) that might easily be forgotten. Adjust the test (which iterates over the QMetaEnum for QCryptographicHash::Algorithm, so finds NumAlgorithms and tries to pass it to the hash() function which responds with a Q_UNREACHABLE(). Only test hashLength() == 0 for that enum value. Pick-to: 6.5 Change-Id: I70155d2460464f0b2094e136eb6bea185effc9d5 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'tests/auto/corelib/tools')
-rw-r--r--tests/auto/corelib/tools/qcryptographichash/tst_qcryptographichash.cpp11
1 files changed, 9 insertions, 2 deletions
diff --git a/tests/auto/corelib/tools/qcryptographichash/tst_qcryptographichash.cpp b/tests/auto/corelib/tools/qcryptographichash/tst_qcryptographichash.cpp
index 00f7afc876..4ac176fea1 100644
--- a/tests/auto/corelib/tools/qcryptographichash/tst_qcryptographichash.cpp
+++ b/tests/auto/corelib/tools/qcryptographichash/tst_qcryptographichash.cpp
@@ -401,8 +401,15 @@ void tst_QCryptographicHash::hashLength()
{
QFETCH(const QCryptographicHash::Algorithm, algorithm);
- QByteArray output = QCryptographicHash::hash("test", algorithm);
- QCOMPARE(QCryptographicHash::hashLength(algorithm), output.size());
+ qsizetype expectedSize;
+ if (algorithm == QCryptographicHash::NumAlgorithms) {
+ // It's UB to call ::hash() with NumAlgorithms, but hashLength() is
+ // fine and returns 0 for invalid values:
+ expectedSize = 0;
+ } else {
+ expectedSize = QCryptographicHash::hash("test", algorithm).size();
+ }
+ QCOMPARE(QCryptographicHash::hashLength(algorithm), expectedSize);
}
void tst_QCryptographicHash::move()