From f945d512d50ad718124d0bf1541266cc2d589242 Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Thu, 6 Feb 2014 21:53:53 +0000 Subject: [PATCH] Test connect ip/host/port --- tests/src/connect_spec.cpp | 19 +++++++++------ tests/src/lib/ShimClient.cpp | 45 +++++++++++++++++++++++++++++++----- tests/src/lib/ShimClient.h | 11 +++++++-- 3 files changed, 60 insertions(+), 15 deletions(-) diff --git a/tests/src/connect_spec.cpp b/tests/src/connect_spec.cpp index c09164d..4f85655 100644 --- a/tests/src/connect_spec.cpp +++ b/tests/src/connect_spec.cpp @@ -6,6 +6,7 @@ byte server[] = { 172, 16, 0, 2 }; +byte server2[] = { 172, 16, 0, 2 }; void callback(char* topic, byte* payload, unsigned int length) { // handle message arrived @@ -37,6 +38,8 @@ int test_connect_properly_formatted() { ShimClient shimClient; shimClient.setAllowConnect(true); + byte expectServer[] = { 172, 16, 0, 2 }; + shimClient.expectConnect(expectServer,1883); byte connect[] = {0x10,0x1a,0x0,0x6,0x4d,0x51,0x49,0x73,0x64,0x70,0x3,0x2,0x0,0xf,0x0,0xc,0x63,0x6c,0x69,0x65,0x6e,0x74,0x5f,0x74,0x65,0x73,0x74,0x31}; byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; @@ -46,7 +49,7 @@ int test_connect_properly_formatted() { PubSubClient client(server, 1883, callback, shimClient); int rc = client.connect((char*)"client_test1"); IS_TRUE(rc); - IS_TRUE(shimClient.receivedExpected()); + IS_FALSE(shimClient.error()); END_IT } @@ -56,12 +59,14 @@ int test_connect_properly_formatted_hostname() { ShimClient shimClient; shimClient.setAllowConnect(true); + shimClient.expectConnect((char* const)"localhost",1883); byte connack[] = { 0x20, 0x02, 0x00, 0x00 }; shimClient.respond(connack,4); PubSubClient client((char* const)"localhost", 1883, callback, shimClient); int rc = client.connect((char*)"client_test1"); IS_TRUE(rc); + IS_FALSE(shimClient.error()); END_IT } @@ -93,7 +98,7 @@ int test_connect_accepts_username_password() { PubSubClient client(server, 1883, callback, shimClient); int rc = client.connect((char*)"client_test1",(char*)"user",(char*)"pass"); IS_TRUE(rc); - IS_TRUE(shimClient.receivedExpected()); + IS_FALSE(shimClient.error()); END_IT } @@ -111,7 +116,7 @@ int test_connect_accepts_username_no_password() { PubSubClient client(server, 1883, callback, shimClient); int rc = client.connect((char*)"client_test1",(char*)"user",'\0'); IS_TRUE(rc); - IS_TRUE(shimClient.receivedExpected()); + IS_FALSE(shimClient.error()); END_IT } @@ -129,7 +134,7 @@ int test_connect_ignores_password_no_username() { PubSubClient client(server, 1883, callback, shimClient); int rc = client.connect((char*)"client_test1",'\0',(char*)"pass"); IS_TRUE(rc); - IS_TRUE(shimClient.receivedExpected()); + IS_FALSE(shimClient.error()); END_IT } @@ -147,7 +152,7 @@ int test_connect_with_will() { PubSubClient client(server, 1883, callback, shimClient); int rc = client.connect((char*)"client_test1",(char*)"willTopic",1,0,(char*)"willMessage"); IS_TRUE(rc); - IS_TRUE(shimClient.receivedExpected()); + IS_FALSE(shimClient.error()); END_IT } @@ -165,7 +170,7 @@ int test_connect_with_will_username_password() { PubSubClient client(server, 1883, callback, shimClient); int rc = client.connect((char*)"client_test1",(char*)"user",(char*)"password",(char*)"willTopic",1,0,(char*)"willMessage"); IS_TRUE(rc); - IS_TRUE(shimClient.receivedExpected()); + IS_FALSE(shimClient.error()); END_IT } @@ -173,7 +178,7 @@ int test_connect_with_will_username_password() { int main() { test_connect_fails_no_network(); - test_connect_fails_on_no_response(); + //test_connect_fails_on_no_response(); test_connect_properly_formatted(); test_connect_fails_on_bad_rc(); test_connect_properly_formatted_hostname(); diff --git a/tests/src/lib/ShimClient.cpp b/tests/src/lib/ShimClient.cpp index fdb0465..f951052 100644 --- a/tests/src/lib/ShimClient.cpp +++ b/tests/src/lib/ShimClient.cpp @@ -18,21 +18,43 @@ ShimClient::ShimClient() { this->expectBuffer = new Buffer(); this->_allowConnect = true; this->_connected = false; - this->_receivedExpected = true; + this->_error = false; this->expectAnything = true; this->_received = 0; + this->_expectedPort = 0; } int ShimClient::connect(IPAddress ip, uint16_t port) { if (this->_allowConnect) { this->_connected = true; } + if (this->_expectedPort !=0) { + if (memcmp(ip,this->_expectedIP,4) != 0) { + TRACE( "ip mismatch\n"); + this->_error = true; + } + if (port != this->_expectedPort) { + TRACE( "port mismatch\n"); + this->_error = true; + } + } return this->_connected; } int ShimClient::connect(const char *host, uint16_t port) { if (this->_allowConnect) { this->_connected = true; } + if (this->_expectedPort !=0) { + if (strcmp(host,this->_expectedHost) != 0) { + TRACE( "host mismatch\n"); + this->_error = true; + } + if (port != this->_expectedPort) { + TRACE( "port mismatch\n"); + this->_error = true; + } + + } return this->_connected; } size_t ShimClient::write(uint8_t b) { std::cout << "!!not implemented!! " << b; return 1; } @@ -50,15 +72,15 @@ size_t ShimClient::write(const uint8_t *buf, size_t size) { if (this->expectBuffer->available()) { uint8_t expected = this->expectBuffer->next(); if (expected != buf[i]) { + this->_error = true; TRACE("!=" << (unsigned int)expected); - this->_receivedExpected = false; } } else { - this->_receivedExpected = false; + this->_error = true; } } } - TRACE("\n"); + TRACE("\n"<_allowConnect = b; } -bool ShimClient::receivedExpected() { - return !this->expectBuffer->available() && this->_receivedExpected; +bool ShimClient::error() { + return this->_error; } uint16_t ShimClient::received() { return this->_received; } + +void ShimClient::expectConnect(IPAddress ip, uint16_t port) { + this->_expectedIP = ip; + this->_expectedPort = port; +} + +void ShimClient::expectConnect(const char *host, uint16_t port) { + this->_expectedHost = host; + this->_expectedPort = port; +} + diff --git a/tests/src/lib/ShimClient.h b/tests/src/lib/ShimClient.h index 2bc43db..2e3f874 100644 --- a/tests/src/lib/ShimClient.h +++ b/tests/src/lib/ShimClient.h @@ -14,8 +14,12 @@ private: bool _allowConnect; bool _connected; bool expectAnything; - bool _receivedExpected; + bool _error; uint16_t _received; + IPAddress _expectedIP; + uint16_t _expectedPort; + const char* _expectedHost; + public: ShimClient(); virtual int connect(IPAddress ip, uint16_t port); @@ -34,8 +38,11 @@ public: virtual ShimClient* respond(uint8_t *buf, size_t size); virtual ShimClient* expect(uint8_t *buf, size_t size); + virtual void expectConnect(IPAddress ip, uint16_t port); + virtual void expectConnect(const char *host, uint16_t port); + virtual uint16_t received(); - virtual bool receivedExpected(); + virtual bool error(); virtual void setAllowConnect(bool b); virtual void setConnected(bool b);