]> git.proxmox.com Git - systemd.git/blob - src/libsystemd-network/test-ipv4ll.c
Enable seccomp support on powerpc, ppc64el, and s390x
[systemd.git] / src / libsystemd-network / test-ipv4ll.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2 /***
3 This file is part of systemd.
4
5 Copyright (C) 2014 Axis Communications AB. All rights reserved.
6
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 #include <assert.h>
22 #include <errno.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <sys/socket.h>
26 #include <sys/types.h>
27 #include <unistd.h>
28
29 #include "sd-ipv4ll.h"
30
31 #include "arp-util.h"
32 #include "event-util.h"
33 #include "fd-util.h"
34 #include "socket-util.h"
35 #include "util.h"
36
37 static bool verbose = false;
38 static bool extended = false;
39 static int test_fd[2];
40
41 static int basic_request_handler_bind = 0;
42 static int basic_request_handler_stop = 0;
43 static void* basic_request_handler_userdata = (void*)0xCABCAB;
44 static void basic_request_handler(sd_ipv4ll *ll, int event, void *userdata) {
45 assert_se(userdata == basic_request_handler_userdata);
46
47 switch(event) {
48 case SD_IPV4LL_EVENT_STOP:
49 basic_request_handler_stop = 1;
50 break;
51 case SD_IPV4LL_EVENT_BIND:
52 basic_request_handler_bind = 1;
53 break;
54 default:
55 assert_se(0);
56 break;
57 }
58 }
59
60 static int arp_network_send_raw_socket(int fd, int ifindex,
61 const struct ether_arp *arp) {
62 assert_se(arp);
63 assert_se(ifindex > 0);
64 assert_se(fd >= 0);
65
66 if (send(fd, arp, sizeof(struct ether_arp), 0) < 0)
67 return -errno;
68
69 return 0;
70 }
71
72 int arp_send_probe(int fd, int ifindex,
73 be32_t pa, const struct ether_addr *ha) {
74 struct ether_arp ea = {};
75
76 assert(fd >= 0);
77 assert(ifindex > 0);
78 assert(pa != 0);
79 assert(ha);
80
81 return arp_network_send_raw_socket(fd, ifindex, &ea);
82 }
83
84 int arp_send_announcement(int fd, int ifindex,
85 be32_t pa, const struct ether_addr *ha) {
86 struct ether_arp ea = {};
87
88 assert(fd >= 0);
89 assert(ifindex > 0);
90 assert(pa != 0);
91 assert(ha);
92
93 return arp_network_send_raw_socket(fd, ifindex, &ea);
94 }
95
96 int arp_network_bind_raw_socket(int index, be32_t address, const struct ether_addr *eth_mac) {
97 if (socketpair(AF_UNIX, SOCK_DGRAM | SOCK_NONBLOCK, 0, test_fd) < 0)
98 return -errno;
99
100 return test_fd[0];
101 }
102
103 static void test_public_api_setters(sd_event *e) {
104 struct in_addr address = {};
105 unsigned seed = 0;
106 sd_ipv4ll *ll;
107 struct ether_addr mac_addr = {
108 .ether_addr_octet = {'A', 'B', 'C', '1', '2', '3'}};
109
110 if (verbose)
111 printf("* %s\n", __FUNCTION__);
112
113 assert_se(sd_ipv4ll_new(&ll) == 0);
114 assert_se(ll);
115
116 assert_se(sd_ipv4ll_attach_event(NULL, NULL, 0) == -EINVAL);
117 assert_se(sd_ipv4ll_attach_event(ll, e, 0) == 0);
118 assert_se(sd_ipv4ll_attach_event(ll, e, 0) == -EBUSY);
119
120 assert_se(sd_ipv4ll_set_callback(NULL, NULL, NULL) == -EINVAL);
121 assert_se(sd_ipv4ll_set_callback(ll, NULL, NULL) == 0);
122
123 assert_se(sd_ipv4ll_set_address(ll, &address) == -EINVAL);
124 address.s_addr |= htobe32(169U << 24 | 254U << 16);
125 assert_se(sd_ipv4ll_set_address(ll, &address) == -EINVAL);
126 address.s_addr |= htobe32(0x00FF);
127 assert_se(sd_ipv4ll_set_address(ll, &address) == -EINVAL);
128 address.s_addr |= htobe32(0xF000);
129 assert_se(sd_ipv4ll_set_address(ll, &address) == 0);
130 address.s_addr |= htobe32(0x0F00);
131 assert_se(sd_ipv4ll_set_address(ll, &address) == -EINVAL);
132
133 assert_se(sd_ipv4ll_set_address_seed(NULL, seed) == -EINVAL);
134 assert_se(sd_ipv4ll_set_address_seed(ll, seed) == 0);
135
136 assert_se(sd_ipv4ll_set_mac(NULL, NULL) == -EINVAL);
137 assert_se(sd_ipv4ll_set_mac(ll, NULL) == -EINVAL);
138 assert_se(sd_ipv4ll_set_mac(ll, &mac_addr) == 0);
139
140 assert_se(sd_ipv4ll_set_index(NULL, -1) == -EINVAL);
141 assert_se(sd_ipv4ll_set_index(ll, -1) == -EINVAL);
142 assert_se(sd_ipv4ll_set_index(ll, -99) == -EINVAL);
143 assert_se(sd_ipv4ll_set_index(ll, 1) == 0);
144 assert_se(sd_ipv4ll_set_index(ll, 99) == 0);
145
146 assert_se(sd_ipv4ll_ref(ll) == ll);
147 assert_se(sd_ipv4ll_unref(ll) == NULL);
148
149 /* Cleanup */
150 assert_se(sd_ipv4ll_unref(ll) == NULL);
151 }
152
153 static void test_basic_request(sd_event *e) {
154
155 sd_ipv4ll *ll;
156 struct ether_arp arp;
157 struct ether_addr mac_addr = {
158 .ether_addr_octet = {'A', 'B', 'C', '1', '2', '3'}};
159
160 if (verbose)
161 printf("* %s\n", __FUNCTION__);
162
163 assert_se(sd_ipv4ll_new(&ll) == 0);
164 assert_se(sd_ipv4ll_start(ll) == -EINVAL);
165
166 assert_se(sd_ipv4ll_attach_event(ll, e, 0) == 0);
167 assert_se(sd_ipv4ll_start(ll) == -EINVAL);
168
169 assert_se(sd_ipv4ll_set_mac(ll, &mac_addr) == 0);
170 assert_se(sd_ipv4ll_start(ll) == -EINVAL);
171
172 assert_se(sd_ipv4ll_set_callback(ll, basic_request_handler,
173 basic_request_handler_userdata) == 0);
174 assert_se(sd_ipv4ll_start(ll) == -EINVAL);
175
176 assert_se(sd_ipv4ll_set_index(ll, 1) == 0);
177 assert_se(sd_ipv4ll_start(ll) == 0);
178
179 sd_event_run(e, (uint64_t) -1);
180 assert_se(sd_ipv4ll_start(ll) == -EBUSY);
181
182 assert_se(sd_ipv4ll_is_running(ll));
183
184 /* PROBE */
185 sd_event_run(e, (uint64_t) -1);
186 assert_se(read(test_fd[1], &arp, sizeof(struct ether_arp)) == sizeof(struct ether_arp));
187
188 if (extended) {
189 /* PROBE */
190 sd_event_run(e, (uint64_t) -1);
191 assert_se(read(test_fd[1], &arp, sizeof(struct ether_arp)) == sizeof(struct ether_arp));
192
193 /* PROBE */
194 sd_event_run(e, (uint64_t) -1);
195 assert_se(read(test_fd[1], &arp, sizeof(struct ether_arp)) == sizeof(struct ether_arp));
196
197 sd_event_run(e, (uint64_t) -1);
198 assert_se(basic_request_handler_bind == 1);
199 }
200
201 sd_ipv4ll_stop(ll);
202 assert_se(basic_request_handler_stop == 1);
203
204 /* Cleanup */
205 assert_se(sd_ipv4ll_unref(ll) == NULL);
206 safe_close(test_fd[1]);
207 }
208
209 int main(int argc, char *argv[]) {
210 _cleanup_event_unref_ sd_event *e = NULL;
211
212 log_set_max_level(LOG_DEBUG);
213 log_parse_environment();
214 log_open();
215
216 assert_se(sd_event_new(&e) >= 0);
217
218 test_public_api_setters(e);
219 test_basic_request(e);
220
221 return 0;
222 }