diff options
-rw-r--r-- | tests/auto/qlowenergycontroller-gattserver/test/test.pro | 3 | ||||
-rw-r--r-- | tests/auto/qlowenergycontroller-gattserver/test/tst_qlowenergycontroller-gattserver.cpp | 49 |
2 files changed, 52 insertions, 0 deletions
diff --git a/tests/auto/qlowenergycontroller-gattserver/test/test.pro b/tests/auto/qlowenergycontroller-gattserver/test/test.pro index fc9c7a18..5f80e660 100644 --- a/tests/auto/qlowenergycontroller-gattserver/test/test.pro +++ b/tests/auto/qlowenergycontroller-gattserver/test/test.pro @@ -7,3 +7,6 @@ qtConfig(linux_crypto_api): DEFINES += CONFIG_LINUX_CRYPTO_API qtConfig(bluez_le): DEFINES += CONFIG_BLUEZ_LE SOURCES += tst_qlowenergycontroller-gattserver.cpp + +# qemu doesn't support all the needed socket operations +boot2qt: DEFINES += CHECK_CMAC_SUPPORT diff --git a/tests/auto/qlowenergycontroller-gattserver/test/tst_qlowenergycontroller-gattserver.cpp b/tests/auto/qlowenergycontroller-gattserver/test/tst_qlowenergycontroller-gattserver.cpp index e01457eb..81dfd286 100644 --- a/tests/auto/qlowenergycontroller-gattserver/test/tst_qlowenergycontroller-gattserver.cpp +++ b/tests/auto/qlowenergycontroller-gattserver/test/tst_qlowenergycontroller-gattserver.cpp @@ -76,6 +76,10 @@ private: QBluetoothAddress m_serverAddress; QBluetoothDeviceInfo m_serverInfo; QScopedPointer<QLowEnergyController> m_leController; + +#if defined(CHECK_CMAC_SUPPORT) + bool checkCmacSupport(const quint128& csrkMsb); +#endif }; @@ -169,6 +173,13 @@ void TestQLowEnergyControllerGattServer::cmacVerifier() }; QFETCH(QByteArray, message); QFETCH(quint64, expectedMac); + +#if defined(CHECK_CMAC_SUPPORT) + if (!checkCmacSupport(csrk)) { + QSKIP("Needed socket options not available. Running qemu?"); + } +#endif + const bool success = LeCmacCalculator().verify(message, csrk, expectedMac); QVERIFY(success); #else // CONFIG_LINUX_CRYPTO_API @@ -177,6 +188,44 @@ void TestQLowEnergyControllerGattServer::cmacVerifier() #endif // Q_OS_LINUX } +#if defined(CHECK_CMAC_SUPPORT) +#include <sys/socket.h> +#include <linux/if_alg.h> +#include <unistd.h> + +bool TestQLowEnergyControllerGattServer::checkCmacSupport(const quint128& csrk) +{ + bool retval = false; +#if defined(CONFIG_LINUX_CRYPTO_API) && defined(QT_BUILD_INTERNAL) && defined(CONFIG_BLUEZ_LE) + quint128 csrkMsb; + std::reverse_copy(std::begin(csrk.data), std::end(csrk.data), std::begin(csrkMsb.data)); + + int testSocket = socket(AF_ALG, SOCK_SEQPACKET, 0); + if (testSocket != -1) { + sockaddr_alg sa; + using namespace std; + memset(&sa, 0, sizeof sa); + sa.salg_family = AF_ALG; + strcpy(reinterpret_cast<char *>(sa.salg_type), "hash"); + strcpy(reinterpret_cast<char *>(sa.salg_name), "cmac(aes)"); + if (::bind(testSocket, reinterpret_cast<sockaddr *>(&sa), sizeof sa) != -1) { + if (setsockopt(testSocket, 279 /* SOL_ALG */, ALG_SET_KEY, csrkMsb.data, sizeof csrkMsb) != -1) { + retval = true; + } else { + QWARN("Needed socket options (SOL_ALG) not available"); + } + } else { + QWARN("bind() failed for crypto socket:"); + } + close(testSocket); + } else { + QWARN("Unable to create test socket"); + } +#endif + return retval; +} +#endif + void TestQLowEnergyControllerGattServer::cmacVerifier_data() { QTest::addColumn<QByteArray>("message"); |