simple-socket library 1.2.0
SocketUtils_TEST.cpp
Go to the documentation of this file.
1#include <cppunit/extensions/HelperMacros.h>
2#include "../SocketUtils.h"
3#include <iostream>
4
5class SocketUtils_TEST : public CppUnit::TestFixture
6{
7 CPPUNIT_TEST_SUITE( SocketUtils_TEST);
8 CPPUNIT_TEST( resolveHostname );
9 CPPUNIT_TEST( resolveService );
10 CPPUNIT_TEST( getNetworkInterfaces );
11 CPPUNIT_TEST( getInterfaceAddress );
12 CPPUNIT_TEST( getBroadcastAddress );
13 CPPUNIT_TEST( getNetmask );
14 CPPUNIT_TEST( getHardwareAddress );
15 CPPUNIT_TEST_SUITE_END();
16
17public:
18 void setUp() {}
19 void tearDown() {}
20
21 // should work as long as one has a resonable setup
22 void resolveHostname() {
23 CPPUNIT_ASSERT_EQUAL(
24 std::string("127.0.0.1"),
25 NET::resolveHostname( "localhost"));
26 }
27
28 void resolveService() {
29 // HTTP over TCP is known, also over UDP, but not via DDP
30 CPPUNIT_ASSERT( NET::resolveService( "http") == 80);
31 CPPUNIT_ASSERT( NET::resolveService( "http", "") == 80);
32 CPPUNIT_ASSERT( NET::resolveService( "http", "udp") == 80);
33 CPPUNIT_ASSERT( NET::resolveService( "http", "ddp") == 0);
34
35 // but there is no rtmp over TCP or UDP
36 CPPUNIT_ASSERT( NET::resolveService( "rtmp") == 0);
37 CPPUNIT_ASSERT( NET::resolveService( "rtmp", "udp") == 0);
38
39 // but there is over DDP
40 CPPUNIT_ASSERT( NET::resolveService( "rtmp", "ddp") == 1);
41 CPPUNIT_ASSERT( NET::resolveService( "rtmp", "") == 1);
42
43 // there is no IANA protocol named 'foo'
44 CPPUNIT_ASSERT( NET::resolveService( "foo") == 0);
45
46 // also not on other transport layers
47 CPPUNIT_ASSERT( NET::resolveService( "foo", "") == 0);
48 }
49
50 void getNetworkInterfaces() {
51 auto list = NET::getNetworkInterfaces();
52 CPPUNIT_ASSERT( list.size() >= 1 );
53 CPPUNIT_ASSERT_EQUAL(
54 std::string("lo"),
55 list[0]);
56 }
57
58 void getInterfaceAddress() {
59 CPPUNIT_ASSERT_EQUAL(
60 std::string("127.0.0.1"),
62 }
63
64 void getBroadcastAddress() {
65 CPPUNIT_ASSERT_EQUAL(
66 std::string("0.0.0.0"),
68 }
69
70 void getNetmask() {
71 CPPUNIT_ASSERT_EQUAL(
72 std::string("255.0.0.0"),
73 NET::getNetmask("lo"));
74 }
75
76 void getHardwareAddress() {
77 auto list = NET::getNetworkInterfaces();
78 CPPUNIT_ASSERT( list.size() >= 2 );
79 auto mac = NET::getHardwareAddress(list[1]);
80 CPPUNIT_ASSERT( mac.size() == 17);
81 }
82};
83
CPPUNIT_TEST_SUITE_REGISTRATION(SocketUtils_TEST)
std::string getHardwareAddress(std::string_view interface)
Return the MAC address of the given network interfaces.
std::string getInterfaceAddress(std::string_view interface)
Return the IPv4 address of the given network interface.
uint16_t resolveService(const std::string &service, const std::string &protocol="tcp")
std::vector< std::string > getNetworkInterfaces()
Return a list of available network interfaces.
std::string getBroadcastAddress(std::string_view interface)
Return the IPv4 broadcast address of the given network interface.
std::string getNetmask(std::string_view interface)
Return the IPv4 netmask of the given network interface.
std::string resolveHostname(const std::string &hostname)