I have two gen_servers that communicate using gen_tcp sockets.
The first gen_server exports a function, that when called builds (calling another function) a RFC 791 packet, connects to a socket where the other gen_server is listening for incoming connections, and sends the packet to it.
I tested this in the shell and it is working, but what would be the right tool/way to test such a code? Should I use eunit or or is there any other tool more suitable?
Moreover I would like to know what should I actually test? Only the sending part or also function for packet construction?
You can definitely write some EUnit tests for every gen_servers:
http://www.erlang.org/doc/apps/eunit/chapter.html
http://learnyousomeerlang.com/eunit#the-need-for-tests
You can also have a look at Common Test to test the interaction:
http://www.erlang.org/doc/apps/common_test/basics_chapter.html
http://learnyousomeerlang.com/common-test-for-uncommon-tests#what-is-common-test
Since your implementation strongly depends on the data passed, I would have a look to generators provided by QuickCheck Mini or PropEr:
http://www.quviq.com/
http://proper.softlab.ntua.gr/
A brief explaination on how you can improve your unit tests with something like QuickCheck mini is available here:
http://www.erlang-solutions.com/upload/docs/85/EUG-London-Apr2011.pdf
As a start, I would focus on testing the functions you export (the module interface). You can still add more tests later.