libnm-util: allow setting-only hashes with nm_connection_update_secrets()

It's easier to be able to pass both a whole hashed connection, or just
a hashed setting to the function, and have it figure out what needs
to be updated based on the given setting_name.

Add some testcases to make sure that all works correctly too.
This commit is contained in:
Dan Williams 2011-03-09 09:31:44 -06:00
parent 4963825d0c
commit 72dac886a7
3 changed files with 217 additions and 25 deletions

View file

@ -735,34 +735,46 @@ nm_connection_verify (NMConnection *connection, GError **error)
* nm_connection_update_secrets:
* @connection: the #NMConnection
* @setting_name: the setting object name to which the secrets apply
* @secrets: (element-type utf8 GObject.Value): a #GHashTable mapping string:#GValue of setting property names and
* secrets
* @setting_secrets: (element-type utf8 GObject.Value): a #GHashTable mapping
* string:#GValue of setting property names and secrets of the given @setting_name
* @error: location to store error, or %NULL
*
* Update the specified setting's secrets, given a hash table of secrets
* intended for that setting (deserialized from D-Bus for example).
*
* intended for that setting (deserialized from D-Bus for example). Will also
* extract the given setting's secrets hash if given a hash of hashes, as would
* be returned from nm_connection_to_hash().
*
* Returns: %TRUE if the secrets were successfully updated and the connection
* is valid, %FALSE on failure or if the setting was never added to the connection
**/
gboolean
nm_connection_update_secrets (NMConnection *connection,
const char *setting_name,
GHashTable *all_secrets,
GHashTable *setting_secrets,
GError **error)
{
NMSetting *setting;
gboolean success;
GHashTable *setting_secrets;
GHashTable *tmp;
GType setting_type;
g_return_val_if_fail (connection != NULL, FALSE);
g_return_val_if_fail (NM_IS_CONNECTION (connection), FALSE);
g_return_val_if_fail (setting_name != NULL, FALSE);
g_return_val_if_fail (all_secrets != NULL, FALSE);
g_return_val_if_fail (setting_secrets != NULL, FALSE);
if (error)
g_return_val_if_fail (*error == NULL, FALSE);
setting = nm_connection_get_setting (connection, nm_connection_lookup_setting_type (setting_name));
setting_type = nm_connection_lookup_setting_type (setting_name);
if (!setting_type) {
g_set_error_literal (error,
NM_CONNECTION_ERROR,
NM_CONNECTION_ERROR_CONNECTION_SETTING_NOT_FOUND,
setting_name);
return FALSE;
}
setting = nm_connection_get_setting (connection, setting_type);
if (!setting) {
g_set_error_literal (error,
NM_CONNECTION_ERROR,
@ -771,16 +783,12 @@ nm_connection_update_secrets (NMConnection *connection,
return FALSE;
}
setting_secrets = g_hash_table_lookup (all_secrets, setting_name);
if (!setting_secrets) {
g_set_error_literal (error,
NM_CONNECTION_ERROR,
NM_CONNECTION_ERROR_CONNECTION_SETTING_NOT_FOUND,
setting_name);
return FALSE;
}
/* Check if this is a hash of hashes, ie a full deserialized connection,
* not just a single hashed setting.
*/
tmp = g_hash_table_lookup (setting_secrets, setting_name);
success = nm_setting_update_secrets (setting, setting_secrets, error);
success = nm_setting_update_secrets (setting, tmp ? tmp : setting_secrets, error);
if (success)
g_signal_emit (connection, signals[SECRETS_UPDATED], 0, setting_name);
return success;

View file

@ -7,7 +7,7 @@ INCLUDES = \
noinst_PROGRAMS = \
test-settings-defaults \
test-crypto \
test-need-secrets \
test-secrets \
test-general \
test-setting-8021x
@ -34,15 +34,15 @@ test_crypto_LDADD = \
$(top_builddir)/libnm-util/libnm-util.la \
$(GLIB_LIBS)
test_need_secrets_SOURCES = \
test-need-secrets.c
test_secrets_SOURCES = \
test-secrets.c
test_need_secrets_CPPFLAGS = \
test_secrets_CPPFLAGS = \
-DTEST_CERT_DIR=\"$(top_srcdir)/libnm-util/tests/certs/\" \
$(GLIB_CFLAGS) \
$(DBUS_CFLAGS)
test_need_secrets_LDADD = \
test_secrets_LDADD = \
$(top_builddir)/libnm-util/libnm-util.la \
$(GLIB_LIBS) \
$(DBUS_LIBS)
@ -73,9 +73,9 @@ test_setting_8021x_LDADD = \
if WITH_TESTS
check-local: test-settings-defaults test-crypto test-need-secrets
check-local: test-settings-defaults test-crypto test-secrets
$(abs_builddir)/test-settings-defaults
$(abs_builddir)/test-need-secrets
$(abs_builddir)/test-secrets
$(abs_builddir)/test-general
# Private key and CA certificate in the same file (PEM)

View file

@ -15,7 +15,7 @@
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Copyright (C) 2008 - 2009 Red Hat, Inc.
* Copyright (C) 2008 - 2011 Red Hat, Inc.
*
*/
@ -30,6 +30,7 @@
#include "nm-setting-wired.h"
#include "nm-setting-8021x.h"
#include "nm-setting-ip4-config.h"
#include "nm-setting-wireless.h"
#include "nm-setting-wireless-security.h"
#include "nm-setting-cdma.h"
#include "nm-setting-gsm.h"
@ -418,6 +419,185 @@ test_need_tls_phase2_secrets_blob (void)
g_object_unref (connection);
}
static NMConnection *
wifi_connection_new (void)
{
NMConnection *connection;
NMSettingConnection *s_con;
NMSettingWireless *s_wifi;
NMSettingWirelessSecurity *s_wsec;
unsigned char tmpssid[] = { 0x31, 0x33, 0x33, 0x37 };
char *uuid;
GByteArray *ssid;
connection = nm_connection_new ();
g_assert (connection);
/* Connection setting */
s_con = (NMSettingConnection *) nm_setting_connection_new ();
g_assert (s_con);
uuid = nm_utils_uuid_generate ();
g_object_set (s_con,
NM_SETTING_CONNECTION_ID, "Test Wireless",
NM_SETTING_CONNECTION_UUID, uuid,
NM_SETTING_CONNECTION_AUTOCONNECT, FALSE,
NM_SETTING_CONNECTION_TYPE, NM_SETTING_WIRELESS_SETTING_NAME,
NULL);
g_free (uuid);
nm_connection_add_setting (connection, NM_SETTING (s_con));
/* Wireless setting */
s_wifi = (NMSettingWireless *) nm_setting_wireless_new ();
g_assert (s_wifi);
ssid = g_byte_array_sized_new (sizeof (tmpssid));
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));
/* Wifi security */
s_wsec = (NMSettingWirelessSecurity *) nm_setting_wireless_security_new ();
g_assert (s_wsec);
g_object_set (G_OBJECT (s_wsec),
NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, "none",
NULL);
nm_connection_add_setting (connection, NM_SETTING (s_wsec));
return connection;
}
static void
value_destroy (gpointer data)
{
GValue *value = (GValue *) data;
g_value_unset (value);
g_slice_free (GValue, value);
}
static GValue *
string_to_gvalue (const char *str)
{
GValue *val = g_slice_new0 (GValue);
g_value_init (val, G_TYPE_STRING);
g_value_set_string (val, str);
return val;
}
static GValue *
uint_to_gvalue (guint32 i)
{
GValue *val;
val = g_slice_new0 (GValue);
g_value_init (val, G_TYPE_UINT);
g_value_set_uint (val, i);
return val;
}
static void
test_update_secrets_wifi_single_setting (void)
{
NMConnection *connection;
NMSettingWirelessSecurity *s_wsec;
GHashTable *secrets;
GError *error = NULL;
gboolean success;
const char *wepkey = "11111111111111111111111111";
const char *tmp;
connection = wifi_connection_new ();
/* Build up the secrets hash */
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,
NM_SETTING_WIRELESS_SECURITY_SETTING_NAME,
secrets,
&error);
g_assert_no_error (error);
g_assert (success);
/* Make sure the secret is now in the connection */
s_wsec = (NMSettingWirelessSecurity *) nm_connection_get_setting (connection, NM_TYPE_SETTING_WIRELESS_SECURITY);
g_assert (s_wsec);
tmp = nm_setting_wireless_security_get_wep_key (s_wsec, 0);
g_assert_cmpstr (tmp, ==, wepkey);
g_object_unref (connection);
}
static void
test_update_secrets_wifi_full_hash (void)
{
NMConnection *connection;
NMSettingWirelessSecurity *s_wsec;
GHashTable *secrets, *all;
GError *error = NULL;
gboolean success;
const char *wepkey = "11111111111111111111111111";
const char *tmp;
connection = wifi_connection_new ();
/* Build up the secrets hash */
all = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, (GDestroyNotify) g_hash_table_destroy);
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));
g_hash_table_insert (all, NM_SETTING_WIRELESS_SECURITY_SETTING_NAME, secrets);
success = nm_connection_update_secrets (connection,
NM_SETTING_WIRELESS_SECURITY_SETTING_NAME,
all,
&error);
g_assert_no_error (error);
g_assert (success);
/* Make sure the secret is now in the connection */
s_wsec = (NMSettingWirelessSecurity *) nm_connection_get_setting (connection, NM_TYPE_SETTING_WIRELESS_SECURITY);
g_assert (s_wsec);
tmp = nm_setting_wireless_security_get_wep_key (s_wsec, 0);
g_assert_cmpstr (tmp, ==, wepkey);
g_object_unref (connection);
}
static void
test_update_secrets_wifi_bad_setting_name (void)
{
NMConnection *connection;
GHashTable *secrets;
GError *error = NULL;
gboolean success;
const char *wepkey = "11111111111111111111111111";
connection = wifi_connection_new ();
/* Build up the secrets hash */
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,
"asdfasdfasdfasf",
secrets,
&error);
g_assert_error (error, NM_CONNECTION_ERROR, NM_CONNECTION_ERROR_CONNECTION_SETTING_NOT_FOUND);
g_assert (success == FALSE);
g_object_unref (connection);
}
int main (int argc, char **argv)
{
GError *error = NULL;
@ -436,6 +616,10 @@ int main (int argc, char **argv)
test_need_tls_phase2_secrets_path ();
test_need_tls_phase2_secrets_blob ();
test_update_secrets_wifi_single_setting ();
test_update_secrets_wifi_full_hash ();
test_update_secrets_wifi_bad_setting_name ();
base = g_path_get_basename (argv[0]);
fprintf (stdout, "%s: SUCCESS\n", base);
g_free (base);