about summary refs log tree commit diff
path: root/libnm-util/tests/test-secrets.c
diff options
context:
space:
mode:
authorMichael Biebl <biebl@debian.org>2014-07-06 02:16:10 +0200
committerMichael Biebl <biebl@debian.org>2014-07-06 02:16:10 +0200
commit33491bc4279481db8ae47213e34a6d695a0e8830 (patch)
tree097d2b0fdff3fae6885381ae5e57a182cd8cbbba /libnm-util/tests/test-secrets.c
parent59c3714a494c3b3765657c0551ad82842d98a7d2 (diff)
Imported Upstream version 0.9.10.0 upstream/0.9.10.0
Diffstat (limited to 'libnm-util/tests/test-secrets.c')
-rw-r--r--libnm-util/tests/test-secrets.c108
1 files changed, 78 insertions, 30 deletions
diff --git a/libnm-util/tests/test-secrets.c b/libnm-util/tests/test-secrets.c
index 0e859abc..73fc9224 100644
--- a/libnm-util/tests/test-secrets.c
+++ b/libnm-util/tests/test-secrets.c
@@ -22,7 +22,6 @@
 #include <glib.h>
 #include <string.h>
 
-#include "nm-test-helpers.h"
 #include <nm-utils.h>
 
 #include "nm-setting-connection.h"
@@ -37,6 +36,7 @@
 #include "nm-setting-pppoe.h"
 #include "nm-setting-vpn.h"
 
+#include "nm-test-utils.h"
 
 #define TEST_NEED_SECRETS_EAP_TLS_CA_CERT TEST_CERT_DIR "/test_ca_cert.pem"
 #define TEST_NEED_SECRETS_EAP_TLS_CLIENT_CERT TEST_CERT_DIR "/test_key_and_cert.pem"
@@ -67,14 +67,9 @@ make_tls_connection (const char *detail, NMSetting8021xCKScheme scheme)
 	GError *error = NULL;
 
 	connection = nm_connection_new ();
-	ASSERT (connection != NULL,
-	        detail, "failed to allocate new connection");
 
 	/* Connection setting */
 	s_con = (NMSettingConnection *) nm_setting_connection_new ();
-	ASSERT (s_con != NULL,
-	        detail, "failed to allocate new %s setting",
-	        NM_SETTING_CONNECTION_SETTING_NAME);
 	nm_connection_add_setting (connection, NM_SETTING (s_con));
 
 	uuid = nm_utils_uuid_generate ();
@@ -88,16 +83,10 @@ make_tls_connection (const char *detail, NMSetting8021xCKScheme scheme)
 
 	/* Wired setting */
 	s_wired = (NMSettingWired *) nm_setting_wired_new ();
-	ASSERT (s_wired != NULL,
-	        detail, "failed to allocate new %s setting",
-	        NM_SETTING_WIRED_SETTING_NAME);
 	nm_connection_add_setting (connection, NM_SETTING (s_wired));
 
 	/* Wireless security setting */
 	s_8021x = (NMSetting8021x *) nm_setting_802_1x_new ();
-	ASSERT (s_8021x != NULL,
-	        detail, "failed to allocate new %s setting",
-	        NM_SETTING_802_1X_SETTING_NAME);
 	nm_connection_add_setting (connection, NM_SETTING (s_8021x));
 
 	g_object_set (s_8021x, NM_SETTING_802_1X_IDENTITY, "Bill Smith", NULL);
@@ -134,9 +123,6 @@ make_tls_connection (const char *detail, NMSetting8021xCKScheme scheme)
 
 	/* IP4 setting */
 	s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new ();
-	ASSERT (s_ip4 != NULL,
-			detail, "failed to allocate new %s setting",
-			NM_SETTING_IP4_CONFIG_SETTING_NAME);
 	nm_connection_add_setting (connection, NM_SETTING (s_ip4));
 
 	g_object_set (s_ip4, NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL);
@@ -247,14 +233,9 @@ make_tls_phase2_connection (const char *detail, NMSetting8021xCKScheme scheme)
 	GError *error = NULL;
 
 	connection = nm_connection_new ();
