diff options
| author | Sebastien Bacher <seb128@ubuntu.com> | 2019-03-12 15:16:42 +0100 |
|---|---|---|
| committer | Sebastien Bacher <seb128@ubuntu.com> | 2019-03-12 15:16:42 +0100 |
| commit | cc4ab276f923ded9f415c1c2bf192994367ab0bb (patch) | |
| tree | ac3a7775665992b27d07eb44186d38ff961a8cd7 /libnm-core/tests/test-general.c | |
| parent | bb1cf58350bb34463e9ffc5f96ac4f9b6bf46d28 (diff) | |
| parent | dd428301eb6f02542015121d7b08d9997f137e50 (diff) | |
Update upstream source from tag 'upstream/1.15.91'
Update to upstream version '1.15.91' with Debian dir 74de38245314cab529c6c94cd9d0b874ce0c2994
Diffstat (limited to 'libnm-core/tests/test-general.c')
| -rw-r--r-- | libnm-core/tests/test-general.c | 158 |
1 files changed, 155 insertions, 3 deletions
diff --git a/libnm-core/tests/test-general.c b/libnm-core/tests/test-general.c index 0806e7b9..7d97296a 100644 --- a/libnm-core/tests/test-general.c +++ b/libnm-core/tests/test-general.c @@ -23,8 +23,6 @@ #include "nm-default.h" -#include <string.h> - #include "nm-utils/c-list-util.h" #include "nm-utils/nm-enum-utils.h" @@ -5524,6 +5522,158 @@ test_setting_user_data (void) /*****************************************************************************/ +typedef union { + struct sockaddr sa; + struct sockaddr_in in; + struct sockaddr_in6 in6; +} SockAddrUnion; + +static void +_sock_addr_endpoint (const char *endpoint, + const char *host, + gint32 port) +{ + nm_auto_unref_sockaddrendpoint NMSockAddrEndpoint *ep = NULL; + const char *s_endpoint; + const char *s_host; + gint32 s_port; + SockAddrUnion sockaddr = { }; + + g_assert (endpoint); + g_assert (!host == (port == -1)); + g_assert (port >= -1 && port <= G_MAXUINT16); + + ep = nm_sock_addr_endpoint_new (endpoint); + g_assert (ep); + + s_endpoint = nm_sock_addr_endpoint_get_endpoint (ep); + s_host = nm_sock_addr_endpoint_get_host (ep); + s_port = nm_sock_addr_endpoint_get_port (ep); + g_assert_cmpstr (endpoint, ==, s_endpoint); + g_assert_cmpstr (host, ==, s_host); + g_assert_cmpint (port, ==, s_port); + + g_assert (!nm_sock_addr_endpoint_get_fixed_sockaddr (ep, &sockaddr)); + + if (endpoint[0] != ' ') { + gs_free char *endpoint2 = NULL; + + /* also test with a leading space */ + endpoint2 = g_strdup_printf (" %s", endpoint); + _sock_addr_endpoint (endpoint2, host, port); + } + + if (endpoint[0] && endpoint[strlen (endpoint) - 1] != ' ') { + gs_free char *endpoint2 = NULL; + + /* also test with a trailing space */ + endpoint2 = g_strdup_printf ("%s ", endpoint); + _sock_addr_endpoint (endpoint2, host, port); + } +} + +static void +_sock_addr_endpoint_fixed (const char *endpoint, + const char *host, + guint16 port, + guint scope_id) +{ + nm_auto_unref_sockaddrendpoint NMSockAddrEndpoint *ep = NULL; + const char *s_endpoint; + const char *s_host; + gint32 s_port; + int addr_family; + NMIPAddr addrbin; + SockAddrUnion sockaddr = { }; + + g_assert (endpoint); + g_assert (host); + g_assert (port > 0); + + if (!nm_utils_parse_inaddr_bin (AF_UNSPEC, host, &addr_family, &addrbin)) + g_assert_not_reached (); + + ep = nm_sock_addr_endpoint_new (endpoint); + g_assert (ep); + + s_endpoint = nm_sock_addr_endpoint_get_endpoint (ep); + s_host = nm_sock_addr_endpoint_get_host (ep); + s_port = nm_sock_addr_endpoint_get_port (ep); + g_assert_cmpstr (endpoint, ==, s_endpoint); + g_assert_cmpstr (NULL, !=, s_host); + g_assert_cmpint (port, ==, s_port); + + if (!nm_sock_addr_endpoint_get_fixed_sockaddr (ep, &sockaddr)) + g_assert_not_reached (); + + g_assert_cmpint (sockaddr.sa.sa_family, ==, addr_family); + if (addr_family == AF_INET) { + const SockAddrUnion s = { + .in = { + .sin_family = AF_INET, + .sin_addr = addrbin.addr4_struct, + .sin_port = htons (port), + }, + }; + + g_assert_cmpint (sockaddr.in.sin_addr.s_addr, ==, addrbin.addr4); + g_assert_cmpint (sockaddr.in.sin_port, ==, htons (port)); + g_assert (memcmp (&s, &sockaddr, sizeof (s.in)) == 0); + } else if (addr_family == AF_INET6) { + const SockAddrUnion s = { + .in6 = { + .sin6_family = AF_INET6, + .sin6_addr = addrbin.addr6, + .sin6_scope_id = scope_id, + .sin6_port = htons (port), + }, + }; + + g_assert (memcmp (&sockaddr.in6.sin6_addr, &addrbin, sizeof (addrbin.addr6)) == 0); + g_assert_cmpint (sockaddr.in6.sin6_port, ==, htons (port)); + g_assert_cmpint (sockaddr.in6.sin6_scope_id, ==, scope_id); + g_assert_cmpint (sockaddr.in6.sin6_flowinfo, ==, 0); + g_assert (memcmp (&s, &sockaddr, sizeof (s.in6)) == 0); + } else + g_assert_not_reached (); +} + +static void +test_sock_addr_endpoint (void) +{ + _sock_addr_endpoint ("", NULL, -1); + _sock_addr_endpoint (":", NULL, -1); + _sock_addr_endpoint ("a", NULL, -1); + _sock_addr_endpoint ("a:", NULL, -1); + _sock_addr_endpoint (":a", NULL, -1); + _sock_addr_endpoint ("[]:a", NULL, -1); + _sock_addr_endpoint ("[]a", NULL, -1); + _sock_addr_endpoint ("[]:", NULL, -1); + _sock_addr_endpoint ("[a]b", NULL, -1); + _sock_addr_endpoint ("[a:b", NULL, -1); + _sock_addr_endpoint ("[a[:b", NULL, -1); + _sock_addr_endpoint ("a:6", "a", 6); + _sock_addr_endpoint ("a:6", "a", 6); + _sock_addr_endpoint ("[a]:6", "a", 6); + _sock_addr_endpoint ("[a]:6", "a", 6); + _sock_addr_endpoint ("[a]:655", "a", 655); + _sock_addr_endpoint ("[ab]:][6", NULL, -1); + _sock_addr_endpoint ("[ab]:]:[6", NULL, -1); + _sock_addr_endpoint ("[a[]:b", NULL, -1); + _sock_addr_endpoint ("[192.169.6.x]:6", "192.169.6.x", 6); + _sock_addr_endpoint ("[192.169.6.x]:0", NULL, -1); + _sock_addr_endpoint ("192.169.6.7:0", NULL, -1); + + _sock_addr_endpoint_fixed ("192.169.6.7:6", "192.169.6.7", 6, 0); + _sock_addr_endpoint_fixed ("[192.169.6.7]:6", "192.169.6.7", 6, 0); + _sock_addr_endpoint_fixed ("[a:b::]:6", "a:b::", 6, 0); + _sock_addr_endpoint_fixed ("[a:b::%7]:6", "a:b::", 6, 7); + _sock_addr_endpoint_fixed ("a:b::1%75:6", "a:b::1", 6, 75); + _sock_addr_endpoint_fixed ("a:b::1%0:64", "a:b::1", 64, 0); +} + +/*****************************************************************************/ + static void test_hexstr2bin (void) { @@ -5889,7 +6039,7 @@ __test_uuid (const char *expected_uuid, const char *str, gssize slen, char *uuid static void test_nm_utils_uuid_generate_from_strings (void) { - const NMUuid uuid0 = { 0 }; + const NMUuid uuid0 = { }; g_assert_cmpmem (&uuid0, sizeof (uuid0), _uuid ("00000000-0000-0000-0000-000000000000"), 16); @@ -7737,6 +7887,8 @@ int main (int argc, char **argv) g_test_add_func ("/core/general/test_setting_compare_default_strv", test_setting_compare_default_strv); g_test_add_func ("/core/general/test_setting_user_data", test_setting_user_data); + g_test_add_func ("/core/general/test_sock_addr_endpoint", test_sock_addr_endpoint); + g_test_add_func ("/core/general/hexstr2bin", test_hexstr2bin); g_test_add_func ("/core/general/nm_strquote", test_nm_strquote); g_test_add_func ("/core/general/test_nm_utils_uuid_generate_from_string", test_nm_utils_uuid_generate_from_string); |