From: Maksym Mamontov Date: Wed, 17 Aug 2022 20:40:36 +0000 (+0300) Subject: Convert some tests from TUT to Boost.Test X-Git-Url: https://git.stg.codes/stg.git/commitdiff_plain/5e4339fa861d8f6aadf5cd731e8c38dd569aa013?hp=4ec483cf24cf5474cdcacf355132070a3661c186 Convert some tests from TUT to Boost.Test --- diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index cedaaebd..2f9283a8 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,23 +1,41 @@ set ( CPP_FILES main.cpp - test_raw_ip.cpp - test_admin_conf.cpp - test_tariff.cpp test_conffiles.cpp test_fee_charge_rules.cpp test_reconnect_on_tariff_change.cpp test_disable_session_log.cpp test_filter_params_log.cpp - test_crypto.cpp - test_bfstream.cpp ../projects/stargazer/tariff_impl.cpp ../projects/stargazer/user_impl.cpp ../projects/stargazer/user_property.cpp ) set ( THREADS_PREFER_PTHREAD_FLAG ON ) find_package ( Threads REQUIRED ) +find_package ( Boost REQUIRED unit_test_framework ) +add_definitions ( -DBOOST_TEST_DYN_LINK ) add_definitions ( -DUSE_ABSTRACT_SETTINGS ) +add_executable ( test_admin_conf test_admin_conf.cpp ) +target_link_libraries ( test_admin_conf Boost::unit_test_framework ) +add_test ( admin_conf test_admin_conf ) + +add_executable ( test_raw_ip test_raw_ip.cpp ) +target_link_libraries ( test_raw_ip Boost::unit_test_framework ) +add_test ( raw_ip test_raw_ip ) + +add_executable ( test_bfstream test_bfstream.cpp ) +target_link_libraries ( test_bfstream crypto Boost::unit_test_framework ) +add_test ( bfstream test_bfstream ) + +add_executable ( test_crypto test_crypto.cpp ) +target_link_libraries ( test_crypto crypto Boost::unit_test_framework ) +add_test ( stgcrypto test_crypto ) + +add_executable ( test_tariff test_tariff.cpp ../projects/stargazer/tariff_impl.cpp ) +target_link_libraries ( test_tariff crypto common Boost::unit_test_framework ) +target_include_directories ( test_tariff PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ../projects/stargazer ) +add_test ( tariff test_tariff ) + add_executable ( tests ${CPP_FILES} ) target_link_libraries ( tests conffiles crypto logger scriptexecuter common Threads::Threads ) diff --git a/tests/longstring.h b/tests/longstring.h index 2f41c371..2926bdef 100644 --- a/tests/longstring.h +++ b/tests/longstring.h @@ -1,6 +1,5 @@ -#ifndef __LONG_STRING_H__ -#define __LONG_STRING_H__ +#pragma once -std::string longString = "
-#endif +const std::string longString = "
tg; - tg priv_test_group("PRIV tests group"); - - typedef tg::object testobject; - - template<> - template<> - void testobject::test<1>() - { - set_test_name("Check default constructor"); - - STG::Priv zero; - - ensure("zero.userStat == 0", zero.userStat == 0); - ensure("zero.userConf == 0", zero.userConf == 0); - ensure("zero.userCash == 0", zero.userCash == 0); - ensure("zero.userPasswd == 0", zero.userPasswd == 0); - ensure("zero.userAddDel == 0", zero.userAddDel == 0); - ensure("zero.adminChg == 0", zero.adminChg == 0); - ensure("zero.tariffChg == 0", zero.tariffChg == 0); - ensure("zero.serviceChg == 0", zero.serviceChg == 0); - ensure("zero.corpChg == 0", zero.corpChg == 0); - - ensure("zero.toInt() == 0", zero.toInt() == 0); - } - - template<> - template<> - void testobject::test<2>() - { - set_test_name("Check uint32_t conversions"); - - for (uint8_t i = 0; i < 4; ++i) { - - // 'i' is extra trash in high bits - - STG::Priv priv1(ONES | (i << 0x12)); // All 1 - - ensure_equals("priv1.userStat == 1", priv1.userStat, 1); - ensure_equals("priv1.userConf == 1", priv1.userConf, 1); - ensure_equals("priv1.userCash == 1", priv1.userCash, 1); - ensure_equals("priv1.userPasswd == 1", priv1.userPasswd, 1); - ensure_equals("priv1.userAddDel == 1", priv1.userAddDel, 1); - ensure_equals("priv1.adminChg == 1", priv1.adminChg, 1); - ensure_equals("priv1.tariffChg == 1", priv1.tariffChg, 1); - ensure_equals("priv1.serviceChg == 1", priv1.serviceChg, 1); - ensure_equals("priv1.corpChg == 1", priv1.corpChg, 1); - - ensure_equals("priv1.toInt() == 0x00015555", priv1.toInt(), static_cast(ONES)); - - STG::Priv priv2(TWOS | (i << 0x12)); // All 2 - - ensure_equals("priv2.userStat == 2", priv2.userStat, 2); - ensure_equals("priv2.userConf == 2", priv2.userConf, 2); - ensure_equals("priv2.userCash == 2", priv2.userCash, 2); - ensure_equals("priv2.userPasswd == 2", priv2.userPasswd, 2); - ensure_equals("priv2.userAddDel == 2", priv2.userAddDel, 2); - ensure_equals("priv2.adminChg == 2", priv2.adminChg, 2); - ensure_equals("priv2.tariffChg == 2", priv2.tariffChg, 2); - ensure_equals("priv2.serviceChg == 2", priv2.serviceChg, 2); - ensure_equals("priv2.corpChg == 2", priv2.corpChg, 2); - - ensure_equals("priv2.toInt() = 0x0002AAAA", priv2.toInt(), static_cast(TWOS)); - - STG::Priv priv3(THREES | (i << 0x12)); // All 3 - - ensure_equals("priv3.userStat == 3", priv3.userStat, 3); - ensure_equals("priv3.userConf == 3", priv3.userConf, 3); - ensure_equals("priv3.userCash == 3", priv3.userCash, 3); - ensure_equals("priv3.userPasswd == 3", priv3.userPasswd, 3); - ensure_equals("priv3.userAddDel == 3", priv3.userAddDel, 3); - ensure_equals("priv3.adminChg == 3", priv3.adminChg, 3); - ensure_equals("priv3.tariffChg == 3", priv3.tariffChg, 3); - ensure_equals("priv3.serviceChg == 3", priv3.serviceChg, 3); - ensure_equals("priv3.corpChg == 3", priv3.corpChg, 3); - - ensure_equals("priv3.toInt() = 0x0003FFFF", priv3.toInt(), static_cast(THREES)); - - STG::Priv pm1(MIX1 | (i << 0x12)); // 012301230 - - ensure_equals("pm1.userStat == 0", pm1.userStat, 0); - ensure_equals("pm1.userConf == 1", pm1.userConf, 1); - ensure_equals("pm1.userCash == 2", pm1.userCash, 2); - ensure_equals("pm1.userPasswd == 3", pm1.userPasswd, 3); - ensure_equals("pm1.userAddDel == 0", pm1.userAddDel, 0); - ensure_equals("pm1.adminChg == 1", pm1.adminChg, 1); - ensure_equals("pm1.tariffChg == 2", pm1.tariffChg, 2); - ensure_equals("pm1.serviceChg == 3", pm1.serviceChg, 3); - ensure_equals("pm1.corpChg == 0", pm1.corpChg, 0); - - ensure_equals("pm1.toInt() = 0xE4E4", pm1.toInt(), static_cast(MIX1)); - - STG::Priv pm2(MIX2 | (i << 0x12)); // 210321032 - - ensure_equals("pm2.userStat == 2", pm2.userStat, 2); - ensure_equals("pm2.userConf == 1", pm2.userConf, 1); - ensure_equals("pm2.userCash == 0", pm2.userCash, 0); - ensure_equals("pm2.userPasswd == 3", pm2.userPasswd, 3); - ensure_equals("pm2.userAddDel == 2", pm2.userAddDel, 2); - ensure_equals("pm2.adminChg == 1", pm2.adminChg, 1); - ensure_equals("pm2.tariffChg == 0", pm2.tariffChg, 0); - ensure_equals("pm2.serviceChg == 3", pm2.serviceChg, 3); - ensure_equals("pm2.corpChg == 2", pm2.corpChg, 2); - - ensure_equals("pm2.toInt() = 0x0002C6C6", pm2.toInt(), static_cast(MIX2)); - - STG::Priv pm3(MIX3 | (i << 0x12)); // 321032103 - - ensure_equals("pm3.userStat == 3", pm3.userStat, 3); - ensure_equals("pm3.userConf == 2", pm3.userConf, 2); - ensure_equals("pm3.userCash == 1", pm3.userCash, 1); - ensure_equals("pm3.userPasswd == 0", pm3.userPasswd, 0); - ensure_equals("pm3.userAddDel == 3", pm3.userAddDel, 3); - ensure_equals("pm3.adminChg == 2", pm3.adminChg, 2); - ensure_equals("pm3.tariffChg == 1", pm3.tariffChg, 1); - ensure_equals("pm3.serviceChg == 0", pm3.serviceChg, 0); - ensure_equals("pm3.corpChg == 3", pm3.corpChg, 3); - - ensure_equals("pm3.toInt() = 0x00031B1B", pm3.toInt(), static_cast(MIX3)); - - } - - } - - template<> - template<> - void testobject::test<3>() - { - set_test_name("Check explicit uint32_t conversions"); - - for (uint8_t i = 0; i < 4; ++i) { - - // 'i' is extra trash in high bits - - STG::Priv priv1(ONES | (i << 0x12)); // All 1 - - - ensure_equals("priv1.userStat == 1", priv1.userStat, 1); - ensure_equals("priv1.userConf == 1", priv1.userConf, 1); - ensure_equals("priv1.userCash == 1", priv1.userCash, 1); - ensure_equals("priv1.userPasswd == 1", priv1.userPasswd, 1); - ensure_equals("priv1.userAddDel == 1", priv1.userAddDel, 1); - ensure_equals("priv1.adminChg == 1", priv1.adminChg, 1); - ensure_equals("priv1.tariffChg == 1", priv1.tariffChg, 1); - ensure_equals("priv1.serviceChg == 1", priv1.serviceChg, 1); - ensure_equals("priv1.corpChg == 1", priv1.corpChg, 1); - - ensure_equals("priv1.toInt() == 0x00015555", priv1.toInt(), static_cast(ONES)); +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wold-style-cast" +#pragma GCC diagnostic ignored "-Wunused-parameter" +#pragma GCC diagnostic ignored "-Wsign-compare" +#pragma GCC diagnostic ignored "-Wparentheses" +#include +#pragma GCC diagnostic pop - STG::Priv priv2(TWOS | (i << 0x12)); // All 2 +#include - ensure_equals("priv2.userStat == 2", priv2.userStat, 2); - ensure_equals("priv2.userConf == 2", priv2.userConf, 2); - ensure_equals("priv2.userCash == 2", priv2.userCash, 2); - ensure_equals("priv2.userPasswd == 2", priv2.userPasswd, 2); - ensure_equals("priv2.userAddDel == 2", priv2.userAddDel, 2); - ensure_equals("priv2.adminChg == 2", priv2.adminChg, 2); - ensure_equals("priv2.tariffChg == 2", priv2.tariffChg, 2); - ensure_equals("priv2.serviceChg == 2", priv2.serviceChg, 2); - ensure_equals("priv2.corpChg == 2", priv2.corpChg, 2); - - ensure_equals("priv2.toInt() = 0x0002AAAA", priv2.toInt(), static_cast(TWOS)); - - STG::Priv priv3(THREES | (i << 0x12)); // All 3 - - ensure_equals("priv3.userStat == 3", priv3.userStat, 3); - ensure_equals("priv3.userConf == 3", priv3.userConf, 3); - ensure_equals("priv3.userCash == 3", priv3.userCash, 3); - ensure_equals("priv3.userPasswd == 3", priv3.userPasswd, 3); - ensure_equals("priv3.userAddDel == 3", priv3.userAddDel, 3); - ensure_equals("priv3.adminChg == 3", priv3.adminChg, 3); - ensure_equals("priv3.tariffChg == 3", priv3.tariffChg, 3); - ensure_equals("priv3.serviceChg == 3", priv3.serviceChg, 3); - ensure_equals("priv3.corpChg == 3", priv3.corpChg, 3); - - ensure_equals("priv3.toInt() = 0x0003FFFF", priv3.toInt(), static_cast(THREES)); - - STG::Priv pm1(MIX1 | (i << 0x12)); // 012301230 - - ensure_equals("pm1.userStat == 0", pm1.userStat, 0); - ensure_equals("pm1.userConf == 1", pm1.userConf, 1); - ensure_equals("pm1.userCash == 2", pm1.userCash, 2); - ensure_equals("pm1.userPasswd == 3", pm1.userPasswd, 3); - ensure_equals("pm1.userAddDel == 0", pm1.userAddDel, 0); - ensure_equals("pm1.adminChg == 1", pm1.adminChg, 1); - ensure_equals("pm1.tariffChg == 2", pm1.tariffChg, 2); - ensure_equals("pm1.serviceChg == 3", pm1.serviceChg, 3); - ensure_equals("pm1.corpChg == 0", pm1.corpChg, 0); - - ensure_equals("pm1.toInt() = 0xE4E4", pm1.toInt(), static_cast(MIX1)); - - STG::Priv pm2(MIX2 | (i << 0x12)); // 210321032 - - ensure_equals("pm2.userStat == 2", pm2.userStat, 2); - ensure_equals("pm2.userConf == 1", pm2.userConf, 1); - ensure_equals("pm2.userCash == 0", pm2.userCash, 0); - ensure_equals("pm2.userPasswd == 3", pm2.userPasswd, 3); - ensure_equals("pm2.userAddDel == 2", pm2.userAddDel, 2); - ensure_equals("pm2.adminChg == 1", pm2.adminChg, 1); - ensure_equals("pm2.tariffChg == 0", pm2.tariffChg, 0); - ensure_equals("pm2.serviceChg == 3", pm2.serviceChg, 3); - ensure_equals("pm2.corpChg == 2", pm2.corpChg, 2); - - ensure_equals("pm2.toInt() = 0x0002C6C6", pm2.toInt(), static_cast(MIX2)); +namespace +{ - STG::Priv pm3(MIX3 | (i << 0x12)); // 321032103 +constexpr uint32_t MIX2 = 0x0002C6C6; // 210321032 +constexpr uint32_t ONES = 0x00015555; +constexpr uint32_t MIX3 = 0x00031B1B; // 321032103 +constexpr uint32_t TWOS = 0x0002AAAA; +constexpr uint32_t MIX1 = 0x0000E4E4; // 012301230 +constexpr uint32_t THREES = 0x0003FFFF; - ensure_equals("pm3.userStat == 3", pm3.userStat, 3); - ensure_equals("pm3.userConf == 2", pm3.userConf, 2); - ensure_equals("pm3.userCash == 1", pm3.userCash, 1); - ensure_equals("pm3.userPasswd == 0", pm3.userPasswd, 0); - ensure_equals("pm3.userAddDel == 3", pm3.userAddDel, 3); - ensure_equals("pm3.adminChg == 2", pm3.adminChg, 2); - ensure_equals("pm3.tariffChg == 1", pm3.tariffChg, 1); - ensure_equals("pm3.serviceChg == 0", pm3.serviceChg, 0); - ensure_equals("pm3.corpChg == 3", pm3.corpChg, 3); +} - ensure_equals("pm3.toInt() = 0x00031B1B", pm3.toInt(), static_cast(MIX3)); +BOOST_AUTO_TEST_SUITE(AdminConf) - } +BOOST_AUTO_TEST_CASE(DefaultConstructor) +{ + STG::Priv zero; + + BOOST_CHECK_EQUAL(zero.userStat, 0); + BOOST_CHECK_EQUAL(zero.userConf, 0); + BOOST_CHECK_EQUAL(zero.userCash, 0); + BOOST_CHECK_EQUAL(zero.userPasswd, 0); + BOOST_CHECK_EQUAL(zero.userAddDel, 0); + BOOST_CHECK_EQUAL(zero.adminChg, 0); + BOOST_CHECK_EQUAL(zero.tariffChg, 0); + BOOST_CHECK_EQUAL(zero.serviceChg, 0); + BOOST_CHECK_EQUAL(zero.corpChg, 0); + + BOOST_CHECK_EQUAL(zero.toInt(), 0); +} +BOOST_AUTO_TEST_CASE(UINT32Conversions) +{ + for (uint8_t i = 0; i < 4; ++i) + { + // 'i' is extra garbage in high bits + + STG::Priv priv1(ONES | (i << 0x12)); // All 1 + + BOOST_CHECK_EQUAL(priv1.userStat, 1); + BOOST_CHECK_EQUAL(priv1.userConf, 1); + BOOST_CHECK_EQUAL(priv1.userCash, 1); + BOOST_CHECK_EQUAL(priv1.userPasswd, 1); + BOOST_CHECK_EQUAL(priv1.userAddDel, 1); + BOOST_CHECK_EQUAL(priv1.adminChg, 1); + BOOST_CHECK_EQUAL(priv1.tariffChg, 1); + BOOST_CHECK_EQUAL(priv1.serviceChg, 1); + BOOST_CHECK_EQUAL(priv1.corpChg, 1); + + BOOST_CHECK_EQUAL(priv1.toInt(), static_cast(ONES)); + + STG::Priv priv2(TWOS | (i << 0x12)); // All 2 + + BOOST_CHECK_EQUAL(priv2.userStat, 2); + BOOST_CHECK_EQUAL(priv2.userConf, 2); + BOOST_CHECK_EQUAL(priv2.userCash, 2); + BOOST_CHECK_EQUAL(priv2.userPasswd, 2); + BOOST_CHECK_EQUAL(priv2.userAddDel, 2); + BOOST_CHECK_EQUAL(priv2.adminChg, 2); + BOOST_CHECK_EQUAL(priv2.tariffChg, 2); + BOOST_CHECK_EQUAL(priv2.serviceChg, 2); + BOOST_CHECK_EQUAL(priv2.corpChg, 2); + + BOOST_CHECK_EQUAL(priv2.toInt(), static_cast(TWOS)); + + STG::Priv priv3(THREES | (i << 0x12)); // All 3 + + BOOST_CHECK_EQUAL(priv3.userStat, 3); + BOOST_CHECK_EQUAL(priv3.userConf, 3); + BOOST_CHECK_EQUAL(priv3.userCash, 3); + BOOST_CHECK_EQUAL(priv3.userPasswd, 3); + BOOST_CHECK_EQUAL(priv3.userAddDel, 3); + BOOST_CHECK_EQUAL(priv3.adminChg, 3); + BOOST_CHECK_EQUAL(priv3.tariffChg, 3); + BOOST_CHECK_EQUAL(priv3.serviceChg, 3); + BOOST_CHECK_EQUAL(priv3.corpChg, 3); + + BOOST_CHECK_EQUAL(priv3.toInt(), static_cast(THREES)); + + STG::Priv pm1(MIX1 | (i << 0x12)); // 012301230 + + BOOST_CHECK_EQUAL(pm1.userStat, 0); + BOOST_CHECK_EQUAL(pm1.userConf, 1); + BOOST_CHECK_EQUAL(pm1.userCash, 2); + BOOST_CHECK_EQUAL(pm1.userPasswd, 3); + BOOST_CHECK_EQUAL(pm1.userAddDel, 0); + BOOST_CHECK_EQUAL(pm1.adminChg, 1); + BOOST_CHECK_EQUAL(pm1.tariffChg, 2); + BOOST_CHECK_EQUAL(pm1.serviceChg, 3); + BOOST_CHECK_EQUAL(pm1.corpChg, 0); + + BOOST_CHECK_EQUAL(pm1.toInt(), static_cast(MIX1)); + + STG::Priv pm2(MIX2 | (i << 0x12)); // 210321032 + + BOOST_CHECK_EQUAL(pm2.userStat, 2); + BOOST_CHECK_EQUAL(pm2.userConf, 1); + BOOST_CHECK_EQUAL(pm2.userCash, 0); + BOOST_CHECK_EQUAL(pm2.userPasswd, 3); + BOOST_CHECK_EQUAL(pm2.userAddDel, 2); + BOOST_CHECK_EQUAL(pm2.adminChg, 1); + BOOST_CHECK_EQUAL(pm2.tariffChg, 0); + BOOST_CHECK_EQUAL(pm2.serviceChg, 3); + BOOST_CHECK_EQUAL(pm2.corpChg, 2); + + BOOST_CHECK_EQUAL(pm2.toInt(), static_cast(MIX2)); + + STG::Priv pm3(MIX3 | (i << 0x12)); // 321032103 + + BOOST_CHECK_EQUAL(pm3.userStat, 3); + BOOST_CHECK_EQUAL(pm3.userConf, 2); + BOOST_CHECK_EQUAL(pm3.userCash, 1); + BOOST_CHECK_EQUAL(pm3.userPasswd, 0); + BOOST_CHECK_EQUAL(pm3.userAddDel, 3); + BOOST_CHECK_EQUAL(pm3.adminChg, 2); + BOOST_CHECK_EQUAL(pm3.tariffChg, 1); + BOOST_CHECK_EQUAL(pm3.serviceChg, 0); + BOOST_CHECK_EQUAL(pm3.corpChg, 3); + + BOOST_CHECK_EQUAL(pm3.toInt(), static_cast(MIX3)); } - } + +BOOST_AUTO_TEST_SUITE_END() diff --git a/tests/test_bfstream.cpp b/tests/test_bfstream.cpp index a3252c9e..f6c5bad7 100644 --- a/tests/test_bfstream.cpp +++ b/tests/test_bfstream.cpp @@ -1,9 +1,17 @@ -#include "tut/tut.hpp" +#define BOOST_TEST_MODULE STGBFStream #include "longstring.h" #include "stg/bfstream.h" +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wold-style-cast" +#pragma GCC diagnostic ignored "-Wunused-parameter" +#pragma GCC diagnostic ignored "-Wsign-compare" +#pragma GCC diagnostic ignored "-Wparentheses" +#include +#pragma GCC diagnostic pop + #include #include #include @@ -12,61 +20,58 @@ namespace { -class TRACKER +class Tracker { public: - TRACKER() : m_lastSize(0), m_callCount(0), m_lastBlock(NULL) {} - ~TRACKER() { delete[] m_lastBlock; } - bool Call(const void * block, size_t size) + Tracker() : m_lastSize(0), m_callCount(0), m_lastBlock(NULL) {} + ~Tracker() { delete[] m_lastBlock; } + void Call(const void* block, size_t size) { - delete[] m_lastBlock; - if (size > 0) + delete[] m_lastBlock; + if (size > 0) { - m_lastBlock = new char[size]; - memcpy(m_lastBlock, block, size); - m_result.append(m_lastBlock, size); + m_lastBlock = new char[size]; + memcpy(m_lastBlock, block, size); + m_result.append(m_lastBlock, size); } - else - m_lastBlock = NULL; - m_lastSize = size; - ++m_callCount; - return true; + else + m_lastBlock = NULL; + m_lastSize = size; + ++m_callCount; } size_t LastSize() const { return m_lastSize; } size_t CallCount() const { return m_callCount; } - const void * LastBlock() const { return m_lastBlock; } + const void* LastBlock() const { return m_lastBlock; } const std::string& Result() const { return m_result; } private: size_t m_lastSize; size_t m_callCount; - char * m_lastBlock; + char* m_lastBlock; std::string m_result; }; -bool DecryptCallback(const void * block, size_t size, void * data); +bool DecryptCallback(const void* block, size_t size, void* data); class Decryptor { public: - Decryptor(const std::string & key) + Decryptor(const std::string& key) : m_stream(key, DecryptCallback, this) {} - bool Call(const void * block, size_t size) + void Call(const void* block, size_t size) { m_stream.Put(block, size); - return true; } - bool Put(const void * block, size_t size) + void Put(const void* block, size_t size) { - const char * data = static_cast(block); + const auto* data = static_cast(block); size = strnlen(data, size); m_result.append(data, size); - return true; } void Flush() @@ -81,159 +86,123 @@ class Decryptor std::string m_result; }; -bool EncryptCallback(const void * block, size_t size, void * data) +bool EncryptCallback(const void* block, size_t size, void* data) +{ + auto& decryptor = *static_cast(data); + decryptor.Call(block, size); + return true; +} + +bool DecryptCallback(const void* block, size_t size, void* data) { -Decryptor & decryptor = *static_cast(data); -return decryptor.Call(block, size); + auto& decryptor = *static_cast(data); + decryptor.Put(block, size); + return true; +} + +bool Callback(const void* block, size_t size, void* data) +{ + auto& tracker = *static_cast(data); + tracker.Call(block, size); + return true; +} + } -bool DecryptCallback(const void * block, size_t size, void * data) +BOOST_AUTO_TEST_SUITE(BFStream) + +BOOST_AUTO_TEST_CASE(Mechanics) { -Decryptor & decryptor = *static_cast(data); -return decryptor.Put(block, size); + Tracker tracker; + STG::ENCRYPT_STREAM stream("pr7Hhen", Callback, &tracker); + BOOST_CHECK_EQUAL(tracker.CallCount(), 0); + + uint32_t block[2] = {0x12345678, 0x87654321}; + stream.Put(&block[0], sizeof(block[0])); + BOOST_CHECK_EQUAL(tracker.CallCount(), 0); + stream.Put(&block[1], sizeof(block[1])); + BOOST_CHECK_EQUAL(tracker.CallCount(), 1); + + uint32_t block2[4] = {0x12345678, 0x87654321, 0x12345678, 0x87654321}; + stream.Put(&block2[0], sizeof(block2[0]) * 3); + BOOST_CHECK_EQUAL(tracker.CallCount(), 2); + stream.Put(&block2[3], sizeof(block2[3])); + BOOST_CHECK_EQUAL(tracker.CallCount(), 3); } -bool Callback(const void * block, size_t size, void * data) +BOOST_AUTO_TEST_CASE(Encryption) { -TRACKER & tracker = *static_cast(data); -return tracker.Call(block, size); + Tracker tracker; + STG::ENCRYPT_STREAM stream("pr7Hhen", Callback, &tracker); + + uint32_t block[2] = {0x12345678, 0x87654321}; + stream.Put(&block[0], sizeof(block[0])); + BOOST_CHECK_EQUAL(tracker.LastSize(), 0); + BOOST_CHECK_EQUAL(tracker.LastBlock(), static_cast(NULL)); + stream.Put(&block[1], sizeof(block[1])); + BOOST_CHECK_EQUAL(tracker.LastSize(), 8); + const uint32_t * ptr = static_cast(tracker.LastBlock()); + BOOST_CHECK_EQUAL(ptr[0], 0xd3988cd); + BOOST_CHECK_EQUAL(ptr[1], 0x7996c6d6); + + uint32_t block2[4] = {0x12345678, 0x87654321, 0x12345678, 0x87654321}; + stream.Put(&block2[0], sizeof(block2[0]) * 3); + BOOST_CHECK_EQUAL(tracker.LastSize(), 8); + ptr = static_cast(tracker.LastBlock()); + BOOST_CHECK_EQUAL(ptr[0], 0xd3988cd); + BOOST_CHECK_EQUAL(ptr[1], 0x7996c6d6); + + stream.Put(&block2[3], sizeof(block2[3])); + BOOST_CHECK_EQUAL(tracker.LastSize(), 8); + ptr = static_cast(tracker.LastBlock()); + BOOST_CHECK_EQUAL(ptr[0], 0xd3988cd); + BOOST_CHECK_EQUAL(ptr[1], 0x7996c6d6); } +BOOST_AUTO_TEST_CASE(LongStringProcessing) +{ + Tracker tracker; + STG::ENCRYPT_STREAM estream("pr7Hhen", Callback, &tracker); + const std::string source = "This is a test long string for checking stream encryption/decryption. \"abcdefghijklmnopqrstuvwxyz 0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ\""; + std::vector buffer(source.length() + 9, 0); + + estream.Put(source.c_str(), source.length() + 1, true); + BOOST_CHECK(tracker.LastSize() >= source.length() + 1); + BOOST_CHECK(tracker.LastBlock() != NULL); + memcpy(buffer.data(), tracker.LastBlock(), std::min(tracker.LastSize(), buffer.size())); + + STG::DECRYPT_STREAM dstream("pr7Hhen", Callback, &tracker); + dstream.Put(buffer.data(), buffer.size(), true); + BOOST_CHECK(tracker.LastSize() >= buffer.size()); + BOOST_CHECK(tracker.LastBlock() != NULL); + memcpy(buffer.data(), tracker.LastBlock(), std::min(tracker.LastSize(), buffer.size())); + + BOOST_CHECK_EQUAL(std::string(buffer.data()), source); } -namespace tut +BOOST_AUTO_TEST_CASE(VeryLongStringProcessing) { - struct bfstream_data { - }; - - typedef test_group tg; - tg bfstream_test_group("BFStream tests group"); - - typedef tg::object testobject; - - template<> - template<> - void testobject::test<1>() - { - set_test_name("Check bfstream mechanics"); - - TRACKER tracker; - STG::ENCRYPT_STREAM stream("pr7Hhen", Callback, &tracker); - ensure_equals("CallCount() == 0 after construction", tracker.CallCount(), 0); - - uint32_t block[2] = {0x12345678, 0x87654321}; - stream.Put(&block[0], sizeof(block[0])); - ensure_equals("CallCount() == 0 after first put", tracker.CallCount(), 0); - stream.Put(&block[1], sizeof(block[1])); - ensure_equals("CallCount() == 1 after second put", tracker.CallCount(), 1); - - uint32_t block2[4] = {0x12345678, 0x87654321, 0x12345678, 0x87654321}; - stream.Put(&block2[0], sizeof(block2[0]) * 3); - ensure_equals("CallCount() == 2 after third put", tracker.CallCount(), 2); - stream.Put(&block2[3], sizeof(block2[3])); - ensure_equals("CallCount() == 3 after fourth put", tracker.CallCount(), 3); - } - - template<> - template<> - void testobject::test<2>() - { - set_test_name("Check bfstream encryption"); - - TRACKER tracker; - STG::ENCRYPT_STREAM stream("pr7Hhen", Callback, &tracker); - - uint32_t block[2] = {0x12345678, 0x87654321}; - stream.Put(&block[0], sizeof(block[0])); - ensure_equals("LastSize() == 0 after first put", tracker.LastSize(), 0); - ensure_equals("LastBlock() == NULL after first put", tracker.LastBlock(), static_cast(NULL)); - stream.Put(&block[1], sizeof(block[1])); - ensure_equals("LastSize() == 8 after second put", tracker.LastSize(), 8); - const uint32_t * ptr = static_cast(tracker.LastBlock()); - ensure_equals("ptr[0] == 0xd3988cd after second put", ptr[0], 0xd3988cd); - ensure_equals("ptr[1] == 0x7996c6d6 after second put", ptr[1], 0x7996c6d6); - - uint32_t block2[4] = {0x12345678, 0x87654321, 0x12345678, 0x87654321}; - stream.Put(&block2[0], sizeof(block2[0]) * 3); - ensure_equals("LastSize() == 8 after third put", tracker.LastSize(), 8); - ptr = static_cast(tracker.LastBlock()); - ensure_equals("ptr[0] == 0xd3988cd after third put", ptr[0], 0xd3988cd); - ensure_equals("ptr[1] == 0x7996c6d6 after third put", ptr[1], 0x7996c6d6); - - stream.Put(&block2[3], sizeof(block2[3])); - ensure_equals("LastSize() == 8 after fourth put", tracker.LastSize(), 8); - ptr = static_cast(tracker.LastBlock()); - ensure_equals("ptr[0] == 0xd3988cd after fourth put", ptr[0], 0xd3988cd); - ensure_equals("ptr[1] == 0x7996c6d6 after fourth put", ptr[1], 0x7996c6d6); - } - - template<> - template<> - void testobject::test<3>() - { - set_test_name("Check bfstream long string processing"); - - TRACKER tracker; - STG::ENCRYPT_STREAM estream("pr7Hhen", Callback, &tracker); - std::string source = "This is a test long string for checking stream encryption/decryption. \"abcdefghijklmnopqrstuvwxyz 0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ\""; - char buffer[source.length() + 9]; - memset(buffer, 0, sizeof(buffer)); - - estream.Put(source.c_str(), source.length() + 1, true); - ensure("Encryption long string LastSize()", tracker.LastSize() >= source.length() + 1); - ensure("Encryption long string LastBlock() != NULL", tracker.LastBlock() != NULL); - memcpy(buffer, tracker.LastBlock(), std::min(tracker.LastSize(), sizeof(buffer))); - - STG::DECRYPT_STREAM dstream("pr7Hhen", Callback, &tracker); - dstream.Put(buffer, sizeof(buffer), true); - ensure("Decryption long string LastSize() decryption", tracker.LastSize() >= sizeof(buffer)); - ensure("Decryption long string LastBlock() != NULL", tracker.LastBlock() != NULL); - memcpy(buffer, tracker.LastBlock(), std::min(tracker.LastSize(), sizeof(buffer))); - - ensure_equals("Decrypt(Encrypt(source)) == source", std::string(buffer), source); - } - - template<> - template<> - void testobject::test<4>() - { - set_test_name("Check bfstream very long string processing"); - - Decryptor decryptor("pr7Hhen"); - STG::ENCRYPT_STREAM estream("pr7Hhen", EncryptCallback, &decryptor); - //char buffer[source.length() + 9]; - //memset(buffer, 0, sizeof(buffer)); - - estream.Put(longString.c_str(), longString.length() + 1, true); - //ensure("Encryption long string LastSize()", tracker.LastSize() >= source.length() + 1); - //ensure("Encryption long string LastBlock() != NULL", tracker.LastBlock() != NULL); - //memcpy(buffer, tracker.LastBlock(), std::min(tracker.LastSize(), sizeof(buffer))); - - //dstream.Put(buffer, sizeof(buffer), true); - //ensure("Decryption long string LastSize() decryption", tracker.LastSize() >= sizeof(buffer)); - //ensure("Decryption long string LastBlock() != NULL", tracker.LastBlock() != NULL); - //memcpy(buffer, tracker.LastBlock(), std::min(tracker.LastSize(), sizeof(buffer))); - - ensure_equals("Decrypt(Encrypt(source)) == source", decryptor.Result(), longString); - } - - template<> - template<> - void testobject::test<5>() - { - set_test_name("Check bfstream mechanics"); - - TRACKER tracker; - STG::ENCRYPT_STREAM stream("pr7Hhen", Callback, &tracker); - ensure_equals("CallCount() == 0 after construction", tracker.CallCount(), 0); - - uint32_t block[2] = {0x12345678, 0x87654321}; - stream.Put(&block[0], sizeof(block[0])); - ensure_equals("CallCount() == 0 after first put", tracker.CallCount(), 0); - stream.Put(&block[1], sizeof(block[1])); - ensure_equals("CallCount() == 1 after second put", tracker.CallCount(), 1); - stream.Put(&block[0], 0, true); // Check last callback - ensure_equals("CallCount() == 2 after third (null) put", tracker.CallCount(), 2); - } + Decryptor decryptor("pr7Hhen"); + STG::ENCRYPT_STREAM estream("pr7Hhen", EncryptCallback, &decryptor); + + estream.Put(longString.c_str(), longString.length() + 1, true); + BOOST_CHECK_EQUAL(decryptor.Result(), longString); } + +BOOST_AUTO_TEST_CASE(Mechanics2) +{ + Tracker tracker; + STG::ENCRYPT_STREAM stream("pr7Hhen", Callback, &tracker); + BOOST_CHECK_EQUAL(tracker.CallCount(), 0); + + uint32_t block[2] = {0x12345678, 0x87654321}; + stream.Put(&block[0], sizeof(block[0])); + BOOST_CHECK_EQUAL(tracker.CallCount(), 0); + stream.Put(&block[1], sizeof(block[1])); + BOOST_CHECK_EQUAL(tracker.CallCount(), 1); + stream.Put(&block[0], 0, true); // Check last callback + BOOST_CHECK_EQUAL(tracker.CallCount(), 2); +} + +BOOST_AUTO_TEST_SUITE_END() diff --git a/tests/test_crypto.cpp b/tests/test_crypto.cpp index 8db8ab72..bf4ecde3 100644 --- a/tests/test_crypto.cpp +++ b/tests/test_crypto.cpp @@ -1,20 +1,34 @@ -#include "tut/tut.hpp" +#define BOOST_TEST_MODULE STGCrypto #include "stg/blowfish.h" +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wold-style-cast" +#pragma GCC diagnostic ignored "-Wunused-parameter" +#pragma GCC diagnostic ignored "-Wsign-compare" +#pragma GCC diagnostic ignored "-Wparentheses" +#include +#pragma GCC diagnostic pop + +#include +#include +#include + namespace { -bool equalCtx(const BLOWFISH_CTX & a, const BLOWFISH_CTX & b) +bool equalCtx(const BLOWFISH_CTX& a, const BLOWFISH_CTX& b) { for (size_t i = 0; i < sizeof(a.P); ++i) - if (a.P[i] != b.P[i]) { + if (a.P[i] != b.P[i]) + { //printf("Failed for P at %d: 0%x != 0%x\n", i, a.P[i], b.P[i]); return false; } for (size_t i = 0; i < 4; ++i) for (size_t j = 0; j < 256; ++j) - if (a.S[i][j] != b.S[i][j]) { + if (a.S[i][j] != b.S[i][j]) + { //printf("Failed for S at %d, %d: 0%x != 0%x\n", i, j, a.S[i][j], b.S[i][j]); return false; @@ -24,13 +38,13 @@ bool equalCtx(const BLOWFISH_CTX & a, const BLOWFISH_CTX & b) bool equalString(const char* a, const char* b, size_t length) { - bool res = true; for (size_t i = 0; i < length; ++i) - if (a[i] != b[i]) { + if (a[i] != b[i]) + { //printf("Failed at pos %d: %hhu != %hhu\n", i, a[i], b[i]); - res = false; + return false; } - return res; + return true; } const BLOWFISH_CTX testCtx = @@ -318,153 +332,109 @@ const unsigned char testString[] = { 68, 100, 2, 115, 6, 54, 226, 228 }; } // namespace anonymous -namespace tut -{ - struct crypto_data { - }; - - typedef test_group tg; - tg crypto_test_group("Crypto tests group"); - - typedef tg::object testobject; - - template<> - template<> - void testobject::test<1>() - { - set_test_name("Check context creation"); - - BLOWFISH_CTX ctx; - InitContext("pr7Hhen", 7, &ctx); - ensure("ctx == testCtx", equalCtx(ctx, testCtx)); - } - - template<> - template<> - void testobject::test<2>() - { - set_test_name("Check encryption"); - - BLOWFISH_CTX ctx; - InitContext("pr7Hhen", 7, &ctx); - uint32_t a = 0x12345678; - uint32_t b = 0x87654321; - Blowfish_Encrypt(&ctx, &a, &b); - - ensure_equals("a == 0xd3988cd", a, 0xd3988cd); - ensure_equals("b == 0x7996c6d6", b, 0x7996c6d6); - } - - template<> - template<> - void testobject::test<3>() - { - set_test_name("Check decryption"); - - BLOWFISH_CTX ctx; - InitContext("pr7Hhen", 7, &ctx); - uint32_t a = 0xd3988cd; - uint32_t b = 0x7996c6d6; - Blowfish_Decrypt(&ctx, &a, &b); - - ensure_equals("a == 0x12345678", a, 0x12345678); - ensure_equals("b == 0x87654321", b, 0x87654321); - } - - template<> - template<> - void testobject::test<4>() - { - set_test_name("Check block encryption"); - - BLOWFISH_CTX ctx; - InitContext("pr7Hhen", 7, &ctx); - uint32_t block[2] = {0x12345678, 0x87654321}; - EncryptBlock(&block, &block, &ctx); - - ensure_equals("block[0] == 0xd3988cd", block[0], 0xd3988cd); - ensure_equals("block[1] == 0x7996c6d6", block[1], 0x7996c6d6); - } - - template<> - template<> - void testobject::test<5>() - { - set_test_name("Check block decryption"); - - BLOWFISH_CTX ctx; - InitContext("pr7Hhen", 7, &ctx); - uint32_t block[2] = {0xd3988cd, 0x7996c6d6}; - DecryptBlock(&block, &block, &ctx); - - ensure_equals("block[0] == 0x12345678", block[0], 0x12345678); - ensure_equals("block[1] == 0x87654321", block[1], 0x87654321); - } - - template<> - template<> - void testobject::test<6>() - { - set_test_name("Check string encryption"); +BOOST_AUTO_TEST_SUITE(Crypto) - BLOWFISH_CTX ctx; - InitContext("pr7Hhen", 7, &ctx); - char res[8]; - EncryptString(res, "testtest", 8, &ctx); +BOOST_AUTO_TEST_CASE(ContextCreation) +{ + BLOWFISH_CTX ctx; + InitContext("pr7Hhen", 7, &ctx); + BOOST_CHECK(equalCtx(ctx, testCtx)); +} - ensure("EncryptString(\"testtest\") == testString", equalString(res, (char *)testString, 8)); - } +BOOST_AUTO_TEST_CASE(Encryption) +{ + BLOWFISH_CTX ctx; + InitContext("pr7Hhen", 7, &ctx); + uint32_t a = 0x12345678; + uint32_t b = 0x87654321; + Blowfish_Encrypt(&ctx, &a, &b); + + BOOST_CHECK_EQUAL(a, 0xd3988cd); + BOOST_CHECK_EQUAL(b, 0x7996c6d6); +} - template<> - template<> - void testobject::test<7>() - { - set_test_name("Check long string encryption"); +BOOST_AUTO_TEST_CASE(Decryption) +{ + BLOWFISH_CTX ctx; + InitContext("pr7Hhen", 7, &ctx); + uint32_t a = 0xd3988cd; + uint32_t b = 0x7996c6d6; + Blowfish_Decrypt(&ctx, &a, &b); + + BOOST_CHECK_EQUAL(a, 0x12345678); + BOOST_CHECK_EQUAL(b, 0x87654321); +} - BLOWFISH_CTX ctx; - InitContext("pr7Hhen", 7, &ctx); - std::string source("abcdefghijklmnopqrstuvwxyz 0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ"); - char longTest[source.length() + 8]; - EncryptString(longTest, source.c_str(), source.length() + 1, &ctx); - DecryptString(longTest, longTest, sizeof(longTest), &ctx); +BOOST_AUTO_TEST_CASE(BlockEncryption) +{ + BLOWFISH_CTX ctx; + InitContext("pr7Hhen", 7, &ctx); + uint32_t block[2] = {0x12345678, 0x87654321}; + EncryptBlock(&block, &block, &ctx); - ensure_equals("DecryptString(EncryptString(longTest)) == longTest", source, std::string(longTest)); - } + BOOST_CHECK_EQUAL(block[0], 0xd3988cd); + BOOST_CHECK_EQUAL(block[1], 0x7996c6d6); +} - template<> - template<> - void testobject::test<8>() - { - set_test_name("Check old string encryption"); +BOOST_AUTO_TEST_CASE(BlockDecryption) +{ + BLOWFISH_CTX ctx; + InitContext("pr7Hhen", 7, &ctx); + uint32_t block[2] = {0xd3988cd, 0x7996c6d6}; + DecryptBlock(&block, &block, &ctx); - BLOWFISH_CTX ctx; - InitContext("123456", 7, &ctx); - const unsigned char source[] = {0xe9, 0xfe, 0xcb, 0xc5, 0xad, 0x3e, 0x87, 0x39, - 0x3d, 0xd5, 0xf4, 0xed, 0xb0, 0x15, 0xe6, 0xcb, - 0x3d, 0xd5, 0xf4, 0xed, 0xb0, 0x15, 0xe6, 0xcb, - 0x3d, 0xd5, 0xf4, 0xed, 0xb0, 0x15, 0xe6, 0xcb}; - char res[32]; - DecryptString(res, source, 32, &ctx); + BOOST_CHECK_EQUAL(block[0], 0x12345678); + BOOST_CHECK_EQUAL(block[1], 0x87654321); +} - ensure_equals("DecryptString(...) == 'admin'", std::string(res), "admin"); - } +BOOST_AUTO_TEST_CASE(StringEncryption) +{ + BLOWFISH_CTX ctx; + InitContext("pr7Hhen", 7, &ctx); + char res[8]; + EncryptString(res, "testtest", 8, &ctx); - template<> - template<> - void testobject::test<9>() - { - set_test_name("Check new string encryption"); + BOOST_CHECK(equalString(res, reinterpret_cast(testString), 8)); +} - BLOWFISH_CTX ctx; - InitContext("123456", 7, &ctx); - const unsigned char source[] = {0xe9, 0xfe, 0xcb, 0xc5, 0xad, 0x3e, 0x87, 0x39, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; - char res[32]; - DecryptString(res, source, 32, &ctx); +BOOST_AUTO_TEST_CASE(LongStringEncryption) +{ + BLOWFISH_CTX ctx; + InitContext("pr7Hhen", 7, &ctx); + const std::string source("abcdefghijklmnopqrstuvwxyz 0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ"); + std::vector longTest(source.length() + 8); + EncryptString(longTest.data(), source.c_str(), source.length() + 1, &ctx); + DecryptString(longTest.data(), longTest.data(), longTest.size(), &ctx); + + BOOST_CHECK_EQUAL(source, std::string(longTest.data())); +} - ensure_equals("DecryptString(...) == 'admin'", std::string(res), "admin"); - } +BOOST_AUTO_TEST_CASE(OldStringEncryption) +{ + BLOWFISH_CTX ctx; + InitContext("123456", 7, &ctx); + const unsigned char source[] = {0xe9, 0xfe, 0xcb, 0xc5, 0xad, 0x3e, 0x87, 0x39, + 0x3d, 0xd5, 0xf4, 0xed, 0xb0, 0x15, 0xe6, 0xcb, + 0x3d, 0xd5, 0xf4, 0xed, 0xb0, 0x15, 0xe6, 0xcb, + 0x3d, 0xd5, 0xf4, 0xed, 0xb0, 0x15, 0xe6, 0xcb}; + std::array res{}; + DecryptString(res.data(), source, res.size(), &ctx); + + BOOST_CHECK_EQUAL(std::string(res.data()), "admin"); +} +BOOST_AUTO_TEST_CASE(NewStringEncryption) +{ + BLOWFISH_CTX ctx; + InitContext("123456", 7, &ctx); + const unsigned char source[] = {0xe9, 0xfe, 0xcb, 0xc5, 0xad, 0x3e, 0x87, 0x39, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + std::array res{}; + DecryptString(res.data(), source, 32, &ctx); + + BOOST_CHECK_EQUAL(std::string(res.data()), "admin"); } + +BOOST_AUTO_TEST_SUITE_END() diff --git a/tests/test_raw_ip.cpp b/tests/test_raw_ip.cpp index 90a2cdd5..a7b3e8a5 100644 --- a/tests/test_raw_ip.cpp +++ b/tests/test_raw_ip.cpp @@ -1,114 +1,108 @@ -#include -#include -#include +#define BOOST_TEST_MODULE STGRawIP + +#include "raw_ip_packet_old.h" +#include "stg/raw_ip_packet.h" + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wold-style-cast" +#pragma GCC diagnostic ignored "-Wunused-parameter" +#pragma GCC diagnostic ignored "-Wsign-compare" +#pragma GCC diagnostic ignored "-Wparentheses" +#include +#pragma GCC diagnostic pop #include #include #include #include -#include "tut/tut.hpp" - -#include "raw_ip_packet_old.h" -#include "stg/raw_ip_packet.h" +#include +#include +#include -#ifndef ITERATIONS -#define ITERATIONS 1000 -#endif +namespace +{ -void genVector(uint8_t * buf); +constexpr size_t ITERATIONS = 1000; -namespace STG +std::array genVector() { -std::ostream & operator<<(std::ostream & stream, const RawPacket & p); + std::array res; + for (size_t i = 0; i < 68; ++i) + res[i] = rand() % 256; + res[0] = (res[0] & 0xF0) | 0x05; // Fix header length + return res; } -namespace tut -{ - struct rp_data { - }; - - typedef test_group tg; - tg rp_test_group("STG::RawPacket tests group"); - - typedef tg::object testobject; - - template<> - template<> - void testobject::test<1>() - { - set_test_name("Check structure consistency"); - - STG::RawPacket rp; - rp.rawPacket.header.ipHeader.ip_v = 4; - rp.rawPacket.header.ipHeader.ip_hl = 5; - rp.rawPacket.header.ipHeader.ip_tos = 0; - rp.rawPacket.header.ipHeader.ip_len = htons(40); // 20 of header + 20 of data - rp.rawPacket.header.ipHeader.ip_p = 6; - rp.rawPacket.header.ipHeader.ip_src.s_addr = inet_addr("192.168.0.1"); - rp.rawPacket.header.ipHeader.ip_dst.s_addr = inet_addr("192.168.0.101"); - rp.rawPacket.header.sPort = htons(80); - rp.rawPacket.header.dPort = htons(38546); - - ensure_equals("IP header size (explicitly)", sizeof(rp.rawPacket.header.ipHeader), static_cast(20)); - ensure_equals("IP version", rp.GetIPVersion(), 4); - ensure_equals("IP header size (with options)", rp.GetHeaderLen(), 20); - ensure_equals("Underlying protocol version", rp.GetProto(), 6); - ensure_equals("Packet length", rp.GetLen(), static_cast(40)); - ensure_equals("Source IP address", rp.GetSrcIP(), inet_addr("192.168.0.1")); - ensure_equals("Destination IP address", rp.GetDstIP(), inet_addr("192.168.0.101")); - ensure_equals("Source port number", rp.GetSrcPort(), 80); - ensure_equals("Destination port number", rp.GetDstPort(), 38546); - } - - template<> - template<> - void testobject::test<2>() - { - srand(time(NULL)); - for (size_t i = 0; i < ITERATIONS; ++i) { - RAW_PACKET_OLD p1; - STG::RawPacket p2; - STG::RawPacket p3; - - uint8_t buf[68]; - genVector(buf); - - memcpy(p1.pckt, buf, 68); - memcpy(p2.rawPacket.data, buf, 68); - memcpy(p3.rawPacket.data, buf, 68); - - ensure_equals("IP versions", p1.GetIPVersion(), p2.GetIPVersion()); - ensure_equals("IP headers length", p1.GetHeaderLen(), p2.GetHeaderLen()); - ensure_equals("Protocols", p1.GetProto(), p2.GetProto()); - ensure_equals("Source IPs", p1.GetSrcIP(), p2.GetSrcIP()); - ensure_equals("Destination IPs", p1.GetDstIP(), p2.GetDstIP()); - ensure_equals("Source ports", p1.GetSrcPort(), p2.GetSrcPort()); - ensure_equals("Destination ports", p1.GetDstPort(), p2.GetDstPort()); - - ensure_equals("Self equallity", p2, p3); - ensure_equals("Reverse self equallity", p3, p2); - } - } } -inline -void genVector(uint8_t * buf) +namespace std { - for (size_t i = 0; i < 68; ++i) { - buf[i] = rand() % 256; - } - buf[0] = (buf[0] & 0xF0) | 0x05; // Fix header length -} -std::ostream & STG::operator<<(std::ostream & stream, const RawPacket & p) +std::ostream & operator<<(std::ostream& stream, const STG::RawPacket& p) { - stream.unsetf(std::ios::dec); stream.setf(std::ios::hex); for (size_t i = 0; i < sizeof(p.rawPacket.data); ++i) { stream << static_cast(p.rawPacket.data[i]); } - stream.unsetf(std::ios::hex); stream.setf(std::ios::dec); return stream; } + +} + +BOOST_AUTO_TEST_SUITE(RawIP) + +BOOST_AUTO_TEST_CASE(StructureConsistency) +{ + STG::RawPacket rp; + rp.rawPacket.header.ipHeader.ip_v = 4; + rp.rawPacket.header.ipHeader.ip_hl = 5; + rp.rawPacket.header.ipHeader.ip_tos = 0; + rp.rawPacket.header.ipHeader.ip_len = htons(40); // 20 of header + 20 of data + rp.rawPacket.header.ipHeader.ip_p = 6; + rp.rawPacket.header.ipHeader.ip_src.s_addr = inet_addr("192.168.0.1"); + rp.rawPacket.header.ipHeader.ip_dst.s_addr = inet_addr("192.168.0.101"); + rp.rawPacket.header.sPort = htons(80); + rp.rawPacket.header.dPort = htons(38546); + + BOOST_CHECK_EQUAL(sizeof(rp.rawPacket.header.ipHeader), static_cast(20)); + BOOST_CHECK_EQUAL(rp.GetIPVersion(), 4); + BOOST_CHECK_EQUAL(rp.GetHeaderLen(), 20); + BOOST_CHECK_EQUAL(rp.GetProto(), 6); + BOOST_CHECK_EQUAL(rp.GetLen(), static_cast(40)); + BOOST_CHECK_EQUAL(rp.GetSrcIP(), inet_addr("192.168.0.1")); + BOOST_CHECK_EQUAL(rp.GetDstIP(), inet_addr("192.168.0.101")); + BOOST_CHECK_EQUAL(rp.GetSrcPort(), 80); + BOOST_CHECK_EQUAL(rp.GetDstPort(), 38546); +} + +BOOST_AUTO_TEST_CASE(RandomTests) +{ + srand(time(NULL)); + for (size_t i = 0; i < ITERATIONS; ++i) + { + RAW_PACKET_OLD p1; + STG::RawPacket p2; + STG::RawPacket p3; + + const auto buf = genVector(); + + memcpy(p1.pckt, buf.data(), 68); + memcpy(p2.rawPacket.data, buf.data(), 68); + memcpy(p3.rawPacket.data, buf.data(), 68); + + BOOST_CHECK_EQUAL(p1.GetIPVersion(), p2.GetIPVersion()); + BOOST_CHECK_EQUAL(p1.GetHeaderLen(), p2.GetHeaderLen()); + BOOST_CHECK_EQUAL(p1.GetProto(), p2.GetProto()); + BOOST_CHECK_EQUAL(p1.GetSrcIP(), p2.GetSrcIP()); + BOOST_CHECK_EQUAL(p1.GetDstIP(), p2.GetDstIP()); + BOOST_CHECK_EQUAL(p1.GetSrcPort(), p2.GetSrcPort()); + BOOST_CHECK_EQUAL(p1.GetDstPort(), p2.GetDstPort()); + + BOOST_CHECK_EQUAL(p2, p3); + BOOST_CHECK_EQUAL(p3, p2); + } +} + +BOOST_AUTO_TEST_SUITE_END() diff --git a/tests/test_tariff.cpp b/tests/test_tariff.cpp index f5b7d57e..da681a21 100644 --- a/tests/test_tariff.cpp +++ b/tests/test_tariff.cpp @@ -1,509 +1,458 @@ -#include "tut/tut.hpp" +#define BOOST_TEST_MODULE STGTariff #include "stg/tariff_conf.h" #include "tariff_impl.h" -namespace tut +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wold-style-cast" +#pragma GCC diagnostic ignored "-Wunused-parameter" +#pragma GCC diagnostic ignored "-Wsign-compare" +#pragma GCC diagnostic ignored "-Wparentheses" +#include +#pragma GCC diagnostic pop + +BOOST_AUTO_TEST_SUITE(Tariffs) + +BOOST_AUTO_TEST_CASE(Construction) +{ + STG::TariffData td("test"); + td.tariffConf.fee = 1; + td.tariffConf.free = 2; + td.tariffConf.traffType = STG::Tariff::TRAFF_UP_DOWN; + td.tariffConf.passiveCost = 4; + td.dirPrice[0].mDay = 30; + td.dirPrice[0].hDay = 9; + td.dirPrice[0].mNight = 30; + td.dirPrice[0].hNight = 21; + td.dirPrice[0].priceDayA = 0; + td.dirPrice[0].priceDayB = 1; + td.dirPrice[0].priceNightA = 2; + td.dirPrice[0].priceNightB = 3; + td.dirPrice[0].threshold = 4; + td.dirPrice[0].singlePrice = 0; + td.dirPrice[0].noDiscount = 0; + const STG::TariffImpl tariff(td); + + BOOST_CHECK(tariff.GetFreeMb() == td.tariffConf.free); + BOOST_CHECK(tariff.GetPassiveCost() == td.tariffConf.passiveCost); + BOOST_CHECK(tariff.GetFee() == td.tariffConf.fee); + BOOST_CHECK(tariff.GetFree() == td.tariffConf.free); + BOOST_CHECK(tariff.GetName() == td.tariffConf.name); + BOOST_CHECK(tariff.GetTraffType() == td.tariffConf.traffType); + BOOST_CHECK(tariff.GetThreshold(0) == td.dirPrice[0].threshold); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(6, 0), 6); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(5, 1), 6); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(4, 2), 6); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(3, 3), 6); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(2, 4), 6); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(1, 5), 6); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(0, 6), 6); +} + +BOOST_AUTO_TEST_CASE(TraffTypes) +{ + STG::TariffData td("test"); + td.tariffConf.fee = 1; + td.tariffConf.free = 2; + td.tariffConf.traffType = STG::Tariff::TRAFF_UP; + td.tariffConf.passiveCost = 4; + td.dirPrice[0].mDay = 30; + td.dirPrice[0].hDay = 9; + td.dirPrice[0].mNight = 30; + td.dirPrice[0].hNight = 21; + td.dirPrice[0].priceDayA = 0; + td.dirPrice[0].priceDayB = 1; + td.dirPrice[0].priceNightA = 2; + td.dirPrice[0].priceNightB = 3; + td.dirPrice[0].threshold = 4; + td.dirPrice[0].singlePrice = 0; + td.dirPrice[0].noDiscount = 0; + STG::TariffImpl tariff(td); + + BOOST_CHECK(tariff.GetTraffType() == STG::Tariff::TRAFF_UP); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(6, 0), 6); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(5, 1), 5); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(4, 2), 4); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(3, 3), 3); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(2, 4), 2); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(1, 5), 1); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(0, 6), 0); + + td.tariffConf.traffType = STG::Tariff::TRAFF_DOWN; + tariff = td; + + BOOST_CHECK(tariff.GetTraffType() == STG::Tariff::TRAFF_DOWN); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(6, 0), 0); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(5, 1), 1); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(4, 2), 2); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(3, 3), 3); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(2, 4), 4); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(1, 5), 5); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(0, 6), 6); + + td.tariffConf.traffType = STG::Tariff::TRAFF_MAX; + tariff = td; + + BOOST_CHECK(tariff.GetTraffType() == STG::Tariff::TRAFF_MAX); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(6, 0), 6); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(5, 1), 5); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(4, 2), 4); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(3, 3), 3); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(2, 4), 4); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(1, 5), 5); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(0, 6), 6); + + td.tariffConf.traffType = STG::Tariff::TRAFF_UP_DOWN; + tariff = td; + + BOOST_CHECK(tariff.GetTraffType() == STG::Tariff::TRAFF_UP_DOWN); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(6, 0), 6); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(5, 1), 6); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(4, 2), 6); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(3, 3), 6); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(2, 4), 6); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(1, 5), 6); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(0, 6), 6); +} + +BOOST_AUTO_TEST_CASE(NormalIntervalPrices) +{ + STG::TariffData td("test"); + td.tariffConf.fee = 1; + td.tariffConf.free = 2; + td.tariffConf.traffType = STG::Tariff::TRAFF_UP_DOWN; + td.tariffConf.passiveCost = 4; + td.dirPrice[0].mDay = 30; + td.dirPrice[0].hDay = 9; + td.dirPrice[0].mNight = 30; + td.dirPrice[0].hNight = 21; + td.dirPrice[0].priceDayA = 0; + td.dirPrice[0].priceDayB = 1; + td.dirPrice[0].priceNightA = 2; + td.dirPrice[0].priceNightB = 3; + td.dirPrice[0].threshold = 4; + td.dirPrice[0].singlePrice = 0; + td.dirPrice[0].noDiscount = 0; + STG::TariffImpl tariff(td); + + BOOST_CHECK_EQUAL(tariff.GetPriceWithTraffType(0, 0 * 1024 * 1024, 0, 1286461245), 0); // Near 17:30, 0 < 4 DA + BOOST_CHECK_EQUAL(tariff.GetPriceWithTraffType(0, 6 * 1024 * 1024, 0, 1286461245), 1); // Near 17:30, 6 > 4 DB + BOOST_CHECK_EQUAL(tariff.GetPriceWithTraffType(0, 0 * 1024 * 1024, 0, 1286479245), 2); // Near 22:30, 0 < 4 NA + BOOST_CHECK_EQUAL(tariff.GetPriceWithTraffType(0, 6 * 1024 * 1024, 0, 1286479245), 3); // Near 22:30, 6 > 4 NB + + td.dirPrice[0].singlePrice = 1; + tariff = td; + + BOOST_CHECK_EQUAL(tariff.GetPriceWithTraffType(0, 0 * 1024 * 1024, 0, 1286461245), 0); // Near 17:30, 0 < 4 DA + BOOST_CHECK_EQUAL(tariff.GetPriceWithTraffType(0, 6 * 1024 * 1024, 0, 1286461245), 1); // Near 17:30, 6 > 4 DB + BOOST_CHECK_EQUAL(tariff.GetPriceWithTraffType(0, 0 * 1024 * 1024, 0, 1286479245), 0); // Near 22:30, 0 < 4 DA + BOOST_CHECK_EQUAL(tariff.GetPriceWithTraffType(0, 6 * 1024 * 1024, 0, 1286479245), 1); // Near 22:30, 6 > 4 DB + + td.dirPrice[0].singlePrice = 0; + td.dirPrice[0].noDiscount = 1; + tariff = td; + + BOOST_CHECK_EQUAL(tariff.GetPriceWithTraffType(0, 0 * 1024 * 1024, 0, 1286461245), 0); // Near 17:30, 0 < 4 DA + BOOST_CHECK_EQUAL(tariff.GetPriceWithTraffType(0, 6 * 1024 * 1024, 0, 1286461245), 0); // Near 17:30, 6 > 4 DA + BOOST_CHECK_EQUAL(tariff.GetPriceWithTraffType(0, 0 * 1024 * 1024, 0, 1286479245), 2); // Near 22:30, 0 < 4 NA + BOOST_CHECK_EQUAL(tariff.GetPriceWithTraffType(0, 6 * 1024 * 1024, 0, 1286479245), 2); // Near 22:30, 6 > 4 NA + + td.dirPrice[0].singlePrice = 1; + td.dirPrice[0].noDiscount = 1; + tariff = td; + + BOOST_CHECK_EQUAL(tariff.GetPriceWithTraffType(0, 0 * 1024 * 1024, 0, 1286461245), 0); // Near 17:30, 0 < 4 DA + BOOST_CHECK_EQUAL(tariff.GetPriceWithTraffType(0, 6 * 1024 * 1024, 0, 1286461245), 0); // Near 17:30, 6 > 4 DA + BOOST_CHECK_EQUAL(tariff.GetPriceWithTraffType(0, 0 * 1024 * 1024, 0, 1286479245), 0); // Near 22:30, 0 < 4 DA + BOOST_CHECK_EQUAL(tariff.GetPriceWithTraffType(0, 6 * 1024 * 1024, 0, 1286479245), 0); // Near 22:30, 6 > 4 DA +} + +BOOST_AUTO_TEST_CASE(ConstructionForDayNightInversion) +{ + STG::TariffData td("test"); + td.tariffConf.fee = 1; + td.tariffConf.free = 2; + td.tariffConf.traffType = STG::Tariff::TRAFF_UP_DOWN; + td.tariffConf.passiveCost = 4; + td.dirPrice[0].mDay = 30; + td.dirPrice[0].hDay = 21; + td.dirPrice[0].mNight = 30; + td.dirPrice[0].hNight = 9; + td.dirPrice[0].priceDayA = 0; + td.dirPrice[0].priceDayB = 1; + td.dirPrice[0].priceNightA = 2; + td.dirPrice[0].priceNightB = 3; + td.dirPrice[0].threshold = 4; + td.dirPrice[0].singlePrice = 0; + td.dirPrice[0].noDiscount = 0; + const STG::TariffImpl tariff(td); + + BOOST_CHECK(tariff.GetFreeMb() == td.tariffConf.free); + BOOST_CHECK(tariff.GetPassiveCost() == td.tariffConf.passiveCost); + BOOST_CHECK(tariff.GetFee() == td.tariffConf.fee); + BOOST_CHECK(tariff.GetFree() == td.tariffConf.free); + BOOST_CHECK(tariff.GetName() == td.tariffConf.name); + BOOST_CHECK(tariff.GetTraffType() == td.tariffConf.traffType); + BOOST_CHECK(tariff.GetThreshold(0) == td.dirPrice[0].threshold); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(6, 0), 6); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(5, 1), 6); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(4, 2), 6); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(3, 3), 6); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(2, 4), 6); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(1, 5), 6); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(0, 6), 6); +} + +BOOST_AUTO_TEST_CASE(TraffTypeForDayNightInversion) +{ + STG::TariffData td("test"); + td.tariffConf.fee = 1; + td.tariffConf.free = 2; + td.tariffConf.traffType = STG::Tariff::TRAFF_UP; + td.tariffConf.passiveCost = 4; + td.dirPrice[0].mDay = 30; + td.dirPrice[0].hDay = 21; + td.dirPrice[0].mNight = 30; + td.dirPrice[0].hNight = 9; + td.dirPrice[0].priceDayA = 0; + td.dirPrice[0].priceDayB = 1; + td.dirPrice[0].priceNightA = 2; + td.dirPrice[0].priceNightB = 3; + td.dirPrice[0].threshold = 4; + td.dirPrice[0].singlePrice = 0; + td.dirPrice[0].noDiscount = 0; + STG::TariffImpl tariff(td); + + BOOST_CHECK(tariff.GetTraffType() == STG::Tariff::TRAFF_UP); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(6, 0), 6); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(5, 1), 5); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(4, 2), 4); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(3, 3), 3); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(2, 4), 2); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(1, 5), 1); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(0, 6), 0); + + td.tariffConf.traffType = STG::Tariff::TRAFF_DOWN; + tariff = td; + + BOOST_CHECK(tariff.GetTraffType() == STG::Tariff::TRAFF_DOWN); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(6, 0), 0); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(5, 1), 1); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(4, 2), 2); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(3, 3), 3); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(2, 4), 4); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(1, 5), 5); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(0, 6), 6); + + td.tariffConf.traffType = STG::Tariff::TRAFF_MAX; + tariff = td; + + BOOST_CHECK(tariff.GetTraffType() == STG::Tariff::TRAFF_MAX); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(6, 0), 6); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(5, 1), 5); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(4, 2), 4); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(3, 3), 3); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(2, 4), 4); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(1, 5), 5); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(0, 6), 6); + + td.tariffConf.traffType = STG::Tariff::TRAFF_UP_DOWN; + tariff = td; + + BOOST_CHECK(tariff.GetTraffType() == STG::Tariff::TRAFF_UP_DOWN); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(6, 0), 6); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(5, 1), 6); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(4, 2), 6); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(3, 3), 6); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(2, 4), 6); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(1, 5), 6); + BOOST_CHECK_EQUAL(tariff.GetTraffByType(0, 6), 6); +} + +BOOST_AUTO_TEST_CASE(NormalIntervalPricesForDayNightInversion) +{ + STG::TariffData td("test"); + td.tariffConf.fee = 1; + td.tariffConf.free = 2; + td.tariffConf.traffType = STG::Tariff::TRAFF_UP_DOWN; + td.tariffConf.passiveCost = 4; + td.dirPrice[0].mDay = 30; + td.dirPrice[0].hDay = 21; + td.dirPrice[0].mNight = 30; + td.dirPrice[0].hNight = 9; + td.dirPrice[0].priceDayA = 0; + td.dirPrice[0].priceDayB = 1; + td.dirPrice[0].priceNightA = 2; + td.dirPrice[0].priceNightB = 3; + td.dirPrice[0].threshold = 4; + td.dirPrice[0].singlePrice = 0; + td.dirPrice[0].noDiscount = 0; + STG::TariffImpl tariff(td); + + BOOST_CHECK_EQUAL(tariff.GetPriceWithTraffType(0, 0 * 1024 * 1024, 0, 1286461245), 2); // Near 17:30, 0 < 4 NA + BOOST_CHECK_EQUAL(tariff.GetPriceWithTraffType(0, 6 * 1024 * 1024, 0, 1286461245), 3); // Near 17:30, 6 > 4 NB + BOOST_CHECK_EQUAL(tariff.GetPriceWithTraffType(0, 0 * 1024 * 1024, 0, 1286479245), 0); // Near 22:30, 0 < 4 DA + BOOST_CHECK_EQUAL(tariff.GetPriceWithTraffType(0, 6 * 1024 * 1024, 0, 1286479245), 1); // Near 22:30, 6 > 4 DB + + td.dirPrice[0].singlePrice = 1; + tariff = td; + + BOOST_CHECK_EQUAL(tariff.GetPriceWithTraffType(0, 0 * 1024 * 1024, 0, 1286461245), 0); // Near 17:30, 0 < 4 DA (ignore night) + BOOST_CHECK_EQUAL(tariff.GetPriceWithTraffType(0, 6 * 1024 * 1024, 0, 1286461245), 1); // Near 17:30, 6 > 4 DB (ignore night) + BOOST_CHECK_EQUAL(tariff.GetPriceWithTraffType(0, 0 * 1024 * 1024, 0, 1286479245), 0); // Near 22:30, 0 < 4 DA (ignore night) + BOOST_CHECK_EQUAL(tariff.GetPriceWithTraffType(0, 6 * 1024 * 1024, 0, 1286479245), 1); // Near 22:30, 6 > 4 DB (ignore night) + + td.dirPrice[0].singlePrice = 0; + td.dirPrice[0].noDiscount = 1; + tariff = td; + + BOOST_CHECK_EQUAL(tariff.GetPriceWithTraffType(0, 0 * 1024 * 1024, 0, 1286461245), 2); // Near 17:30, 0 < 4 NA + BOOST_CHECK_EQUAL(tariff.GetPriceWithTraffType(0, 6 * 1024 * 1024, 0, 1286461245), 2); // Near 17:30, 6 > 4 NA + BOOST_CHECK_EQUAL(tariff.GetPriceWithTraffType(0, 0 * 1024 * 1024, 0, 1286479245), 0); // Near 22:30, 0 < 4 DA + BOOST_CHECK_EQUAL(tariff.GetPriceWithTraffType(0, 6 * 1024 * 1024, 0, 1286479245), 0); // Near 22:30, 6 > 4 DA + + td.dirPrice[0].singlePrice = 1; + td.dirPrice[0].noDiscount = 1; + tariff = td; + + BOOST_CHECK_EQUAL(tariff.GetPriceWithTraffType(0, 0 * 1024 * 1024, 0, 1286461245), 0); // Near 17:30, 0 < 4 DA (ignore night) + BOOST_CHECK_EQUAL(tariff.GetPriceWithTraffType(0, 6 * 1024 * 1024, 0, 1286461245), 0); // Near 17:30, 6 > 4 DA (ignore night) + BOOST_CHECK_EQUAL(tariff.GetPriceWithTraffType(0, 0 * 1024 * 1024, 0, 1286479245), 0); // Near 22:30, 0 < 4 DA (ignore night) + BOOST_CHECK_EQUAL(tariff.GetPriceWithTraffType(0, 6 * 1024 * 1024, 0, 1286479245), 0); // Near 22:30, 6 > 4 DA (ignore night) +} + +BOOST_AUTO_TEST_CASE(ChangePolicyAllow) +{ + STG::TariffData td("test"); + td.tariffConf.changePolicy = STG::Tariff::ALLOW; + td.tariffConf.fee = 100; + const STG::TariffImpl tariff(td); + + td.tariffConf.fee = 50; + const STG::TariffImpl cheaper(td); + + BOOST_CHECK_EQUAL(tariff.TariffChangeIsAllowed(cheaper, 1461606400).empty(), true); + + td.tariffConf.fee = 100; + const STG::TariffImpl equal(td); + + BOOST_CHECK_EQUAL(tariff.TariffChangeIsAllowed(equal, 1461606400).empty(), true); + + td.tariffConf.fee = 150; + const STG::TariffImpl expensive(td); + + BOOST_CHECK_EQUAL(tariff.TariffChangeIsAllowed(expensive, 1461606400).empty(), true); +} + +BOOST_AUTO_TEST_CASE(ChangePolicyToCheap) { - struct tariff_data { - }; - - typedef test_group tg; - tg tariff_test_group("TARIFF tests group"); - - typedef tg::object testobject; - - template<> - template<> - void testobject::test<1>() - { - set_test_name("Check construction"); - - STG::TariffData td("test"); - td.tariffConf.fee = 1; - td.tariffConf.free = 2; - td.tariffConf.traffType = STG::Tariff::TRAFF_UP_DOWN; - td.tariffConf.passiveCost = 4; - td.dirPrice[0].mDay = 30; - td.dirPrice[0].hDay = 9; - td.dirPrice[0].mNight = 30; - td.dirPrice[0].hNight = 21; - td.dirPrice[0].priceDayA = 0; - td.dirPrice[0].priceDayB = 1; - td.dirPrice[0].priceNightA = 2; - td.dirPrice[0].priceNightB = 3; - td.dirPrice[0].threshold = 4; - td.dirPrice[0].singlePrice = 0; - td.dirPrice[0].noDiscount = 0; - STG::TariffImpl tariff(td); - - ensure("freeMb = 2", tariff.GetFreeMb() == td.tariffConf.free); - ensure("passiveCost = 4", tariff.GetPassiveCost() == td.tariffConf.passiveCost); - ensure("fee = 1", tariff.GetFee() == td.tariffConf.fee); - ensure("free (alias of freeMb) = 2", tariff.GetFree() == td.tariffConf.free); - ensure("name = \"test\"'", tariff.GetName() == td.tariffConf.name); - ensure("traffType = TRAFF_UP_DOWN", tariff.GetTraffType() == td.tariffConf.traffType); - ensure("threshold[0] = 4", tariff.GetThreshold(0) == td.dirPrice[0].threshold); - ensure_equals("traffByType(6, 0) = 6", tariff.GetTraffByType(6, 0), 6); - ensure_equals("traffByType(5, 1) = 6", tariff.GetTraffByType(5, 1), 6); - ensure_equals("traffByType(4, 2) = 6", tariff.GetTraffByType(4, 2), 6); - ensure_equals("traffByType(3, 3) = 6", tariff.GetTraffByType(3, 3), 6); - ensure_equals("traffByType(2, 4) = 6", tariff.GetTraffByType(2, 4), 6); - ensure_equals("traffByType(1, 5) = 6", tariff.GetTraffByType(1, 5), 6); - ensure_equals("traffByType(0, 6) = 6", tariff.GetTraffByType(0, 6), 6); - } - - template<> - template<> - void testobject::test<2>() - { - set_test_name("Check traff types"); - - STG::TariffData td("test"); - td.tariffConf.fee = 1; - td.tariffConf.free = 2; - td.tariffConf.traffType = STG::Tariff::TRAFF_UP; - td.tariffConf.passiveCost = 4; - td.dirPrice[0].mDay = 30; - td.dirPrice[0].hDay = 9; - td.dirPrice[0].mNight = 30; - td.dirPrice[0].hNight = 21; - td.dirPrice[0].priceDayA = 0; - td.dirPrice[0].priceDayB = 1; - td.dirPrice[0].priceNightA = 2; - td.dirPrice[0].priceNightB = 3; - td.dirPrice[0].threshold = 4; - td.dirPrice[0].singlePrice = 0; - td.dirPrice[0].noDiscount = 0; - STG::TariffImpl tariff(td); - - ensure("traffType = TRAFF_UP", tariff.GetTraffType() == STG::Tariff::TRAFF_UP); - ensure_equals("traffByType(6, 0) = 6 for UP", tariff.GetTraffByType(6, 0), 6); - ensure_equals("traffByType(5, 1) = 5 for UP", tariff.GetTraffByType(5, 1), 5); - ensure_equals("traffByType(4, 2) = 4 for UP", tariff.GetTraffByType(4, 2), 4); - ensure_equals("traffByType(3, 3) = 3 for UP", tariff.GetTraffByType(3, 3), 3); - ensure_equals("traffByType(2, 4) = 2 for UP", tariff.GetTraffByType(2, 4), 2); - ensure_equals("traffByType(1, 5) = 1 for UP", tariff.GetTraffByType(1, 5), 1); - ensure_equals("traffByType(0, 6) = 0 for UP", tariff.GetTraffByType(0, 6), 0); - - td.tariffConf.traffType = STG::Tariff::TRAFF_DOWN; - tariff = td; - - ensure("traffType = TRAFF_DOWN", tariff.GetTraffType() == STG::Tariff::TRAFF_DOWN); - ensure_equals("traffByType(6, 0) = 0 for DOWN", tariff.GetTraffByType(6, 0), 0); - ensure_equals("traffByType(5, 1) = 1 for DOWN", tariff.GetTraffByType(5, 1), 1); - ensure_equals("traffByType(4, 2) = 2 for DOWN", tariff.GetTraffByType(4, 2), 2); - ensure_equals("traffByType(3, 3) = 3 for DOWN", tariff.GetTraffByType(3, 3), 3); - ensure_equals("traffByType(2, 4) = 4 for DOWN", tariff.GetTraffByType(2, 4), 4); - ensure_equals("traffByType(1, 5) = 5 for DOWN", tariff.GetTraffByType(1, 5), 5); - ensure_equals("traffByType(0, 6) = 6 for DOWN", tariff.GetTraffByType(0, 6), 6); - - td.tariffConf.traffType = STG::Tariff::TRAFF_MAX; - tariff = td; - - ensure("traffType = TRAFF_MAX", tariff.GetTraffType() == STG::Tariff::TRAFF_MAX); - ensure_equals("traffByType(6, 0) = 6 for MAX", tariff.GetTraffByType(6, 0), 6); - ensure_equals("traffByType(5, 1) = 5 for MAX", tariff.GetTraffByType(5, 1), 5); - ensure_equals("traffByType(4, 2) = 4 for MAX", tariff.GetTraffByType(4, 2), 4); - ensure_equals("traffByType(3, 3) = 3 for MAX", tariff.GetTraffByType(3, 3), 3); - ensure_equals("traffByType(2, 4) = 4 for MAX", tariff.GetTraffByType(2, 4), 4); - ensure_equals("traffByType(1, 5) = 5 for MAX", tariff.GetTraffByType(1, 5), 5); - ensure_equals("traffByType(0, 6) = 6 for MAX", tariff.GetTraffByType(0, 6), 6); - - td.tariffConf.traffType = STG::Tariff::TRAFF_UP_DOWN; - tariff = td; - - ensure("traffType = TRAFF_UP_DOWN", tariff.GetTraffType() == STG::Tariff::TRAFF_UP_DOWN); - ensure_equals("traffByType(6, 0) = 6 for UP_DOWN", tariff.GetTraffByType(6, 0), 6); - ensure_equals("traffByType(5, 1) = 6 for UP_DOWN", tariff.GetTraffByType(5, 1), 6); - ensure_equals("traffByType(4, 2) = 6 for UP_DOWN", tariff.GetTraffByType(4, 2), 6); - ensure_equals("traffByType(3, 3) = 6 for UP_DOWN", tariff.GetTraffByType(3, 3), 6); - ensure_equals("traffByType(2, 4) = 6 for UP_DOWN", tariff.GetTraffByType(2, 4), 6); - ensure_equals("traffByType(1, 5) = 6 for UP_DOWN", tariff.GetTraffByType(1, 5), 6); - ensure_equals("traffByType(0, 6) = 6 for UP_DOWN", tariff.GetTraffByType(0, 6), 6); - } - - template<> - template<> - void testobject::test<3>() - { - set_test_name("Check normal interval prices"); - - STG::TariffData td("test"); - td.tariffConf.fee = 1; - td.tariffConf.free = 2; - td.tariffConf.traffType = STG::Tariff::TRAFF_UP_DOWN; - td.tariffConf.passiveCost = 4; - td.dirPrice[0].mDay = 30; - td.dirPrice[0].hDay = 9; - td.dirPrice[0].mNight = 30; - td.dirPrice[0].hNight = 21; - td.dirPrice[0].priceDayA = 0; - td.dirPrice[0].priceDayB = 1; - td.dirPrice[0].priceNightA = 2; - td.dirPrice[0].priceNightB = 3; - td.dirPrice[0].threshold = 4; - td.dirPrice[0].singlePrice = 0; - td.dirPrice[0].noDiscount = 0; - STG::TariffImpl tariff(td); - - ensure_equals("0000 == 0", tariff.GetPriceWithTraffType(0, 0 * 1024 * 1024, 0, 1286461245), 0); // Near 17:30, 0 < 4 DA - ensure_equals("0001 == 0", tariff.GetPriceWithTraffType(0, 6 * 1024 * 1024, 0, 1286461245), 1); // Near 17:30, 6 > 4 DB - ensure_equals("0010 == 0", tariff.GetPriceWithTraffType(0, 0 * 1024 * 1024, 0, 1286479245), 2); // Near 22:30, 0 < 4 NA - ensure_equals("0011 == 0", tariff.GetPriceWithTraffType(0, 6 * 1024 * 1024, 0, 1286479245), 3); // Near 22:30, 6 > 4 NB - - td.dirPrice[0].singlePrice = 1; - tariff = td; - - ensure_equals("0100 == 0", tariff.GetPriceWithTraffType(0, 0 * 1024 * 1024, 0, 1286461245), 0); // Near 17:30, 0 < 4 DA - ensure_equals("0101 == 0", tariff.GetPriceWithTraffType(0, 6 * 1024 * 1024, 0, 1286461245), 1); // Near 17:30, 6 > 4 DB - ensure_equals("0110 == 0", tariff.GetPriceWithTraffType(0, 0 * 1024 * 1024, 0, 1286479245), 0); // Near 22:30, 0 < 4 DA - ensure_equals("0111 == 0", tariff.GetPriceWithTraffType(0, 6 * 1024 * 1024, 0, 1286479245), 1); // Near 22:30, 6 > 4 DB - - td.dirPrice[0].singlePrice = 0; - td.dirPrice[0].noDiscount = 1; - tariff = td; - - ensure_equals("1000 == 0", tariff.GetPriceWithTraffType(0, 0 * 1024 * 1024, 0, 1286461245), 0); // Near 17:30, 0 < 4 DA - ensure_equals("1001 == 0", tariff.GetPriceWithTraffType(0, 6 * 1024 * 1024, 0, 1286461245), 0); // Near 17:30, 6 > 4 DA - ensure_equals("1010 == 0", tariff.GetPriceWithTraffType(0, 0 * 1024 * 1024, 0, 1286479245), 2); // Near 22:30, 0 < 4 NA - ensure_equals("1011 == 0", tariff.GetPriceWithTraffType(0, 6 * 1024 * 1024, 0, 1286479245), 2); // Near 22:30, 6 > 4 NA - - td.dirPrice[0].singlePrice = 1; - td.dirPrice[0].noDiscount = 1; - tariff = td; - - ensure_equals("1100 == 0", tariff.GetPriceWithTraffType(0, 0 * 1024 * 1024, 0, 1286461245), 0); // Near 17:30, 0 < 4 DA - ensure_equals("1101 == 0", tariff.GetPriceWithTraffType(0, 6 * 1024 * 1024, 0, 1286461245), 0); // Near 17:30, 6 > 4 DA - ensure_equals("1110 == 0", tariff.GetPriceWithTraffType(0, 0 * 1024 * 1024, 0, 1286479245), 0); // Near 22:30, 0 < 4 DA - ensure_equals("1111 == 0", tariff.GetPriceWithTraffType(0, 6 * 1024 * 1024, 0, 1286479245), 0); // Near 22:30, 6 > 4 DA - } - - template<> - template<> - void testobject::test<4>() - { - set_test_name("Check construction for day-night inversion"); - - STG::TariffData td("test"); - td.tariffConf.fee = 1; - td.tariffConf.free = 2; - td.tariffConf.traffType = STG::Tariff::TRAFF_UP_DOWN; - td.tariffConf.passiveCost = 4; - td.dirPrice[0].mDay = 30; - td.dirPrice[0].hDay = 21; - td.dirPrice[0].mNight = 30; - td.dirPrice[0].hNight = 9; - td.dirPrice[0].priceDayA = 0; - td.dirPrice[0].priceDayB = 1; - td.dirPrice[0].priceNightA = 2; - td.dirPrice[0].priceNightB = 3; - td.dirPrice[0].threshold = 4; - td.dirPrice[0].singlePrice = 0; - td.dirPrice[0].noDiscount = 0; - STG::TariffImpl tariff(td); - - ensure("freeMb = 2", tariff.GetFreeMb() == td.tariffConf.free); - ensure("passiveCost = 4", tariff.GetPassiveCost() == td.tariffConf.passiveCost); - ensure("fee = 1", tariff.GetFee() == td.tariffConf.fee); - ensure("free (alias of freeMb) = 2", tariff.GetFree() == td.tariffConf.free); - ensure("name = \"test\"'", tariff.GetName() == td.tariffConf.name); - ensure("traffType = TRAFF_UP_DOWN", tariff.GetTraffType() == td.tariffConf.traffType); - ensure("threshold[0] = 4", tariff.GetThreshold(0) == td.dirPrice[0].threshold); - ensure_equals("traffByType(6, 0) = 6", tariff.GetTraffByType(6, 0), 6); - ensure_equals("traffByType(5, 1) = 6", tariff.GetTraffByType(5, 1), 6); - ensure_equals("traffByType(4, 2) = 6", tariff.GetTraffByType(4, 2), 6); - ensure_equals("traffByType(3, 3) = 6", tariff.GetTraffByType(3, 3), 6); - ensure_equals("traffByType(2, 4) = 6", tariff.GetTraffByType(2, 4), 6); - ensure_equals("traffByType(1, 5) = 6", tariff.GetTraffByType(1, 5), 6); - ensure_equals("traffByType(0, 6) = 6", tariff.GetTraffByType(0, 6), 6); - } - - template<> - template<> - void testobject::test<5>() - { - set_test_name("Check traff types for day-night inversion"); - - STG::TariffData td("test"); - td.tariffConf.fee = 1; - td.tariffConf.free = 2; - td.tariffConf.traffType = STG::Tariff::TRAFF_UP; - td.tariffConf.passiveCost = 4; - td.dirPrice[0].mDay = 30; - td.dirPrice[0].hDay = 21; - td.dirPrice[0].mNight = 30; - td.dirPrice[0].hNight = 9; - td.dirPrice[0].priceDayA = 0; - td.dirPrice[0].priceDayB = 1; - td.dirPrice[0].priceNightA = 2; - td.dirPrice[0].priceNightB = 3; - td.dirPrice[0].threshold = 4; - td.dirPrice[0].singlePrice = 0; - td.dirPrice[0].noDiscount = 0; - STG::TariffImpl tariff(td); - - ensure("traffType = TRAFF_UP", tariff.GetTraffType() == STG::Tariff::TRAFF_UP); - ensure_equals("traffByType(6, 0) = 6 for UP", tariff.GetTraffByType(6, 0), 6); - ensure_equals("traffByType(5, 1) = 5 for UP", tariff.GetTraffByType(5, 1), 5); - ensure_equals("traffByType(4, 2) = 4 for UP", tariff.GetTraffByType(4, 2), 4); - ensure_equals("traffByType(3, 3) = 3 for UP", tariff.GetTraffByType(3, 3), 3); - ensure_equals("traffByType(2, 4) = 2 for UP", tariff.GetTraffByType(2, 4), 2); - ensure_equals("traffByType(1, 5) = 1 for UP", tariff.GetTraffByType(1, 5), 1); - ensure_equals("traffByType(0, 6) = 0 for UP", tariff.GetTraffByType(0, 6), 0); - - td.tariffConf.traffType = STG::Tariff::TRAFF_DOWN; - tariff = td; - - ensure("traffType = TRAFF_DOWN", tariff.GetTraffType() == STG::Tariff::TRAFF_DOWN); - ensure_equals("traffByType(6, 0) = 0 for DOWN", tariff.GetTraffByType(6, 0), 0); - ensure_equals("traffByType(5, 1) = 1 for DOWN", tariff.GetTraffByType(5, 1), 1); - ensure_equals("traffByType(4, 2) = 2 for DOWN", tariff.GetTraffByType(4, 2), 2); - ensure_equals("traffByType(3, 3) = 3 for DOWN", tariff.GetTraffByType(3, 3), 3); - ensure_equals("traffByType(2, 4) = 4 for DOWN", tariff.GetTraffByType(2, 4), 4); - ensure_equals("traffByType(1, 5) = 5 for DOWN", tariff.GetTraffByType(1, 5), 5); - ensure_equals("traffByType(0, 6) = 6 for DOWN", tariff.GetTraffByType(0, 6), 6); - - td.tariffConf.traffType = STG::Tariff::TRAFF_MAX; - tariff = td; - - ensure("traffType = TRAFF_MAX", tariff.GetTraffType() == STG::Tariff::TRAFF_MAX); - ensure_equals("traffByType(6, 0) = 6 for MAX", tariff.GetTraffByType(6, 0), 6); - ensure_equals("traffByType(5, 1) = 5 for MAX", tariff.GetTraffByType(5, 1), 5); - ensure_equals("traffByType(4, 2) = 4 for MAX", tariff.GetTraffByType(4, 2), 4); - ensure_equals("traffByType(3, 3) = 3 for MAX", tariff.GetTraffByType(3, 3), 3); - ensure_equals("traffByType(2, 4) = 4 for MAX", tariff.GetTraffByType(2, 4), 4); - ensure_equals("traffByType(1, 5) = 5 for MAX", tariff.GetTraffByType(1, 5), 5); - ensure_equals("traffByType(0, 6) = 6 for MAX", tariff.GetTraffByType(0, 6), 6); - - td.tariffConf.traffType = STG::Tariff::TRAFF_UP_DOWN; - tariff = td; - - ensure("traffType = TRAFF_UP_DOWN", tariff.GetTraffType() == STG::Tariff::TRAFF_UP_DOWN); - ensure_equals("traffByType(6, 0) = 6 for UP_DOWN", tariff.GetTraffByType(6, 0), 6); - ensure_equals("traffByType(5, 1) = 6 for UP_DOWN", tariff.GetTraffByType(5, 1), 6); - ensure_equals("traffByType(4, 2) = 6 for UP_DOWN", tariff.GetTraffByType(4, 2), 6); - ensure_equals("traffByType(3, 3) = 6 for UP_DOWN", tariff.GetTraffByType(3, 3), 6); - ensure_equals("traffByType(2, 4) = 6 for UP_DOWN", tariff.GetTraffByType(2, 4), 6); - ensure_equals("traffByType(1, 5) = 6 for UP_DOWN", tariff.GetTraffByType(1, 5), 6); - ensure_equals("traffByType(0, 6) = 6 for UP_DOWN", tariff.GetTraffByType(0, 6), 6); - } - - template<> - template<> - void testobject::test<6>() - { - set_test_name("Check normal interval prices for day-night inversion"); - - STG::TariffData td("test"); - td.tariffConf.fee = 1; - td.tariffConf.free = 2; - td.tariffConf.traffType = STG::Tariff::TRAFF_UP_DOWN; - td.tariffConf.passiveCost = 4; - td.dirPrice[0].mDay = 30; - td.dirPrice[0].hDay = 21; - td.dirPrice[0].mNight = 30; - td.dirPrice[0].hNight = 9; - td.dirPrice[0].priceDayA = 0; - td.dirPrice[0].priceDayB = 1; - td.dirPrice[0].priceNightA = 2; - td.dirPrice[0].priceNightB = 3; - td.dirPrice[0].threshold = 4; - td.dirPrice[0].singlePrice = 0; - td.dirPrice[0].noDiscount = 0; - STG::TariffImpl tariff(td); - - ensure_equals("0000 == 0", tariff.GetPriceWithTraffType(0, 0 * 1024 * 1024, 0, 1286461245), 2); // Near 17:30, 0 < 4 NA - ensure_equals("0001 == 0", tariff.GetPriceWithTraffType(0, 6 * 1024 * 1024, 0, 1286461245), 3); // Near 17:30, 6 > 4 NB - ensure_equals("0010 == 0", tariff.GetPriceWithTraffType(0, 0 * 1024 * 1024, 0, 1286479245), 0); // Near 22:30, 0 < 4 DA - ensure_equals("0011 == 0", tariff.GetPriceWithTraffType(0, 6 * 1024 * 1024, 0, 1286479245), 1); // Near 22:30, 6 > 4 DB - - td.dirPrice[0].singlePrice = 1; - tariff = td; - - ensure_equals("0100 == 0", tariff.GetPriceWithTraffType(0, 0 * 1024 * 1024, 0, 1286461245), 0); // Near 17:30, 0 < 4 DA (ignore night) - ensure_equals("0101 == 0", tariff.GetPriceWithTraffType(0, 6 * 1024 * 1024, 0, 1286461245), 1); // Near 17:30, 6 > 4 DB (ignore night) - ensure_equals("0110 == 0", tariff.GetPriceWithTraffType(0, 0 * 1024 * 1024, 0, 1286479245), 0); // Near 22:30, 0 < 4 DA (ignore night) - ensure_equals("0111 == 0", tariff.GetPriceWithTraffType(0, 6 * 1024 * 1024, 0, 1286479245), 1); // Near 22:30, 6 > 4 DB (ignore night) - - td.dirPrice[0].singlePrice = 0; - td.dirPrice[0].noDiscount = 1; - tariff = td; - - ensure_equals("1000 == 0", tariff.GetPriceWithTraffType(0, 0 * 1024 * 1024, 0, 1286461245), 2); // Near 17:30, 0 < 4 NA - ensure_equals("1001 == 0", tariff.GetPriceWithTraffType(0, 6 * 1024 * 1024, 0, 1286461245), 2); // Near 17:30, 6 > 4 NA - ensure_equals("1010 == 0", tariff.GetPriceWithTraffType(0, 0 * 1024 * 1024, 0, 1286479245), 0); // Near 22:30, 0 < 4 DA - ensure_equals("1011 == 0", tariff.GetPriceWithTraffType(0, 6 * 1024 * 1024, 0, 1286479245), 0); // Near 22:30, 6 > 4 DA - - td.dirPrice[0].singlePrice = 1; - td.dirPrice[0].noDiscount = 1; - tariff = td; - - ensure_equals("1100 == 0", tariff.GetPriceWithTraffType(0, 0 * 1024 * 1024, 0, 1286461245), 0); // Near 17:30, 0 < 4 DA (ignore night) - ensure_equals("1101 == 0", tariff.GetPriceWithTraffType(0, 6 * 1024 * 1024, 0, 1286461245), 0); // Near 17:30, 6 > 4 DA (ignore night) - ensure_equals("1110 == 0", tariff.GetPriceWithTraffType(0, 0 * 1024 * 1024, 0, 1286479245), 0); // Near 22:30, 0 < 4 DA (ignore night) - ensure_equals("1111 == 0", tariff.GetPriceWithTraffType(0, 6 * 1024 * 1024, 0, 1286479245), 0); // Near 22:30, 6 > 4 DA (ignore night) - } - - template<> - template<> - void testobject::test<7>() - { - set_test_name("Check changePolicy - ALLOW"); - - STG::TariffData td("test"); - td.tariffConf.changePolicy = STG::Tariff::ALLOW; - td.tariffConf.fee = 100; - STG::TariffImpl tariff(td); - - td.tariffConf.fee = 50; - STG::TariffImpl cheaper(td); - - ensure_equals("Allow cheaper", tariff.TariffChangeIsAllowed(cheaper, 1461606400).empty(), true); - - td.tariffConf.fee = 100; - STG::TariffImpl equal(td); - - ensure_equals("Allow equal", tariff.TariffChangeIsAllowed(equal, 1461606400).empty(), true); - - td.tariffConf.fee = 150; - STG::TariffImpl expensive(td); - - ensure_equals("Allow expensive", tariff.TariffChangeIsAllowed(expensive, 1461606400).empty(), true); - } - - template<> - template<> - void testobject::test<8>() - { - set_test_name("Check changePolicy - TO_CHEAP"); - - STG::TariffData td("test"); - td.tariffConf.changePolicy = STG::Tariff::TO_CHEAP; - td.tariffConf.fee = 100; - STG::TariffImpl tariff(td); - - td.tariffConf.fee = 50; - STG::TariffImpl cheaper(td); - - ensure_equals("Allow cheaper", tariff.TariffChangeIsAllowed(cheaper, 1461606400).empty(), true); - - td.tariffConf.fee = 100; - STG::TariffImpl equal(td); - - ensure_equals("Prohibit equal", tariff.TariffChangeIsAllowed(equal, 1461606400).empty(), false); - - td.tariffConf.fee = 150; - STG::TariffImpl expensive(td); - - ensure_equals("Prohibit expensive", tariff.TariffChangeIsAllowed(expensive, 1461606400).empty(), false); - } - - template<> - template<> - void testobject::test<9>() - { - set_test_name("Check changePolicy - TO_EXPENSIVE"); + STG::TariffData td("test"); + td.tariffConf.changePolicy = STG::Tariff::TO_CHEAP; + td.tariffConf.fee = 100; + const STG::TariffImpl tariff(td); - STG::TariffData td("test"); - td.tariffConf.changePolicy = STG::Tariff::TO_EXPENSIVE; - td.tariffConf.fee = 100; - STG::TariffImpl tariff(td); + td.tariffConf.fee = 50; + const STG::TariffImpl cheaper(td); - td.tariffConf.fee = 50; - STG::TariffImpl cheaper(td); + BOOST_CHECK_EQUAL(tariff.TariffChangeIsAllowed(cheaper, 1461606400).empty(), true); - ensure_equals("Prohibit cheaper", tariff.TariffChangeIsAllowed(cheaper, 1461606400).empty(), false); + td.tariffConf.fee = 100; + const STG::TariffImpl equal(td); - td.tariffConf.fee = 100; - STG::TariffImpl equal(td); + BOOST_CHECK_EQUAL(tariff.TariffChangeIsAllowed(equal, 1461606400).empty(), false); - ensure_equals("Allow equal", tariff.TariffChangeIsAllowed(equal, 1461606400).empty(), true); + td.tariffConf.fee = 150; + const STG::TariffImpl expensive(td); - td.tariffConf.fee = 150; - STG::TariffImpl expensive(td); + BOOST_CHECK_EQUAL(tariff.TariffChangeIsAllowed(expensive, 1461606400).empty(), false); +} - ensure_equals("Allow expensive", tariff.TariffChangeIsAllowed(expensive, 1461606400).empty(), true); - } +BOOST_AUTO_TEST_CASE(ChangePolicyToExpensive) +{ + STG::TariffData td("test"); + td.tariffConf.changePolicy = STG::Tariff::TO_EXPENSIVE; + td.tariffConf.fee = 100; + const STG::TariffImpl tariff(td); - template<> - template<> - void testobject::test<10>() - { - set_test_name("Check changePolicy - DENY"); + td.tariffConf.fee = 50; + const STG::TariffImpl cheaper(td); - STG::TariffData td("test"); - td.tariffConf.changePolicy = STG::Tariff::DENY; - td.tariffConf.fee = 100; - STG::TariffImpl tariff(td); + BOOST_CHECK_EQUAL(tariff.TariffChangeIsAllowed(cheaper, 1461606400).empty(), false); - td.tariffConf.fee = 50; - STG::TariffImpl cheaper(td); + td.tariffConf.fee = 100; + const STG::TariffImpl equal(td); - ensure_equals("Prohibit cheaper", tariff.TariffChangeIsAllowed(cheaper, 1461606400).empty(), false); + BOOST_CHECK_EQUAL(tariff.TariffChangeIsAllowed(equal, 1461606400).empty(), true); - td.tariffConf.fee = 100; - STG::TariffImpl equal(td); + td.tariffConf.fee = 150; + const STG::TariffImpl expensive(td); - ensure_equals("Prohibit equal", tariff.TariffChangeIsAllowed(equal, 1461606400).empty(), false); + BOOST_CHECK_EQUAL(tariff.TariffChangeIsAllowed(expensive, 1461606400).empty(), true); +} - td.tariffConf.fee = 150; - STG::TariffImpl expensive(td); +BOOST_AUTO_TEST_CASE(ChangePolicyDeny) +{ + STG::TariffData td("test"); + td.tariffConf.changePolicy = STG::Tariff::DENY; + td.tariffConf.fee = 100; + const STG::TariffImpl tariff(td); + + td.tariffConf.fee = 50; + const STG::TariffImpl cheaper(td); - ensure_equals("Prohibit expensive", tariff.TariffChangeIsAllowed(expensive, 1461606400).empty(), false); - } + BOOST_CHECK_EQUAL(tariff.TariffChangeIsAllowed(cheaper, 1461606400).empty(), false); - template<> - template<> - void testobject::test<11>() - { - set_test_name("Check changePolicyTimeout < current time"); + td.tariffConf.fee = 100; + const STG::TariffImpl equal(td); - STG::TariffData td("test"); - td.tariffConf.changePolicyTimeout = 1451606400; - td.tariffConf.changePolicy = STG::Tariff::TO_EXPENSIVE; - td.tariffConf.fee = 100; - STG::TariffImpl tariff(td); + BOOST_CHECK_EQUAL(tariff.TariffChangeIsAllowed(equal, 1461606400).empty(), false); - td.tariffConf.fee = 50; - STG::TariffImpl cheaper(td); + td.tariffConf.fee = 150; + const STG::TariffImpl expensive(td); - ensure_equals("Allow cheaper", tariff.TariffChangeIsAllowed(cheaper, 1461606400).empty(), true); - } + BOOST_CHECK_EQUAL(tariff.TariffChangeIsAllowed(expensive, 1461606400).empty(), false); +} + +BOOST_AUTO_TEST_CASE(ChangePolicyTimeoutInThePast) +{ + STG::TariffData td("test"); + td.tariffConf.changePolicyTimeout = 1451606400; + td.tariffConf.changePolicy = STG::Tariff::TO_EXPENSIVE; + td.tariffConf.fee = 100; + const STG::TariffImpl tariff(td); - template<> - template<> - void testobject::test<12>() - { - set_test_name("Check changePolicyTimeout > current time"); + td.tariffConf.fee = 50; + const STG::TariffImpl cheaper(td); - STG::TariffData td("test"); - td.tariffConf.changePolicyTimeout = 1483228800; - td.tariffConf.changePolicy = STG::Tariff::TO_EXPENSIVE; - td.tariffConf.fee = 100; - STG::TariffImpl tariff(td); + BOOST_CHECK_EQUAL(tariff.TariffChangeIsAllowed(cheaper, 1461606400).empty(), true); +} - td.tariffConf.fee = 50; - STG::TariffImpl cheaper(td); +BOOST_AUTO_TEST_CASE(ChangePolicyTimeoutInFuture) +{ + STG::TariffData td("test"); + td.tariffConf.changePolicyTimeout = 1483228800; + td.tariffConf.changePolicy = STG::Tariff::TO_EXPENSIVE; + td.tariffConf.fee = 100; + const STG::TariffImpl tariff(td); - ensure_equals("Prohibit cheaper", tariff.TariffChangeIsAllowed(cheaper, 1461606400).empty(), false); - } + td.tariffConf.fee = 50; + const STG::TariffImpl cheaper(td); - template<> - template<> - void testobject::test<13>() - { - set_test_name("Check changePolicyTimeout = 0"); + BOOST_CHECK_EQUAL(tariff.TariffChangeIsAllowed(cheaper, 1461606400).empty(), false); +} - STG::TariffData td("test"); - td.tariffConf.changePolicyTimeout = 0; - td.tariffConf.changePolicy = STG::Tariff::TO_EXPENSIVE; - td.tariffConf.fee = 100; - STG::TariffImpl tariff(td); +BOOST_AUTO_TEST_CASE(ChangePolicyTimeoutNow) +{ + STG::TariffData td("test"); + td.tariffConf.changePolicyTimeout = 0; + td.tariffConf.changePolicy = STG::Tariff::TO_EXPENSIVE; + td.tariffConf.fee = 100; + const STG::TariffImpl tariff(td); - td.tariffConf.fee = 50; - STG::TariffImpl cheaper(td); + td.tariffConf.fee = 50; + const STG::TariffImpl cheaper(td); - ensure_equals("Prohibit cheaper", tariff.TariffChangeIsAllowed(cheaper, 1461606400).empty(), false); - } + BOOST_CHECK_EQUAL(tariff.TariffChangeIsAllowed(cheaper, 1461606400).empty(), false); } + +BOOST_AUTO_TEST_SUITE_END()