diff options
author | Lubomir Rintel <lkundrak@v3.sk> | 2019-03-06 16:59:53 +0100 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2019-03-07 22:22:39 +0100 |
commit | bf365e9762dfcb2cd0f9f557b19c8cf541854de1 (patch) | |
tree | 340f9cbfabf42b88b900120fc69ccad1749fc00a | |
parent | a6ee43d1c19bd7bb0905ea990b7f16a2aacf43b6 (diff) | |
download | NetworkManager-bf365e9762dfcb2cd0f9f557b19c8cf541854de1.tar.gz |
clients/tests: add wireguard import tests
(cherry picked from commit c152ca37efb67543b34868d5415bf8e03052e34e)
-rw-r--r-- | Makefile.am | 7 | ||||
-rw-r--r-- | clients/common/tests/test-general.c | 96 | ||||
-rw-r--r-- | clients/common/tests/wg-test0.conf | 18 | ||||
-rw-r--r-- | clients/common/tests/wg-test1.conf | 3 | ||||
-rw-r--r-- | clients/common/tests/wg-test2.conf | 8 | ||||
-rw-r--r-- | clients/common/tests/wg-test3.conf | 3 |
6 files changed, 135 insertions, 0 deletions
diff --git a/Makefile.am b/Makefile.am index b5dc5f9440..b180466adc 100644 --- a/Makefile.am +++ b/Makefile.am @@ -4004,6 +4004,13 @@ clients_common_tests_test_general_LDADD = \ $(clients_common_tests_test_general_OBJECTS): $(libnm_core_lib_h_pub_mkenums) +EXTRA_DIST += \ + clients/common/tests/wg-test0.conf \ + clients/common/tests/wg-test1.conf \ + clients/common/tests/wg-test2.conf \ + clients/common/tests/wg-test3.conf \ + $(NULL) + ############################################################################### # clients/cli ############################################################################### diff --git a/clients/common/tests/test-general.c b/clients/common/tests/test-general.c index 8f96eb262f..c39901bae8 100644 --- a/clients/common/tests/test-general.c +++ b/clients/common/tests/test-general.c @@ -20,6 +20,7 @@ #include "nm-default.h" #include "nm-meta-setting-access.h" +#include "nm-vpn-helpers.h" #include "nm-utils/nm-test-utils.h" @@ -145,6 +146,96 @@ test_client_meta_check (void) /*****************************************************************************/ +static void +test_client_import_wireguard_test0 (void) +{ + gs_unref_object NMConnection *connection; + NMSettingWireGuard *s_wg; + NMWireGuardPeer *peer; + gs_free_error GError *error = NULL; + + connection = nm_vpn_wireguard_import (NM_BUILD_SRCDIR"/clients/common/tests/wg-test0.conf", + &error); + + g_assert_no_error (error); + + g_assert_cmpstr (nm_connection_get_id (connection), ==, "wg-test0"); + g_assert_cmpstr (nm_connection_get_interface_name (connection), ==, "wg-test0"); + g_assert_cmpstr (nm_connection_get_connection_type (connection), ==, NM_SETTING_WIREGUARD_SETTING_NAME); + + s_wg = NM_SETTING_WIREGUARD (nm_connection_get_setting (connection, NM_TYPE_SETTING_WIREGUARD)); + + g_assert_cmpint (nm_setting_wireguard_get_listen_port (s_wg), ==, 51820); + g_assert_cmpstr (nm_setting_wireguard_get_private_key (s_wg), ==, "yAnz5TF+lXXJte14tji3zlMNq+hd2rYUIgJBgB3fBmk="); + + g_assert_cmpint (nm_setting_wireguard_get_peers_len (s_wg), ==, 3); + + peer = nm_setting_wireguard_get_peer (s_wg, 0); + g_assert_cmpstr (nm_wireguard_peer_get_public_key (peer), ==, "xTIBA5rboUvnH4htodjb6e697QjLERt1NAB4mZqp8Dg="); + g_assert_cmpstr (nm_wireguard_peer_get_endpoint (peer), ==, "192.95.5.67:1234"); + g_assert_cmpint (nm_wireguard_peer_get_allowed_ips_len (peer), ==, 2); + g_assert_cmpstr (nm_wireguard_peer_get_allowed_ip (peer, 0, NULL), ==, "10.192.122.3/32"); + g_assert_cmpstr (nm_wireguard_peer_get_allowed_ip (peer, 1, NULL), ==, "10.192.124.1/24"); + + peer = nm_setting_wireguard_get_peer (s_wg, 1); + g_assert_cmpstr (nm_wireguard_peer_get_public_key (peer), ==, "TrMvSoP4jYQlY6RIzBgbssQqY3vxI2Pi+y71lOWWXX0="); + g_assert_cmpstr (nm_wireguard_peer_get_endpoint (peer), ==, "[2607:5300:60:6b0::c05f:543]:2468"); + g_assert_cmpint (nm_wireguard_peer_get_allowed_ips_len (peer), ==, 2); + g_assert_cmpstr (nm_wireguard_peer_get_allowed_ip (peer, 0, NULL), ==, "10.192.122.4/32"); + g_assert_cmpstr (nm_wireguard_peer_get_allowed_ip (peer, 1, NULL), ==, "192.168.0.0/16"); + + peer = nm_setting_wireguard_get_peer (s_wg, 2); + g_assert_cmpstr (nm_wireguard_peer_get_public_key (peer), ==, "gN65BkIKy1eCE9pP1wdc8ROUtkHLF2PfAqYdyYBz6EA="); + g_assert_cmpstr (nm_wireguard_peer_get_endpoint (peer), ==, "test.wireguard.com:18981"); + g_assert_cmpint (nm_wireguard_peer_get_allowed_ips_len (peer), ==, 1); + g_assert_cmpstr (nm_wireguard_peer_get_allowed_ip (peer, 0, NULL), ==, "10.10.10.230/32"); +} + +static void +test_client_import_wireguard_test1 (void) +{ + gs_free_error GError *error = NULL; + + nm_vpn_wireguard_import (NM_BUILD_SRCDIR"/clients/common/tests/wg-test1.conf", &error); + g_assert_error (error, NM_UTILS_ERROR, NM_UTILS_ERROR_INVALID_ARGUMENT); + g_assert (g_str_has_prefix (error->message, "invalid secret 'PrivateKey'")); + g_assert (g_str_has_suffix (error->message, "wg-test1.conf:2")); +} + +static void +test_client_import_wireguard_test2 (void) +{ + gs_free_error GError *error = NULL; + + nm_vpn_wireguard_import (NM_BUILD_SRCDIR"/clients/common/tests/wg-test2.conf", &error); + + g_assert_error (error, NM_UTILS_ERROR, NM_UTILS_ERROR_INVALID_ARGUMENT); + g_assert (g_str_has_prefix (error->message, "unrecognized line at")); + g_assert (g_str_has_suffix (error->message, "wg-test2.conf:5")); +} + +static void +test_client_import_wireguard_test3 (void) +{ + gs_free_error GError *error = NULL; + + nm_vpn_wireguard_import (NM_BUILD_SRCDIR"/clients/common/tests/wg-test3.conf", &error); + g_assert_error (error, NM_UTILS_ERROR, NM_UTILS_ERROR_INVALID_ARGUMENT); + g_assert (g_str_has_prefix (error->message, "invalid value for 'ListenPort'")); + g_assert (g_str_has_suffix (error->message, "wg-test3.conf:3")); +} + +static void +test_client_import_wireguard_missing (void) +{ + gs_free_error GError *error = NULL; + + nm_vpn_wireguard_import (NM_BUILD_SRCDIR"/clients/common/tests/wg-missing.conf", &error); + g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT); +} + +/*****************************************************************************/ + NMTST_DEFINE (); int @@ -153,6 +244,11 @@ main (int argc, char **argv) nmtst_init (&argc, &argv, TRUE); g_test_add_func ("/client/meta/check", test_client_meta_check); + g_test_add_func ("/client/import/wireguard/test0", test_client_import_wireguard_test0); + g_test_add_func ("/client/import/wireguard/test1", test_client_import_wireguard_test1); + g_test_add_func ("/client/import/wireguard/test2", test_client_import_wireguard_test2); + g_test_add_func ("/client/import/wireguard/test3", test_client_import_wireguard_test3); + g_test_add_func ("/client/import/wireguard/missing", test_client_import_wireguard_missing); return g_test_run (); } diff --git a/clients/common/tests/wg-test0.conf b/clients/common/tests/wg-test0.conf new file mode 100644 index 0000000000..61438c2942 --- /dev/null +++ b/clients/common/tests/wg-test0.conf @@ -0,0 +1,18 @@ +[Interface] +PrivateKey = yAnz5TF+lXXJte14tji3zlMNq+hd2rYUIgJBgB3fBmk= +ListenPort = 51820 + +[Peer] +PublicKey = xTIBA5rboUvnH4htodjb6e697QjLERt1NAB4mZqp8Dg= +Endpoint = 192.95.5.67:1234 +AllowedIPs = 10.192.122.3/32, 10.192.124.1/24 + +[Peer] +PublicKey = TrMvSoP4jYQlY6RIzBgbssQqY3vxI2Pi+y71lOWWXX0= +Endpoint = [2607:5300:60:6b0::c05f:543]:2468 +AllowedIPs = 10.192.122.4/32, 192.168.0.0/16 + +[Peer] +PublicKey = gN65BkIKy1eCE9pP1wdc8ROUtkHLF2PfAqYdyYBz6EA= +Endpoint = test.wireguard.com:18981 +AllowedIPs = 10.10.10.230/32 diff --git a/clients/common/tests/wg-test1.conf b/clients/common/tests/wg-test1.conf new file mode 100644 index 0000000000..ceb267acba --- /dev/null +++ b/clients/common/tests/wg-test1.conf @@ -0,0 +1,3 @@ +[Interface] +PrivateKey = bad +ListenPort = 51820 diff --git a/clients/common/tests/wg-test2.conf b/clients/common/tests/wg-test2.conf new file mode 100644 index 0000000000..f691c2af65 --- /dev/null +++ b/clients/common/tests/wg-test2.conf @@ -0,0 +1,8 @@ +[Interface] +PrivateKey = yAnz5TF+lXXJte14tji3zlMNq+hd2rYUIgJBgB3fBmk= +ListenPort = 51820 + +[Beer] +PublicKey = xTIBA5rboUvnH4htodjb6e697QjLERt1NAB4mZqp8Dg= +Endpoint = 192.95.5.67:1234 +AllowedIPs = 10.192.122.3/32, 10.192.124.1/24 diff --git a/clients/common/tests/wg-test3.conf b/clients/common/tests/wg-test3.conf new file mode 100644 index 0000000000..9956e4004d --- /dev/null +++ b/clients/common/tests/wg-test3.conf @@ -0,0 +1,3 @@ +[Interface] +PrivateKey = yAnz5TF+lXXJte14tji3zlMNq+hd2rYUIgJBgB3fBmk= +ListenPort = 666666666 |