test-ipv4ll.c revision 996d16975b4d802335188a3be2bbc3635c1287f3
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering This file is part of systemd.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering Copyright (C) 2014 Axis Communications AB. All rights reserved.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering systemd is free software; you can redistribute it and/or modify it
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering under the terms of the GNU Lesser General Public License as published by
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering (at your option) any later version.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering systemd is distributed in the hope that it will be useful, but
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering Lesser General Public License for more details.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering You should have received a copy of the GNU Lesser General Public License
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersenstatic bool verbose = false;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic bool extended = false;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic int basic_request_handler_bind = 0;
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poetteringstatic int basic_request_handler_stop = 0;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic void* basic_request_handler_userdata = (void*)0xCABCAB;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic void basic_request_handler(sd_ipv4ll *ll, int event, void *userdata) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(userdata == basic_request_handler_userdata);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic int arp_network_send_raw_socket(int fd, int ifindex,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (send(fd, arp, sizeof(struct ether_arp), 0) < 0)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering be32_t pa, const struct ether_addr *ha) {
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen return arp_network_send_raw_socket(fd, ifindex, &ea);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen return arp_network_send_raw_socket(fd, ifindex, &ea);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringint arp_network_bind_raw_socket(int index, be32_t address, const struct ether_addr *eth_mac) {
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if (socketpair(AF_UNIX, SOCK_DGRAM | SOCK_NONBLOCK, 0, test_fd) < 0)
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersenstatic void test_public_api_setters(sd_event *e) {
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen .ether_addr_octet = {'A', 'B', 'C', '1', '2', '3'}};
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen assert_se(sd_ipv4ll_attach_event(NULL, NULL, 0) == -EINVAL);
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering assert_se(sd_ipv4ll_attach_event(ll, e, 0) == 0);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(sd_ipv4ll_attach_event(ll, e, 0) == -EBUSY);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen assert_se(sd_ipv4ll_set_callback(NULL, NULL, NULL) == -EINVAL);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen assert_se(sd_ipv4ll_set_callback(ll, NULL, NULL) == 0);
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering assert_se(sd_ipv4ll_set_address_seed(NULL, NULL) == -EINVAL);
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering assert_se(sd_ipv4ll_set_address_seed(ll, NULL) == -EINVAL);
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering assert_se(sd_ipv4ll_set_address_seed(ll, seed) == 0);
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering assert_se(sd_ipv4ll_set_mac(NULL, NULL) == -EINVAL);
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering assert_se(sd_ipv4ll_set_mac(ll, NULL) == -EINVAL);
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering assert_se(sd_ipv4ll_set_mac(ll, &mac_addr) == 0);
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering assert_se(sd_ipv4ll_set_index(NULL, -1) == -EINVAL);
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering assert_se(sd_ipv4ll_set_index(ll, -1) == -EINVAL);
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering assert_se(sd_ipv4ll_set_index(ll, -99) == -EINVAL);
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering assert_se(sd_ipv4ll_set_index(ll, 1) == 0);
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering assert_se(sd_ipv4ll_set_index(ll, 99) == 0);
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poetteringstatic void test_basic_request(sd_event *e) {
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering .ether_addr_octet = {'A', 'B', 'C', '1', '2', '3'}};
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering assert_se(sd_ipv4ll_attach_event(ll, e, 0) == 0);
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poettering assert_se(sd_ipv4ll_start(ll) == -EINVAL);
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poettering assert_se(sd_ipv4ll_set_mac(ll, &mac_addr) == 0);
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poettering assert_se(sd_ipv4ll_start(ll) == -EINVAL);
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poettering assert_se(sd_ipv4ll_set_callback(ll, basic_request_handler,
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering assert_se(sd_ipv4ll_start(ll) == -EINVAL);
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering assert_se(sd_ipv4ll_set_index(ll, 1) == 0);
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering assert_se(sd_ipv4ll_start(ll) == -EBUSY);
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering assert_se(read(test_fd[1], &arp, sizeof(struct ether_arp)) == sizeof(struct ether_arp));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(read(test_fd[1], &arp, sizeof(struct ether_arp)) == sizeof(struct ether_arp));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(read(test_fd[1], &arp, sizeof(struct ether_arp)) == sizeof(struct ether_arp));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(basic_request_handler_bind == 1);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(basic_request_handler_stop == 1);