Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/keepassxreboot/keepassxc.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'tests/TestYkChallengeResponseKey.cpp')
-rw-r--r--tests/TestYkChallengeResponseKey.cpp106
1 files changed, 49 insertions, 57 deletions
diff --git a/tests/TestYkChallengeResponseKey.cpp b/tests/TestYkChallengeResponseKey.cpp
index a4dd76270..05161544b 100644
--- a/tests/TestYkChallengeResponseKey.cpp
+++ b/tests/TestYkChallengeResponseKey.cpp
@@ -19,82 +19,74 @@
#include "TestYkChallengeResponseKey.h"
#include "TestGlobal.h"
+
+#include "core/Tools.h"
#include "crypto/Crypto.h"
+#include "keys/YkChallengeResponseKey.h"
-#include <QtConcurrentRun>
+#include <QScopedPointer>
+#include <QSignalSpy>
-QTEST_GUILESS_MAIN(TestYubiKeyChalResp)
+QTEST_GUILESS_MAIN(TestYubiKeyChallengeResponse)
-void TestYubiKeyChalResp::initTestCase()
+void TestYubiKeyChallengeResponse::initTestCase()
{
// crypto subsystem needs to be initialized for YubiKey testing
QVERIFY(Crypto::init());
-}
-void TestYubiKeyChalResp::init()
-{
- if (!YubiKey::instance()->init()) {
- QSKIP("Unable to connect to YubiKey");
+ if (!YubiKey::instance()->isInitialized()) {
+ QSKIP("Unable to initialize YubiKey interface.");
}
}
-void TestYubiKeyChalResp::detectDevices()
-{
- connect(YubiKey::instance(), SIGNAL(detected(int, bool)), SLOT(ykDetected(int, bool)), Qt::QueuedConnection);
- QtConcurrent::run(YubiKey::instance(), &YubiKey::detect);
-
- // need to wait for the hardware (that's hopefully plugged in)...
- QTest::qWait(2000);
- QVERIFY2(m_detected > 0, "Is a YubiKey attached?");
-}
-
-void TestYubiKeyChalResp::getSerial()
-{
- unsigned int serial;
- QVERIFY(YubiKey::instance()->getSerial(serial));
-}
-
-void TestYubiKeyChalResp::keyGetName()
-{
- QVERIFY(m_key);
- QVERIFY(m_key->getName().length() > 0);
-}
-
-void TestYubiKeyChalResp::keyIssueChallenge()
+void TestYubiKeyChallengeResponse::testDetectDevices()
{
- QVERIFY(m_key);
- if (m_key->isBlocking()) {
- /* Testing active mode in unit tests is unreasonable */
- QSKIP("YubiKey not in passive mode", SkipSingle);
+ YubiKey::instance()->findValidKeys();
+
+ // Wait for the hardware to respond
+ QSignalSpy detected(YubiKey::instance(), SIGNAL(detectComplete(bool)));
+ QTRY_VERIFY_WITH_TIMEOUT(detected.count() > 0, 2000);
+
+ // Look at the information retrieved from the key(s)
+ for (auto key : YubiKey::instance()->foundKeys()) {
+ auto displayName = YubiKey::instance()->getDisplayName(key);
+ QVERIFY(displayName.contains("Challenge Response - Slot") || displayName.contains("Configured Slot -"));
+ QVERIFY(displayName.contains(QString::number(key.first)));
+ QVERIFY(displayName.contains(QString::number(key.second)));
}
-
- QByteArray ba("UnitTest");
- QVERIFY(m_key->challenge(ba));
-
- /* TODO Determine if it's reasonable to provide a fixed secret key for
- * verification testing. Obviously simple technically, but annoying
- * if devs need to re-program their yubikeys or have a spare test key
- * for unit tests to pass.
- *
- * Might be worth it for integrity verification though.
- */
}
-void TestYubiKeyChalResp::ykDetected(int slot, bool blocking)
+/**
+ * Secret key for the YubiKey slot used by the unit test is
+ * 1c e3 0f d7 8d 20 dc fa 40 b5 0c 18 77 9a fb 0f 02 28 8d b7
+ * This secret can be on either slot but must be passive.
+ */
+void TestYubiKeyChallengeResponse::testKeyChallenge()
{
- Q_UNUSED(blocking);
+ auto keys = YubiKey::instance()->foundKeys();
+ if (keys.isEmpty()) {
+ QSKIP("No YubiKey devices were detected.");
+ }
- if (slot > 0) {
- m_detected++;
+ // Find a key that is configured in passive mode
+ bool wouldBlock = false;
+ YubiKeySlot pKey(0, 0);
+ for (auto key : keys) {
+ if (YubiKey::instance()->testChallenge(key, &wouldBlock) && !wouldBlock) {
+ pKey = key;
+ break;
+ }
+ Tools::wait(100);
}
- /* Key used for later testing */
- if (!m_key) {
- m_key.reset(new YkChallengeResponseKey(slot, blocking));
+ if (pKey.first == 0) {
+ /* Testing active mode in unit tests is unreasonable */
+ QSKIP("No YubiKey contains a slot in passive mode.");
}
-}
-void TestYubiKeyChalResp::deinit()
-{
- QVERIFY(YubiKey::instance()->deinit());
+ QScopedPointer<YkChallengeResponseKey> key(new YkChallengeResponseKey(pKey));
+
+ QByteArray ba("UnitTest");
+ QVERIFY(key->challenge(ba));
+ QCOMPARE(key->rawKey().size(), 20);
}