-	ASSERT (connection != NULL,
-	        detail, "failed to allocate new connection");
 
 	/* Connection setting */
 	s_con = (NMSettingConnection *) nm_setting_connection_new ();
-	ASSERT (s_con != NULL,
-	        detail, "failed to allocate new %s setting",
-	        NM_SETTING_CONNECTION_SETTING_NAME);
 	nm_connection_add_setting (connection, NM_SETTING (s_con));
 
 	uuid = nm_utils_uuid_generate ();
@@ -268,16 +249,10 @@ make_tls_phase2_connection (const char *detail, NMSetting8021xCKScheme scheme)
 
 	/* Wired setting */
 	s_wired = (NMSettingWired *) nm_setting_wired_new ();
-	ASSERT (s_wired != NULL,
-	        detail, "failed to allocate new %s setting",
-	        NM_SETTING_WIRED_SETTING_NAME);
 	nm_connection_add_setting (connection, NM_SETTING (s_wired));
 
 	/* Wireless security setting */
 	s_8021x = (NMSetting8021x *) nm_setting_802_1x_new ();
-	ASSERT (s_8021x != NULL,
-	        detail, "failed to allocate new %s setting",
-	        NM_SETTING_802_1X_SETTING_NAME);
 	nm_connection_add_setting (connection, NM_SETTING (s_8021x));
 
 	g_object_set (s_8021x, NM_SETTING_802_1X_ANONYMOUS_IDENTITY, "blahblah", NULL);
@@ -316,9 +291,6 @@ make_tls_phase2_connection (const char *detail, NMSetting8021xCKScheme scheme)
 
 	/* IP4 setting */
 	s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new ();
-	ASSERT (s_ip4 != NULL,
-			detail, "failed to allocate new %s setting",
-			NM_SETTING_IP4_CONFIG_SETTING_NAME);
 	nm_connection_add_setting (connection, NM_SETTING (s_ip4));
 
 	g_object_set (s_ip4, NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL);
@@ -454,7 +426,6 @@ wifi_connection_new (void)
 	g_byte_array_append (ssid, &tmpssid[0], sizeof (tmpssid));
 	g_object_set (s_wifi,
 	              NM_SETTING_WIRELESS_SSID, ssid,
-	              NM_SETTING_WIRELESS_SEC, NM_SETTING_WIRELESS_SECURITY_SETTING_NAME,
 	              NULL);
 	g_byte_array_free (ssid, TRUE);
 	nm_connection_add_setting (connection, NM_SETTING (s_wifi));
@@ -512,6 +483,8 @@ test_update_secrets_wifi_single_setting (void)
 	const char *wepkey = "11111111111111111111111111";
 	const char *tmp;
 
+	/* Test update with a hashed setting of 802-11-wireless secrets */
+
 	connection = wifi_connection_new ();
 
 	/* Build up the secrets hash */
@@ -546,6 +519,10 @@ test_update_secrets_wifi_full_hash (void)
 	const char *wepkey = "11111111111111111111111111";
 	const char *tmp;
 
+	/* Test update with a hashed connection containing only 802-11-wireless
+	 * setting and secrets.
+	 */
+
 	connection = wifi_connection_new ();
 
 	/* Build up the secrets hash */
@@ -580,6 +557,10 @@ test_update_secrets_wifi_bad_setting_name (void)
 	gboolean success;
 	const char *wepkey = "11111111111111111111111111";
 
+	/* Test that passing an invalid setting name to
+	 * nm_connection_update_secrets() fails with the correct error.
+	 */
+
 	connection = wifi_connection_new ();
 
 	/* Build up the secrets hash */
@@ -607,6 +588,10 @@ test_update_secrets_whole_connection (void)
 	gboolean success;
 	const char *wepkey = "11111111111111111111111111";
 
+	/* Test calling nm_connection_update_secrets() with an entire hashed
+	 * connection including non-secrets.
+	 */
+
 	connection = wifi_connection_new ();
 
 	/* Build up the secrets hash */
