From 1a669f2f2ab771fac49d859851bd1cdba28ab3f8 Mon Sep 17 00:00:00 2001 From: Steve Brokenshire Date: Fri, 29 Jul 2016 18:42:47 +0100 Subject: [PATCH] Added code and unit tests to FakeConnectionObject class --- source/connobject/ConnectionObject.h | 36 ++- source/tests/classes/FakeConnectionObject.cpp | 37 ++- source/tests/classes/FakeConnectionObject.h | 20 +- source/tests/xestiaab_carddav.cpp | 222 +++++++++++++++++- 4 files changed, 297 insertions(+), 18 deletions(-) diff --git a/source/connobject/ConnectionObject.h b/source/connobject/ConnectionObject.h index c8d1466..798ebae 100644 --- a/source/connobject/ConnectionObject.h +++ b/source/connobject/ConnectionObject.h @@ -26,6 +26,20 @@ enum COConnectResult { COCONNECT_AUTHFAIL }; +enum CORequestResult { + COREQUEST_UNITTESTFAIL = -1, + COREQUEST_OK, + COREQUEST_ERROR_NOTCONNECTED, + COREQUEST_ERROR_SERVER +}; + +struct COServerResponse { + CORequestResult RequestResult; + std::string EntityTag; + int ResultCode; + std::string ResultMessage; +}; + class ConnectionObject{ public: @@ -39,14 +53,23 @@ class ConnectionObject{ virtual COConnectResult Connect() {}; + virtual std::string GetDefaultPrefix() {}; + //virtual void SetupData(std::string Method, std::string Location, std::string Data) {}; + virtual COServerResponse AddContact(std::string Location, std::string Data) {}; + virtual bool CanDoProcessing() {}; virtual bool CanDoSSL() {}; virtual bool SSLVerify() {}; virtual bool AbleToLogin() {}; virtual bool HasValidResponse() {}; - virtual bool IsSelfSigned() {}; protected: + // Test Mode. + + bool TestMode = false; + + // Server variables. + std::string ServerAddress = ""; unsigned int ServerPort = 8080; std::string ServerUser = ""; @@ -56,7 +79,16 @@ class ConnectionObject{ bool ServerSSL = true; std::string ErrorMessage = ""; std::string ErrorBufferMessage = ""; - bool TestMode = false; + + // Connect results. + + bool SSLStatus = false; + bool SSLVerified = false; + bool ValidResponse = false; + bool AuthPassed = false; + bool CanProcess = false; + bool SSLSelfSigned = false; + private: }; \ No newline at end of file diff --git a/source/tests/classes/FakeConnectionObject.cpp b/source/tests/classes/FakeConnectionObject.cpp index fa8f39d..147b01c 100644 --- a/source/tests/classes/FakeConnectionObject.cpp +++ b/source/tests/classes/FakeConnectionObject.cpp @@ -19,10 +19,35 @@ #include "FakeConnectionObject.h" COConnectResult FakeConnectionObject::Connect(){ + COConnectResult ConnectResult = ResultStatus; + AuthPassed = ResultAuthPassed; + CanProcess = ResultCanProcess; + SSLStatus = ResultSSLStatus; + SSLVerified = ResultSSLVerified; + ValidResponse = ResultValidResponse; + SSLSelfSigned = ResultSelfSigned; + ServerPrefix = ResultServerPrefix; return ConnectResult; } +std::string FakeConnectionObject::GetDefaultPrefix(){ + return ServerPrefix; +} + +COServerResponse FakeConnectionObject::AddContact(std::string Location, std::string Data){ + + COServerResponse AddContactResult; + + AddContactResult.RequestResult = TestRequestResult; + AddContactResult.EntityTag = TestEntityTag; + AddContactResult.ResultCode = TestResultCode; + AddContactResult.ResultMessage = TestResultMessage; + + return AddContactResult; + +} + std::string FakeConnectionObject::GetServerAddress(){ return ServerAddress; } @@ -56,25 +81,25 @@ bool FakeConnectionObject::GetTestMode(){ } bool FakeConnectionObject::CanDoProcessing(){ - return false; + return CanProcess; } bool FakeConnectionObject::CanDoSSL(){ - return false; + return SSLStatus; } bool FakeConnectionObject::SSLVerify(){ - return false; + return SSLVerified; } bool FakeConnectionObject::AbleToLogin(){ - return false; + return AuthPassed; } bool FakeConnectionObject::HasValidResponse(){ - return false; + return ValidResponse; } bool FakeConnectionObject::IsSelfSigned(){ - return false; + return SSLSelfSigned; } \ No newline at end of file diff --git a/source/tests/classes/FakeConnectionObject.h b/source/tests/classes/FakeConnectionObject.h index c26bfd4..96f03d0 100644 --- a/source/tests/classes/FakeConnectionObject.h +++ b/source/tests/classes/FakeConnectionObject.h @@ -37,6 +37,11 @@ class FakeConnectionObject : public ConnectionObject { // Functions from the ConnectionObject interface. COConnectResult Connect(); + std::string GetDefaultPrefix(); + COServerResponse AddContact(std::string Location, std::string Data); + + void SetupData(std::string Method, std::string Location, std::string Data); + bool CanDoProcessing(); bool CanDoSSL(); bool SSLVerify(); @@ -46,8 +51,19 @@ class FakeConnectionObject : public ConnectionObject { // Variables to set for fake connection object. - std::string ServerPrefixInput = ""; - COConnectResult ResultStatus; + std::string ResultServerPrefix = ""; + COConnectResult ResultStatus = COCONNECT_UNITTESTFAIL; + bool ResultAuthPassed = false; + bool ResultCanProcess = false; + bool ResultSSLStatus = false; + bool ResultSSLVerified = false; + bool ResultValidResponse = false; + bool ResultSelfSigned = false; + bool NoConnection = false; + CORequestResult TestRequestResult; + std::string TestEntityTag; + int TestResultCode; + std::string TestResultMessage; protected: private: diff --git a/source/tests/xestiaab_carddav.cpp b/source/tests/xestiaab_carddav.cpp index e3ae7ae..e71f4d5 100644 --- a/source/tests/xestiaab_carddav.cpp +++ b/source/tests/xestiaab_carddav.cpp @@ -120,8 +120,6 @@ TEST(CardDAV, Use_Fake_To_Simulate_A_Connection_Returning_Success){ std::string ServerAddress1 = "gibberish.invalid"; std::string ServerUser1 = "user"; std::string ServerPass1 = "pass"; - std::string ServerPrefix1 = "/prefix"; - std::string ServerAccount1 = "Account1"; FakeConnectionObject FakeConnection(ServerAddress1, 8080, ServerUser1, ServerPass1, true); FakeConnection.ResultStatus = COCONNECT_OK; @@ -135,8 +133,6 @@ TEST(CardDAV, Use_Fake_To_Simulate_A_Connection_Returning_InvalidAddress){ std::string ServerAddress1 = "gibberish.invalid"; std::string ServerUser1 = "user"; std::string ServerPass1 = "pass"; - std::string ServerPrefix1 = "/prefix"; - std::string ServerAccount1 = "Account1"; FakeConnectionObject FakeConnection(ServerAddress1, 8080, ServerUser1, ServerPass1, true); FakeConnection.ResultStatus = COCONNECT_INVALID; @@ -150,8 +146,6 @@ TEST(CardDAV, Use_Fake_To_Simulate_A_Connection_Returning_Timeout){ std::string ServerAddress1 = "gibberish.invalid"; std::string ServerUser1 = "user"; std::string ServerPass1 = "pass"; - std::string ServerPrefix1 = "/prefix"; - std::string ServerAccount1 = "Account1"; FakeConnectionObject FakeConnection(ServerAddress1, 8080, ServerUser1, ServerPass1, true); FakeConnection.ResultStatus = COCONNECT_TIMEOUT; @@ -165,12 +159,224 @@ TEST(CardDAV, Use_Fake_To_Simulate_A_Connection_Returning_Authentication_Failure std::string ServerAddress1 = "gibberish.invalid"; std::string ServerUser1 = "user"; std::string ServerPass1 = "pass"; - std::string ServerPrefix1 = "/prefix"; - std::string ServerAccount1 = "Account1"; FakeConnectionObject FakeConnection(ServerAddress1, 8080, ServerUser1, ServerPass1, true); FakeConnection.ResultStatus = COCONNECT_AUTHFAIL; EXPECT_EQ(COCONNECT_AUTHFAIL, FakeConnection.Connect()); +} + +TEST(CardDAV, Use_Fake_To_Simulate_A_Connection_That_Is_Able_To_Login){ + + std::string ServerAddress1 = "gibberish.invalid"; + std::string ServerUser1 = "user"; + std::string ServerPass1 = "pass"; + + FakeConnectionObject FakeConnection(ServerAddress1, 8080, ServerUser1, ServerPass1, true); + FakeConnection.ResultAuthPassed = true; + FakeConnection.Connect(); + + EXPECT_EQ(true, FakeConnection.AbleToLogin()); + +} + +TEST(CardDAV, Use_Fake_To_Simulate_A_Connection_That_Is_Unable_To_Login){ + + std::string ServerAddress1 = "gibberish.invalid"; + std::string ServerUser1 = "user"; + std::string ServerPass1 = "pass"; + + FakeConnectionObject FakeConnection(ServerAddress1, 8080, ServerUser1, ServerPass1, true); + FakeConnection.ResultAuthPassed = false; + FakeConnection.Connect(); + + EXPECT_EQ(false, FakeConnection.AbleToLogin()); + +} + +TEST(CardDAV, Use_Fake_To_Simulate_A_Connection_That_Can_Do_Processing){ + + std::string ServerAddress1 = "gibberish.invalid"; + std::string ServerUser1 = "user"; + std::string ServerPass1 = "pass"; + + FakeConnectionObject FakeConnection(ServerAddress1, 8080, ServerUser1, ServerPass1, true); + FakeConnection.ResultCanProcess = true; + FakeConnection.Connect(); + + EXPECT_EQ(true, FakeConnection.CanDoProcessing()); + +} + +TEST(CardDAV, Use_Fake_To_Simulate_A_Connection_That_Supports_SSL){ + + std::string ServerAddress1 = "gibberish.invalid"; + std::string ServerUser1 = "user"; + std::string ServerPass1 = "pass"; + + FakeConnectionObject FakeConnection(ServerAddress1, 8080, ServerUser1, ServerPass1, true); + FakeConnection.ResultSSLStatus = true; + FakeConnection.Connect(); + + EXPECT_EQ(true, FakeConnection.CanDoSSL()); + +} + +TEST(CardDAV, Use_Fake_To_Simulate_A_Connection_That_Does_Not_Support_SSL){ + + std::string ServerAddress1 = "gibberish.invalid"; + std::string ServerUser1 = "user"; + std::string ServerPass1 = "pass"; + + FakeConnectionObject FakeConnection(ServerAddress1, 8080, ServerUser1, ServerPass1, true); + FakeConnection.ResultSSLStatus = false; + FakeConnection.Connect(); + + EXPECT_EQ(false, FakeConnection.CanDoSSL()); + +} + +TEST(CardDAV, Use_Fake_To_Simulate_A_Connection_That_Has_Valid_SSL_Certificate_Data){ + + std::string ServerAddress1 = "gibberish.invalid"; + std::string ServerUser1 = "user"; + std::string ServerPass1 = "pass"; + + FakeConnectionObject FakeConnection(ServerAddress1, 8080, ServerUser1, ServerPass1, true); + FakeConnection.ResultSSLVerified = true; + FakeConnection.Connect(); + + EXPECT_EQ(true, FakeConnection.SSLVerify()); + +} + +TEST(CardDAV, Use_Fake_To_Simulate_A_Connection_That_Has_Invalid_SSL_Certificate_Data){ + + std::string ServerAddress1 = "gibberish.invalid"; + std::string ServerUser1 = "user"; + std::string ServerPass1 = "pass"; + + FakeConnectionObject FakeConnection(ServerAddress1, 8080, ServerUser1, ServerPass1, true); + FakeConnection.ResultSSLVerified = false; + FakeConnection.Connect(); + + EXPECT_EQ(false, FakeConnection.SSLVerify()); + +} + +TEST(CardDAV, Use_Fake_To_Simulate_A_Connection_That_Has_A_Valid_Response){ + + std::string ServerAddress1 = "gibberish.invalid"; + std::string ServerUser1 = "user"; + std::string ServerPass1 = "pass"; + + FakeConnectionObject FakeConnection(ServerAddress1, 8080, ServerUser1, ServerPass1, true); + FakeConnection.ResultValidResponse = true; + FakeConnection.Connect(); + + EXPECT_EQ(true, FakeConnection.HasValidResponse()); + +} + +TEST(CardDAV, Use_Fake_To_Simulate_A_Connection_That_Has_A_Invalid_Response){ + + std::string ServerAddress1 = "gibberish.invalid"; + std::string ServerUser1 = "user"; + std::string ServerPass1 = "pass"; + + FakeConnectionObject FakeConnection(ServerAddress1, 8080, ServerUser1, ServerPass1, true); + FakeConnection.ResultValidResponse = false; + FakeConnection.Connect(); + + EXPECT_EQ(false, FakeConnection.HasValidResponse()); + +} + +TEST(CardDAV, Use_Fake_To_Simulate_A_Connection_That_Uses_A_Self_Signed_SSL_Certificate){ + + std::string ServerAddress1 = "gibberish.invalid"; + std::string ServerUser1 = "user"; + std::string ServerPass1 = "pass"; + + FakeConnectionObject FakeConnection(ServerAddress1, 8080, ServerUser1, ServerPass1, true); + FakeConnection.ResultSelfSigned = true; + FakeConnection.Connect(); + + EXPECT_EQ(true, FakeConnection.IsSelfSigned()); + +} + +TEST(CardDAV, Use_Fake_To_Simulate_A_Connection_That_Doesnt_Use_A_Self_Signed_SSL_Certificate){ + + std::string ServerAddress1 = "gibberish.invalid"; + std::string ServerUser1 = "user"; + std::string ServerPass1 = "pass"; + + FakeConnectionObject FakeConnection(ServerAddress1, 8080, ServerUser1, ServerPass1, true); + FakeConnection.ResultSelfSigned = false; + FakeConnection.Connect(); + + EXPECT_EQ(false, FakeConnection.IsSelfSigned()); + +} + +TEST(CardDAV, Use_Fake_To_Simulate_A_Connection_That_Gets_The_Server_Prefix_Of_Prefix_Test_A){ + + std::string ServerAddress1 = "gibberish.invalid"; + std::string ServerUser1 = "user"; + std::string ServerPass1 = "pass"; + + FakeConnectionObject FakeConnection(ServerAddress1, 8080, ServerUser1, ServerPass1, true); + FakeConnection.ResultServerPrefix = "/prefix/test/a"; + FakeConnection.Connect(); + + EXPECT_EQ("/prefix/test/a", FakeConnection.GetDefaultPrefix()); + +} + +TEST(CardDAV, Use_Fake_To_Simulate_A_Connection_That_Gets_The_Server_Prefix_Of_Prefix_Test_B){ + + std::string ServerAddress1 = "gibberish.invalid"; + std::string ServerUser1 = "user"; + std::string ServerPass1 = "pass"; + + FakeConnectionObject FakeConnection(ServerAddress1, 8080, ServerUser1, ServerPass1, true); + FakeConnection.ResultServerPrefix = "/prefix/test/b"; + FakeConnection.Connect(); + + EXPECT_EQ("/prefix/test/b", FakeConnection.GetDefaultPrefix()); + +} + +TEST(CardDAV, Use_Fake_To_Simulate_A_Connection_That_Adds_A_Contact_Successfully){ + + std::string ServerAddress1 = "gibberish.invalid"; + std::string ServerUser1 = "user"; + std::string ServerPass1 = "pass"; + + FakeConnectionObject FakeConnection(ServerAddress1, 8080, ServerUser1, ServerPass1, true); + FakeConnection.ResultServerPrefix = "/prefix/test/b"; + FakeConnection.Connect(); + + std::string ContactFile = + "BEGIN:VCARD\n" + "VERSION:4.0\n" + "UID:84q2ioj13jofiujqwr\n" + "N:;;Meep;Moop;;\n" + "FN:The Meep Moop\n" + "END:VCARD"; + + FakeConnection.TestRequestResult = COREQUEST_OK; + FakeConnection.TestEntityTag = "4324svafhuiaffsdhui"; + FakeConnection.TestResultCode = 200; + FakeConnection.TestResultMessage = ""; + + COServerResponse AddContactResult = FakeConnection.AddContact("testfile.vcf", ContactFile); + + EXPECT_EQ(COREQUEST_OK, AddContactResult.RequestResult); + EXPECT_EQ("4324svafhuiaffsdhui", AddContactResult.EntityTag); + EXPECT_EQ(200, AddContactResult.ResultCode); + EXPECT_EQ("", AddContactResult.ResultMessage); + } \ No newline at end of file -- 2.39.5