summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorKari Oikarinen <kari.oikarinen@qt.io>2016-12-08 13:11:36 +0200
committerKari Oikarinen <kari.oikarinen@qt.io>2016-12-22 08:43:03 +0000
commit7e1e1889736a5c0f6f6324d0ad8173ff0a8679a8 (patch)
treecf36d5a059dd76444daa801bcf0df93cf3809054 /tests
parentdb61153547cb8aafaf374f43932047ea400bd482 (diff)
Handle host-device version mismatches forward-compatibly
There is no support for multiple versions, but the behavior changes in this commit should allow adding that in the future. Device responds with a new Refuse message, if it does not support the requested version. Change-Id: I8a747654edb1c6efab485808b2692cd9689bd100 Reviewed-by: Samuli Piippo <samuli.piippo@qt.io>
Diffstat (limited to 'tests')
-rw-r--r--tests/servicetest.cpp38
-rw-r--r--tests/streamtest.cpp69
2 files changed, 95 insertions, 12 deletions
diff --git a/tests/servicetest.cpp b/tests/servicetest.cpp
index a895257..76af6cf 100644
--- a/tests/servicetest.cpp
+++ b/tests/servicetest.cpp
@@ -34,18 +34,39 @@
const int testTimeout = 500; // in milliseconds
// Helper to initialize Connection in testcases
-struct ConnectionContext
+class ConnectionContext
{
+public:
ConnectionContext()
- : deviceEnumerator{},
- connection{new QdbTransport{new UsbConnection{deviceEnumerator.listUsbDevices()[0]}}}
+ : m_connection{nullptr},
+ m_valid{false}
{
- QVERIFY(connection.initialize());
+ UsbDeviceEnumerator deviceEnumerator;
+ const auto devices = deviceEnumerator.listUsbDevices();
+ if (devices.empty())
+ QFAIL("Could not find QDB USB device to run the test against");
- connection.connect();
+ m_connection = make_unique<Connection>(new QdbTransport{new UsbConnection{devices[0]}});
+
+ QVERIFY(m_connection->initialize());
+ m_valid = true;
+
+ m_connection->connect();
+ }
+
+ Connection *connection()
+ {
+ return m_connection.get();
}
- UsbDeviceEnumerator deviceEnumerator;
- Connection connection;
+
+ bool isValid()
+ {
+ return m_valid;
+ }
+
+private:
+ std::unique_ptr<Connection> m_connection;
+ bool m_valid;
};
class ServiceTest : public QObject
@@ -58,8 +79,9 @@ private slots:
void ServiceTest::echo()
{
ConnectionContext ctx;
+ QVERIFY(ctx.isValid());
- EchoService echo{&ctx.connection};
+ EchoService echo{ctx.connection()};
connect(&echo, &EchoService::initialized, [&]() {
echo.send("ABCD");
});
diff --git a/tests/streamtest.cpp b/tests/streamtest.cpp
index 0e3068b..67ff33c 100644
--- a/tests/streamtest.cpp
+++ b/tests/streamtest.cpp
@@ -46,7 +46,11 @@ public slots:
void run()
{
UsbDeviceEnumerator deviceManager;
- m_transport = make_unique<QdbTransport>(new UsbConnection{deviceManager.listUsbDevices()[0]});
+ const auto devices = deviceManager.listUsbDevices();
+ if (devices.empty())
+ QFAIL("Could not find QDB USB device to run the test against");
+
+ m_transport = make_unique<QdbTransport>(new UsbConnection{devices[0]});
if (m_transport->open()) {
qDebug() << "opened transport";
connect(m_transport.get(), &QdbTransport::messageAvailable, this, &TestCase::testPhases);
@@ -179,7 +183,7 @@ public slots:
{
switch (m_phase) {
case 0: {
- QByteArray unsupported{};
+ QByteArray unsupported;
QDataStream dataStream{&unsupported, QIODevice::WriteOnly};
dataStream << (qdbProtocolVersion + 1);
QdbMessage connect{QdbMessage::Connect, 0, 0, unsupported};
@@ -188,10 +192,16 @@ public slots:
}
case 1: {
QdbMessage response = m_transport->receive();
- QCOMPARE(response.command(), QdbMessage::Connect);
+ QCOMPARE(response.command(), QdbMessage::Refuse);
QCOMPARE(response.hostStream(), 0u);
QCOMPARE(response.deviceStream(), 0u);
- QCOMPARE(response.data(), m_versionBuffer);
+ QDataStream dataStream{response.data()};
+ uint32_t reason;
+ dataStream >> reason;
+ QCOMPARE(reason, static_cast<uint32_t>(RefuseReason::UnknownVersion));
+ uint32_t version;
+ dataStream >> version;
+ QCOMPARE(version, qdbProtocolVersion);
emit passed();
break;
@@ -464,6 +474,51 @@ private:
StreamId m_deviceId2 = 0;
};
+class WriteWithoutConnectingTest : public TestCase
+{
+ Q_OBJECT
+public slots:
+ void testPhases() override
+ {
+ switch (m_phase) {
+ case 0: {
+ // Send Connect with unknown version to ensure not connected state.
+ // Otherwise the device will still be connected due to previous
+ // tests establishing the connection.
+ QByteArray unsupported;
+ QDataStream dataStream{&unsupported, QIODevice::WriteOnly};
+ dataStream << (qdbProtocolVersion + 1);
+ QdbMessage connect{QdbMessage::Connect, 0, 0, unsupported};
+ QVERIFY(m_transport->send(connect));
+ break;
+ }
+ case 1: {
+ QdbMessage response = m_transport->receive();
+ QCOMPARE(response.command(), QdbMessage::Refuse);
+
+ QByteArray data{"ABCD"};
+ QdbMessage write{QdbMessage::Write, 1, 1, data};
+ QVERIFY(m_transport->send(write));
+ break;
+ }
+ case 2: {
+ QdbMessage response = m_transport->receive();
+ QCOMPARE(response.command(), QdbMessage::Refuse);
+ QCOMPARE(response.hostStream(), 0u);
+ QCOMPARE(response.deviceStream(), 0u);
+ QByteArray expectedReason;
+ QDataStream dataStream{&expectedReason, QIODevice::WriteOnly};
+ dataStream << static_cast<uint32_t>(RefuseReason::NotConnected);
+ QCOMPARE(response.data(), expectedReason);
+
+ emit passed();
+ break;
+ }
+ }
+ ++m_phase;
+ }
+};
+
void testCase(TestCase *test)
{
QSignalSpy spy{test, &TestCase::passed};
@@ -511,6 +566,12 @@ private slots:
TwoEchoStreamsTest test;
testCase(&test);
}
+
+ void writeWithoutConnecting()
+ {
+ WriteWithoutConnectingTest test;
+ testCase(&test);
+ }
};
QTEST_GUILESS_MAIN(StreamTest)