f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen This file is part of systemd.
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen Copyright 2013 Tom Gundersen <teg@jklm.no>
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen systemd is free software; you can redistribute it and/or modify it
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen under the terms of the GNU Lesser General Public License as published by
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen the Free Software Foundation; either version 2.1 of the License, or
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen (at your option) any later version.
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen systemd is distributed in the hope that it will be useful, but
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen WITHOUT ANY WARRANTY; without even the implied warranty of
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen Lesser General Public License for more details.
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen You should have received a copy of the GNU Lesser General Public License
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen along with systemd; If not, see <http://www.gnu.org/licenses/>.
09bee74d7a5f266b175baa19892fa84a9da51d7fTom Gundersen _cleanup_free_ struct in_addr *addresses = NULL;
09bee74d7a5f266b175baa19892fa84a9da51d7fTom Gundersen _cleanup_free_ struct in6_addr *addresses6 = NULL;
09bee74d7a5f266b175baa19892fa84a9da51d7fTom Gundersen struct in_addr a, b, c;
09bee74d7a5f266b175baa19892fa84a9da51d7fTom Gundersen struct in6_addr d, e, f;
09bee74d7a5f266b175baa19892fa84a9da51d7fTom Gundersen const char *addresses_string = "192.168.0.1 0:0:0:0:0:FFFF:204.152.189.116 192.168.0.2 ::1 192.168.0.3 1:0:0:0:0:0:0:8";
09bee74d7a5f266b175baa19892fa84a9da51d7fTom Gundersen assert_se(inet_pton(AF_INET, "0:0:0:0:0:FFFF:204.152.189.116", &a) == 0);
09bee74d7a5f266b175baa19892fa84a9da51d7fTom Gundersen assert_se(inet_pton(AF_INET6, "192.168.0.1", &d) == 0);
09bee74d7a5f266b175baa19892fa84a9da51d7fTom Gundersen assert_se(inet_pton(AF_INET, "192.168.0.1", &a) == 1);
09bee74d7a5f266b175baa19892fa84a9da51d7fTom Gundersen assert_se(inet_pton(AF_INET, "192.168.0.2", &b) == 1);
09bee74d7a5f266b175baa19892fa84a9da51d7fTom Gundersen assert_se(inet_pton(AF_INET, "192.168.0.3", &c) == 1);
09bee74d7a5f266b175baa19892fa84a9da51d7fTom Gundersen assert_se(inet_pton(AF_INET6, "0:0:0:0:0:FFFF:204.152.189.116", &d) == 1);
09bee74d7a5f266b175baa19892fa84a9da51d7fTom Gundersen assert_se(inet_pton(AF_INET6, "::1", &e) == 1);
09bee74d7a5f266b175baa19892fa84a9da51d7fTom Gundersen assert_se(inet_pton(AF_INET6, "1:0:0:0:0:0:0:8", &f) == 1);
a2ba62c719224a4b47751623ca5e8b0333f49721Lennart Poettering assert_se((size = deserialize_in_addrs(&addresses, addresses_string)) >= 0);
09bee74d7a5f266b175baa19892fa84a9da51d7fTom Gundersen assert_se(!memcmp(&a, &addresses[0], sizeof(struct in_addr)));
09bee74d7a5f266b175baa19892fa84a9da51d7fTom Gundersen assert_se(!memcmp(&b, &addresses[1], sizeof(struct in_addr)));
09bee74d7a5f266b175baa19892fa84a9da51d7fTom Gundersen assert_se(!memcmp(&c, &addresses[2], sizeof(struct in_addr)));
a2ba62c719224a4b47751623ca5e8b0333f49721Lennart Poettering assert_se((size = deserialize_in6_addrs(&addresses6, addresses_string)) >= 0);
09bee74d7a5f266b175baa19892fa84a9da51d7fTom Gundersen assert_se(!memcmp(&d, &addresses6[0], sizeof(struct in6_addr)));
09bee74d7a5f266b175baa19892fa84a9da51d7fTom Gundersen assert_se(!memcmp(&e, &addresses6[1], sizeof(struct in6_addr)));
09bee74d7a5f266b175baa19892fa84a9da51d7fTom Gundersen assert_se(!memcmp(&f, &addresses6[2], sizeof(struct in6_addr)));
e1ea665edac17d75fce01b72dadfa3211b60df2cEugene Yakubovichstatic void test_deserialize_dhcp_routes(void) {
e1ea665edac17d75fce01b72dadfa3211b60df2cEugene Yakubovich _cleanup_free_ struct sd_dhcp_route *routes = NULL;
e1ea665edac17d75fce01b72dadfa3211b60df2cEugene Yakubovich assert_se(deserialize_dhcp_routes(&routes, &size, &allocated, "") >= 0);
e1ea665edac17d75fce01b72dadfa3211b60df2cEugene Yakubovich /* no errors */
e1ea665edac17d75fce01b72dadfa3211b60df2cEugene Yakubovich _cleanup_free_ struct sd_dhcp_route *routes = NULL;
e1ea665edac17d75fce01b72dadfa3211b60df2cEugene Yakubovich const char *routes_string = "192.168.0.0/16,192.168.0.1 10.1.2.0/24,10.1.2.1 0.0.0.0/0,10.0.1.1";
e1ea665edac17d75fce01b72dadfa3211b60df2cEugene Yakubovich assert_se(deserialize_dhcp_routes(&routes, &size, &allocated, routes_string) >= 0);
e1ea665edac17d75fce01b72dadfa3211b60df2cEugene Yakubovich assert_se(routes[0].dst_addr.s_addr == inet_addr("192.168.0.0"));
e1ea665edac17d75fce01b72dadfa3211b60df2cEugene Yakubovich assert_se(routes[0].gw_addr.s_addr == inet_addr("192.168.0.1"));
e1ea665edac17d75fce01b72dadfa3211b60df2cEugene Yakubovich assert_se(routes[1].dst_addr.s_addr == inet_addr("10.1.2.0"));
e1ea665edac17d75fce01b72dadfa3211b60df2cEugene Yakubovich assert_se(routes[1].gw_addr.s_addr == inet_addr("10.1.2.1"));
e1ea665edac17d75fce01b72dadfa3211b60df2cEugene Yakubovich assert_se(routes[2].dst_addr.s_addr == inet_addr("0.0.0.0"));
e1ea665edac17d75fce01b72dadfa3211b60df2cEugene Yakubovich assert_se(routes[2].gw_addr.s_addr == inet_addr("10.0.1.1"));
e1ea665edac17d75fce01b72dadfa3211b60df2cEugene Yakubovich /* error in second word */
e1ea665edac17d75fce01b72dadfa3211b60df2cEugene Yakubovich _cleanup_free_ struct sd_dhcp_route *routes = NULL;
e1ea665edac17d75fce01b72dadfa3211b60df2cEugene Yakubovich const char *routes_string = "192.168.0.0/16,192.168.0.1 10.1.2.0#24,10.1.2.1 0.0.0.0/0,10.0.1.1";
e1ea665edac17d75fce01b72dadfa3211b60df2cEugene Yakubovich assert_se(deserialize_dhcp_routes(&routes, &size, &allocated, routes_string) >= 0);
e1ea665edac17d75fce01b72dadfa3211b60df2cEugene Yakubovich assert_se(routes[0].dst_addr.s_addr == inet_addr("192.168.0.0"));
e1ea665edac17d75fce01b72dadfa3211b60df2cEugene Yakubovich assert_se(routes[0].gw_addr.s_addr == inet_addr("192.168.0.1"));
5bb14c8651b017983bb9cbd1444969c7a1bda14eTom Gundersen assert_se(routes[1].dst_addr.s_addr == inet_addr("0.0.0.0"));
5bb14c8651b017983bb9cbd1444969c7a1bda14eTom Gundersen assert_se(routes[1].gw_addr.s_addr == inet_addr("10.0.1.1"));
e1ea665edac17d75fce01b72dadfa3211b60df2cEugene Yakubovich /* error in every word */
e1ea665edac17d75fce01b72dadfa3211b60df2cEugene Yakubovich _cleanup_free_ struct sd_dhcp_route *routes = NULL;
e1ea665edac17d75fce01b72dadfa3211b60df2cEugene Yakubovich const char *routes_string = "192.168.0.0/55,192.168.0.1 10.1.2.0#24,10.1.2.1 0.0.0.0/0,10.0.1.X";
e1ea665edac17d75fce01b72dadfa3211b60df2cEugene Yakubovich assert_se(deserialize_dhcp_routes(&routes, &size, &allocated, routes_string) >= 0);
5347925a59530bd4e6616eb8c8d43a02c3e14c46Tom Gundersenstatic int test_load_config(Manager *manager) {
7c99d940c11e4da1863a218b6b70dd16e65b7518Tom Gundersen/* TODO: should_reload, is false if the config dirs do not exist, so
7c99d940c11e4da1863a218b6b70dd16e65b7518Tom Gundersen * so we can't do this test here, move it to a test for paths_check_timestamps
12e0f830f592ec4c6bb49ac7ae1e0e84f74105e3Tom Gundersen * assert_se(network_should_reload(manager) == true);
12e0f830f592ec4c6bb49ac7ae1e0e84f74105e3Tom Gundersen assert_se(manager_should_reload(manager) == false);
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersenstatic void test_network_get(Manager *manager, struct udev_device *loopback) {
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen /* let's assume that the test machine does not have a .network file
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen that applies to the loopback device... */
505f8da7325591defe5f751f328bd26915267602Tom Gundersen assert_se(network_get(manager, loopback, "lo", &mac, &network) == -ENOENT);
09bee74d7a5f266b175baa19892fa84a9da51d7fTom Gundersen _cleanup_address_free_ Address *a1 = NULL, *a2 = NULL;
9505d3c6deda0452c22ab2ed47bca74b98d87a17Tom Gundersen assert_se(inet_pton(AF_INET, "192.168.3.9", &a1->in_addr.in));
9505d3c6deda0452c22ab2ed47bca74b98d87a17Tom Gundersen assert_se(inet_pton(AF_INET, "192.168.3.9", &a2->in_addr.in));
3ac8e5433f20bfb5e12b918252643106a2de52afTom Gundersen assert_se(inet_pton(AF_INET, "192.168.3.10", &a1->in_addr_peer.in));
3ac8e5433f20bfb5e12b918252643106a2de52afTom Gundersen assert_se(inet_pton(AF_INET, "192.168.3.11", &a2->in_addr_peer.in));
9505d3c6deda0452c22ab2ed47bca74b98d87a17Tom Gundersen assert_se(inet_pton(AF_INET6, "2001:4ca0:4f01::2", &a1->in_addr.in6));
9505d3c6deda0452c22ab2ed47bca74b98d87a17Tom Gundersen assert_se(inet_pton(AF_INET6, "2001:4ca0:4f01::2", &a2->in_addr.in6));
9505d3c6deda0452c22ab2ed47bca74b98d87a17Tom Gundersen assert_se(inet_pton(AF_INET6, "2001:4ca0:4f01::1", &a2->in_addr.in6));
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen _cleanup_manager_free_ Manager *manager = NULL;
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen loopback = udev_device_new_from_syspath(udev, "/sys/class/net/lo");
12e0f830f592ec4c6bb49ac7ae1e0e84f74105e3Tom Gundersen assert_se(udev_device_get_ifindex(loopback) == 1);