diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..4e28ccb --- /dev/null +++ b/.travis.yml @@ -0,0 +1,26 @@ +dist: trusty +sudo: required +language: + - cpp +compiler: + - gcc +addons: + apt: + sources: + - ubuntu-toolchain-r-test + packages: + - gcc-6 + - g++-6 + - cmake +script: + # Link gcc-6 and g++-6 to their standard commands + - ln -s /usr/bin/gcc-6 /usr/local/bin/gcc + - ln -s /usr/bin/g++-6 /usr/local/bin/g++ + # Export CC and CXX to tell cmake which compiler to use + - export CC=/usr/bin/gcc-6 + - export CXX=/usr/bin/g++-6 + # Check versions of gcc, g++ and cmake + - gcc -v && g++ -v && cmake --version + # Build commands + - cmake . + - make diff --git a/CMakeLists.txt b/CMakeLists.txt index 03ebef2..5095bb3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,11 +1,11 @@ -cmake_minimum_required(VERSION 3.5) +cmake_minimum_required(VERSION 3.2) project(frnetlib) #Set module path set( CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake_modules) #User options -option(USE_SSL "Use SSL" ON) +option(USE_SSL "Use SSL" OFF) set(FRNETLIB_BUILD_SHARED_LIBS false CACHE BOOL "Build shared library.") #Tests and examples are currently broken on Windows @@ -105,4 +105,4 @@ install( install( DIRECTORY include DESTINATION . -) \ No newline at end of file +) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index aa64e4e..0720da5 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -24,7 +24,7 @@ else() endif() #Link tests -target_link_libraries(${FRNETLIB_TEST} -lgtest -lfrnetlib) +target_link_libraries(${FRNETLIB_TEST} gtest_main frnetlib) add_test(test1 ${FRNETLIB_TEST}) #Run tests automatically diff --git a/tests/PacketTest.cpp b/tests/PacketTest.cpp new file mode 100644 index 0000000..34016f5 --- /dev/null +++ b/tests/PacketTest.cpp @@ -0,0 +1,111 @@ +#include +#include +#include + +TEST(PacketTest, pack_and_unpack_ints) +{ + fr::Packet packet; + double a1 = 11.5f, a2 = 0.f; + float b1 = 11.52, b2 = 0.0; + uint8_t c1 = std::numeric_limits::max(), c2 = 0; + uint16_t d1 = std::numeric_limits::max(), d2 = 0; + uint32_t e1 = std::numeric_limits::max(), e2 = 0; + uint64_t f1 = std::numeric_limits::max(), f2 = 0; + + packet << a1 << b1 << c1 << d1 << e1 << f1; + packet >> a2 >> b2 >> c2 >> d2 >> e2 >> f2; + + ASSERT_EQ(a1, a2); + ASSERT_EQ(b1, b2); + ASSERT_EQ(c1, c2); + ASSERT_EQ(d1, d2); + ASSERT_EQ(e1, e2); + ASSERT_EQ(f1, f2); + +} + +TEST(PacketTest, pack_and_unpack_stl) +{ + fr::Packet packet; + std::string a1 = "I'm a string", a2; + std::vector b1 = {"hello", "there", "a"}, b2; + std::pair c1 = {1, "a"}, c2; + + packet << a1 << b1 << c1; + packet >> a2 >> b2 >> c2; + + ASSERT_EQ(a1, a2); + ASSERT_EQ(b1, b2); + ASSERT_EQ(c1, c2); +} + +TEST(PacketTest, variadic_packet_constructor) +{ + int a1 = 10, a2; + std::string b1 = "hey", b2; + int64_t c1 = 90, c2; + + fr::Packet packet(a1, b1, c1); + packet >> a2 >> b2 >> c2; + + ASSERT_EQ(a1, a2); + ASSERT_EQ(b1, b2); + ASSERT_EQ(c1, c2); +} + +TEST(PacketTest, raw_data) +{ + std::string a1 = "hello", a2; + std::string b1(13, 'c'), b2(13, '\0'); + uint32_t c1 = std::numeric_limits::max(), c2; + fr::Packet packet; + + packet << a1; + packet.add_raw(b1.c_str(), b1.size()); + packet << c1; + + packet >> a2; + packet.extract_raw(&b2[0], b1.size()); + packet >> c2; + + ASSERT_EQ(a1, a2); + ASSERT_EQ(b1, b2); + ASSERT_EQ(c1, c2); +} + +TEST(PacketTest, out_of_bounds_protection) +{ + int32_t a = 99; + fr::Packet packet(a, a, a, a); + packet >> a >> a >> a >> a; + + try + { + packet >> a; + FAIL(); + } + catch(const std::out_of_range &e) + { + + } +} + +TEST(PacketTest, read_cursor) +{ + int32_t a1 = 20, a2; + std::string b1 = "hello", b2; + fr::Packet packet(a1, b1); + + packet >> a2 >> b2; + ASSERT_EQ(a1, a2); + ASSERT_EQ(b1, b2); + + packet.reset_read_cursor(); + packet >> a2 >> b2; + ASSERT_EQ(a1, a2); + ASSERT_EQ(b1, b2); + + packet.reset_read_cursor((sizeof(a1))); + packet >> b2; + ASSERT_EQ(b1, b2); +} \ No newline at end of file