@@ -634,6 +619,8 @@ test_update_secrets_whole_connection_empty_hash (void)
 	GError *error = NULL;
 	gboolean success;
 
+	/* Test that updating secrets with an empty hash returns success */
+
 	connection = wifi_connection_new ();
 	secrets = g_hash_table_new (g_str_hash, g_str_equal);
 	success = nm_connection_update_secrets (connection, NULL, secrets, &error);
@@ -651,6 +638,10 @@ test_update_secrets_whole_connection_bad_setting (void)
 	gboolean success;
 	const char *wepkey = "11111111111111111111111111";
 
+	/* Test that sending a hashed connection containing an invalid setting
+	 * name fails with the right error.
+	 */
+
 	connection = wifi_connection_new ();
 
 	/* Build up the secrets hash */
@@ -674,12 +665,67 @@ test_update_secrets_whole_connection_bad_setting (void)
 	g_object_unref (connection);
 }
 
+static void
+test_update_secrets_whole_connection_empty_base_setting (void)
+{
+	NMConnection *connection;
+	GHashTable *secrets;
+	GError *error = NULL;
+	gboolean success;
+
+	/* Test that a hashed connection which does not have any hashed secrets
+	 * for the requested setting returns success.
+	 */
+
+	connection = wifi_connection_new ();
+	secrets = nm_connection_to_hash (connection, NM_SETTING_HASH_FLAG_ONLY_SECRETS);
+	g_assert_cmpint (g_hash_table_size (secrets), ==, 1);
+	g_assert (g_hash_table_lookup (secrets, NM_SETTING_WIRELESS_SETTING_NAME));
+
+	success = nm_connection_update_secrets (connection,
+	                                        NM_SETTING_WIRELESS_SECURITY_SETTING_NAME,
+	                                        secrets,
+	                                        &error);
+	g_assert_no_error (error);
+	g_assert (success);
+
+	g_hash_table_destroy (secrets);
+	g_object_unref (connection);
+}
+
+static void
+test_update_secrets_null_setting_name_with_setting_hash (void)
+{
+	NMConnection *connection;
+	GHashTable *secrets;
+	GError *error = NULL;
+	gboolean success;
+	const char *wepkey = "11111111111111111111111111";
+
+	/* Ensure that a NULL setting name and only a hashed setting fails */
+
+	connection = wifi_connection_new ();
+
+	secrets = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, value_destroy);
+	g_hash_table_insert (secrets, NM_SETTING_WIRELESS_SECURITY_WEP_KEY0, string_to_gvalue (wepkey));
+	g_hash_table_insert (secrets, NM_SETTING_WIRELESS_SECURITY_WEP_KEY_TYPE, uint_to_gvalue (NM_WEP_KEY_TYPE_KEY));
+
+	success = nm_connection_update_secrets (connection, NULL, secrets, &error);
+	g_assert_error (error, NM_CONNECTION_ERROR, NM_CONNECTION_ERROR_SETTING_NOT_FOUND);
+	g_assert (!success);
+
+	g_hash_table_destroy (secrets);
+	g_object_unref (connection);
+}
+
 int main (int argc, char **argv)
 {
 	GError *error = NULL;
 	char *base;
 
+#if !GLIB_CHECK_VERSION (2, 35, 0)
 	g_type_init ();
+#endif
 
 	if (!nm_utils_init (&error))
 		FAIL ("nm-utils-init", "failed to initialize libnm-util: %s", error->message);
@@ -697,6 +743,8 @@ int main (int argc, char **argv)
 	test_update_secrets_whole_connection ();
 	test_update_secrets_whole_connection_empty_hash ();
 	test_update_secrets_whole_connection_bad_setting ();
+	test_update_secrets_whole_connection_empty_base_setting ();
+	test_update_secrets_null_setting_name_with_setting_hash ();
 
 	base = g_path_get_basename (argv[0]);
 	fprintf (stdout, "%s: SUCCESS\n", base);