From 08d912e6f4e22ad46e064278892042a4ab8acef1 Mon Sep 17 00:00:00 2001 From: Lomtev Dmytro Date: Fri, 13 Oct 2017 10:04:42 +0300 Subject: [PATCH] SECIOTSRK-596 Tests coverage increased. Added tests for EasySetupServer and NMExceptions. --- device_core/iotivity_lib/inc/easysetup_server.h | 12 ++- device_core/iotivity_lib/inc/iotutils.h | 40 --------- device_core/iotivity_lib/inc/nmexceptions.h | 2 +- device_core/iotivity_lib/src/easysetup_server.cpp | 5 +- device_core/utest/test_easysetup_server.cpp | 102 ++++++++++++++++++++++ device_core/utest/test_nmexceptions.cpp | 72 +++++++++++++++ 6 files changed, 181 insertions(+), 52 deletions(-) create mode 100644 device_core/utest/test_easysetup_server.cpp create mode 100644 device_core/utest/test_nmexceptions.cpp diff --git a/device_core/iotivity_lib/inc/easysetup_server.h b/device_core/iotivity_lib/inc/easysetup_server.h index c9bd4e2..d9814d2 100644 --- a/device_core/iotivity_lib/inc/easysetup_server.h +++ b/device_core/iotivity_lib/inc/easysetup_server.h @@ -10,28 +10,26 @@ #ifndef __EASYSETUP_SERVER_H__ #define __EASYSETUP_SERVER_H__ -#include "easysetup.h" -#include "escommon.h" -#include "ESEnrolleeCommon.h" #include #include #include +#include +#include "easysetup.h" +#include "escommon.h" +#include "ESEnrolleeCommon.h" namespace NetworkManager { -typedef struct { - int some_value; -} EasySetupUserData; #define ES_STRING_MAX_VALUE (OIC_STRING_MAX_VALUE + 1) + typedef struct { char ssid[ES_STRING_MAX_VALUE]; char pwd[ES_STRING_MAX_VALUE]; WIFI_AUTHTYPE authtype; WIFI_ENCTYPE enctype; - EasySetupUserData userdata; } EasySetupWiFiInfo; typedef struct { diff --git a/device_core/iotivity_lib/inc/iotutils.h b/device_core/iotivity_lib/inc/iotutils.h index e838134..c5b5595 100644 --- a/device_core/iotivity_lib/inc/iotutils.h +++ b/device_core/iotivity_lib/inc/iotutils.h @@ -20,46 +20,6 @@ namespace NetworkManager { -class ScopedProfiler -{ -public: - ScopedProfiler(const char* sc, const char* f, int ln, const char* n) - : scope(sc), file(f), line(ln), name(n), stopped(false), reported(false) { - begining = std::chrono::system_clock::now(); - } - - ~ScopedProfiler() { - stop(); - if (!reported) { - report("end of scope", -1); - } - } - - void stop() { - if (!stopped) { - stopped = true; - end = std::chrono::system_clock::now(); - } - } - - void report(const char* f, int ln) { - stop(); - reported = true; - float t = std::chrono::duration_cast(end - begining).count(); - LOG_D("NMProfiler", "Profile - %s [%s]:\n\tFrom: %s:%d\n\tTo: %s:%d\n\tTime: %f s", - name.c_str(), scope.c_str(), file.c_str(), line, f, ln, t / 1000); - } - - std::chrono::time_point begining; - std::chrono::time_point end; - std::string scope; - std::string file; - int line; - std::string name; - bool stopped; - bool reported; -}; - /** * @brief printRepresentation used to print complex representaions * @param rep OCRepresentation object diff --git a/device_core/iotivity_lib/inc/nmexceptions.h b/device_core/iotivity_lib/inc/nmexceptions.h index 4553a7e..5db9b96 100644 --- a/device_core/iotivity_lib/inc/nmexceptions.h +++ b/device_core/iotivity_lib/inc/nmexceptions.h @@ -10,7 +10,7 @@ namespace NetworkManager class NMexception: public std::exception { public: - NMexception(const std::string& msg): message(msg) {} + NMexception(const std::string& msg): code(-1), message(msg) {} NMexception(const std::string& msg, const int error_code): code(error_code), message(msg) {} diff --git a/device_core/iotivity_lib/src/easysetup_server.cpp b/device_core/iotivity_lib/src/easysetup_server.cpp index afb6753..1d2e8e9 100644 --- a/device_core/iotivity_lib/src/easysetup_server.cpp +++ b/device_core/iotivity_lib/src/easysetup_server.cpp @@ -8,8 +8,8 @@ * @author Mail to: Dmytro Lomtev, d.lomtev@samsung.com */ +#include #include "easysetup_server.h" -#include "cstring" namespace NetworkManager { @@ -49,9 +49,6 @@ void cb_wifi_prov(ESWiFiConfData* _data) strncpy(info.wifi_info.pwd, _data->pwd, OIC_STRING_MAX_VALUE); info.wifi_info.authtype = _data->authtype; info.wifi_info.enctype = _data->enctype; - if (_data->userdata) { - memcpy(&info.wifi_info.userdata, _data->userdata, sizeof(info.wifi_info.userdata)); - } EasySetupServer::instance().running = false; EasySetupServer::instance().signal.notify_all(); diff --git a/device_core/utest/test_easysetup_server.cpp b/device_core/utest/test_easysetup_server.cpp new file mode 100644 index 0000000..025a044 --- /dev/null +++ b/device_core/utest/test_easysetup_server.cpp @@ -0,0 +1,102 @@ +#include +#include +#include +#include "easysetup_server.h" +#include "iotivity.h" + +using namespace NetworkManager; + +/** + * @brief TEST for EasySetup service callbacks + */ +TEST(TestEasySetupServer, callbacks) +{ + EXPECT_NO_THROW(cb_connect_request(nullptr)); + EXPECT_NO_THROW(cb_conf_prov(nullptr)); + + ESWiFiConfData wifi_conf = { + "wifi-ap-ssid", + "wifi-password", + WPA2_PSK, + TKIP_AES, + nullptr + }; + + EXPECT_NO_THROW(cb_wifi_prov(&wifi_conf)); + const EasySetupInfo& info = EasySetupServer::instance().getInfo(); + EXPECT_STREQ(wifi_conf.ssid, info.wifi_info.ssid); + EXPECT_STREQ(wifi_conf.pwd, info.wifi_info.pwd); + EXPECT_EQ(wifi_conf.authtype, info.wifi_info.authtype); + EXPECT_EQ(wifi_conf.enctype, info.wifi_info.enctype); + + ESCoapCloudConfData cloud_conf = { + "auth-code", + "access-token", + OAUTH_TOKENTYPE_MAC, + "samsung", + "http://auth.server.com", + nullptr + }; + + EXPECT_NO_THROW(cb_cloud_prov(&cloud_conf)); + const EasySetupInfo& info2 = EasySetupServer::instance().getInfo(); + EXPECT_STREQ(cloud_conf.ciServer, info2.cloud_info.host); + EXPECT_STREQ(cloud_conf.authProvider, info2.cloud_info.auth_provider); + EXPECT_STREQ(cloud_conf.authCode, info2.cloud_info.auth_code); +} + +TEST(TestEasySetupServer, run_with_cloud) +{ + ESCoapCloudConfData cloud_conf = { + "auth-code", + "access-token", + OAUTH_TOKENTYPE_MAC, + "samsung", + "http://auth.server.com", + nullptr + }; + + IoTivity* iotivity = IoTivity::getInstance(); + iotivity->registerDeviceInfo("device-name", "device-model", "oic.d.phone"); + + EasySetupServer& easy_setup = EasySetupServer::instance(); + EXPECT_NO_THROW(easy_setup.setWiFiFrequency(WiFiFrequency::Freq2_4GHz)); + EXPECT_NO_THROW(easy_setup.setWiFiModes({WiFiMode::G, WiFiMode::N, WiFiMode::AC})); + EXPECT_NO_THROW(easy_setup.setDeviceName("Device #1")); + std::thread executor(&EasySetupServer::run, &easy_setup, true); + std::this_thread::sleep_for(std::chrono::milliseconds(300)); + EXPECT_NO_THROW(cb_cloud_prov(&cloud_conf)); + executor.join(); + const EasySetupInfo& info = EasySetupServer::instance().getInfo(); + EXPECT_STREQ(cloud_conf.ciServer, info.cloud_info.host); + EXPECT_STREQ(cloud_conf.authProvider, info.cloud_info.auth_provider); + EXPECT_STREQ(cloud_conf.authCode, info.cloud_info.auth_code); +} + +TEST(TestEasySetupServer, run_without_cloud) +{ + ESWiFiConfData wifi_conf = { + "wifi-ap-ssid", + "wifi-password", + WPA2_PSK, + TKIP_AES, + nullptr + }; + + IoTivity* iotivity = IoTivity::getInstance(); + iotivity->registerDeviceInfo("device-name", "device-model", "oic.d.phone"); + + EasySetupServer& easy_setup = EasySetupServer::instance(); + EXPECT_NO_THROW(easy_setup.setWiFiFrequency(WiFiFrequency::Freq2_4GHz)); + EXPECT_NO_THROW(easy_setup.setWiFiModes({WiFiMode::G, WiFiMode::N, WiFiMode::AC})); + EXPECT_NO_THROW(easy_setup.setDeviceName("Device #1")); + std::thread executor(&EasySetupServer::run, &easy_setup, false); + std::this_thread::sleep_for(std::chrono::milliseconds(300)); + EXPECT_NO_THROW(cb_wifi_prov(&wifi_conf)); + executor.join(); + const EasySetupInfo& info = EasySetupServer::instance().getInfo(); + EXPECT_STREQ(wifi_conf.ssid, info.wifi_info.ssid); + EXPECT_STREQ(wifi_conf.pwd, info.wifi_info.pwd); + EXPECT_EQ(wifi_conf.authtype, info.wifi_info.authtype); + EXPECT_EQ(wifi_conf.enctype, info.wifi_info.enctype); +} diff --git a/device_core/utest/test_nmexceptions.cpp b/device_core/utest/test_nmexceptions.cpp new file mode 100644 index 0000000..805b8eb --- /dev/null +++ b/device_core/utest/test_nmexceptions.cpp @@ -0,0 +1,72 @@ +#include +#include "nmexceptions.h" + +using namespace NetworkManager; + +/** + * @brief TEST NMException derived classes creation + */ +TEST(TestNMexception, construction) +{ + const std::string msg{"test-message"}; + const int TEST_CODE = 555; + + NMexception edef1(msg); + EXPECT_EQ(msg, edef1.what()); + EXPECT_EQ(-1, edef1.errorCode()); + + NMexception e1(msg, TEST_CODE); + EXPECT_EQ(msg, e1.what()); + EXPECT_EQ(TEST_CODE, e1.errorCode()); + + NotAuthorized edef2(msg); + EXPECT_EQ(msg, edef2.what()); + EXPECT_EQ(-1, edef2.errorCode()); + + NotAuthorized e2(msg, TEST_CODE); + EXPECT_EQ(msg, e2.what()); + EXPECT_EQ(TEST_CODE, e2.errorCode()); + + SessionExpirted edef3(msg); + EXPECT_EQ(msg, edef3.what()); + EXPECT_EQ(-1, edef3.errorCode()); + + SessionExpirted e3(msg, TEST_CODE); + EXPECT_EQ(msg, e3.what()); + EXPECT_EQ(TEST_CODE, e3.errorCode()); + + AuthException edef4(msg); + EXPECT_EQ(msg, edef4.what()); + EXPECT_EQ(-1, edef4.errorCode()); + + AuthException e4(msg, TEST_CODE); + EXPECT_EQ(msg, e4.what()); + EXPECT_EQ(TEST_CODE, e4.errorCode()); + + IoTInternalError edef5(msg); + EXPECT_EQ(msg, edef5.what()); + EXPECT_EQ(-1, edef5.errorCode()); + + IoTInternalError e5(msg, TEST_CODE); + EXPECT_EQ(msg, e5.what()); + EXPECT_EQ(TEST_CODE, e5.errorCode()); + + BadParameterException edef6(msg); + EXPECT_EQ(msg, edef6.what()); + EXPECT_EQ(1, edef6.errorCode()); + + BadParameterException e6(msg, TEST_CODE); + EXPECT_EQ(msg, e6.what()); + EXPECT_EQ(TEST_CODE, e6.errorCode()); + + HTTPError edef7(msg); + EXPECT_EQ(msg, edef7.what()); + EXPECT_EQ(1, edef7.errorCode()); + + HTTPError e7(msg, TEST_CODE); + EXPECT_EQ(msg, e7.what()); + EXPECT_EQ(TEST_CODE, e7.errorCode()); + + CurlException c(2); + EXPECT_EQ(2, c.errorCode()); +} -- 2.7.4