Line data Source code
1 : /**
2 : * @file
3 : *
4 : * @brief
5 : *
6 : * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
7 : */
8 :
9 : #ifdef HAVE_KDBCONFIG_H
10 : #include "kdbconfig.h"
11 : #endif
12 :
13 : #include <stdio.h>
14 :
15 : #include "network.h"
16 :
17 : #include <tests.h>
18 :
19 : #define PLUGIN_NAME "network"
20 :
21 : static void testPorts (void);
22 :
23 : #include "../ipaddr/test_ipaddr.h"
24 :
25 2 : int main (int argc, char ** argv)
26 : {
27 2 : printf ("NETWORK TESTS\n");
28 2 : printf ("===============\n\n");
29 :
30 2 : init (argc, argv);
31 :
32 2 : testIPAll ();
33 2 : testPorts ();
34 :
35 2 : print_result ("testmod_network");
36 :
37 2 : return nbError;
38 : }
39 :
40 18 : static void testPort (char const * const port, const int ret, char const * const version, char const * const metaName)
41 : {
42 18 : Key * parentKey = keyNew ("user/tests/port", KEY_VALUE, "", KEY_END);
43 18 : KeySet * conf = ksNew (0, KS_END);
44 18 : KeySet * ks = ksNew (10, keyNew ("user/test/port/totest", KEY_VALUE, port, KEY_META, metaName, version, KEY_END), KS_END);
45 18 : PLUGIN_OPEN (PLUGIN_NAME);
46 18 : const int pluginStatus = plugin->kdbSet (plugin, ks, parentKey);
47 : char message[200];
48 18 : (void) snprintf (message, 200, "validation of %s ā%sā returned %d instead of %d", version[0] == '\0' ? "Port" : version, port,
49 : pluginStatus, ret);
50 18 : succeed_if (pluginStatus == ret, message);
51 18 : ksDel (ks);
52 18 : keyDel (parentKey);
53 18 : PLUGIN_CLOSE ();
54 18 : }
55 :
56 : static inline void testPortAny (char const * const port, int ret)
57 : {
58 18 : testPort (port, ret, "", "check/port");
59 : }
60 :
61 2 : static void testPorts (void)
62 : {
63 2 : testPortAny ("0", 1);
64 2 : testPortAny ("1234", 1);
65 2 : testPortAny ("65535", 1);
66 2 : testPortAny ("ssh", 1);
67 2 : testPortAny ("https", 1);
68 :
69 2 : testPortAny ("65536", -1);
70 2 : testPortAny ("-1", -1);
71 2 : testPortAny ("22d", -1);
72 2 : testPortAny ("myInvalidServiceName", -1);
73 :
74 : // Tests for ListenPort are not portable, even system ports in a range from 1-1000 can some short time be reachable
75 : // https://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers
76 2 : }
|