1 #include "test-common.h"
2 #include "nm-glib-compat.h"
3
4 SignalData *
5 add_signal_full (const char *name, GCallback callback, int ifindex, const char *ifname)
6 {
7 SignalData *data = g_new0 (SignalData, 1);
8
9 data->name = name;
10 data->received = FALSE;
11 data->handler_id = g_signal_connect (nm_platform_get (), name, callback, data);
12 data->ifindex = ifindex;
13 data->ifname = ifname;
14
15 g_assert (data->handler_id >= 0);
16
17 return data;
18 }
19
20 void
21 accept_signal (SignalData *data)
22 {
23 debug ("Accepting signal '%s' ifindex %d ifname %s.", data->name, data->ifindex, data->ifname);
24 if (!data->received)
25 g_error ("Attemted to accept a non-received signal '%s'.", data->name);
26
27 data->received = FALSE;
28 }
29
30 void
31 wait_signal (SignalData *data)
32 {
33 data->loop = g_main_loop_new (NULL, FALSE);
34 g_main_loop_run (data->loop);
35 g_clear_pointer (&data->loop, g_main_loop_unref);
36
37 accept_signal (data);
38 }
39
40 void
41 free_signal (SignalData *data)
42 {
43 if (data->received)
44 g_error ("Attempted to free received but not accepted signal '%s'.", data->name);
45
46 g_signal_handler_disconnect (nm_platform_get (), data->handler_id);
47 g_free (data);
48 }
49
50 void
51 link_callback (NMPlatform *platform, int ifindex, NMPlatformLink *received, NMPlatformReason reason, SignalData *data)
52 {
53
54 GArray *links;
55 NMPlatformLink *cached;
56 int i;
57
58 g_assert (received);
59 g_assert_cmpint (received->ifindex, ==, ifindex);
60
61 if (data->ifindex && data->ifindex != received->ifindex)
62 return;
63 if (data->ifname && g_strcmp0 (data->ifname, nm_platform_link_get_name (ifindex)) != 0)
64 return;
65
66 if (data->loop) {
67 debug ("Quitting main loop.");
68 g_main_loop_quit (data->loop);
69 }
70
71 if (data->received)
72 g_error ("Received signal '%s' a second time.", data->name);
73
74 debug ("Received signal '%s' ifindex %d ifname '%s'.", data->name, ifindex, received->name);
75 data->received = TRUE;
76
77 /* Check the data */
78 g_assert (received->ifindex > 0);
79 links = nm_platform_link_get_all ();
80 for (i = 0; i < links->len; i++) {
81 cached = &g_array_index (links, NMPlatformLink, i);
82 if (cached->ifindex == received->ifindex) {
83 g_assert (!memcmp (cached, received, sizeof (*cached)));
84 if (!g_strcmp0 (data->name, NM_PLATFORM_LINK_REMOVED)) {
85 g_error ("Deleted link still found in the local cache.");
86 }
87 g_array_unref (links);
88 return;
89 }
90 }
91 g_array_unref (links);
92
93 if (g_strcmp0 (data->name, NM_PLATFORM_LINK_REMOVED))
94 g_error ("Added/changed link not found in the local cache.");
95 }
96
97 void
98 run_command (const char *format, ...)
99 {
100 char *command;
101 va_list ap;
102
(1) Event va_init: |
Initializing va_list "ap". |
Also see events: |
[missing_va_end] |
103 va_start (ap, format);
104
105 command = g_strdup_vprintf (format, ap);
106 debug ("Running command: %s", command);
(2) Event cond_false: |
Condition "!system(command)", taking false branch |
(3) Event else_branch: |
Reached else branch |
(4) Event cond_true: |
Condition "({...})", taking true branch |
(5) Event if_fallthrough: |
Falling through to end of if statement |
(6) Event if_end: |
End of if statement |
107 g_assert (!system (command));
108 debug ("Command finished.");
109 g_free (command);
(7) Event missing_va_end: |
va_end was not called for "ap". |
Also see events: |
[va_init] |
110 }
111
112 int
113 main (int argc, char **argv)
114 {
115 int result;
116
117 openlog (G_LOG_DOMAIN, LOG_CONS | LOG_PERROR, LOG_DAEMON);
118 g_type_init ();
119 g_test_init (&argc, &argv, NULL);
120 /* Enable debug messages if called with --debug */
121 for (; *argv; argv++) {
122 if (!g_strcmp0 (*argv, "--debug")) {
123 nm_logging_setup ("debug", NULL, NULL);
124 }
125 }
126
127 SETUP ();
128
129 setup_tests ();
130
131 result = g_test_run ();
132
133 nm_platform_link_delete (nm_platform_link_get_ifindex (DEVICE_NAME));
134
135 nm_platform_free ();
136 return result;
137 }
138