Skip to content
This repository was archived by the owner on Apr 16, 2018. It is now read-only.

Add test for recv and send methods for Socket#8

Merged
agauniyal merged 3 commits intofeature/socketfrom
recvTesting
Mar 21, 2017
Merged

Add test for recv and send methods for Socket#8
agauniyal merged 3 commits intofeature/socketfrom
recvTesting

Conversation

@tkhurana96
Copy link
Copy Markdown
Contributor

No description provided.

@tkhurana96 tkhurana96 self-assigned this Mar 17, 2017
@tkhurana96 tkhurana96 requested a review from agauniyal March 17, 2017 16:27
using namespace net;
using namespace std::chrono_literals;

auto msgLen2 = 15000;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There shouldn't be any global variables, make them const if possible and move them under appropriate function or atleast some namespace.

for (int i = 1; i <= 1; i++) {
auto res = serverSocket.recv(msgLen2, [](AddrIPv4 &s) {
std::string str(100, ' ');
auto ptr = inet_ntop(AF_INET, &s.sin_addr.s_addr, &str.front(),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make it const if possible

void udpIPv4ServerProcessing(Socket &serverSocket)
{
for (int i = 1; i <= 1; i++) {
auto res = serverSocket.recv(msgLen2, [](AddrIPv4 &s) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

provide some sort of assertion for res too

for (int i = 1; i <= 1; i++) {
auto res = serverSocket.recv(msgLen2, [](AddrIPv6 &s) {
std::string str(100, ' ');
auto ptr = inet_ntop(AF_INET6, &s.sin6_addr.s6_addr, &str.front(),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make it const if possible

struct sockaddr_in actualPeerIp;
socklen_t actualPeerIpLen = sizeof(actualPeerIp);

auto status = getpeername(peer.getSocket(), (sockaddr *) &actualPeerIp,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const if possible

actualIp.end());
return actualIp;
} else
throw std::runtime_error("Error in getPeerIp");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better error message - what is exact error?

actualIp.erase(std::remove(actualIp.begin(), actualIp.end(), ' '),
actualIp.end());
return actualIp;
} else
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use braces please


void tcpIPv4ServerProcessing(Socket &serverSocket)
{
for (int i = 1; i <= 1; i++) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need a loop here?

void tcpIPv4ServerProcessing(Socket &serverSocket)
{
for (int i = 1; i <= 1; i++) {
auto peer = serverSocket.accept();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const everything you can

auto peer = serverSocket.accept();

auto msg = peer.read(msgLen2);
if (msg == msg1)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

braces

@agauniyal
Copy link
Copy Markdown
Member

agauniyal commented Mar 20, 2017

@tkhurana96 see failing tests

Running main() from gtest_main.cc
[==========] Running 14 tests from 3 test cases.
[----------] Global test environment set-up.
[----------] 5 tests from Socket
[ RUN      ] Socket.bind
[       OK ] Socket.bind (0 ms)
[ RUN      ] Socket.ConstructorRaw
[       OK ] Socket.ConstructorRaw (0 ms)
[ RUN      ] Socket.getSocket
[       OK ] Socket.getSocket (1 ms)
[ RUN      ] Socket.IPv4Send
../test/socket_send_test.cpp:129: Failure
Expected: (client1.read(5)) != (std::to_string(msgLen)), actual: "15000" vs "15000"
[  FAILED  ] Socket.IPv4Send (2000 ms)
[ RUN      ] Socket.IPv6Send
../test/socket_send_test.cpp:163: Failure
Expected: (client2.read(5)) != (std::to_string(msgLen)), actual: "15000" vs "15000"
[  FAILED  ] Socket.IPv6Send (2001 ms)
[----------] 5 tests from Socket (4002 ms total)

[----------] 1 test from socket
[ RUN      ] socket.constructor
[       OK ] socket.constructor (37 ms)
[----------] 1 test from socket (37 ms total)

[----------] 8 tests from SocketOptions
[ RUN      ] SocketOptions.getType
[       OK ] SocketOptions.getType (1 ms)
[ RUN      ] SocketOptions.CastError
[       OK ] SocketOptions.CastError (0 ms)
[ RUN      ] SocketOptions.Broadcast
[       OK ] SocketOptions.Broadcast (0 ms)
[ RUN      ] SocketOptions.Linger
[       OK ] SocketOptions.Linger (0 ms)
[ RUN      ] SocketOptions.DontRoute
[       OK ] SocketOptions.DontRoute (0 ms)
[ RUN      ] SocketOptions.KeepAlive
[       OK ] SocketOptions.KeepAlive (0 ms)
[ RUN      ] SocketOptions.OOBInline
[       OK ] SocketOptions.OOBInline (0 ms)
[ RUN      ] SocketOptions.RCVLOWAT
[       OK ] SocketOptions.RCVLOWAT (0 ms)
[----------] 8 tests from SocketOptions (1 ms total)

[----------] Global test environment tear-down
[==========] 14 tests from 3 test cases ran. (4040 ms total)
[  PASSED  ] 12 tests.
[  FAILED  ] 2 tests, listed below:
[  FAILED  ] Socket.IPv4Send
[  FAILED  ] Socket.IPv6Send

 2 FAILED TESTS

@agauniyal
Copy link
Copy Markdown
Member

agauniyal commented Mar 21, 2017

@tkhurana96 see memory leaks -

==20839== Syscall param unlink(pathname) points to uninitialised byte(s)
==20839==    at 0x56EDEA7: unlink (syscall-template.S:84)
==20839==    by 0x40A17A: net::Socket::~Socket() (socket.hpp:869)
==20839==    by 0x4130E1: Socket_GetSocket_Test::TestBody() (socket_getSocket_test.cpp:18)
==20839==    by 0x43F6BD: void testing::internal::HandleSehExceptionsInMethodIfSupported<testing::Test, void>(testing::Test*, void (testing::Test::*)(), char const*) (gtest.cc:2078)
==20839==    by 0x43A942: void testing::internal::HandleExceptionsInMethodIfSupported<testing::Test, void>(testing::Test*, void (testing::Test::*)(), char const*) (gtest.cc:2114)
==20839==    by 0x420B71: testing::Test::Run() (gtest.cc:2151)
==20839==    by 0x4213B3: testing::TestInfo::Run() (gtest.cc:2326)
==20839==    by 0x421A10: testing::TestCase::Run() (gtest.cc:2444)
==20839==    by 0x4288CF: testing::internal::UnitTestImpl::RunAllTests() (gtest.cc:4315)
==20839==    by 0x440602: bool testing::internal::HandleSehExceptionsInMethodIfSupported<testing::internal::UnitTestImpl, bool>(testing::internal::UnitTestImpl*, bool (testing::internal::UnitTestImpl::*)(), char const*) (gtest.cc:2078)
==20839==    by 0x43B782: bool testing::internal::HandleExceptionsInMethodIfSupported<testing::internal::UnitTestImpl, bool>(testing::internal::UnitTestImpl*, bool (testing::internal::UnitTestImpl::*)(), char const*) (gtest.cc:2114)
==20839==    by 0x4274CB: testing::UnitTest::Run() (gtest.cc:3926)
==20839==  Address 0xffeffffe2 is on thread 1's stack
==20839==  in frame #2, created by Socket_GetSocket_Test::TestBody() (socket_getSocket_test.cpp:7)
==20839== 
[       OK ] Socket.GetSocket (13 ms)
[ RUN      ] Socket.IPv4Send
../test/socket_send_test.cpp:129: Failure
Expected: (client1.read(5)) != (std::to_string(msgLen)), actual: "15000" vs "15000"
[  FAILED  ] Socket.IPv4Send (2110 ms)
[ RUN      ] Socket.IPv6Send
==20839== Syscall param socketcall.sendto(to.sin_addr) points to uninitialised byte(s)
==20839==    at 0x53E9AD3: ??? (syscall-template.S:84)
==20839==    by 0x41773E: auto net::Socket::low_write<long (&)(int, void const*, unsigned long, int, sockaddr const*, unsigned int), int const&, sockaddr*, unsigned long>(long (&)(int, void const*, unsigned long, int, sockaddr const*, unsigned int), std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int const&, sockaddr*&&, unsigned long&&) const (socket.hpp:50)
==20839==    by 0x4163E4: decltype (({parm#2}((declval<sockaddr_in6&>)())),((void)())) const net::Socket::send<Socket_IPv6Send_Test::TestBody()::{lambda(sockaddr_in6&)#1}>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, Socket_IPv6Send_Test::TestBody()::{lambda(sockaddr_in6&)#1}, net::Send, bool*) const (socket.hpp:575)
==20839==    by 0x41565D: Socket_IPv6Send_Test::TestBody() (socket_send_test.cpp:157)
==20839==    by 0x43F6BD: void testing::internal::HandleSehExceptionsInMethodIfSupported<testing::Test, void>(testing::Test*, void (testing::Test::*)(), char const*) (gtest.cc:2078)
==20839==    by 0x43A942: void testing::internal::HandleExceptionsInMethodIfSupported<testing::Test, void>(testing::Test*, void (testing::Test::*)(), char const*) (gtest.cc:2114)
==20839==    by 0x420B71: testing::Test::Run() (gtest.cc:2151)
==20839==    by 0x4213B3: testing::TestInfo::Run() (gtest.cc:2326)
==20839==    by 0x421A10: testing::TestCase::Run() (gtest.cc:2444)
==20839==    by 0x4288CF: testing::internal::UnitTestImpl::RunAllTests() (gtest.cc:4315)
==20839==    by 0x440602: bool testing::internal::HandleSehExceptionsInMethodIfSupported<testing::internal::UnitTestImpl, bool>(testing::internal::UnitTestImpl*, bool (testing::internal::UnitTestImpl::*)(), char const*) (gtest.cc:2078)
==20839==    by 0x43B782: bool testing::internal::HandleExceptionsInMethodIfSupported<testing::internal::UnitTestImpl, bool>(testing::internal::UnitTestImpl*, bool (testing::internal::UnitTestImpl::*)(), char const*) (gtest.cc:2114)
==20839==  Address 0xffefffe14 is on thread 1's stack
==20839==  in frame #2, created by decltype (({parm#2}((declval<sockaddr_in6&>)())),((void)())) const net::Socket::send<Socket_IPv6Send_Test::TestBody()::{lambda(sockaddr_in6&)#1}>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, Socket_IPv6Send_Test::TestBody()::{lambda(sockaddr_in6&)#1}, net::Send, bool*) const (socket.hpp:560)
==20839== 
../test/socket_send_test.cpp:163: Failure
Expected: (client2.read(5)) != (std::to_string(msgLen)), actual: "15000" vs "15000"
[  FAILED  ] Socket.IPv6Send (2026 ms)
[ RUN      ] Socket.Connectv4
[       OK ] Socket.Connectv4 (1011 ms)
==20839== Thread 2:
==20839== Invalid read of size 4
==20839==    at 0x44B6B1: net::Socket::accept(bool*) const (socket.cpp:168)
==20839==    by 0x4197C4: (anonymous namespace)::runServer(net::Socket&, unsigned int) (socket_connect_test.cpp:24)
==20839==    by 0x41AB04: void std::_Bind_simple<void (*(std::reference_wrapper<net::Socket>, int))(net::Socket&, unsigned int)>::_M_invoke<0ul, 1ul>(std::_Index_tuple<0ul, 1ul>) (functional:1400)
==20839==    by 0x41A9EB: std::_Bind_simple<void (*(std::reference_wrapper<net::Socket>, int))(net::Socket&, unsigned int)>::operator()() (functional:1389)
==20839==    by 0x41A91F: std::thread::_State_impl<std::_Bind_simple<void (*(std::reference_wrapper<net::Socket>, int))(net::Socket&, unsigned int)> >::_M_run() (thread:196)
==20839==    by 0x4EF554E: ??? (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.22)
==20839==    by 0x53E06B9: start_thread (pthread_create.c:333)
==20839==    by 0x56FC82C: clone (clone.S:109)
==20839==  Address 0xfff000068 is on thread 1's stack
==20839==  184 bytes below stack pointer
==20839== 
==20839== Invalid read of size 4
==20839==    at 0x44B6BE: net::Socket::accept(bool*) const (socket.cpp:168)
==20839==    by 0x4197C4: (anonymous namespace)::runServer(net::Socket&, unsigned int) (socket_connect_test.cpp:24)
==20839==    by 0x41AB04: void std::_Bind_simple<void (*(std::reference_wrapper<net::Socket>, int))(net::Socket&, unsigned int)>::_M_invoke<0ul, 1ul>(std::_Index_tuple<0ul, 1ul>) (functional:1400)
==20839==    by 0x41A9EB: std::_Bind_simple<void (*(std::reference_wrapper<net::Socket>, int))(net::Socket&, unsigned int)>::operator()() (functional:1389)
==20839==    by 0x41A91F: std::thread::_State_impl<std::_Bind_simple<void (*(std::reference_wrapper<net::Socket>, int))(net::Socket&, unsigned int)> >::_M_run() (thread:196)
==20839==    by 0x4EF554E: ??? (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.22)
==20839==    by 0x53E06B9: start_thread (pthread_create.c:333)
==20839==    by 0x56FC82C: clone (clone.S:109)
==20839==  Address 0xfff000064 is on thread 1's stack
==20839==  188 bytes below stack pointer
==20839== 

@agauniyal agauniyal merged commit 7d0c27f into feature/socket Mar 21, 2017
@agauniyal agauniyal deleted the recvTesting branch March 21, 2017 17:52
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants