From 2e5fa45ddfbb5cffa1e78221f1cea706e2f298af Mon Sep 17 00:00:00 2001 From: Michael Biebl Date: Wed, 31 Jul 2019 10:51:42 +0200 Subject: New upstream version 1.19.90 --- src/devices/wifi/nm-device-iwd.c | 32 +- src/devices/wifi/nm-device-iwd.h | 1 - src/devices/wifi/nm-device-olpc-mesh.c | 2 +- src/devices/wifi/nm-device-olpc-mesh.h | 2 - src/devices/wifi/nm-device-wifi-p2p.c | 1 + src/devices/wifi/nm-device-wifi.c | 166 ++- src/devices/wifi/nm-device-wifi.h | 1 - src/devices/wifi/nm-iwd-manager.c | 20 +- src/devices/wifi/nm-iwd-manager.h | 1 - src/devices/wifi/nm-wifi-ap.c | 19 +- src/devices/wifi/nm-wifi-ap.h | 1 - src/devices/wifi/nm-wifi-common.c | 1 - src/devices/wifi/nm-wifi-common.h | 1 - src/devices/wifi/nm-wifi-factory.c | 1 - src/devices/wifi/nm-wifi-utils.c | 55 +- src/devices/wifi/nm-wifi-utils.h | 2 +- src/devices/wifi/tests/meson.build | 6 +- src/devices/wifi/tests/test-devices-wifi.c | 1519 ++++++++++++++++++++++++++++ src/devices/wifi/tests/test-general.c | 1519 ---------------------------- 19 files changed, 1722 insertions(+), 1628 deletions(-) create mode 100644 src/devices/wifi/tests/test-devices-wifi.c delete mode 100644 src/devices/wifi/tests/test-general.c (limited to 'src/devices/wifi') diff --git a/src/devices/wifi/nm-device-iwd.c b/src/devices/wifi/nm-device-iwd.c index da7d0685..9cfe5f70 100644 --- a/src/devices/wifi/nm-device-iwd.c +++ b/src/devices/wifi/nm-device-iwd.c @@ -1,4 +1,3 @@ -/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ /* NetworkManager -- Network link manager * * This program is free software; you can redistribute it and/or modify @@ -780,12 +779,10 @@ complete_connection (NMDevice *device, NMDeviceIwd *self = NM_DEVICE_IWD (device); NMDeviceIwdPrivate *priv = NM_DEVICE_IWD_GET_PRIVATE (self); NMSettingWireless *s_wifi; - const char *setting_mac; gs_free char *ssid_utf8 = NULL; NMWifiAP *ap; GBytes *ssid; GBytes *setting_ssid = NULL; - const char *perm_hw_addr; const char *mode; s_wifi = nm_connection_get_setting_wireless (connection); @@ -875,36 +872,9 @@ complete_connection (NMDevice *device, ssid_utf8, ssid_utf8, NULL, + nm_setting_wireless_get_mac_address (s_wifi) ? NULL : nm_device_get_iface (device), TRUE); - perm_hw_addr = nm_device_get_permanent_hw_address (device); - if (perm_hw_addr) { - setting_mac = nm_setting_wireless_get_mac_address (s_wifi); - if (setting_mac) { - /* Make sure the setting MAC (if any) matches the device's permanent MAC */ - if (!nm_utils_hwaddr_matches (setting_mac, -1, perm_hw_addr, -1)) { - g_set_error_literal (error, - NM_CONNECTION_ERROR, - NM_CONNECTION_ERROR_INVALID_PROPERTY, - "connection does not match device"); - g_prefix_error (error, "%s.%s: ", NM_SETTING_WIRELESS_SETTING_NAME, NM_SETTING_WIRELESS_MAC_ADDRESS); - return FALSE; - } - } else { - guint8 tmp[ETH_ALEN]; - - /* Lock the connection to this device by default if it uses a - * permanent MAC address (ie not a 'locally administered' one) - */ - nm_utils_hwaddr_aton (perm_hw_addr, tmp, ETH_ALEN); - if (!(tmp[0] & 0x02)) { - g_object_set (G_OBJECT (s_wifi), - NM_SETTING_WIRELESS_MAC_ADDRESS, perm_hw_addr, - NULL); - } - } - } - return TRUE; } diff --git a/src/devices/wifi/nm-device-iwd.h b/src/devices/wifi/nm-device-iwd.h index 4a2bd31e..aab45b7b 100644 --- a/src/devices/wifi/nm-device-iwd.h +++ b/src/devices/wifi/nm-device-iwd.h @@ -1,4 +1,3 @@ -/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ /* NetworkManager -- Network link manager * * This program is free software; you can redistribute it and/or modify diff --git a/src/devices/wifi/nm-device-olpc-mesh.c b/src/devices/wifi/nm-device-olpc-mesh.c index 1172a613..3c865c55 100644 --- a/src/devices/wifi/nm-device-olpc-mesh.c +++ b/src/devices/wifi/nm-device-olpc-mesh.c @@ -1,4 +1,3 @@ -/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ /* NetworkManager -- Network link manager * * Dan Williams @@ -133,6 +132,7 @@ complete_connection (NMDevice *device, NULL, _("Mesh"), NULL, + NULL, FALSE); /* No IPv6 by default */ return TRUE; diff --git a/src/devices/wifi/nm-device-olpc-mesh.h b/src/devices/wifi/nm-device-olpc-mesh.h index bf0e4da9..619fc46a 100644 --- a/src/devices/wifi/nm-device-olpc-mesh.h +++ b/src/devices/wifi/nm-device-olpc-mesh.h @@ -1,5 +1,3 @@ -/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ - /* NetworkManager -- Network link manager * * Dan Williams diff --git a/src/devices/wifi/nm-device-wifi-p2p.c b/src/devices/wifi/nm-device-wifi-p2p.c index 8381ebc7..c5826e24 100644 --- a/src/devices/wifi/nm-device-wifi-p2p.c +++ b/src/devices/wifi/nm-device-wifi-p2p.c @@ -331,6 +331,7 @@ complete_connection (NMDevice *device, setting_name, setting_name, NULL, + NULL, TRUE); return TRUE; diff --git a/src/devices/wifi/nm-device-wifi.c b/src/devices/wifi/nm-device-wifi.c index 3a58c620..2ffba47a 100644 --- a/src/devices/wifi/nm-device-wifi.c +++ b/src/devices/wifi/nm-device-wifi.c @@ -1,4 +1,3 @@ -/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ /* NetworkManager -- Network link manager * * This program is free software; you can redistribute it and/or modify @@ -544,6 +543,60 @@ wake_on_wlan_restore (NMDeviceWifi *self) w); } +static void +disconnect_cb (NMSupplicantInterface *iface, GError *error, gpointer user_data) +{ + gs_unref_object NMDeviceWifi *self = NULL; + NMDeviceDeactivateCallback callback; + gpointer callback_user_data; + + nm_utils_user_data_unpack (user_data, &self, &callback, &callback_user_data); + + /* error will be freed by sup_iface */ + callback (NM_DEVICE (self), error, callback_user_data); +} + +static void +disconnect_cb_on_idle (gpointer user_data, + GCancellable *cancellable) +{ + gs_unref_object NMDeviceWifi *self = NULL; + NMDeviceDeactivateCallback callback; + gpointer callback_user_data; + gs_free_error GError *cancelled_error = NULL; + + nm_utils_user_data_unpack (user_data, &self, &callback, &callback_user_data); + + g_cancellable_set_error_if_cancelled (cancellable, &cancelled_error); + callback (NM_DEVICE (self), cancelled_error, callback_user_data); +} + +static void +deactivate_async (NMDevice *device, + GCancellable *cancellable, + NMDeviceDeactivateCallback callback, + gpointer callback_user_data) { + NMDeviceWifi *self = NM_DEVICE_WIFI (device); + NMDeviceWifiPrivate *priv = NM_DEVICE_WIFI_GET_PRIVATE (self); + gpointer user_data; + + nm_assert (G_IS_CANCELLABLE (cancellable)); + nm_assert (callback); + + user_data = nm_utils_user_data_pack (g_object_ref (self), callback, callback_user_data); + if (!priv->sup_iface) { + nm_utils_invoke_on_idle (disconnect_cb_on_idle, user_data, cancellable); + return; + } + + cleanup_association_attempt (self, FALSE); + + nm_supplicant_interface_disconnect_async (priv->sup_iface, + cancellable, + disconnect_cb, + user_data); +} + static void deactivate (NMDevice *device) { @@ -695,6 +748,20 @@ check_connection_compatible (NMDevice *device, NMConnection *connection, GError return FALSE; } } + } else if (g_strcmp0 (mode, NM_SETTING_WIRELESS_MODE_MESH) == 0) { + if (!(priv->capabilities & NM_WIFI_DEVICE_CAP_MESH)) { + nm_utils_error_set_literal (error, NM_UTILS_ERROR_CONNECTION_AVAILABLE_TEMPORARY, + "the device does not support Mesh mode"); + return FALSE; + } + + if (priv->sup_iface) { + if (nm_supplicant_interface_get_mesh_support (priv->sup_iface) == NM_SUPPLICANT_FEATURE_NO) { + nm_utils_error_set_literal (error, NM_UTILS_ERROR_CONNECTION_AVAILABLE_TEMPORARY, + "wpa_supplicant does not support Mesh mode"); + return FALSE; + } + } } // FIXME: check channel/freq/band against bands the hardware supports @@ -739,12 +806,13 @@ check_connection_available (NMDevice *device, return TRUE; } - /* Ad-Hoc and AP connections are always available because they may be + /* Ad-Hoc, AP and Mesh connections are always available because they may be * started at any time. */ mode = nm_setting_wireless_get_mode (s_wifi); if ( g_strcmp0 (mode, NM_SETTING_WIRELESS_MODE_ADHOC) == 0 - || g_strcmp0 (mode, NM_SETTING_WIRELESS_MODE_AP) == 0) + || g_strcmp0 (mode, NM_SETTING_WIRELESS_MODE_AP) == 0 + || g_strcmp0 (mode, NM_SETTING_WIRELESS_MODE_MESH) == 0) return TRUE; /* Hidden SSIDs obviously don't always appear in the scan list either. @@ -778,13 +846,11 @@ complete_connection (NMDevice *device, NMDeviceWifi *self = NM_DEVICE_WIFI (device); NMDeviceWifiPrivate *priv = NM_DEVICE_WIFI_GET_PRIVATE (self); NMSettingWireless *s_wifi; - const char *setting_mac; gs_free char *ssid_utf8 = NULL; NMWifiAP *ap; GBytes *ssid = NULL; GBytes *setting_ssid = NULL; gboolean hidden = FALSE; - const char *perm_hw_addr; const char *mode; s_wifi = nm_connection_get_setting_wireless (connection); @@ -908,27 +974,12 @@ complete_connection (NMDevice *device, ssid_utf8, ssid_utf8, NULL, + nm_setting_wireless_get_mac_address (s_wifi) ? NULL : nm_device_get_iface (device), TRUE); if (hidden) g_object_set (s_wifi, NM_SETTING_WIRELESS_HIDDEN, TRUE, NULL); - perm_hw_addr = nm_device_get_permanent_hw_address (device); - if (perm_hw_addr) { - setting_mac = nm_setting_wireless_get_mac_address (s_wifi); - if (setting_mac) { - /* Make sure the setting MAC (if any) matches the device's permanent MAC */ - if (!nm_utils_hwaddr_matches (setting_mac, -1, perm_hw_addr, -1)) { - g_set_error_literal (error, - NM_CONNECTION_ERROR, - NM_CONNECTION_ERROR_INVALID_PROPERTY, - _("connection does not match device")); - g_prefix_error (error, "%s.%s: ", NM_SETTING_WIRELESS_SETTING_NAME, NM_SETTING_WIRELESS_MAC_ADDRESS); - return FALSE; - } - } - } - return TRUE; } @@ -972,7 +1023,8 @@ can_auto_connect (NMDevice *device, NMConnection *connection; NMSettingWireless *s_wifi; NMWifiAP *ap; - const char *method, *mode; + const char *method6, *mode; + gboolean auto4, auto6; guint64 timestamp = 0; nm_assert (!specific_object || !*specific_object); @@ -985,13 +1037,20 @@ can_auto_connect (NMDevice *device, s_wifi = nm_connection_get_setting_wireless (connection); g_return_val_if_fail (s_wifi, FALSE); - /* Always allow autoconnect for AP and non-autoconf Ad-Hoc */ - method = nm_utils_get_ip_config_method (connection, AF_INET); + /* Always allow autoconnect for AP and non-autoconf Ad-Hoc or Mesh */ + auto4 = nm_streq0 (nm_utils_get_ip_config_method (connection, AF_INET), + NM_SETTING_IP4_CONFIG_METHOD_AUTO); + method6 = nm_utils_get_ip_config_method (connection, AF_INET6); + auto6 = nm_streq0 (method6, NM_SETTING_IP6_CONFIG_METHOD_AUTO) + || nm_streq0 (method6, NM_SETTING_IP6_CONFIG_METHOD_DHCP); + mode = nm_setting_wireless_get_mode (s_wifi); + if (nm_streq0 (mode, NM_SETTING_WIRELESS_MODE_AP)) return TRUE; - else if ( nm_streq0 (mode, NM_SETTING_WIRELESS_MODE_ADHOC) - && !nm_streq0 (method, NM_SETTING_IP4_CONFIG_METHOD_AUTO)) + else if (!auto4 && nm_streq0 (mode, NM_SETTING_WIRELESS_MODE_ADHOC)) + return TRUE; + else if (!auto4 && !auto6 && nm_streq0 (mode, NM_SETTING_WIRELESS_MODE_MESH)) return TRUE; /* Don't autoconnect to networks that have been tried at least once @@ -1531,13 +1590,14 @@ try_fill_ssid_for_hidden_ap (NMDeviceWifi *self, NMSettingsConnection *sett_conn = connections[i]; NMSettingWireless *s_wifi; + if (!nm_settings_connection_has_seen_bssid (sett_conn, bssid)) + continue; s_wifi = nm_connection_get_setting_wireless (nm_settings_connection_get_connection (sett_conn)); - if (s_wifi) { - if (nm_settings_connection_has_seen_bssid (sett_conn, bssid)) { - nm_wifi_ap_set_ssid (ap, nm_setting_wireless_get_ssid (s_wifi)); - break; - } - } + if (!s_wifi) + continue; + + nm_wifi_ap_set_ssid (ap, nm_setting_wireless_get_ssid (s_wifi)); + break; } } @@ -1777,9 +1837,10 @@ supplicant_iface_wps_credentials_cb (NMSupplicantInterface *iface, } if (secrets) { if (nm_settings_connection_new_secrets (nm_act_request_get_settings_connection (req), - nm_act_request_get_applied_connection (req), - NM_SETTING_WIRELESS_SECURITY_SETTING_NAME, - secrets, &error)) { + nm_act_request_get_applied_connection (req), + NM_SETTING_WIRELESS_SECURITY_SETTING_NAME, + secrets, + &error)) { wifi_secrets_cancel (self); nm_device_activate_schedule_stage1_device_prepare (NM_DEVICE (self)); } else { @@ -2398,6 +2459,7 @@ supplicant_connection_timeout_cb (gpointer user_data) g_assert (connection); if ( priv->mode == NM_802_11_MODE_ADHOC + || priv->mode == NM_802_11_MODE_MESH || priv->mode == NM_802_11_MODE_AP) { /* In Ad-Hoc and AP modes there's nothing to check the encryption key * (if any), so supplicant timeouts here are almost certainly the wifi @@ -2468,7 +2530,9 @@ build_supplicant_config (NMDeviceWifi *self, config = nm_supplicant_config_new ( nm_supplicant_interface_get_pmf_support (priv->sup_iface) == NM_SUPPLICANT_FEATURE_YES, - nm_supplicant_interface_get_fils_support (priv->sup_iface) == NM_SUPPLICANT_FEATURE_YES); + nm_supplicant_interface_get_fils_support (priv->sup_iface) == NM_SUPPLICANT_FEATURE_YES, + nm_supplicant_interface_get_ft_support (priv->sup_iface) == NM_SUPPLICANT_FEATURE_YES, + nm_supplicant_interface_get_sha384_support (priv->sup_iface) == NM_SUPPLICANT_FEATURE_YES); /* Warn if AP mode may not be supported */ if ( g_strcmp0 (nm_setting_wireless_get_mode (s_wireless), NM_SETTING_WIRELESS_MODE_AP) == 0 @@ -2635,7 +2699,8 @@ act_stage1_prepare (NMDevice *device, NMDeviceStateReason *out_failure_reason) /* Scanning not done in AP mode; clear the scan list */ remove_all_aps (self); - } + } else if (g_strcmp0 (mode, NM_SETTING_WIRELESS_MODE_MESH) == 0) + priv->mode = NM_802_11_MODE_MESH; _notify (self, PROP_MODE); /* The kernel doesn't support Ad-Hoc WPA connections well at this time, @@ -2655,8 +2720,8 @@ act_stage1_prepare (NMDevice *device, NMDeviceStateReason *out_failure_reason) if (!nm_device_hw_addr_set_cloned (device, connection, TRUE)) return NM_ACT_STAGE_RETURN_FAILURE; - /* AP mode never uses a specific object or existing scanned AP */ - if (priv->mode != NM_802_11_MODE_AP) { + /* AP and Mesh modes never use a specific object or existing scanned AP */ + if (priv->mode != NM_802_11_MODE_AP && priv->mode != NM_802_11_MODE_MESH) { ap_path = nm_active_connection_get_specific_object (NM_ACTIVE_CONNECTION (req)); ap = ap_path ? nm_wifi_ap_lookup_for_device (NM_DEVICE (self), ap_path) : NULL; if (ap) @@ -2672,10 +2737,10 @@ act_stage1_prepare (NMDevice *device, NMDeviceStateReason *out_failure_reason) } /* If the user is trying to connect to an AP that NM doesn't yet know about - * (hidden network or something) or starting a Hotspot, create an fake AP - * from the security settings in the connection. This "fake" AP gets used - * until the real one is found in the scan list (Ad-Hoc or Hidden), or until - * the device is deactivated (Hotspot). + * (hidden network or something), starting a Hotspot or joining a Mesh, + * create a fake APfrom the security settings in the connection. This "fake" + * AP gets used until the real one is found in the scan list (Ad-Hoc or Hidden), + * or until the device is deactivated (Hotspot). */ ap = nm_wifi_ap_new_fake_from_connection (connection); g_return_val_if_fail (ap != NULL, NM_ACT_STAGE_RETURN_FAILURE); @@ -2763,6 +2828,7 @@ act_stage2_config (NMDevice *device, NMDeviceStateReason *out_failure_reason) NMDeviceWifiPrivate *priv = NM_DEVICE_WIFI_GET_PRIVATE (self); NMActStageReturn ret = NM_ACT_STAGE_RETURN_FAILURE; NMSupplicantConfig *config = NULL; + NM80211Mode ap_mode; NMActRequest *req; NMWifiAP *ap; NMConnection *connection; @@ -2783,6 +2849,7 @@ act_stage2_config (NMDevice *device, NMDeviceStateReason *out_failure_reason) NM_SET_OUT (out_failure_reason, NM_DEVICE_STATE_REASON_SUPPLICANT_FAILED); goto out; } + ap_mode = nm_wifi_ap_get_mode (ap); connection = nm_act_request_get_applied_connection (req); g_assert (connection); @@ -2822,14 +2889,16 @@ act_stage2_config (NMDevice *device, NMDeviceStateReason *out_failure_reason) priv->ssid_found = FALSE; - /* Supplicant requires an initial frequency for Ad-Hoc and Hotspot; if the user - * didn't specify one and we didn't find an AP that matched the connection, - * just pick a frequency the device supports. + /* Supplicant requires an initial frequency for Ad-Hoc, Hotspot and Mesh; + * if the user didn't specify one and we didn't find an AP that matched + * the connection, just pick a frequency the device supports. */ - if ((nm_wifi_ap_get_mode (ap) == NM_802_11_MODE_ADHOC) || nm_wifi_ap_is_hotspot (ap)) + if ( ap_mode == NM_802_11_MODE_ADHOC + || ap_mode == NM_802_11_MODE_MESH + || nm_wifi_ap_is_hotspot (ap)) ensure_hotspot_frequency (self, s_wireless, ap); - if (nm_wifi_ap_get_mode (ap) == NM_802_11_MODE_INFRA) + if (ap_mode == NM_802_11_MODE_INFRA) set_powersave (device); /* Build up the supplicant configuration */ @@ -3386,6 +3455,7 @@ nm_device_wifi_class_init (NMDeviceWifiClass *klass) device_class->get_configured_mtu = get_configured_mtu; device_class->act_stage3_ip_config_start = act_stage3_ip_config_start; device_class->act_stage4_ip_config_timeout = act_stage4_ip_config_timeout; + device_class->deactivate_async = deactivate_async; device_class->deactivate = deactivate; device_class->deactivate_reset_hw_addr = deactivate_reset_hw_addr; device_class->unmanaged_on_quit = unmanaged_on_quit; diff --git a/src/devices/wifi/nm-device-wifi.h b/src/devices/wifi/nm-device-wifi.h index 116ad11e..82f62be6 100644 --- a/src/devices/wifi/nm-device-wifi.h +++ b/src/devices/wifi/nm-device-wifi.h @@ -1,4 +1,3 @@ -/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ /* NetworkManager -- Network link manager * * This program is free software; you can redistribute it and/or modify diff --git a/src/devices/wifi/nm-iwd-manager.c b/src/devices/wifi/nm-iwd-manager.c index 494fca32..5b550ea4 100644 --- a/src/devices/wifi/nm-iwd-manager.c +++ b/src/devices/wifi/nm-iwd-manager.c @@ -1,4 +1,3 @@ -/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ /* NetworkManager -- Network link manager * * This program is free software; you can redistribute it and/or modify @@ -437,7 +436,6 @@ mirror_8021x_connection (NMIwdManager *self, NM_SETTING_CONNECTION_TYPE, NM_SETTING_WIRELESS_SETTING_NAME, NM_SETTING_CONNECTION_ID, name, NM_SETTING_CONNECTION_UUID, nm_utils_uuid_generate_buf (uuid), - NM_SETTING_CONNECTION_READ_ONLY, TRUE, NULL)); nm_connection_add_setting (connection, setting); @@ -470,19 +468,19 @@ mirror_8021x_connection (NMIwdManager *self, if (!nm_connection_normalize (connection, NULL, NULL, NULL)) return NULL; - settings_connection = nm_settings_add_connection (priv->settings, connection, - FALSE, &error); - if (!settings_connection) { + if (!nm_settings_add_connection (priv->settings, + connection, + NM_SETTINGS_CONNECTION_PERSIST_MODE_IN_MEMORY_ONLY, + NM_SETTINGS_CONNECTION_ADD_REASON_NONE, + NM_SETTINGS_CONNECTION_INT_FLAGS_NM_GENERATED, + &settings_connection, + &error)) { _LOGW ("failed to add a mirror NMConnection for IWD's Known Network '%s': %s", name, error->message); g_error_free (error); return NULL; } - nm_settings_connection_set_flags (settings_connection, - NM_SETTINGS_CONNECTION_INT_FLAGS_NM_GENERATED | - NM_SETTINGS_CONNECTION_INT_FLAGS_UNSAVED, - TRUE); return settings_connection; } @@ -499,7 +497,7 @@ mirror_8021x_connection_take_and_delete (NMSettingsConnection *sett_conn) /* If connection has not been saved since we created it * in interface_added it too can be removed now. */ if (NM_FLAGS_HAS (flags, NM_SETTINGS_CONNECTION_INT_FLAGS_NM_GENERATED)) - nm_settings_connection_delete (sett_conn, NULL); + nm_settings_connection_delete (sett_conn, FALSE); g_object_unref (sett_conn); } @@ -900,7 +898,7 @@ nm_iwd_manager_init (NMIwdManager *self) g_signal_connect (priv->manager, NM_MANAGER_DEVICE_ADDED, G_CALLBACK (device_added), self); - priv->settings = g_object_ref (nm_settings_get ()); + priv->settings = g_object_ref (NM_SETTINGS_GET); g_signal_connect (priv->settings, NM_SETTINGS_SIGNAL_CONNECTION_REMOVED, G_CALLBACK (connection_removed), self); diff --git a/src/devices/wifi/nm-iwd-manager.h b/src/devices/wifi/nm-iwd-manager.h index 57b7007a..b410e4ce 100644 --- a/src/devices/wifi/nm-iwd-manager.h +++ b/src/devices/wifi/nm-iwd-manager.h @@ -1,4 +1,3 @@ -/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ /* NetworkManager -- Network link manager * * This program is free software; you can redistribute it and/or modify diff --git a/src/devices/wifi/nm-wifi-ap.c b/src/devices/wifi/nm-wifi-ap.c index 87daff59..c7ab7f04 100644 --- a/src/devices/wifi/nm-wifi-ap.c +++ b/src/devices/wifi/nm-wifi-ap.c @@ -1,4 +1,3 @@ -/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ /* NetworkManager -- Network link manager * * This program is free software; you can redistribute it and/or modify @@ -260,7 +259,8 @@ nm_wifi_ap_set_mode (NMWifiAP *ap, const NM80211Mode mode) g_return_val_if_fail (NM_IS_WIFI_AP (ap), FALSE); g_return_val_if_fail ( mode == NM_802_11_MODE_ADHOC - || mode == NM_802_11_MODE_INFRA, FALSE); + || mode == NM_802_11_MODE_INFRA + || mode == NM_802_11_MODE_MESH, FALSE); priv = NM_WIFI_AP_GET_PRIVATE (ap); @@ -418,9 +418,11 @@ security_from_vardict (GVariant *security) if ( g_variant_lookup (security, "KeyMgmt", "^a&s", &array) && array) { - if (g_strv_contains (array, "wpa-psk")) + if (g_strv_contains (array, "wpa-psk") || + g_strv_contains (array, "wpa-ft-psk")) flags |= NM_802_11_AP_SEC_KEY_MGMT_PSK; if (g_strv_contains (array, "wpa-eap") || + g_strv_contains (array, "wpa-ft-eap") || g_strv_contains (array, "wpa-fils-sha256") || g_strv_contains (array, "wpa-fils-sha384")) flags |= NM_802_11_AP_SEC_KEY_MGMT_802_1X; @@ -815,6 +817,8 @@ nm_wifi_ap_update_from_properties (NMWifiAP *ap, changed |= nm_wifi_ap_set_mode (ap, NM_802_11_MODE_INFRA); else if (!g_strcmp0 (s, "ad-hoc")) changed |= nm_wifi_ap_set_mode (ap, NM_802_11_MODE_ADHOC); + else if (!g_strcmp0 (s, "mesh")) + changed |= nm_wifi_ap_set_mode (ap, NM_802_11_MODE_MESH); } if (g_variant_lookup (properties, "Signal", "n", &i16)) @@ -1007,7 +1011,9 @@ nm_wifi_ap_to_string (const NMWifiAP *self, ? '#' : (priv->fake ? 'f' - : 'a'))), + : (priv->mode == NM_802_11_MODE_MESH + ? 'm' + : 'a')))), chan, priv->strength, priv->flags & NM_802_11_AP_FLAGS_PRIVACY ? 'P' : '_', @@ -1072,6 +1078,8 @@ nm_wifi_ap_check_compatible (NMWifiAP *self, if ( !strcmp (mode, "ap") && (priv->mode != NM_802_11_MODE_INFRA || priv->hotspot != TRUE)) return FALSE; + if (!strcmp (mode, "mesh") && (priv->mode != NM_802_11_MODE_MESH)) + return FALSE; } band = nm_setting_wireless_get_band (s_wireless); @@ -1115,6 +1123,7 @@ nm_wifi_ap_complete_connection (NMWifiAP *self, return nm_wifi_utils_complete_connection (priv->ssid, priv->address, priv->mode, + priv->freq, priv->flags, priv->wpa_flags, priv->rsn_flags, @@ -1245,6 +1254,8 @@ nm_wifi_ap_new_fake_from_connection (NMConnection *connection) nm_wifi_ap_set_mode (ap, NM_802_11_MODE_INFRA); else if (!strcmp (mode, "adhoc")) nm_wifi_ap_set_mode (ap, NM_802_11_MODE_ADHOC); + else if (!strcmp (mode, "mesh")) + nm_wifi_ap_set_mode (ap, NM_802_11_MODE_MESH); else if (!strcmp (mode, "ap")) { nm_wifi_ap_set_mode (ap, NM_802_11_MODE_INFRA); NM_WIFI_AP_GET_PRIVATE (ap)->hotspot = TRUE; diff --git a/src/devices/wifi/nm-wifi-ap.h b/src/devices/wifi/nm-wifi-ap.h index 7462e9d1..755e722c 100644 --- a/src/devices/wifi/nm-wifi-ap.h +++ b/src/devices/wifi/nm-wifi-ap.h @@ -1,4 +1,3 @@ -/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ /* NetworkManager -- Network link manager * * This program is free software; you can redistribute it and/or modify diff --git a/src/devices/wifi/nm-wifi-common.c b/src/devices/wifi/nm-wifi-common.c index c95620e7..96828d59 100644 --- a/src/devices/wifi/nm-wifi-common.c +++ b/src/devices/wifi/nm-wifi-common.c @@ -1,4 +1,3 @@ -/*-*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ /* * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public diff --git a/src/devices/wifi/nm-wifi-common.h b/src/devices/wifi/nm-wifi-common.h index 91cbeb55..81d657ec 100644 --- a/src/devices/wifi/nm-wifi-common.h +++ b/src/devices/wifi/nm-wifi-common.h @@ -1,4 +1,3 @@ -/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ /* * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public diff --git a/src/devices/wifi/nm-wifi-factory.c b/src/devices/wifi/nm-wifi-factory.c index 9a89bbe9..2f069882 100644 --- a/src/devices/wifi/nm-wifi-factory.c +++ b/src/devices/wifi/nm-wifi-factory.c @@ -1,4 +1,3 @@ -/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ /* NetworkManager -- Network link manager * * This program is free software; you can redistribute it and/or modify diff --git a/src/devices/wifi/nm-wifi-utils.c b/src/devices/wifi/nm-wifi-utils.c index c6e8b3e0..426eeea8 100644 --- a/src/devices/wifi/nm-wifi-utils.c +++ b/src/devices/wifi/nm-wifi-utils.c @@ -1,4 +1,3 @@ -/*-*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ /* * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -25,6 +24,7 @@ #include #include "nm-utils.h" +#include "nm-core-internal.h" static gboolean verify_no_wep (NMSettingWirelessSecurity *s_wsec, const char *tag, GError **error) @@ -527,6 +527,7 @@ gboolean nm_wifi_utils_complete_connection (GBytes *ap_ssid, const char *bssid, NM80211Mode ap_mode, + guint32 ap_freq, guint32 ap_flags, guint32 ap_wpa_flags, guint32 ap_rsn_flags, @@ -540,6 +541,7 @@ nm_wifi_utils_complete_connection (GBytes *ap_ssid, GBytes *ssid; const char *mode, *key_mgmt, *auth_alg, *leap_username; gboolean adhoc = FALSE; + gboolean mesh = FALSE; s_wifi = nm_connection_get_setting_wireless (connection); g_assert (s_wifi); @@ -576,6 +578,10 @@ nm_wifi_utils_complete_connection (GBytes *ap_ssid, if (ap_mode == NM_802_11_MODE_ADHOC) valid = TRUE; adhoc = TRUE; + } else if (!strcmp (mode, NM_SETTING_WIRELESS_MODE_MESH)) { + if (ap_mode == NM_802_11_MODE_MESH) + valid = TRUE; + mesh = TRUE; } if (valid == FALSE) { @@ -591,10 +597,57 @@ nm_wifi_utils_complete_connection (GBytes *ap_ssid, if (ap_mode == NM_802_11_MODE_ADHOC) { mode = NM_SETTING_WIRELESS_MODE_ADHOC; adhoc = TRUE; + } else if (ap_mode == NM_802_11_MODE_MESH) { + mode = NM_SETTING_WIRELESS_MODE_MESH; + mesh = TRUE; } g_object_set (G_OBJECT (s_wifi), NM_SETTING_WIRELESS_MODE, mode, NULL); } + /* For now mesh requires channel and band, fill them only if both not present. + * Do not check existing values against an existing ap/mesh point, + * mesh join will start a new network if required */ + if (mesh) { + const char *band; + guint32 channel; + gboolean band_valid = TRUE; + gboolean chan_valid = TRUE; + gboolean valid; + + band = nm_setting_wireless_get_band (s_wifi); + channel = nm_setting_wireless_get_channel (s_wifi); + + valid = ((band == NULL) && (channel == 0)) + || ((band != NULL) && (channel != 0)); + + if ((band == NULL) && (channel == 0)) { + channel = nm_utils_wifi_freq_to_channel (ap_freq); + if (channel) { + g_object_set (s_wifi, + NM_SETTING_WIRELESS_CHANNEL, channel, + NULL); + } else { + chan_valid = FALSE; + } + + band = nm_utils_wifi_freq_to_band (ap_freq); + if (band) { + g_object_set (s_wifi, NM_SETTING_WIRELESS_BAND, band, NULL); + } else { + band_valid = FALSE; + } + } + + if (!valid || !chan_valid || !band_valid) { + g_set_error (error, + NM_CONNECTION_ERROR, + NM_CONNECTION_ERROR_INVALID_PROPERTY, + _("connection does not match mesh point")); + g_prefix_error (error, "%s.%s: ", NM_SETTING_WIRELESS_SETTING_NAME, NM_SETTING_WIRELESS_MODE); + return FALSE; + } + } + /* Security */ /* Open */ diff --git a/src/devices/wifi/nm-wifi-utils.h b/src/devices/wifi/nm-wifi-utils.h index 03238c24..251d122f 100644 --- a/src/devices/wifi/nm-wifi-utils.h +++ b/src/devices/wifi/nm-wifi-utils.h @@ -1,4 +1,3 @@ -/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ /* * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -37,6 +36,7 @@ typedef enum { gboolean nm_wifi_utils_complete_connection (GBytes *ssid, const char *bssid, NM80211Mode mode, + guint32 ap_freq, guint32 flags, guint32 wpa_flags, guint32 rsn_flags, diff --git a/src/devices/wifi/tests/meson.build b/src/devices/wifi/tests/meson.build index 895853c4..ba756d53 100644 --- a/src/devices/wifi/tests/meson.build +++ b/src/devices/wifi/tests/meson.build @@ -1,13 +1,13 @@ -test_unit = 'test-general' +test_unit = 'test-devices-wifi' exe = executable( - 'wifi-' + test_unit, + test_unit, [test_unit + '.c'] + common_sources, dependencies: test_nm_dep, ) test( - 'devices/wifi/' + test_unit, + test_unit, test_script, args: test_args + [exe.full_path()], timeout: default_test_timeout, diff --git a/src/devices/wifi/tests/test-devices-wifi.c b/src/devices/wifi/tests/test-devices-wifi.c new file mode 100644 index 00000000..a0b3e17f --- /dev/null +++ b/src/devices/wifi/tests/test-devices-wifi.c @@ -0,0 +1,1519 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Copyright (C) 2011 Red Hat, Inc. + * + */ + +#include "nm-default.h" + +#include "devices/wifi/nm-wifi-utils.h" +#include "nm-core-internal.h" + +#include "nm-test-utils-core.h" + +#define DEBUG 1 + +/*****************************************************************************/ + +#define COMPARE(src, expected, success, error, edomain, ecode) \ +{ \ + if (expected) { \ + if (!success) { \ + g_assert (error != NULL); \ + g_warning ("Failed to complete connection: %s", error->message); \ + } \ + g_assert (success == TRUE); \ + g_assert (error == NULL); \ +\ + success = nm_connection_compare (src, expected, NM_SETTING_COMPARE_FLAG_EXACT); \ + if (success == FALSE && DEBUG) { \ + g_print ("\n- COMPLETED ---------------------------------\n"); \ + nm_connection_dump (src); \ + g_print ("+ EXPECTED ++++++++++++++++++++++++++++++++++++\n"); \ + nm_connection_dump (expected); \ + g_print ("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n"); \ + } \ + g_assert (success == TRUE); \ + } else { \ + if (success) { \ + g_print ("\n- COMPLETED ---------------------------------\n"); \ + nm_connection_dump (src); \ + } \ + g_assert (success == FALSE); \ + g_assert_error (error, edomain, ecode); \ + } \ + \ + g_clear_error (&error); \ +} + +static gboolean +complete_connection (const char *ssid, + const char *bssid, + NM80211Mode mode, + guint32 flags, + guint32 wpa_flags, + guint32 rsn_flags, + gboolean lock_bssid, + NMConnection *src, + GError **error) +{ + gs_unref_bytes GBytes *ssid_b = NULL; + NMSettingWireless *s_wifi; + + /* Add a wifi setting if one doesn't exist */ + s_wifi = nm_connection_get_setting_wireless (src); + if (!s_wifi) { + s_wifi = (NMSettingWireless *) nm_setting_wireless_new (); + nm_connection_add_setting (src, NM_SETTING (s_wifi)); + } + + ssid_b = g_bytes_new (ssid, strlen (ssid)); + + return nm_wifi_utils_complete_connection (ssid_b, + bssid, + mode, + 0, + flags, + wpa_flags, + rsn_flags, + src, + lock_bssid, + error); +} + +typedef struct { + const char *key; + const char *str; + guint32 uint; +} KeyData; + +static void +set_items (NMSetting *setting, const KeyData *items) +{ + const KeyData *item; + GParamSpec *pspec; + GBytes *tmp; + + for (item = items; item && item->key; item++) { + g_assert (item->key); + pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (setting), item->key); + g_assert (pspec); + + if (pspec->value_type == G_TYPE_STRING) { + g_assert (item->uint == 0); + if (item->str) + g_object_set (G_OBJECT (setting), item->key, item->str, NULL); + } else if (pspec->value_type == G_TYPE_UINT) { + g_assert (item->str == NULL); + g_object_set (G_OBJECT (setting), item->key, item->uint, NULL); + } else if (pspec->value_type == G_TYPE_INT) { + int foo = (int) item->uint; + + g_assert (item->str == NULL); + g_object_set (G_OBJECT (setting), item->key, foo, NULL); + } else if (pspec->value_type == G_TYPE_BOOLEAN) { + gboolean foo = !! (item->uint); + + g_assert (item->str == NULL); + g_object_set (G_OBJECT (setting), item->key, foo, NULL); + } else if (pspec->value_type == G_TYPE_BYTES) { + g_assert (item->str); + tmp = g_bytes_new (item->str, strlen (item->str)); + g_object_set (G_OBJECT (setting), item->key, tmp, NULL); + g_bytes_unref (tmp); + } else { + /* Special types, check based on property name */ + if (!strcmp (item->key, NM_SETTING_WIRELESS_SECURITY_PROTO)) + nm_setting_wireless_security_add_proto (NM_SETTING_WIRELESS_SECURITY (setting), item->str); + else if (!strcmp (item->key, NM_SETTING_WIRELESS_SECURITY_PAIRWISE)) + nm_setting_wireless_security_add_pairwise (NM_SETTING_WIRELESS_SECURITY (setting), item->str); + else if (!strcmp (item->key, NM_SETTING_WIRELESS_SECURITY_GROUP)) + nm_setting_wireless_security_add_group (NM_SETTING_WIRELESS_SECURITY (setting), item->str); + else if (!strcmp (item->key, NM_SETTING_802_1X_EAP)) + nm_setting_802_1x_add_eap_method (NM_SETTING_802_1X (setting), item->str); + } + } +} + +static NMSettingWireless * +fill_wifi_empty (NMConnection *connection) +{ + NMSettingWireless *s_wifi; + + s_wifi = nm_connection_get_setting_wireless (connection); + if (!s_wifi) { + s_wifi = (NMSettingWireless *) nm_setting_wireless_new (); + nm_connection_add_setting (connection, NM_SETTING (s_wifi)); + } + return s_wifi; +} + +static NMSettingWireless * +fill_wifi (NMConnection *connection, const KeyData items[]) +{ + NMSettingWireless *s_wifi; + + s_wifi = nm_connection_get_setting_wireless (connection); + if (!s_wifi) { + s_wifi = (NMSettingWireless *) nm_setting_wireless_new (); + nm_connection_add_setting (connection, NM_SETTING (s_wifi)); + } + + set_items (NM_SETTING (s_wifi), items); + return s_wifi; +} + +static NMSettingWirelessSecurity * +fill_wsec (NMConnection *connection, const KeyData items[]) +{ + NMSettingWirelessSecurity *s_wsec; + + s_wsec = nm_connection_get_setting_wireless_security (connection); + if (!s_wsec) { + s_wsec = (NMSettingWirelessSecurity *) nm_setting_wireless_security_new (); + nm_connection_add_setting (connection, NM_SETTING (s_wsec)); + } + + set_items (NM_SETTING (s_wsec), items); + return s_wsec; +} + +static NMSetting8021x * +fill_8021x (NMConnection *connection, const KeyData items[]) +{ + NMSetting8021x *s_8021x; + + s_8021x = nm_connection_get_setting_802_1x (connection); + if (!s_8021x) { + s_8021x = (NMSetting8021x *) nm_setting_802_1x_new (); + nm_connection_add_setting (connection, NM_SETTING (s_8021x)); + } + + set_items (NM_SETTING (s_8021x), items); + return s_8021x; +} + +static NMConnection * +create_basic (const char *ssid, + const char *bssid, + NM80211Mode mode) +{ + NMConnection *connection; + NMSettingWireless *s_wifi = NULL; + GBytes *tmp; + + connection = nm_simple_connection_new (); + + s_wifi = (NMSettingWireless *) nm_setting_wireless_new (); + nm_connection_add_setting (connection, NM_SETTING (s_wifi)); + + /* SSID */ + tmp = g_bytes_new (ssid, strlen (ssid)); + g_object_set (G_OBJECT (s_wifi), NM_SETTING_WIRELESS_SSID, tmp, NULL); + g_bytes_unref (tmp); + + /* BSSID */ + if (bssid) + g_object_set (G_OBJECT (s_wifi), NM_SETTING_WIRELESS_BSSID, bssid, NULL); + + if (mode == NM_802_11_MODE_INFRA) + g_object_set (G_OBJECT (s_wifi), NM_SETTING_WIRELESS_MODE, "infrastructure", NULL); + else if (mode == NM_802_11_MODE_ADHOC) + g_object_set (G_OBJECT (s_wifi), NM_SETTING_WIRELESS_MODE, "adhoc", NULL); + else + g_assert_not_reached (); + + return connection; +} + +/*****************************************************************************/ + +static void +test_lock_bssid (void) +{ + NMConnection *src, *expected; + const char *bssid = "01:02:03:04:05:06"; + const char *ssid = "blahblah"; + gboolean success; + GError *error = NULL; + + src = nm_simple_connection_new (); + success = complete_connection (ssid, bssid, + NM_802_11_MODE_INFRA, NM_802_11_AP_FLAGS_NONE, + NM_802_11_AP_SEC_NONE, NM_802_11_AP_SEC_NONE, + TRUE, + src, &error); + expected = create_basic (ssid, bssid, NM_802_11_MODE_INFRA); + COMPARE (src, expected, success, error, 0, 0); + + g_object_unref (src); + g_object_unref (expected); +} + +/*****************************************************************************/ + +static void +test_open_ap_empty_connection (void) +{ + NMConnection *src, *expected; + const char *bssid = "01:02:03:04:05:06"; + const char *ssid = "blahblah"; + gboolean success; + GError *error = NULL; + + /* Test that an empty source connection is correctly filled with the + * SSID and Infra modes of the given AP details. + */ + + src = nm_simple_connection_new (); + success = complete_connection (ssid, bssid, + NM_802_11_MODE_INFRA, NM_802_11_AP_FLAGS_NONE, + NM_802_11_AP_SEC_NONE, NM_802_11_AP_SEC_NONE, + FALSE, + src, &error); + expected = create_basic (ssid, NULL, NM_802_11_MODE_INFRA); + COMPARE (src, expected, success, error, 0, 0); + + g_object_unref (src); + g_object_unref (expected); +} + +/*****************************************************************************/ + +static void +test_open_ap_leap_connection_1 (gconstpointer add_wifi) +{ + NMConnection *src; + const char *bssid = "01:02:03:04:05:06"; + const KeyData src_wsec[] = { { NM_SETTING_WIRELESS_SECURITY_LEAP_USERNAME, "Bill Smith", 0 }, { NULL } }; + gboolean success; + GError *error = NULL; + + /* Test that a basic connection filled with a LEAP username is + * rejected when completion is attempted with an open AP. LEAP requires + * the AP to have the Privacy bit set. + */ + + src = nm_simple_connection_new (); + if (add_wifi) + fill_wifi_empty (src); + fill_wsec (src, src_wsec); + + success = complete_connection ("blahblah", bssid, + NM_802_11_MODE_INFRA, NM_802_11_AP_FLAGS_NONE, + NM_802_11_AP_SEC_NONE, NM_802_11_AP_SEC_NONE, + FALSE, + src, &error); + /* We expect failure */ + COMPARE (src, NULL, success, error, NM_CONNECTION_ERROR, NM_CONNECTION_ERROR_INVALID_SETTING); + + g_object_unref (src); +} + +/*****************************************************************************/ + +static void +test_open_ap_leap_connection_2 (void) +{ + NMConnection *src; + const char *bssid = "01:02:03:04:05:06"; + const KeyData src_wsec[] = { { NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, "ieee8021x", 0 }, { NULL } }; + gboolean success; + GError *error = NULL; + + /* Test that a basic connection specifying IEEE8021x security (ie, Dynamic + * WEP or LEAP) is rejected when completion is attempted with an open AP. + */ + + src = nm_simple_connection_new (); + fill_wifi_empty (src); + fill_wsec (src, src_wsec); + + success = complete_connection ("blahblah", bssid, + NM_802_11_MODE_INFRA, NM_802_11_AP_FLAGS_NONE, + NM_802_11_AP_SEC_NONE, NM_802_11_AP_SEC_NONE, + FALSE, + src, &error); + /* We expect failure */ + COMPARE (src, NULL, success, error, NM_CONNECTION_ERROR, NM_CONNECTION_ERROR_INVALID_SETTING); + + g_object_unref (src); +} + +/*****************************************************************************/ + +static void +test_open_ap_wep_connection (gconstpointer add_wifi) +{ + NMConnection *src; + const char *bssid = "01:02:03:04:05:06"; + const KeyData src_wsec[] = { + { NM_SETTING_WIRELESS_SECURITY_WEP_KEY0, "11111111111111111111111111", 0 }, + { NM_SETTING_WIRELESS_SECURITY_WEP_TX_KEYIDX, NULL, 0 }, + { NM_SETTING_WIRELESS_SECURITY_AUTH_ALG, "open", 0 }, + { NULL } }; + gboolean success; + GError *error = NULL; + + /* Test that a static WEP connection is rejected when completion is + * attempted with an open AP. + */ + + src = nm_simple_connection_new (); + if (add_wifi) + fill_wifi_empty (src); + fill_wsec (src, src_wsec); + success = complete_connection ("blahblah", bssid, + NM_802_11_MODE_INFRA, NM_802_11_AP_FLAGS_NONE, + NM_802_11_AP_SEC_NONE, NM_802_11_AP_SEC_NONE, + FALSE, + src, &error); + /* We expect failure */ + COMPARE (src, NULL, success, error, NM_CONNECTION_ERROR, NM_CONNECTION_ERROR_INVALID_SETTING); + + g_object_unref (src); +} + +/*****************************************************************************/ + +static void +test_ap_wpa_psk_connection_base (const char *key_mgmt, + const char *auth_alg, + guint32 flags, + guint32 wpa_flags, + guint32 rsn_flags, + gboolean add_wifi, + guint error_code, + NMConnection *expected) +{ + NMConnection *src; + const char *ssid = "blahblah"; + const char *bssid = "01:02:03:04:05:06"; + const KeyData exp_wifi[] = { + { NM_SETTING_WIRELESS_SSID, ssid, 0 }, + { NM_SETTING_WIRELESS_MODE, "infrastructure", 0 }, + { NULL } }; + const KeyData both_wsec[] = { + { NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, key_mgmt, 0 }, + { NM_SETTING_WIRELESS_SECURITY_AUTH_ALG, auth_alg, 0 }, + { NM_SETTING_WIRELESS_SECURITY_PSK, "asdfasdfasdfasdfasdfafs", 0 }, + { NULL } }; + gboolean success; + GError *error = NULL; + + src = nm_simple_connection_new (); + if (add_wifi) + fill_wifi_empty (src); + fill_wsec (src, both_wsec); + success = complete_connection (ssid, bssid, NM_802_11_MODE_INFRA, + flags, wpa_flags, rsn_flags, + FALSE, src, &error); + if (expected) { + fill_wifi (expected, exp_wifi); + fill_wsec (expected, both_wsec); + } + COMPARE (src, expected, success, error, NM_CONNECTION_ERROR, error_code); + + g_object_unref (src); +} + +static void +test_open_ap_wpa_psk_connection_1 (void) +{ + /* Test that a WPA-PSK connection filling only the PSK itself and *not* + * filling the wifi setting is rejected when completion is attempted with + * an open AP. + */ + test_ap_wpa_psk_connection_base (NULL, NULL, + NM_802_11_AP_FLAGS_NONE, + NM_802_11_AP_SEC_NONE, + NM_802_11_AP_SEC_NONE, + FALSE, + NM_CONNECTION_ERROR_INVALID_SETTING, + NULL); +} + +static void +test_open_ap_wpa_psk_connection_2 (void) +{ + /* Test that a WPA-PSK connection filling only the PSK itself and also + * filling the wifi setting is rejected when completion is attempted with + * an open AP. + */ + test_ap_wpa_psk_connection_base (NULL, NULL, + NM_802_11_AP_FLAGS_NONE, + NM_802_11_AP_SEC_NONE, + NM_802_11_AP_SEC_NONE, + TRUE, + NM_CONNECTION_ERROR_INVALID_SETTING, + NULL); +} + +static void +test_open_ap_wpa_psk_connection_3 (void) +{ + /* Test that a WPA-PSK connection filling the PSK and setting the auth alg + * to 'open' is rejected when completion is attempted with an open AP. + */ + test_ap_wpa_psk_connection_base (NULL, "open", + NM_802_11_AP_FLAGS_NONE, + NM_802_11_AP_SEC_NONE, + NM_802_11_AP_SEC_NONE, + FALSE, + NM_CONNECTION_ERROR_INVALID_SETTING, + NULL); +} + +static void +test_open_ap_wpa_psk_connection_4 (void) +{ + /* Test that a WPA-PSK connection filling the PSK and setting the auth alg + * to 'shared' is rejected when completion is attempted with an open AP. + * Shared auth cannot be used with WPA. + */ + test_ap_wpa_psk_connection_base (NULL, "shared", + NM_802_11_AP_FLAGS_NONE, + NM_802_11_AP_SEC_NONE, + NM_802_11_AP_SEC_NONE, + FALSE, + NM_CONNECTION_ERROR_INVALID_SETTING, + NULL); +} + +static void +test_open_ap_wpa_psk_connection_5 (void) +{ + /* Test that a WPA-PSK connection filling the PSK, the auth algorithm, and + * key management is rejected when completion is attempted with an open AP. + */ + test_ap_wpa_psk_connection_base ("wpa-psk", "open", + NM_802_11_AP_FLAGS_NONE, + NM_802_11_AP_SEC_NONE, + NM_802_11_AP_SEC_NONE, + FALSE, + NM_CONNECTION_ERROR_INVALID_SETTING, + NULL); +} + +/*****************************************************************************/ + +static void +test_ap_wpa_eap_connection_base (const char *key_mgmt, + const char *auth_alg, + guint32 flags, + guint32 wpa_flags, + guint32 rsn_flags, + gboolean add_wifi, + guint error_code) +{ + NMConnection *src; + const char *bssid = "01:02:03:04:05:06"; + const KeyData src_empty[] = { { NULL } }; + const KeyData src_wsec[] = { + { NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, key_mgmt, 0 }, + { NM_SETTING_WIRELESS_SECURITY_AUTH_ALG, auth_alg, 0 }, + { NULL } }; + gboolean success; + GError *error = NULL; + + src = nm_simple_connection_new (); + if (add_wifi) + fill_wifi_empty (src); + fill_wsec (src, src_wsec); + fill_8021x (src, src_empty); + success = complete_connection ("blahblah", bssid, NM_802_11_MODE_INFRA, + flags, wpa_flags, rsn_flags, + FALSE, src, &error); + /* Failure expected */ + COMPARE (src, NULL, success, error, NM_CONNECTION_ERROR, error_code); + + g_object_unref (src); +} + +enum { + IDX_NONE = 0, + IDX_OPEN, + IDX_PRIV, + IDX_WPA_PSK_PTKIP_GTKIP, + IDX_WPA_PSK_PTKIP_PCCMP_GTKIP, + IDX_WPA_RSN_PSK_PTKIP_PCCMP_GTKIP, + IDX_WPA_RSN_PSK_PCCMP_GCCMP, + IDX_RSN_PSK_PCCMP_GCCMP, + IDX_RSN_PSK_PTKIP_PCCMP_GTKIP, + IDX_WPA_8021X, + IDX_RSN_8021X, +}; + +static guint32 +flags_for_idx (guint32 idx) +{ + if (idx == IDX_OPEN) + return NM_802_11_AP_FLAGS_NONE; + else if ( idx == IDX_PRIV + || idx == IDX_WPA_PSK_PTKIP_GTKIP + || idx == IDX_WPA_PSK_PTKIP_PCCMP_GTKIP + || idx == IDX_RSN_PSK_PCCMP_GCCMP + || idx == IDX_RSN_PSK_PTKIP_PCCMP_GTKIP + || idx == IDX_WPA_RSN_PSK_PTKIP_PCCMP_GTKIP + || idx == IDX_WPA_RSN_PSK_PCCMP_GCCMP + || idx == IDX_WPA_8021X + || idx == IDX_RSN_8021X) + return NM_802_11_AP_FLAGS_PRIVACY; + else + g_assert_not_reached (); +} + +static guint32 +wpa_flags_for_idx (guint32 idx) +{ + if (idx == IDX_OPEN || idx == IDX_PRIV || idx == IDX_RSN_8021X + || idx == IDX_RSN_PSK_PCCMP_GCCMP || idx == IDX_RSN_PSK_PTKIP_PCCMP_GTKIP) + return NM_802_11_AP_SEC_NONE; + else if (idx == IDX_WPA_PSK_PTKIP_GTKIP) + return NM_802_11_AP_SEC_PAIR_TKIP | NM_802_11_AP_SEC_GROUP_TKIP | NM_802_11_AP_SEC_KEY_MGMT_PSK; + else if (idx == IDX_WPA_RSN_PSK_PTKIP_PCCMP_GTKIP) + return NM_802_11_AP_SEC_PAIR_TKIP | NM_802_11_AP_SEC_PAIR_CCMP | NM_802_11_AP_SEC_GROUP_TKIP | NM_802_11_AP_SEC_KEY_MGMT_PSK; + else if (NM_IN_SET (idx, IDX_WPA_PSK_PTKIP_PCCMP_GTKIP, IDX_WPA_RSN_PSK_PCCMP_GCCMP)) + return NM_802_11_AP_SEC_PAIR_CCMP | NM_802_11_AP_SEC_GROUP_CCMP | NM_802_11_AP_SEC_KEY_MGMT_PSK; + else if (idx == IDX_WPA_8021X) + return NM_802_11_AP_SEC_PAIR_TKIP | NM_802_11_AP_SEC_GROUP_TKIP | NM_802_11_AP_SEC_KEY_MGMT_802_1X; + else + g_assert_not_reached (); +} + +static guint32 +rsn_flags_for_idx (guint32 idx) +{ + if (idx == IDX_OPEN || idx == IDX_PRIV || idx == IDX_WPA_8021X + || idx == IDX_WPA_PSK_PTKIP_GTKIP || idx == IDX_WPA_PSK_PTKIP_PCCMP_GTKIP) + return NM_802_11_AP_SEC_NONE; + else if (idx == IDX_RSN_PSK_PCCMP_GCCMP) + return NM_802_11_AP_SEC_PAIR_CCMP | NM_802_11_AP_SEC_GROUP_CCMP | NM_802_11_AP_SEC_KEY_MGMT_PSK; + else if (idx == IDX_RSN_PSK_PTKIP_PCCMP_GTKIP) + return NM_802_11_AP_SEC_PAIR_TKIP | NM_802_11_AP_SEC_PAIR_CCMP | NM_802_11_AP_SEC_GROUP_TKIP | NM_802_11_AP_SEC_KEY_MGMT_PSK; + else if (idx == IDX_WPA_RSN_PSK_PTKIP_PCCMP_GTKIP) + return NM_802_11_AP_SEC_PAIR_TKIP | NM_802_11_AP_SEC_PAIR_CCMP | NM_802_11_AP_SEC_GROUP_TKIP | NM_802_11_AP_SEC_KEY_MGMT_PSK; + else if (idx == IDX_WPA_RSN_PSK_PCCMP_GCCMP) + return NM_802_11_AP_SEC_PAIR_CCMP | NM_802_11_AP_SEC_GROUP_CCMP | NM_802_11_AP_SEC_KEY_MGMT_PSK; + else if (idx == IDX_RSN_8021X) + return NM_802_11_AP_SEC_PAIR_CCMP | NM_802_11_AP_SEC_GROUP_CCMP | NM_802_11_AP_SEC_KEY_MGMT_802_1X; + else + g_assert_not_reached (); +} + +static guint32 +error_code_for_idx (guint32 idx, guint num) +{ + if (idx == IDX_OPEN) + return NM_CONNECTION_ERROR_INVALID_SETTING; + else if (idx == IDX_PRIV) { + if (num <= 3) + return NM_CONNECTION_ERROR_MISSING_PROPERTY; + else + return NM_CONNECTION_ERROR_INVALID_PROPERTY; + } else if ( idx == IDX_WPA_PSK_PTKIP_GTKIP || idx == IDX_WPA_PSK_PTKIP_PCCMP_GTKIP + || idx == IDX_WPA_RSN_PSK_PCCMP_GCCMP || idx == IDX_WPA_RSN_PSK_PTKIP_PCCMP_GTKIP + || idx == IDX_RSN_PSK_PTKIP_PCCMP_GTKIP || idx == IDX_RSN_PSK_PCCMP_GCCMP) + if (num == 4) + return NM_CONNECTION_ERROR_INVALID_PROPERTY; + else + return NM_CONNECTION_ERROR_INVALID_SETTING; + else + g_assert_not_reached (); +} + +static void +test_ap_wpa_eap_connection_1 (gconstpointer data) +{ + guint idx = GPOINTER_TO_UINT (data); + + test_ap_wpa_eap_connection_base (NULL, NULL, + flags_for_idx (idx), + wpa_flags_for_idx (idx), + rsn_flags_for_idx (idx), + FALSE, + error_code_for_idx (idx, 1)); +} + +static void +test_ap_wpa_eap_connection_2 (gconstpointer data) +{ + guint idx = GPOINTER_TO_UINT (data); + + test_ap_wpa_eap_connection_base (NULL, NULL, + flags_for_idx (idx), + wpa_flags_for_idx (idx), + rsn_flags_for_idx (idx), + TRUE, + error_code_for_idx (idx, 2)); +} + +static void +test_ap_wpa_eap_connection_3 (gconstpointer data) +{ + guint idx = GPOINTER_TO_UINT (data); + + test_ap_wpa_eap_connection_base (NULL, "open", + flags_for_idx (idx), + wpa_flags_for_idx (idx), + rsn_flags_for_idx (idx), + FALSE, + error_code_for_idx (idx, 3)); +} + +static void +test_ap_wpa_eap_connection_4 (gconstpointer data) +{ + guint idx = GPOINTER_TO_UINT (data); + + test_ap_wpa_eap_connection_base (NULL, "shared", + flags_for_idx (idx), + wpa_flags_for_idx (idx), + rsn_flags_for_idx (idx), + FALSE, + error_code_for_idx (idx, 4)); +} + +static void +test_ap_wpa_eap_connection_5 (gconstpointer data) +{ + guint idx = GPOINTER_TO_UINT (data); + + test_ap_wpa_eap_connection_base ("wpa-eap", "open", + flags_for_idx (idx), + wpa_flags_for_idx (idx), + rsn_flags_for_idx (idx), + FALSE, + error_code_for_idx (idx, 5)); +} + +/*****************************************************************************/ + +static void +test_priv_ap_empty_connection (void) +{ + NMConnection *src, *expected; + const char *bssid = "01:02:03:04:05:06"; + const char *ssid = "blahblah"; + const KeyData exp_wsec[] = { + { NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, "none", 0 }, + { NULL } }; + gboolean success; + GError *error = NULL; + + /* Test that an empty connection is completed to a valid Static WEP + * connection when completed with an AP with the Privacy bit set. + */ + + src = nm_simple_connection_new (); + success = complete_connection (ssid, bssid, + NM_802_11_MODE_INFRA, NM_802_11_AP_FLAGS_PRIVACY, + NM_802_11_AP_SEC_NONE, NM_802_11_AP_SEC_NONE, + FALSE, + src, &error); + + /* Static WEP connection expected */ + expected = create_basic (ssid, NULL, NM_802_11_MODE_INFRA); + fill_wsec (expected, exp_wsec); + COMPARE (src, expected, success, error, 0, 0); + + g_object_unref (src); + g_object_unref (expected); +} + +/*****************************************************************************/ + +static void +test_priv_ap_leap_connection_1 (gconstpointer add_wifi) +{ + NMConnection *src, *expected; + const char *ssid = "blahblah"; + const char *bssid = "01:02:03:04:05:06"; + const char *leap_username = "Bill Smith"; + const KeyData src_wsec[] = { + { NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, "ieee8021x", 0 }, + { NM_SETTING_WIRELESS_SECURITY_LEAP_USERNAME, leap_username, 0 }, + { NULL } }; + const KeyData exp_wsec[] = { + { NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, "ieee8021x", 0 }, + { NM_SETTING_WIRELESS_SECURITY_AUTH_ALG, "leap", 0 }, + { NM_SETTING_WIRELESS_SECURITY_LEAP_USERNAME, leap_username, 0 }, + { NULL } }; + gboolean success; + GError *error = NULL; + + /* Test that an minimal LEAP connection specifying only key management and + * the LEAP username is completed to a full LEAP connection when completed + * with an AP with the Privacy bit set. + */ + + src = nm_simple_connection_new (); + if (add_wifi) + fill_wifi_empty (src); + fill_wsec (src, src_wsec); + success = complete_connection (ssid, bssid, + NM_802_11_MODE_INFRA, NM_802_11_AP_FLAGS_PRIVACY, + NM_802_11_AP_SEC_NONE, NM_802_11_AP_SEC_NONE, + FALSE, + src, &error); + /* We expect success here; since LEAP APs just set the 'privacy' flag + * there's no way to determine from the AP's beacon whether it's static WEP, + * dynamic WEP, or LEAP. + */ + expected = create_basic (ssid, NULL, NM_802_11_MODE_INFRA); + fill_wsec (expected, exp_wsec); + COMPARE (src, expected, success, error, 0, 0); + + g_object_unref (src); + g_object_unref (expected); +} + +/*****************************************************************************/ + +static void +test_priv_ap_leap_connection_2 (void) +{ + NMConnection *src; + const char *bssid = "01:02:03:04:05:06"; + const KeyData src_wsec[] = { + { NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, "ieee8021x", 0 }, + { NM_SETTING_WIRELESS_SECURITY_AUTH_ALG, "leap", 0 }, + { NULL } }; + gboolean success; + GError *error = NULL; + + /* Test that an minimal LEAP connection specifying only key management and + * the LEAP auth alg is completed to a full LEAP connection when completed + * with an AP with the Privacy bit set. + */ + + src = nm_simple_connection_new (); + fill_wifi_empty (src); + fill_wsec (src, src_wsec); + success = complete_connection ("blahblah", bssid, + NM_802_11_MODE_INFRA, NM_802_11_AP_FLAGS_PRIVACY, + NM_802_11_AP_SEC_NONE, NM_802_11_AP_SEC_NONE, + FALSE, + src, &error); + /* We expect failure here, we need a LEAP username */ + COMPARE (src, NULL, success, error, NM_CONNECTION_ERROR, NM_CONNECTION_ERROR_MISSING_PROPERTY); + + g_object_unref (src); +} + +/*****************************************************************************/ + +static void +test_priv_ap_dynamic_wep_1 (void) +{ + NMConnection *src, *expected; + const char *ssid = "blahblah"; + const char *bssid = "01:02:03:04:05:06"; + const KeyData src_wsec[] = { + { NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, "ieee8021x", 0 }, + { NM_SETTING_WIRELESS_SECURITY_AUTH_ALG, "open", 0 }, + { NULL } }; + const KeyData both_8021x[] = { + { NM_SETTING_802_1X_EAP, "peap", 0 }, + { NM_SETTING_802_1X_IDENTITY, "Bill Smith", 0 }, + { NM_SETTING_802_1X_PHASE2_AUTH, "mschapv2", 0 }, + { NULL } }; + const KeyData exp_wsec[] = { + { NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, "ieee8021x", 0 }, + { NM_SETTING_WIRELESS_SECURITY_AUTH_ALG, "open", 0 }, + { NULL } }; + gboolean success; + GError *error = NULL; + + /* Test that an minimal Dynamic WEP connection specifying key management, + * the auth algorithm, and valid 802.1x setting is completed to a valid + * Dynamic WEP connection when completed with an AP with the Privacy bit set. + */ + + src = nm_simple_connection_new (); + fill_wifi_empty (src); + fill_wsec (src, src_wsec); + fill_8021x (src, both_8021x); + success = complete_connection (ssid, bssid, + NM_802_11_MODE_INFRA, NM_802_11_AP_FLAGS_PRIVACY, + NM_802_11_AP_SEC_NONE, NM_802_11_AP_SEC_NONE, + FALSE, + src, &error); + + /* We expect a completed Dynamic WEP connection */ + expected = create_basic (ssid, NULL, NM_802_11_MODE_INFRA); + fill_wsec (expected, exp_wsec); + fill_8021x (expected, both_8021x); + COMPARE (src, expected, success, error, 0, 0); + + g_object_unref (src); + g_object_unref (expected); +} + +/*****************************************************************************/ + +static void +test_priv_ap_dynamic_wep_2 (void) +{ + NMConnection *src, *expected; + const char *ssid = "blahblah"; + const char *bssid = "01:02:03:04:05:06"; + const KeyData src_wsec[] = { + { NM_SETTING_WIRELESS_SECURITY_AUTH_ALG, "open", 0 }, + { NULL } }; + const KeyData both_8021x[] = { + { NM_SETTING_802_1X_EAP, "peap", 0 }, + { NM_SETTING_802_1X_IDENTITY, "Bill Smith", 0 }, + { NM_SETTING_802_1X_PHASE2_AUTH, "mschapv2", 0 }, + { NULL } }; + const KeyData exp_wsec[] = { + { NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, "ieee8021x", 0 }, + { NM_SETTING_WIRELESS_SECURITY_AUTH_ALG, "open", 0 }, + { NULL } }; + gboolean success; + GError *error = NULL; + + /* Test that an minimal Dynamic WEP connection specifying only the auth + * algorithm and a valid 802.1x setting is completed to a valid Dynamic + * WEP connection when completed with an AP with the Privacy bit set. + */ + + src = nm_simple_connection_new (); + fill_wifi_empty (src); + fill_wsec (src, src_wsec); + fill_8021x (src, both_8021x); + success = complete_connection (ssid, bssid, + NM_802_11_MODE_INFRA, NM_802_11_AP_FLAGS_PRIVACY, + NM_802_11_AP_SEC_NONE, NM_802_11_AP_SEC_NONE, + FALSE, + src, &error); + + /* We expect a completed Dynamic WEP connection */ + expected = create_basic (ssid, NULL, NM_802_11_MODE_INFRA); + fill_wsec (expected, exp_wsec); + fill_8021x (expected, both_8021x); + COMPARE (src, expected, success, error, 0, 0); + + g_object_unref (src); + g_object_unref (expected); +} + +/*****************************************************************************/ + +static void +test_priv_ap_dynamic_wep_3 (void) +{ + NMConnection *src; + const char *bssid = "01:02:03:04:05:06"; + const KeyData src_wsec[] = { + { NM_SETTING_WIRELESS_SECURITY_AUTH_ALG, "shared", 0 }, + { NULL } }; + const KeyData src_8021x[] = { + { NM_SETTING_802_1X_EAP, "peap", 0 }, + { NM_SETTING_802_1X_IDENTITY, "Bill Smith", 0 }, + { NM_SETTING_802_1X_PHASE2_AUTH, "mschapv2", 0 }, + { NULL } }; + gboolean success; + GError *error = NULL; + + /* Ensure that a basic connection specifying 'shared' auth and an 802.1x + * setting is rejected, as 802.1x is incompatible with 'shared' auth. + */ + + src = nm_simple_connection_new (); + fill_wifi_empty (src); + fill_wsec (src, src_wsec); + fill_8021x (src, src_8021x); + success = complete_connection ("blahblah", bssid, + NM_802_11_MODE_INFRA, NM_802_11_AP_FLAGS_PRIVACY, + NM_802_11_AP_SEC_NONE, NM_802_11_AP_SEC_NONE, + FALSE, + src, &error); + /* Expect failure; shared is not compatible with dynamic WEP */ + COMPARE (src, NULL, success, error, NM_CONNECTION_ERROR, NM_CONNECTION_ERROR_INVALID_PROPERTY); + + g_object_unref (src); +} + +/*****************************************************************************/ + +static void +test_priv_ap_wpa_psk_connection_1 (void) +{ + /* Test that a basic WPA-PSK connection is rejected when completion is + * attempted with an AP with just the Privacy bit set. Lack of WPA/RSN + * flags means the AP provides Static/Dynamic WEP or LEAP, not WPA. + */ + test_ap_wpa_psk_connection_base (NULL, NULL, + NM_802_11_AP_FLAGS_PRIVACY, + NM_802_11_AP_SEC_NONE, + NM_802_11_AP_SEC_NONE, + FALSE, + NM_CONNECTION_ERROR_INVALID_PROPERTY, + NULL); +} + +static void +test_priv_ap_wpa_psk_connection_2 (void) +{ + /* Test that a basic WPA-PSK connection is rejected when completion is + * attempted with an AP with just the Privacy bit set. Lack of WPA/RSN + * flags means the AP provides Static/Dynamic WEP or LEAP, not WPA. + */ + test_ap_wpa_psk_connection_base (NULL, NULL, + NM_802_11_AP_FLAGS_PRIVACY, + NM_802_11_AP_SEC_NONE, + NM_802_11_AP_SEC_NONE, + TRUE, + NM_CONNECTION_ERROR_INVALID_PROPERTY, + NULL); +} + +static void +test_priv_ap_wpa_psk_connection_3 (void) +{ + /* Test that a basic WPA-PSK connection specifying only the auth algorithm + * is rejected when completion is attempted with an AP with just the Privacy + * bit set. Lack of WPA/RSN flags means the AP provides Static/Dynamic WEP + * or LEAP, not WPA. + */ + test_ap_wpa_psk_connection_base (NULL, "open", + NM_802_11_AP_FLAGS_PRIVACY, + NM_802_11_AP_SEC_NONE, + NM_802_11_AP_SEC_NONE, + FALSE, + NM_CONNECTION_ERROR_INVALID_PROPERTY, + NULL); +} + +static void +test_priv_ap_wpa_psk_connection_4 (void) +{ + /* Test that a basic WPA-PSK connection specifying only the auth algorithm + * is rejected when completion is attempted with an AP with just the Privacy + * bit set. Lack of WPA/RSN flags means the AP provides Static/Dynamic WEP + * or LEAP, not WPA. Second, 'shared' auth is incompatible with WPA. + */ + test_ap_wpa_psk_connection_base (NULL, "shared", + NM_802_11_AP_FLAGS_PRIVACY, + NM_802_11_AP_SEC_NONE, + NM_802_11_AP_SEC_NONE, + FALSE, + NM_CONNECTION_ERROR_INVALID_PROPERTY, + NULL); +} + +static void +test_priv_ap_wpa_psk_connection_5 (void) +{ + /* Test that a WPA-PSK connection specifying both the key management and + * auth algorithm is rejected when completion is attempted with an AP with + * just the Privacy bit set. Lack of WPA/RSN flags means the AP provides + * Static/Dynamic WEP or LEAP, not WPA. + */ + test_ap_wpa_psk_connection_base ("wpa-psk", "open", + NM_802_11_AP_FLAGS_PRIVACY, + NM_802_11_AP_SEC_NONE, + NM_802_11_AP_SEC_NONE, + FALSE, + NM_CONNECTION_ERROR_INVALID_PROPERTY, + NULL); +} + +/*****************************************************************************/ + +static void +test_wpa_ap_empty_connection (gconstpointer data) +{ + guint idx = GPOINTER_TO_UINT (data); + NMConnection *src, *expected; + const char *bssid = "01:02:03:04:05:06"; + const char *ssid = "blahblah"; + const KeyData exp_wsec[] = { + { NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, "wpa-psk", 0 }, + { NM_SETTING_WIRELESS_SECURITY_AUTH_ALG, "open", 0 }, + { NULL } }; + gboolean success; + GError *error = NULL; + + /* Test that a basic WPA-PSK connection specifying just key management and + * the auth algorithm is completed successfully when given an AP with WPA + * or RSN flags. + */ + + src = nm_simple_connection_new (); + success = complete_connection (ssid, bssid, + NM_802_11_MODE_INFRA, NM_802_11_AP_FLAGS_PRIVACY, + wpa_flags_for_idx (idx), + rsn_flags_for_idx (idx), + FALSE, src, &error); + + /* WPA connection expected */ + expected = create_basic (ssid, NULL, NM_802_11_MODE_INFRA); + fill_wsec (expected, exp_wsec); + COMPARE (src, expected, success, error, 0, 0); + + g_object_unref (src); + g_object_unref (expected); +} + +/*****************************************************************************/ + +static void +test_wpa_ap_leap_connection_1 (gconstpointer data) +{ + guint idx = GPOINTER_TO_UINT (data); + NMConnection *src; + const char *ssid = "blahblah"; + const char *bssid = "01:02:03:04:05:06"; + const char *leap_username = "Bill Smith"; + const KeyData src_wsec[] = { + { NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, "ieee8021x", 0 }, + { NM_SETTING_WIRELESS_SECURITY_LEAP_USERNAME, leap_username, 0 }, + { NULL } }; + gboolean success; + GError *error = NULL; + + /* Test that completion of a LEAP connection with a WPA-enabled AP is + * rejected since WPA APs (usually) do not support LEAP. + */ + + src = nm_simple_connection_new (); + fill_wifi_empty (src); + fill_wsec (src, src_wsec); + success = complete_connection (ssid, bssid, + NM_802_11_MODE_INFRA, NM_802_11_AP_FLAGS_PRIVACY, + wpa_flags_for_idx (idx), + rsn_flags_for_idx (idx), + FALSE, + src, &error); + /* Expect failure here; WPA APs don't support old-school LEAP */ + COMPARE (src, NULL, success, error, NM_CONNECTION_ERROR, NM_CONNECTION_ERROR_INVALID_PROPERTY); + + g_object_unref (src); +} + +/*****************************************************************************/ + +static void +test_wpa_ap_leap_connection_2 (gconstpointer data) +{ + guint idx = GPOINTER_TO_UINT (data); + NMConnection *src; + const char *bssid = "01:02:03:04:05:06"; + const KeyData src_wsec[] = { + { NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, "ieee8021x", 0 }, + { NM_SETTING_WIRELESS_SECURITY_AUTH_ALG, "leap", 0 }, + { NULL } }; + gboolean success; + GError *error = NULL; + + /* Test that completion of a LEAP connection with a WPA-enabled AP is + * rejected since WPA APs (usually) do not support LEAP. + */ + + src = nm_simple_connection_new (); + fill_wifi_empty (src); + fill_wsec (src, src_wsec); + success = complete_connection ("blahblah", bssid, + NM_802_11_MODE_INFRA, NM_802_11_AP_FLAGS_PRIVACY, + wpa_flags_for_idx (idx), + rsn_flags_for_idx (idx), + FALSE, + src, &error); + /* We expect failure here, we need a LEAP username */ + COMPARE (src, NULL, success, error, NM_CONNECTION_ERROR, NM_CONNECTION_ERROR_INVALID_PROPERTY); + + g_object_unref (src); +} + +/*****************************************************************************/ + +static void +test_wpa_ap_dynamic_wep_connection (gconstpointer data) +{ + guint idx = GPOINTER_TO_UINT (data); + NMConnection *src; + const char *bssid = "01:02:03:04:05:06"; + const KeyData src_wsec[] = { + { NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, "ieee8021x", 0 }, + { NULL } }; + gboolean success; + GError *error = NULL; + + /* Test that completion of a Dynamic WEP connection with a WPA-enabled AP is + * rejected since WPA APs (usually) do not support Dynamic WEP. + */ + + src = nm_simple_connection_new (); + fill_wifi_empty (src); + fill_wsec (src, src_wsec); + success = complete_connection ("blahblah", bssid, + NM_802_11_MODE_INFRA, NM_802_11_AP_FLAGS_PRIVACY, + wpa_flags_for_idx (idx), + rsn_flags_for_idx (idx), + FALSE, + src, &error); + /* We expect failure here since Dynamic WEP is incompatible with WPA */ + COMPARE (src, NULL, success, error, NM_CONNECTION_ERROR, NM_CONNECTION_ERROR_INVALID_PROPERTY); + + g_object_unref (src); +} + +/*****************************************************************************/ + +static void +test_wpa_ap_wpa_psk_connection_1 (gconstpointer data) +{ + guint idx = GPOINTER_TO_UINT (data); + NMConnection *expected; + const KeyData exp_wsec[] = { + { NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, "wpa-psk", 0 }, + { NM_SETTING_WIRELESS_SECURITY_AUTH_ALG, "open", 0 }, + { NULL } }; + + expected = nm_simple_connection_new (); + fill_wsec (expected, exp_wsec); + test_ap_wpa_psk_connection_base (NULL, NULL, + NM_802_11_AP_FLAGS_PRIVACY, + wpa_flags_for_idx (idx), + rsn_flags_for_idx (idx), + FALSE, + NM_CONNECTION_ERROR_INVALID_PROPERTY, + expected); + g_object_unref (expected); +} + +static void +test_wpa_ap_wpa_psk_connection_2 (gconstpointer data) +{ + guint idx = GPOINTER_TO_UINT (data); + NMConnection *expected; + const KeyData exp_wsec[] = { + { NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, "wpa-psk", 0 }, + { NM_SETTING_WIRELESS_SECURITY_AUTH_ALG, "open", 0 }, + { NULL } }; + + expected = nm_simple_connection_new (); + fill_wsec (expected, exp_wsec); + test_ap_wpa_psk_connection_base (NULL, NULL, + NM_802_11_AP_FLAGS_PRIVACY, + wpa_flags_for_idx (idx), + rsn_flags_for_idx (idx), + TRUE, + NM_CONNECTION_ERROR_INVALID_PROPERTY, + expected); + g_object_unref (expected); +} + +static void +test_wpa_ap_wpa_psk_connection_3 (gconstpointer data) +{ + guint idx = GPOINTER_TO_UINT (data); + NMConnection *expected; + const KeyData exp_wsec[] = { + { NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, "wpa-psk", 0 }, + { NM_SETTING_WIRELESS_SECURITY_AUTH_ALG, "open", 0 }, + { NULL } }; + + expected = nm_simple_connection_new (); + fill_wsec (expected, exp_wsec); + test_ap_wpa_psk_connection_base (NULL, "open", + NM_802_11_AP_FLAGS_PRIVACY, + wpa_flags_for_idx (idx), + rsn_flags_for_idx (idx), + FALSE, + NM_CONNECTION_ERROR_INVALID_PROPERTY, + expected); + g_object_unref (expected); +} + +static void +test_wpa_ap_wpa_psk_connection_4 (gconstpointer data) +{ + guint idx = GPOINTER_TO_UINT (data); + test_ap_wpa_psk_connection_base (NULL, "shared", + NM_802_11_AP_FLAGS_PRIVACY, + wpa_flags_for_idx (idx), + rsn_flags_for_idx (idx), + FALSE, + NM_CONNECTION_ERROR_INVALID_PROPERTY, + NULL); +} + +static void +test_wpa_ap_wpa_psk_connection_5 (gconstpointer data) +{ + guint idx = GPOINTER_TO_UINT (data); + NMConnection *expected; + const KeyData exp_wsec[] = { + { NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, "wpa-psk", 0 }, + { NM_SETTING_WIRELESS_SECURITY_AUTH_ALG, "open", 0 }, + { NULL } }; + + expected = nm_simple_connection_new (); + fill_wsec (expected, exp_wsec); + test_ap_wpa_psk_connection_base ("wpa-psk", "open", + NM_802_11_AP_FLAGS_PRIVACY, + wpa_flags_for_idx (idx), + rsn_flags_for_idx (idx), + FALSE, + NM_CONNECTION_ERROR_INVALID_PROPERTY, + expected); + g_object_unref (expected); +} + +/*****************************************************************************/ + +static void +test_strength_dbm (void) +{ + /* boundary conditions first */ + g_assert_cmpint (nm_wifi_utils_level_to_quality (-1), ==, 100); + g_assert_cmpint (nm_wifi_utils_level_to_quality (-40), ==, 100); + g_assert_cmpint (nm_wifi_utils_level_to_quality (-30), ==, 100); + g_assert_cmpint (nm_wifi_utils_level_to_quality (-100), ==, 0); + g_assert_cmpint (nm_wifi_utils_level_to_quality (-200), ==, 0); + + g_assert_cmpint (nm_wifi_utils_level_to_quality (-81), ==, 32); + g_assert_cmpint (nm_wifi_utils_level_to_quality (-92), ==, 14); + g_assert_cmpint (nm_wifi_utils_level_to_quality (-74), ==, 44); + g_assert_cmpint (nm_wifi_utils_level_to_quality (-81), ==, 32); + g_assert_cmpint (nm_wifi_utils_level_to_quality (-66), ==, 57); +} + +static void +test_strength_percent (void) +{ + int i; + + /* boundary conditions first */ + g_assert_cmpint (nm_wifi_utils_level_to_quality (0), ==, 0); + g_assert_cmpint (nm_wifi_utils_level_to_quality (100), ==, 100); + g_assert_cmpint (nm_wifi_utils_level_to_quality (110), ==, 100); + + for (i = 0; i <= 100; i++) + g_assert_cmpint (nm_wifi_utils_level_to_quality (i), ==, i); +} + +static void +test_strength_wext (void) +{ + /* boundary conditions that we assume aren't WEXT first */ + g_assert_cmpint (nm_wifi_utils_level_to_quality (256), ==, 100); + g_assert_cmpint (nm_wifi_utils_level_to_quality (110), ==, 100); + + /* boundary conditions that we assume are WEXT */ + g_assert_cmpint (nm_wifi_utils_level_to_quality (111), ==, 0); + g_assert_cmpint (nm_wifi_utils_level_to_quality (150), ==, 0); + g_assert_cmpint (nm_wifi_utils_level_to_quality (225), ==, 100); + g_assert_cmpint (nm_wifi_utils_level_to_quality (255), ==, 100); + + g_assert_cmpint (nm_wifi_utils_level_to_quality (157), ==, 2); + g_assert_cmpint (nm_wifi_utils_level_to_quality (200), ==, 74); + g_assert_cmpint (nm_wifi_utils_level_to_quality (215), ==, 99); +} + +#define _assert_strength_in_range(x) \ + ({ \ + guint32 _x = (x); \ + g_assert_cmpint (_x, >=, 0); \ + g_assert_cmpint (_x, <=, 100); \ + }) + +static void +test_strength_all (void) +{ + int val; + + for (val = -200; val < 300; val++) + _assert_strength_in_range (nm_wifi_utils_level_to_quality (val)); + _assert_strength_in_range (nm_wifi_utils_level_to_quality (G_MININT)); + _assert_strength_in_range (nm_wifi_utils_level_to_quality (G_MAXINT)); + _assert_strength_in_range (nm_wifi_utils_level_to_quality (G_MININT32)); + _assert_strength_in_range (nm_wifi_utils_level_to_quality (G_MAXINT32)); + _assert_strength_in_range (nm_wifi_utils_level_to_quality (G_MININT16)); + _assert_strength_in_range (nm_wifi_utils_level_to_quality (G_MAXINT16)); +} + +/*****************************************************************************/ + +NMTST_DEFINE (); + +int +main (int argc, char **argv) +{ + gsize i; + + nmtst_init_assert_logging (&argc, &argv, "INFO", "DEFAULT"); + + g_test_add_func ("/wifi/lock_bssid", + test_lock_bssid); + + /* Open AP tests; make sure that connections to be completed that have + * various security-related settings already set cause the completion + * to fail. + */ + g_test_add_func ("/wifi/open_ap/empty_connection", + test_open_ap_empty_connection); + g_test_add_data_func ("/wifi/open_ap/leap_connection/1", + (gconstpointer) TRUE, + test_open_ap_leap_connection_1); + g_test_add_data_func ("/wifi/open_ap/leap_connection/1_no_add_wifi", + (gconstpointer) FALSE, + test_open_ap_leap_connection_1); + g_test_add_func ("/wifi/open_ap/leap_connection/2", + test_open_ap_leap_connection_2); + g_test_add_data_func ("/wifi/open_ap/wep_connection_true", + (gconstpointer) TRUE, + test_open_ap_wep_connection); + g_test_add_data_func ("/wifi/open_ap/wep_connection_false", + (gconstpointer) FALSE, + test_open_ap_wep_connection); + + g_test_add_func ("/wifi/open_ap/wpa_psk_connection/1", + test_open_ap_wpa_psk_connection_1); + g_test_add_func ("/wifi/open_ap/wpa_psk_connection/2", + test_open_ap_wpa_psk_connection_2); + g_test_add_func ("/wifi/open_ap/wpa_psk_connection/3", + test_open_ap_wpa_psk_connection_3); + g_test_add_func ("/wifi/open_ap/wpa_psk_connection/4", + test_open_ap_wpa_psk_connection_4); + g_test_add_func ("/wifi/open_ap/wpa_psk_connection/5", + test_open_ap_wpa_psk_connection_5); + + g_test_add_data_func ("/wifi/open_ap/wpa_eap_connection/1", + (gconstpointer) IDX_OPEN, + test_ap_wpa_eap_connection_1); + g_test_add_data_func ("/wifi/open_ap/wpa_eap_connection/2", + (gconstpointer) IDX_OPEN, + test_ap_wpa_eap_connection_2); + g_test_add_data_func ("/wifi/open_ap/wpa_eap_connection/3", + (gconstpointer) IDX_OPEN, + test_ap_wpa_eap_connection_3); + g_test_add_data_func ("/wifi/open_ap/wpa_eap_connection/4", + (gconstpointer) IDX_OPEN, + test_ap_wpa_eap_connection_4); + g_test_add_data_func ("/wifi/open_ap/wpa_eap_connection/5", + (gconstpointer) IDX_OPEN, + test_ap_wpa_eap_connection_5); + + /* WEP AP tests */ + g_test_add_func ("/wifi/priv_ap/empty_connection", + test_priv_ap_empty_connection); + g_test_add_data_func ("/wifi/priv_ap/leap_connection/1", + (gconstpointer) FALSE, + test_priv_ap_leap_connection_1); + g_test_add_func ("/wifi/priv_ap/leap_connection/2", + test_priv_ap_leap_connection_2); + + g_test_add_func ("/wifi/priv_ap/dynamic_wep/1", + test_priv_ap_dynamic_wep_1); + g_test_add_func ("/wifi/priv_ap/dynamic_wep/2", + test_priv_ap_dynamic_wep_2); + g_test_add_func ("/wifi/priv_ap/dynamic_wep/3", + test_priv_ap_dynamic_wep_3); + + g_test_add_func ("/wifi/priv_ap/wpa_psk_connection/1", + test_priv_ap_wpa_psk_connection_1); + g_test_add_func ("/wifi/priv_ap/wpa_psk_connection/2", + test_priv_ap_wpa_psk_connection_2); + g_test_add_func ("/wifi/priv_ap/wpa_psk_connection/3", + test_priv_ap_wpa_psk_connection_3); + g_test_add_func ("/wifi/priv_ap/wpa_psk_connection/4", + test_priv_ap_wpa_psk_connection_4); + g_test_add_func ("/wifi/priv_ap/wpa_psk_connection/5", + test_priv_ap_wpa_psk_connection_5); + + g_test_add_data_func ("/wifi/priv_ap/wpa_eap_connection/1", + (gconstpointer) IDX_PRIV, + test_ap_wpa_eap_connection_1); + g_test_add_data_func ("/wifi/priv_ap/wpa_eap_connection/2", + (gconstpointer) IDX_PRIV, + test_ap_wpa_eap_connection_2); + g_test_add_data_func ("/wifi/priv_ap/wpa_eap_connection/3", + (gconstpointer) IDX_PRIV, + test_ap_wpa_eap_connection_3); + g_test_add_data_func ("/wifi/priv_ap/wpa_eap_connection/4", + (gconstpointer) IDX_PRIV, + test_ap_wpa_eap_connection_4); + g_test_add_data_func ("/wifi/priv_ap/wpa_eap_connection/5", + (gconstpointer) IDX_PRIV, + test_ap_wpa_eap_connection_5); + +#define ADD_FUNC(func) do { \ + char *name_idx = g_strdup_printf ("/wifi/wpa_psk/" G_STRINGIFY (func) "/%zd", i); \ + g_test_add_data_func (name_idx, (gconstpointer) i, func); \ + g_free (name_idx); \ + } while (0) + + /* WPA-PSK tests */ + for (i = IDX_WPA_PSK_PTKIP_GTKIP; i <= IDX_WPA_RSN_PSK_PCCMP_GCCMP; i++) { + ADD_FUNC(test_wpa_ap_empty_connection); + ADD_FUNC(test_wpa_ap_leap_connection_1); + ADD_FUNC(test_wpa_ap_leap_connection_2); + ADD_FUNC(test_wpa_ap_dynamic_wep_connection); + ADD_FUNC(test_wpa_ap_wpa_psk_connection_1); + ADD_FUNC(test_wpa_ap_wpa_psk_connection_2); + ADD_FUNC(test_wpa_ap_wpa_psk_connection_3); + ADD_FUNC(test_wpa_ap_wpa_psk_connection_4); + ADD_FUNC(test_wpa_ap_wpa_psk_connection_5); + ADD_FUNC(test_ap_wpa_eap_connection_1); + ADD_FUNC(test_ap_wpa_eap_connection_2); + ADD_FUNC(test_ap_wpa_eap_connection_3); + ADD_FUNC(test_ap_wpa_eap_connection_4); + ADD_FUNC(test_ap_wpa_eap_connection_5); + } + +#undef ADD_FUNC +#define ADD_FUNC(func) do { \ + char *name_idx = g_strdup_printf ("/wifi/rsn_psk/" G_STRINGIFY (func) "/%zd", i); \ + g_test_add_data_func (name_idx, (gconstpointer) i, func); \ + g_free (name_idx); \ + } while (0) + + /* RSN-PSK tests */ + for (i = IDX_WPA_RSN_PSK_PTKIP_PCCMP_GTKIP; i <= IDX_RSN_PSK_PTKIP_PCCMP_GTKIP; i++) { + ADD_FUNC(test_wpa_ap_empty_connection); + ADD_FUNC(test_wpa_ap_leap_connection_1); + ADD_FUNC(test_wpa_ap_leap_connection_2); + ADD_FUNC(test_wpa_ap_dynamic_wep_connection); + ADD_FUNC(test_wpa_ap_wpa_psk_connection_1); + ADD_FUNC(test_wpa_ap_wpa_psk_connection_2); + ADD_FUNC(test_wpa_ap_wpa_psk_connection_3); + ADD_FUNC(test_wpa_ap_wpa_psk_connection_4); + ADD_FUNC(test_wpa_ap_wpa_psk_connection_5); + ADD_FUNC(test_ap_wpa_eap_connection_1); + ADD_FUNC(test_ap_wpa_eap_connection_2); + ADD_FUNC(test_ap_wpa_eap_connection_3); + ADD_FUNC(test_ap_wpa_eap_connection_4); + ADD_FUNC(test_ap_wpa_eap_connection_5); + } + +#undef ADD_FUNC + + /* Scanned signal strength conversion tests */ + g_test_add_func ("/wifi/strength/dbm", + test_strength_dbm); + g_test_add_func ("/wifi/strength/percent", + test_strength_percent); + g_test_add_func ("/wifi/strength/wext", + test_strength_wext); + g_test_add_func ("/wifi/strength/all", + test_strength_all); + + return g_test_run (); +} diff --git a/src/devices/wifi/tests/test-general.c b/src/devices/wifi/tests/test-general.c deleted file mode 100644 index e0899837..00000000 --- a/src/devices/wifi/tests/test-general.c +++ /dev/null @@ -1,1519 +0,0 @@ -/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ -/* - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2, or (at your option) - * any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Copyright (C) 2011 Red Hat, Inc. - * - */ - -#include "nm-default.h" - -#include "devices/wifi/nm-wifi-utils.h" -#include "nm-core-internal.h" - -#include "nm-test-utils-core.h" - -#define DEBUG 1 - -/*****************************************************************************/ - -#define COMPARE(src, expected, success, error, edomain, ecode) \ -{ \ - if (expected) { \ - if (!success) { \ - g_assert (error != NULL); \ - g_warning ("Failed to complete connection: %s", error->message); \ - } \ - g_assert (success == TRUE); \ - g_assert (error == NULL); \ -\ - success = nm_connection_compare (src, expected, NM_SETTING_COMPARE_FLAG_EXACT); \ - if (success == FALSE && DEBUG) { \ - g_print ("\n- COMPLETED ---------------------------------\n"); \ - nm_connection_dump (src); \ - g_print ("+ EXPECTED ++++++++++++++++++++++++++++++++++++\n"); \ - nm_connection_dump (expected); \ - g_print ("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n"); \ - } \ - g_assert (success == TRUE); \ - } else { \ - if (success) { \ - g_print ("\n- COMPLETED ---------------------------------\n"); \ - nm_connection_dump (src); \ - } \ - g_assert (success == FALSE); \ - g_assert_error (error, edomain, ecode); \ - } \ - \ - g_clear_error (&error); \ -} - -static gboolean -complete_connection (const char *ssid, - const char *bssid, - NM80211Mode mode, - guint32 flags, - guint32 wpa_flags, - guint32 rsn_flags, - gboolean lock_bssid, - NMConnection *src, - GError **error) -{ - gs_unref_bytes GBytes *ssid_b = NULL; - NMSettingWireless *s_wifi; - - /* Add a wifi setting if one doesn't exist */ - s_wifi = nm_connection_get_setting_wireless (src); - if (!s_wifi) { - s_wifi = (NMSettingWireless *) nm_setting_wireless_new (); - nm_connection_add_setting (src, NM_SETTING (s_wifi)); - } - - ssid_b = g_bytes_new (ssid, strlen (ssid)); - - return nm_wifi_utils_complete_connection (ssid_b, - bssid, - mode, - flags, - wpa_flags, - rsn_flags, - src, - lock_bssid, - error); -} - -typedef struct { - const char *key; - const char *str; - guint32 uint; -} KeyData; - -static void -set_items (NMSetting *setting, const KeyData *items) -{ - const KeyData *item; - GParamSpec *pspec; - GBytes *tmp; - - for (item = items; item && item->key; item++) { - g_assert (item->key); - pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (setting), item->key); - g_assert (pspec); - - if (pspec->value_type == G_TYPE_STRING) { - g_assert (item->uint == 0); - if (item->str) - g_object_set (G_OBJECT (setting), item->key, item->str, NULL); - } else if (pspec->value_type == G_TYPE_UINT) { - g_assert (item->str == NULL); - g_object_set (G_OBJECT (setting), item->key, item->uint, NULL); - } else if (pspec->value_type == G_TYPE_INT) { - int foo = (int) item->uint; - - g_assert (item->str == NULL); - g_object_set (G_OBJECT (setting), item->key, foo, NULL); - } else if (pspec->value_type == G_TYPE_BOOLEAN) { - gboolean foo = !! (item->uint); - - g_assert (item->str == NULL); - g_object_set (G_OBJECT (setting), item->key, foo, NULL); - } else if (pspec->value_type == G_TYPE_BYTES) { - g_assert (item->str); - tmp = g_bytes_new (item->str, strlen (item->str)); - g_object_set (G_OBJECT (setting), item->key, tmp, NULL); - g_bytes_unref (tmp); - } else { - /* Special types, check based on property name */ - if (!strcmp (item->key, NM_SETTING_WIRELESS_SECURITY_PROTO)) - nm_setting_wireless_security_add_proto (NM_SETTING_WIRELESS_SECURITY (setting), item->str); - else if (!strcmp (item->key, NM_SETTING_WIRELESS_SECURITY_PAIRWISE)) - nm_setting_wireless_security_add_pairwise (NM_SETTING_WIRELESS_SECURITY (setting), item->str); - else if (!strcmp (item->key, NM_SETTING_WIRELESS_SECURITY_GROUP)) - nm_setting_wireless_security_add_group (NM_SETTING_WIRELESS_SECURITY (setting), item->str); - else if (!strcmp (item->key, NM_SETTING_802_1X_EAP)) - nm_setting_802_1x_add_eap_method (NM_SETTING_802_1X (setting), item->str); - } - } -} - -static NMSettingWireless * -fill_wifi_empty (NMConnection *connection) -{ - NMSettingWireless *s_wifi; - - s_wifi = nm_connection_get_setting_wireless (connection); - if (!s_wifi) { - s_wifi = (NMSettingWireless *) nm_setting_wireless_new (); - nm_connection_add_setting (connection, NM_SETTING (s_wifi)); - } - return s_wifi; -} - -static NMSettingWireless * -fill_wifi (NMConnection *connection, const KeyData items[]) -{ - NMSettingWireless *s_wifi; - - s_wifi = nm_connection_get_setting_wireless (connection); - if (!s_wifi) { - s_wifi = (NMSettingWireless *) nm_setting_wireless_new (); - nm_connection_add_setting (connection, NM_SETTING (s_wifi)); - } - - set_items (NM_SETTING (s_wifi), items); - return s_wifi; -} - -static NMSettingWirelessSecurity * -fill_wsec (NMConnection *connection, const KeyData items[]) -{ - NMSettingWirelessSecurity *s_wsec; - - s_wsec = nm_connection_get_setting_wireless_security (connection); - if (!s_wsec) { - s_wsec = (NMSettingWirelessSecurity *) nm_setting_wireless_security_new (); - nm_connection_add_setting (connection, NM_SETTING (s_wsec)); - } - - set_items (NM_SETTING (s_wsec), items); - return s_wsec; -} - -static NMSetting8021x * -fill_8021x (NMConnection *connection, const KeyData items[]) -{ - NMSetting8021x *s_8021x; - - s_8021x = nm_connection_get_setting_802_1x (connection); - if (!s_8021x) { - s_8021x = (NMSetting8021x *) nm_setting_802_1x_new (); - nm_connection_add_setting (connection, NM_SETTING (s_8021x)); - } - - set_items (NM_SETTING (s_8021x), items); - return s_8021x; -} - -static NMConnection * -create_basic (const char *ssid, - const char *bssid, - NM80211Mode mode) -{ - NMConnection *connection; - NMSettingWireless *s_wifi = NULL; - GBytes *tmp; - - connection = nm_simple_connection_new (); - - s_wifi = (NMSettingWireless *) nm_setting_wireless_new (); - nm_connection_add_setting (connection, NM_SETTING (s_wifi)); - - /* SSID */ - tmp = g_bytes_new (ssid, strlen (ssid)); - g_object_set (G_OBJECT (s_wifi), NM_SETTING_WIRELESS_SSID, tmp, NULL); - g_bytes_unref (tmp); - - /* BSSID */ - if (bssid) - g_object_set (G_OBJECT (s_wifi), NM_SETTING_WIRELESS_BSSID, bssid, NULL); - - if (mode == NM_802_11_MODE_INFRA) - g_object_set (G_OBJECT (s_wifi), NM_SETTING_WIRELESS_MODE, "infrastructure", NULL); - else if (mode == NM_802_11_MODE_ADHOC) - g_object_set (G_OBJECT (s_wifi), NM_SETTING_WIRELESS_MODE, "adhoc", NULL); - else - g_assert_not_reached (); - - return connection; -} - -/*****************************************************************************/ - -static void -test_lock_bssid (void) -{ - NMConnection *src, *expected; - const char *bssid = "01:02:03:04:05:06"; - const char *ssid = "blahblah"; - gboolean success; - GError *error = NULL; - - src = nm_simple_connection_new (); - success = complete_connection (ssid, bssid, - NM_802_11_MODE_INFRA, NM_802_11_AP_FLAGS_NONE, - NM_802_11_AP_SEC_NONE, NM_802_11_AP_SEC_NONE, - TRUE, - src, &error); - expected = create_basic (ssid, bssid, NM_802_11_MODE_INFRA); - COMPARE (src, expected, success, error, 0, 0); - - g_object_unref (src); - g_object_unref (expected); -} - -/*****************************************************************************/ - -static void -test_open_ap_empty_connection (void) -{ - NMConnection *src, *expected; - const char *bssid = "01:02:03:04:05:06"; - const char *ssid = "blahblah"; - gboolean success; - GError *error = NULL; - - /* Test that an empty source connection is correctly filled with the - * SSID and Infra modes of the given AP details. - */ - - src = nm_simple_connection_new (); - success = complete_connection (ssid, bssid, - NM_802_11_MODE_INFRA, NM_802_11_AP_FLAGS_NONE, - NM_802_11_AP_SEC_NONE, NM_802_11_AP_SEC_NONE, - FALSE, - src, &error); - expected = create_basic (ssid, NULL, NM_802_11_MODE_INFRA); - COMPARE (src, expected, success, error, 0, 0); - - g_object_unref (src); - g_object_unref (expected); -} - -/*****************************************************************************/ - -static void -test_open_ap_leap_connection_1 (gconstpointer add_wifi) -{ - NMConnection *src; - const char *bssid = "01:02:03:04:05:06"; - const KeyData src_wsec[] = { { NM_SETTING_WIRELESS_SECURITY_LEAP_USERNAME, "Bill Smith", 0 }, { NULL } }; - gboolean success; - GError *error = NULL; - - /* Test that a basic connection filled with a LEAP username is - * rejected when completion is attempted with an open AP. LEAP requires - * the AP to have the Privacy bit set. - */ - - src = nm_simple_connection_new (); - if (add_wifi) - fill_wifi_empty (src); - fill_wsec (src, src_wsec); - - success = complete_connection ("blahblah", bssid, - NM_802_11_MODE_INFRA, NM_802_11_AP_FLAGS_NONE, - NM_802_11_AP_SEC_NONE, NM_802_11_AP_SEC_NONE, - FALSE, - src, &error); - /* We expect failure */ - COMPARE (src, NULL, success, error, NM_CONNECTION_ERROR, NM_CONNECTION_ERROR_INVALID_SETTING); - - g_object_unref (src); -} - -/*****************************************************************************/ - -static void -test_open_ap_leap_connection_2 (void) -{ - NMConnection *src; - const char *bssid = "01:02:03:04:05:06"; - const KeyData src_wsec[] = { { NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, "ieee8021x", 0 }, { NULL } }; - gboolean success; - GError *error = NULL; - - /* Test that a basic connection specifying IEEE8021x security (ie, Dynamic - * WEP or LEAP) is rejected when completion is attempted with an open AP. - */ - - src = nm_simple_connection_new (); - fill_wifi_empty (src); - fill_wsec (src, src_wsec); - - success = complete_connection ("blahblah", bssid, - NM_802_11_MODE_INFRA, NM_802_11_AP_FLAGS_NONE, - NM_802_11_AP_SEC_NONE, NM_802_11_AP_SEC_NONE, - FALSE, - src, &error); - /* We expect failure */ - COMPARE (src, NULL, success, error, NM_CONNECTION_ERROR, NM_CONNECTION_ERROR_INVALID_SETTING); - - g_object_unref (src); -} - -/*****************************************************************************/ - -static void -test_open_ap_wep_connection (gconstpointer add_wifi) -{ - NMConnection *src; - const char *bssid = "01:02:03:04:05:06"; - const KeyData src_wsec[] = { - { NM_SETTING_WIRELESS_SECURITY_WEP_KEY0, "11111111111111111111111111", 0 }, - { NM_SETTING_WIRELESS_SECURITY_WEP_TX_KEYIDX, NULL, 0 }, - { NM_SETTING_WIRELESS_SECURITY_AUTH_ALG, "open", 0 }, - { NULL } }; - gboolean success; - GError *error = NULL; - - /* Test that a static WEP connection is rejected when completion is - * attempted with an open AP. - */ - - src = nm_simple_connection_new (); - if (add_wifi) - fill_wifi_empty (src); - fill_wsec (src, src_wsec); - success = complete_connection ("blahblah", bssid, - NM_802_11_MODE_INFRA, NM_802_11_AP_FLAGS_NONE, - NM_802_11_AP_SEC_NONE, NM_802_11_AP_SEC_NONE, - FALSE, - src, &error); - /* We expect failure */ - COMPARE (src, NULL, success, error, NM_CONNECTION_ERROR, NM_CONNECTION_ERROR_INVALID_SETTING); - - g_object_unref (src); -} - -/*****************************************************************************/ - -static void -test_ap_wpa_psk_connection_base (const char *key_mgmt, - const char *auth_alg, - guint32 flags, - guint32 wpa_flags, - guint32 rsn_flags, - gboolean add_wifi, - guint error_code, - NMConnection *expected) -{ - NMConnection *src; - const char *ssid = "blahblah"; - const char *bssid = "01:02:03:04:05:06"; - const KeyData exp_wifi[] = { - { NM_SETTING_WIRELESS_SSID, ssid, 0 }, - { NM_SETTING_WIRELESS_MODE, "infrastructure", 0 }, - { NULL } }; - const KeyData both_wsec[] = { - { NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, key_mgmt, 0 }, - { NM_SETTING_WIRELESS_SECURITY_AUTH_ALG, auth_alg, 0 }, - { NM_SETTING_WIRELESS_SECURITY_PSK, "asdfasdfasdfasdfasdfafs", 0 }, - { NULL } }; - gboolean success; - GError *error = NULL; - - src = nm_simple_connection_new (); - if (add_wifi) - fill_wifi_empty (src); - fill_wsec (src, both_wsec); - success = complete_connection (ssid, bssid, NM_802_11_MODE_INFRA, - flags, wpa_flags, rsn_flags, - FALSE, src, &error); - if (expected) { - fill_wifi (expected, exp_wifi); - fill_wsec (expected, both_wsec); - } - COMPARE (src, expected, success, error, NM_CONNECTION_ERROR, error_code); - - g_object_unref (src); -} - -static void -test_open_ap_wpa_psk_connection_1 (void) -{ - /* Test that a WPA-PSK connection filling only the PSK itself and *not* - * filling the wifi setting is rejected when completion is attempted with - * an open AP. - */ - test_ap_wpa_psk_connection_base (NULL, NULL, - NM_802_11_AP_FLAGS_NONE, - NM_802_11_AP_SEC_NONE, - NM_802_11_AP_SEC_NONE, - FALSE, - NM_CONNECTION_ERROR_INVALID_SETTING, - NULL); -} - -static void -test_open_ap_wpa_psk_connection_2 (void) -{ - /* Test that a WPA-PSK connection filling only the PSK itself and also - * filling the wifi setting is rejected when completion is attempted with - * an open AP. - */ - test_ap_wpa_psk_connection_base (NULL, NULL, - NM_802_11_AP_FLAGS_NONE, - NM_802_11_AP_SEC_NONE, - NM_802_11_AP_SEC_NONE, - TRUE, - NM_CONNECTION_ERROR_INVALID_SETTING, - NULL); -} - -static void -test_open_ap_wpa_psk_connection_3 (void) -{ - /* Test that a WPA-PSK connection filling the PSK and setting the auth alg - * to 'open' is rejected when completion is attempted with an open AP. - */ - test_ap_wpa_psk_connection_base (NULL, "open", - NM_802_11_AP_FLAGS_NONE, - NM_802_11_AP_SEC_NONE, - NM_802_11_AP_SEC_NONE, - FALSE, - NM_CONNECTION_ERROR_INVALID_SETTING, - NULL); -} - -static void -test_open_ap_wpa_psk_connection_4 (void) -{ - /* Test that a WPA-PSK connection filling the PSK and setting the auth alg - * to 'shared' is rejected when completion is attempted with an open AP. - * Shared auth cannot be used with WPA. - */ - test_ap_wpa_psk_connection_base (NULL, "shared", - NM_802_11_AP_FLAGS_NONE, - NM_802_11_AP_SEC_NONE, - NM_802_11_AP_SEC_NONE, - FALSE, - NM_CONNECTION_ERROR_INVALID_SETTING, - NULL); -} - -static void -test_open_ap_wpa_psk_connection_5 (void) -{ - /* Test that a WPA-PSK connection filling the PSK, the auth algorithm, and - * key management is rejected when completion is attempted with an open AP. - */ - test_ap_wpa_psk_connection_base ("wpa-psk", "open", - NM_802_11_AP_FLAGS_NONE, - NM_802_11_AP_SEC_NONE, - NM_802_11_AP_SEC_NONE, - FALSE, - NM_CONNECTION_ERROR_INVALID_SETTING, - NULL); -} - -/*****************************************************************************/ - -static void -test_ap_wpa_eap_connection_base (const char *key_mgmt, - const char *auth_alg, - guint32 flags, - guint32 wpa_flags, - guint32 rsn_flags, - gboolean add_wifi, - guint error_code) -{ - NMConnection *src; - const char *bssid = "01:02:03:04:05:06"; - const KeyData src_empty[] = { { NULL } }; - const KeyData src_wsec[] = { - { NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, key_mgmt, 0 }, - { NM_SETTING_WIRELESS_SECURITY_AUTH_ALG, auth_alg, 0 }, - { NULL } }; - gboolean success; - GError *error = NULL; - - src = nm_simple_connection_new (); - if (add_wifi) - fill_wifi_empty (src); - fill_wsec (src, src_wsec); - fill_8021x (src, src_empty); - success = complete_connection ("blahblah", bssid, NM_802_11_MODE_INFRA, - flags, wpa_flags, rsn_flags, - FALSE, src, &error); - /* Failure expected */ - COMPARE (src, NULL, success, error, NM_CONNECTION_ERROR, error_code); - - g_object_unref (src); -} - -enum { - IDX_NONE = 0, - IDX_OPEN, - IDX_PRIV, - IDX_WPA_PSK_PTKIP_GTKIP, - IDX_WPA_PSK_PTKIP_PCCMP_GTKIP, - IDX_WPA_RSN_PSK_PTKIP_PCCMP_GTKIP, - IDX_WPA_RSN_PSK_PCCMP_GCCMP, - IDX_RSN_PSK_PCCMP_GCCMP, - IDX_RSN_PSK_PTKIP_PCCMP_GTKIP, - IDX_WPA_8021X, - IDX_RSN_8021X, -}; - -static guint32 -flags_for_idx (guint32 idx) -{ - if (idx == IDX_OPEN) - return NM_802_11_AP_FLAGS_NONE; - else if ( idx == IDX_PRIV - || idx == IDX_WPA_PSK_PTKIP_GTKIP - || idx == IDX_WPA_PSK_PTKIP_PCCMP_GTKIP - || idx == IDX_RSN_PSK_PCCMP_GCCMP - || idx == IDX_RSN_PSK_PTKIP_PCCMP_GTKIP - || idx == IDX_WPA_RSN_PSK_PTKIP_PCCMP_GTKIP - || idx == IDX_WPA_RSN_PSK_PCCMP_GCCMP - || idx == IDX_WPA_8021X - || idx == IDX_RSN_8021X) - return NM_802_11_AP_FLAGS_PRIVACY; - else - g_assert_not_reached (); -} - -static guint32 -wpa_flags_for_idx (guint32 idx) -{ - if (idx == IDX_OPEN || idx == IDX_PRIV || idx == IDX_RSN_8021X - || idx == IDX_RSN_PSK_PCCMP_GCCMP || idx == IDX_RSN_PSK_PTKIP_PCCMP_GTKIP) - return NM_802_11_AP_SEC_NONE; - else if (idx == IDX_WPA_PSK_PTKIP_GTKIP) - return NM_802_11_AP_SEC_PAIR_TKIP | NM_802_11_AP_SEC_GROUP_TKIP | NM_802_11_AP_SEC_KEY_MGMT_PSK; - else if (idx == IDX_WPA_RSN_PSK_PTKIP_PCCMP_GTKIP) - return NM_802_11_AP_SEC_PAIR_TKIP | NM_802_11_AP_SEC_PAIR_CCMP | NM_802_11_AP_SEC_GROUP_TKIP | NM_802_11_AP_SEC_KEY_MGMT_PSK; - else if (NM_IN_SET (idx, IDX_WPA_PSK_PTKIP_PCCMP_GTKIP, IDX_WPA_RSN_PSK_PCCMP_GCCMP)) - return NM_802_11_AP_SEC_PAIR_CCMP | NM_802_11_AP_SEC_GROUP_CCMP | NM_802_11_AP_SEC_KEY_MGMT_PSK; - else if (idx == IDX_WPA_8021X) - return NM_802_11_AP_SEC_PAIR_TKIP | NM_802_11_AP_SEC_GROUP_TKIP | NM_802_11_AP_SEC_KEY_MGMT_802_1X; - else - g_assert_not_reached (); -} - -static guint32 -rsn_flags_for_idx (guint32 idx) -{ - if (idx == IDX_OPEN || idx == IDX_PRIV || idx == IDX_WPA_8021X - || idx == IDX_WPA_PSK_PTKIP_GTKIP || idx == IDX_WPA_PSK_PTKIP_PCCMP_GTKIP) - return NM_802_11_AP_SEC_NONE; - else if (idx == IDX_RSN_PSK_PCCMP_GCCMP) - return NM_802_11_AP_SEC_PAIR_CCMP | NM_802_11_AP_SEC_GROUP_CCMP | NM_802_11_AP_SEC_KEY_MGMT_PSK; - else if (idx == IDX_RSN_PSK_PTKIP_PCCMP_GTKIP) - return NM_802_11_AP_SEC_PAIR_TKIP | NM_802_11_AP_SEC_PAIR_CCMP | NM_802_11_AP_SEC_GROUP_TKIP | NM_802_11_AP_SEC_KEY_MGMT_PSK; - else if (idx == IDX_WPA_RSN_PSK_PTKIP_PCCMP_GTKIP) - return NM_802_11_AP_SEC_PAIR_TKIP | NM_802_11_AP_SEC_PAIR_CCMP | NM_802_11_AP_SEC_GROUP_TKIP | NM_802_11_AP_SEC_KEY_MGMT_PSK; - else if (idx == IDX_WPA_RSN_PSK_PCCMP_GCCMP) - return NM_802_11_AP_SEC_PAIR_CCMP | NM_802_11_AP_SEC_GROUP_CCMP | NM_802_11_AP_SEC_KEY_MGMT_PSK; - else if (idx == IDX_RSN_8021X) - return NM_802_11_AP_SEC_PAIR_CCMP | NM_802_11_AP_SEC_GROUP_CCMP | NM_802_11_AP_SEC_KEY_MGMT_802_1X; - else - g_assert_not_reached (); -} - -static guint32 -error_code_for_idx (guint32 idx, guint num) -{ - if (idx == IDX_OPEN) - return NM_CONNECTION_ERROR_INVALID_SETTING; - else if (idx == IDX_PRIV) { - if (num <= 3) - return NM_CONNECTION_ERROR_MISSING_PROPERTY; - else - return NM_CONNECTION_ERROR_INVALID_PROPERTY; - } else if ( idx == IDX_WPA_PSK_PTKIP_GTKIP || idx == IDX_WPA_PSK_PTKIP_PCCMP_GTKIP - || idx == IDX_WPA_RSN_PSK_PCCMP_GCCMP || idx == IDX_WPA_RSN_PSK_PTKIP_PCCMP_GTKIP - || idx == IDX_RSN_PSK_PTKIP_PCCMP_GTKIP || idx == IDX_RSN_PSK_PCCMP_GCCMP) - if (num == 4) - return NM_CONNECTION_ERROR_INVALID_PROPERTY; - else - return NM_CONNECTION_ERROR_INVALID_SETTING; - else - g_assert_not_reached (); -} - -static void -test_ap_wpa_eap_connection_1 (gconstpointer data) -{ - guint idx = GPOINTER_TO_UINT (data); - - test_ap_wpa_eap_connection_base (NULL, NULL, - flags_for_idx (idx), - wpa_flags_for_idx (idx), - rsn_flags_for_idx (idx), - FALSE, - error_code_for_idx (idx, 1)); -} - -static void -test_ap_wpa_eap_connection_2 (gconstpointer data) -{ - guint idx = GPOINTER_TO_UINT (data); - - test_ap_wpa_eap_connection_base (NULL, NULL, - flags_for_idx (idx), - wpa_flags_for_idx (idx), - rsn_flags_for_idx (idx), - TRUE, - error_code_for_idx (idx, 2)); -} - -static void -test_ap_wpa_eap_connection_3 (gconstpointer data) -{ - guint idx = GPOINTER_TO_UINT (data); - - test_ap_wpa_eap_connection_base (NULL, "open", - flags_for_idx (idx), - wpa_flags_for_idx (idx), - rsn_flags_for_idx (idx), - FALSE, - error_code_for_idx (idx, 3)); -} - -static void -test_ap_wpa_eap_connection_4 (gconstpointer data) -{ - guint idx = GPOINTER_TO_UINT (data); - - test_ap_wpa_eap_connection_base (NULL, "shared", - flags_for_idx (idx), - wpa_flags_for_idx (idx), - rsn_flags_for_idx (idx), - FALSE, - error_code_for_idx (idx, 4)); -} - -static void -test_ap_wpa_eap_connection_5 (gconstpointer data) -{ - guint idx = GPOINTER_TO_UINT (data); - - test_ap_wpa_eap_connection_base ("wpa-eap", "open", - flags_for_idx (idx), - wpa_flags_for_idx (idx), - rsn_flags_for_idx (idx), - FALSE, - error_code_for_idx (idx, 5)); -} - -/*****************************************************************************/ - -static void -test_priv_ap_empty_connection (void) -{ - NMConnection *src, *expected; - const char *bssid = "01:02:03:04:05:06"; - const char *ssid = "blahblah"; - const KeyData exp_wsec[] = { - { NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, "none", 0 }, - { NULL } }; - gboolean success; - GError *error = NULL; - - /* Test that an empty connection is completed to a valid Static WEP - * connection when completed with an AP with the Privacy bit set. - */ - - src = nm_simple_connection_new (); - success = complete_connection (ssid, bssid, - NM_802_11_MODE_INFRA, NM_802_11_AP_FLAGS_PRIVACY, - NM_802_11_AP_SEC_NONE, NM_802_11_AP_SEC_NONE, - FALSE, - src, &error); - - /* Static WEP connection expected */ - expected = create_basic (ssid, NULL, NM_802_11_MODE_INFRA); - fill_wsec (expected, exp_wsec); - COMPARE (src, expected, success, error, 0, 0); - - g_object_unref (src); - g_object_unref (expected); -} - -/*****************************************************************************/ - -static void -test_priv_ap_leap_connection_1 (gconstpointer add_wifi) -{ - NMConnection *src, *expected; - const char *ssid = "blahblah"; - const char *bssid = "01:02:03:04:05:06"; - const char *leap_username = "Bill Smith"; - const KeyData src_wsec[] = { - { NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, "ieee8021x", 0 }, - { NM_SETTING_WIRELESS_SECURITY_LEAP_USERNAME, leap_username, 0 }, - { NULL } }; - const KeyData exp_wsec[] = { - { NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, "ieee8021x", 0 }, - { NM_SETTING_WIRELESS_SECURITY_AUTH_ALG, "leap", 0 }, - { NM_SETTING_WIRELESS_SECURITY_LEAP_USERNAME, leap_username, 0 }, - { NULL } }; - gboolean success; - GError *error = NULL; - - /* Test that an minimal LEAP connection specifying only key management and - * the LEAP username is completed to a full LEAP connection when completed - * with an AP with the Privacy bit set. - */ - - src = nm_simple_connection_new (); - if (add_wifi) - fill_wifi_empty (src); - fill_wsec (src, src_wsec); - success = complete_connection (ssid, bssid, - NM_802_11_MODE_INFRA, NM_802_11_AP_FLAGS_PRIVACY, - NM_802_11_AP_SEC_NONE, NM_802_11_AP_SEC_NONE, - FALSE, - src, &error); - /* We expect success here; since LEAP APs just set the 'privacy' flag - * there's no way to determine from the AP's beacon whether it's static WEP, - * dynamic WEP, or LEAP. - */ - expected = create_basic (ssid, NULL, NM_802_11_MODE_INFRA); - fill_wsec (expected, exp_wsec); - COMPARE (src, expected, success, error, 0, 0); - - g_object_unref (src); - g_object_unref (expected); -} - -/*****************************************************************************/ - -static void -test_priv_ap_leap_connection_2 (void) -{ - NMConnection *src; - const char *bssid = "01:02:03:04:05:06"; - const KeyData src_wsec[] = { - { NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, "ieee8021x", 0 }, - { NM_SETTING_WIRELESS_SECURITY_AUTH_ALG, "leap", 0 }, - { NULL } }; - gboolean success; - GError *error = NULL; - - /* Test that an minimal LEAP connection specifying only key management and - * the LEAP auth alg is completed to a full LEAP connection when completed - * with an AP with the Privacy bit set. - */ - - src = nm_simple_connection_new (); - fill_wifi_empty (src); - fill_wsec (src, src_wsec); - success = complete_connection ("blahblah", bssid, - NM_802_11_MODE_INFRA, NM_802_11_AP_FLAGS_PRIVACY, - NM_802_11_AP_SEC_NONE, NM_802_11_AP_SEC_NONE, - FALSE, - src, &error); - /* We expect failure here, we need a LEAP username */ - COMPARE (src, NULL, success, error, NM_CONNECTION_ERROR, NM_CONNECTION_ERROR_MISSING_PROPERTY); - - g_object_unref (src); -} - -/*****************************************************************************/ - -static void -test_priv_ap_dynamic_wep_1 (void) -{ - NMConnection *src, *expected; - const char *ssid = "blahblah"; - const char *bssid = "01:02:03:04:05:06"; - const KeyData src_wsec[] = { - { NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, "ieee8021x", 0 }, - { NM_SETTING_WIRELESS_SECURITY_AUTH_ALG, "open", 0 }, - { NULL } }; - const KeyData both_8021x[] = { - { NM_SETTING_802_1X_EAP, "peap", 0 }, - { NM_SETTING_802_1X_IDENTITY, "Bill Smith", 0 }, - { NM_SETTING_802_1X_PHASE2_AUTH, "mschapv2", 0 }, - { NULL } }; - const KeyData exp_wsec[] = { - { NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, "ieee8021x", 0 }, - { NM_SETTING_WIRELESS_SECURITY_AUTH_ALG, "open", 0 }, - { NULL } }; - gboolean success; - GError *error = NULL; - - /* Test that an minimal Dynamic WEP connection specifying key management, - * the auth algorithm, and valid 802.1x setting is completed to a valid - * Dynamic WEP connection when completed with an AP with the Privacy bit set. - */ - - src = nm_simple_connection_new (); - fill_wifi_empty (src); - fill_wsec (src, src_wsec); - fill_8021x (src, both_8021x); - success = complete_connection (ssid, bssid, - NM_802_11_MODE_INFRA, NM_802_11_AP_FLAGS_PRIVACY, - NM_802_11_AP_SEC_NONE, NM_802_11_AP_SEC_NONE, - FALSE, - src, &error); - - /* We expect a completed Dynamic WEP connection */ - expected = create_basic (ssid, NULL, NM_802_11_MODE_INFRA); - fill_wsec (expected, exp_wsec); - fill_8021x (expected, both_8021x); - COMPARE (src, expected, success, error, 0, 0); - - g_object_unref (src); - g_object_unref (expected); -} - -/*****************************************************************************/ - -static void -test_priv_ap_dynamic_wep_2 (void) -{ - NMConnection *src, *expected; - const char *ssid = "blahblah"; - const char *bssid = "01:02:03:04:05:06"; - const KeyData src_wsec[] = { - { NM_SETTING_WIRELESS_SECURITY_AUTH_ALG, "open", 0 }, - { NULL } }; - const KeyData both_8021x[] = { - { NM_SETTING_802_1X_EAP, "peap", 0 }, - { NM_SETTING_802_1X_IDENTITY, "Bill Smith", 0 }, - { NM_SETTING_802_1X_PHASE2_AUTH, "mschapv2", 0 }, - { NULL } }; - const KeyData exp_wsec[] = { - { NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, "ieee8021x", 0 }, - { NM_SETTING_WIRELESS_SECURITY_AUTH_ALG, "open", 0 }, - { NULL } }; - gboolean success; - GError *error = NULL; - - /* Test that an minimal Dynamic WEP connection specifying only the auth - * algorithm and a valid 802.1x setting is completed to a valid Dynamic - * WEP connection when completed with an AP with the Privacy bit set. - */ - - src = nm_simple_connection_new (); - fill_wifi_empty (src); - fill_wsec (src, src_wsec); - fill_8021x (src, both_8021x); - success = complete_connection (ssid, bssid, - NM_802_11_MODE_INFRA, NM_802_11_AP_FLAGS_PRIVACY, - NM_802_11_AP_SEC_NONE, NM_802_11_AP_SEC_NONE, - FALSE, - src, &error); - - /* We expect a completed Dynamic WEP connection */ - expected = create_basic (ssid, NULL, NM_802_11_MODE_INFRA); - fill_wsec (expected, exp_wsec); - fill_8021x (expected, both_8021x); - COMPARE (src, expected, success, error, 0, 0); - - g_object_unref (src); - g_object_unref (expected); -} - -/*****************************************************************************/ - -static void -test_priv_ap_dynamic_wep_3 (void) -{ - NMConnection *src; - const char *bssid = "01:02:03:04:05:06"; - const KeyData src_wsec[] = { - { NM_SETTING_WIRELESS_SECURITY_AUTH_ALG, "shared", 0 }, - { NULL } }; - const KeyData src_8021x[] = { - { NM_SETTING_802_1X_EAP, "peap", 0 }, - { NM_SETTING_802_1X_IDENTITY, "Bill Smith", 0 }, - { NM_SETTING_802_1X_PHASE2_AUTH, "mschapv2", 0 }, - { NULL } }; - gboolean success; - GError *error = NULL; - - /* Ensure that a basic connection specifying 'shared' auth and an 802.1x - * setting is rejected, as 802.1x is incompatible with 'shared' auth. - */ - - src = nm_simple_connection_new (); - fill_wifi_empty (src); - fill_wsec (src, src_wsec); - fill_8021x (src, src_8021x); - success = complete_connection ("blahblah", bssid, - NM_802_11_MODE_INFRA, NM_802_11_AP_FLAGS_PRIVACY, - NM_802_11_AP_SEC_NONE, NM_802_11_AP_SEC_NONE, - FALSE, - src, &error); - /* Expect failure; shared is not compatible with dynamic WEP */ - COMPARE (src, NULL, success, error, NM_CONNECTION_ERROR, NM_CONNECTION_ERROR_INVALID_PROPERTY); - - g_object_unref (src); -} - -/*****************************************************************************/ - -static void -test_priv_ap_wpa_psk_connection_1 (void) -{ - /* Test that a basic WPA-PSK connection is rejected when completion is - * attempted with an AP with just the Privacy bit set. Lack of WPA/RSN - * flags means the AP provides Static/Dynamic WEP or LEAP, not WPA. - */ - test_ap_wpa_psk_connection_base (NULL, NULL, - NM_802_11_AP_FLAGS_PRIVACY, - NM_802_11_AP_SEC_NONE, - NM_802_11_AP_SEC_NONE, - FALSE, - NM_CONNECTION_ERROR_INVALID_PROPERTY, - NULL); -} - -static void -test_priv_ap_wpa_psk_connection_2 (void) -{ - /* Test that a basic WPA-PSK connection is rejected when completion is - * attempted with an AP with just the Privacy bit set. Lack of WPA/RSN - * flags means the AP provides Static/Dynamic WEP or LEAP, not WPA. - */ - test_ap_wpa_psk_connection_base (NULL, NULL, - NM_802_11_AP_FLAGS_PRIVACY, - NM_802_11_AP_SEC_NONE, - NM_802_11_AP_SEC_NONE, - TRUE, - NM_CONNECTION_ERROR_INVALID_PROPERTY, - NULL); -} - -static void -test_priv_ap_wpa_psk_connection_3 (void) -{ - /* Test that a basic WPA-PSK connection specifying only the auth algorithm - * is rejected when completion is attempted with an AP with just the Privacy - * bit set. Lack of WPA/RSN flags means the AP provides Static/Dynamic WEP - * or LEAP, not WPA. - */ - test_ap_wpa_psk_connection_base (NULL, "open", - NM_802_11_AP_FLAGS_PRIVACY, - NM_802_11_AP_SEC_NONE, - NM_802_11_AP_SEC_NONE, - FALSE, - NM_CONNECTION_ERROR_INVALID_PROPERTY, - NULL); -} - -static void -test_priv_ap_wpa_psk_connection_4 (void) -{ - /* Test that a basic WPA-PSK connection specifying only the auth algorithm - * is rejected when completion is attempted with an AP with just the Privacy - * bit set. Lack of WPA/RSN flags means the AP provides Static/Dynamic WEP - * or LEAP, not WPA. Second, 'shared' auth is incompatible with WPA. - */ - test_ap_wpa_psk_connection_base (NULL, "shared", - NM_802_11_AP_FLAGS_PRIVACY, - NM_802_11_AP_SEC_NONE, - NM_802_11_AP_SEC_NONE, - FALSE, - NM_CONNECTION_ERROR_INVALID_PROPERTY, - NULL); -} - -static void -test_priv_ap_wpa_psk_connection_5 (void) -{ - /* Test that a WPA-PSK connection specifying both the key management and - * auth algorithm is rejected when completion is attempted with an AP with - * just the Privacy bit set. Lack of WPA/RSN flags means the AP provides - * Static/Dynamic WEP or LEAP, not WPA. - */ - test_ap_wpa_psk_connection_base ("wpa-psk", "open", - NM_802_11_AP_FLAGS_PRIVACY, - NM_802_11_AP_SEC_NONE, - NM_802_11_AP_SEC_NONE, - FALSE, - NM_CONNECTION_ERROR_INVALID_PROPERTY, - NULL); -} - -/*****************************************************************************/ - -static void -test_wpa_ap_empty_connection (gconstpointer data) -{ - guint idx = GPOINTER_TO_UINT (data); - NMConnection *src, *expected; - const char *bssid = "01:02:03:04:05:06"; - const char *ssid = "blahblah"; - const KeyData exp_wsec[] = { - { NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, "wpa-psk", 0 }, - { NM_SETTING_WIRELESS_SECURITY_AUTH_ALG, "open", 0 }, - { NULL } }; - gboolean success; - GError *error = NULL; - - /* Test that a basic WPA-PSK connection specifying just key management and - * the auth algorithm is completed successfully when given an AP with WPA - * or RSN flags. - */ - - src = nm_simple_connection_new (); - success = complete_connection (ssid, bssid, - NM_802_11_MODE_INFRA, NM_802_11_AP_FLAGS_PRIVACY, - wpa_flags_for_idx (idx), - rsn_flags_for_idx (idx), - FALSE, src, &error); - - /* WPA connection expected */ - expected = create_basic (ssid, NULL, NM_802_11_MODE_INFRA); - fill_wsec (expected, exp_wsec); - COMPARE (src, expected, success, error, 0, 0); - - g_object_unref (src); - g_object_unref (expected); -} - -/*****************************************************************************/ - -static void -test_wpa_ap_leap_connection_1 (gconstpointer data) -{ - guint idx = GPOINTER_TO_UINT (data); - NMConnection *src; - const char *ssid = "blahblah"; - const char *bssid = "01:02:03:04:05:06"; - const char *leap_username = "Bill Smith"; - const KeyData src_wsec[] = { - { NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, "ieee8021x", 0 }, - { NM_SETTING_WIRELESS_SECURITY_LEAP_USERNAME, leap_username, 0 }, - { NULL } }; - gboolean success; - GError *error = NULL; - - /* Test that completion of a LEAP connection with a WPA-enabled AP is - * rejected since WPA APs (usually) do not support LEAP. - */ - - src = nm_simple_connection_new (); - fill_wifi_empty (src); - fill_wsec (src, src_wsec); - success = complete_connection (ssid, bssid, - NM_802_11_MODE_INFRA, NM_802_11_AP_FLAGS_PRIVACY, - wpa_flags_for_idx (idx), - rsn_flags_for_idx (idx), - FALSE, - src, &error); - /* Expect failure here; WPA APs don't support old-school LEAP */ - COMPARE (src, NULL, success, error, NM_CONNECTION_ERROR, NM_CONNECTION_ERROR_INVALID_PROPERTY); - - g_object_unref (src); -} - -/*****************************************************************************/ - -static void -test_wpa_ap_leap_connection_2 (gconstpointer data) -{ - guint idx = GPOINTER_TO_UINT (data); - NMConnection *src; - const char *bssid = "01:02:03:04:05:06"; - const KeyData src_wsec[] = { - { NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, "ieee8021x", 0 }, - { NM_SETTING_WIRELESS_SECURITY_AUTH_ALG, "leap", 0 }, - { NULL } }; - gboolean success; - GError *error = NULL; - - /* Test that completion of a LEAP connection with a WPA-enabled AP is - * rejected since WPA APs (usually) do not support LEAP. - */ - - src = nm_simple_connection_new (); - fill_wifi_empty (src); - fill_wsec (src, src_wsec); - success = complete_connection ("blahblah", bssid, - NM_802_11_MODE_INFRA, NM_802_11_AP_FLAGS_PRIVACY, - wpa_flags_for_idx (idx), - rsn_flags_for_idx (idx), - FALSE, - src, &error); - /* We expect failure here, we need a LEAP username */ - COMPARE (src, NULL, success, error, NM_CONNECTION_ERROR, NM_CONNECTION_ERROR_INVALID_PROPERTY); - - g_object_unref (src); -} - -/*****************************************************************************/ - -static void -test_wpa_ap_dynamic_wep_connection (gconstpointer data) -{ - guint idx = GPOINTER_TO_UINT (data); - NMConnection *src; - const char *bssid = "01:02:03:04:05:06"; - const KeyData src_wsec[] = { - { NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, "ieee8021x", 0 }, - { NULL } }; - gboolean success; - GError *error = NULL; - - /* Test that completion of a Dynamic WEP connection with a WPA-enabled AP is - * rejected since WPA APs (usually) do not support Dynamic WEP. - */ - - src = nm_simple_connection_new (); - fill_wifi_empty (src); - fill_wsec (src, src_wsec); - success = complete_connection ("blahblah", bssid, - NM_802_11_MODE_INFRA, NM_802_11_AP_FLAGS_PRIVACY, - wpa_flags_for_idx (idx), - rsn_flags_for_idx (idx), - FALSE, - src, &error); - /* We expect failure here since Dynamic WEP is incompatible with WPA */ - COMPARE (src, NULL, success, error, NM_CONNECTION_ERROR, NM_CONNECTION_ERROR_INVALID_PROPERTY); - - g_object_unref (src); -} - -/*****************************************************************************/ - -static void -test_wpa_ap_wpa_psk_connection_1 (gconstpointer data) -{ - guint idx = GPOINTER_TO_UINT (data); - NMConnection *expected; - const KeyData exp_wsec[] = { - { NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, "wpa-psk", 0 }, - { NM_SETTING_WIRELESS_SECURITY_AUTH_ALG, "open", 0 }, - { NULL } }; - - expected = nm_simple_connection_new (); - fill_wsec (expected, exp_wsec); - test_ap_wpa_psk_connection_base (NULL, NULL, - NM_802_11_AP_FLAGS_PRIVACY, - wpa_flags_for_idx (idx), - rsn_flags_for_idx (idx), - FALSE, - NM_CONNECTION_ERROR_INVALID_PROPERTY, - expected); - g_object_unref (expected); -} - -static void -test_wpa_ap_wpa_psk_connection_2 (gconstpointer data) -{ - guint idx = GPOINTER_TO_UINT (data); - NMConnection *expected; - const KeyData exp_wsec[] = { - { NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, "wpa-psk", 0 }, - { NM_SETTING_WIRELESS_SECURITY_AUTH_ALG, "open", 0 }, - { NULL } }; - - expected = nm_simple_connection_new (); - fill_wsec (expected, exp_wsec); - test_ap_wpa_psk_connection_base (NULL, NULL, - NM_802_11_AP_FLAGS_PRIVACY, - wpa_flags_for_idx (idx), - rsn_flags_for_idx (idx), - TRUE, - NM_CONNECTION_ERROR_INVALID_PROPERTY, - expected); - g_object_unref (expected); -} - -static void -test_wpa_ap_wpa_psk_connection_3 (gconstpointer data) -{ - guint idx = GPOINTER_TO_UINT (data); - NMConnection *expected; - const KeyData exp_wsec[] = { - { NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, "wpa-psk", 0 }, - { NM_SETTING_WIRELESS_SECURITY_AUTH_ALG, "open", 0 }, - { NULL } }; - - expected = nm_simple_connection_new (); - fill_wsec (expected, exp_wsec); - test_ap_wpa_psk_connection_base (NULL, "open", - NM_802_11_AP_FLAGS_PRIVACY, - wpa_flags_for_idx (idx), - rsn_flags_for_idx (idx), - FALSE, - NM_CONNECTION_ERROR_INVALID_PROPERTY, - expected); - g_object_unref (expected); -} - -static void -test_wpa_ap_wpa_psk_connection_4 (gconstpointer data) -{ - guint idx = GPOINTER_TO_UINT (data); - test_ap_wpa_psk_connection_base (NULL, "shared", - NM_802_11_AP_FLAGS_PRIVACY, - wpa_flags_for_idx (idx), - rsn_flags_for_idx (idx), - FALSE, - NM_CONNECTION_ERROR_INVALID_PROPERTY, - NULL); -} - -static void -test_wpa_ap_wpa_psk_connection_5 (gconstpointer data) -{ - guint idx = GPOINTER_TO_UINT (data); - NMConnection *expected; - const KeyData exp_wsec[] = { - { NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, "wpa-psk", 0 }, - { NM_SETTING_WIRELESS_SECURITY_AUTH_ALG, "open", 0 }, - { NULL } }; - - expected = nm_simple_connection_new (); - fill_wsec (expected, exp_wsec); - test_ap_wpa_psk_connection_base ("wpa-psk", "open", - NM_802_11_AP_FLAGS_PRIVACY, - wpa_flags_for_idx (idx), - rsn_flags_for_idx (idx), - FALSE, - NM_CONNECTION_ERROR_INVALID_PROPERTY, - expected); - g_object_unref (expected); -} - -/*****************************************************************************/ - -static void -test_strength_dbm (void) -{ - /* boundary conditions first */ - g_assert_cmpint (nm_wifi_utils_level_to_quality (-1), ==, 100); - g_assert_cmpint (nm_wifi_utils_level_to_quality (-40), ==, 100); - g_assert_cmpint (nm_wifi_utils_level_to_quality (-30), ==, 100); - g_assert_cmpint (nm_wifi_utils_level_to_quality (-100), ==, 0); - g_assert_cmpint (nm_wifi_utils_level_to_quality (-200), ==, 0); - - g_assert_cmpint (nm_wifi_utils_level_to_quality (-81), ==, 32); - g_assert_cmpint (nm_wifi_utils_level_to_quality (-92), ==, 14); - g_assert_cmpint (nm_wifi_utils_level_to_quality (-74), ==, 44); - g_assert_cmpint (nm_wifi_utils_level_to_quality (-81), ==, 32); - g_assert_cmpint (nm_wifi_utils_level_to_quality (-66), ==, 57); -} - -static void -test_strength_percent (void) -{ - int i; - - /* boundary conditions first */ - g_assert_cmpint (nm_wifi_utils_level_to_quality (0), ==, 0); - g_assert_cmpint (nm_wifi_utils_level_to_quality (100), ==, 100); - g_assert_cmpint (nm_wifi_utils_level_to_quality (110), ==, 100); - - for (i = 0; i <= 100; i++) - g_assert_cmpint (nm_wifi_utils_level_to_quality (i), ==, i); -} - -static void -test_strength_wext (void) -{ - /* boundary conditions that we assume aren't WEXT first */ - g_assert_cmpint (nm_wifi_utils_level_to_quality (256), ==, 100); - g_assert_cmpint (nm_wifi_utils_level_to_quality (110), ==, 100); - - /* boundary conditions that we assume are WEXT */ - g_assert_cmpint (nm_wifi_utils_level_to_quality (111), ==, 0); - g_assert_cmpint (nm_wifi_utils_level_to_quality (150), ==, 0); - g_assert_cmpint (nm_wifi_utils_level_to_quality (225), ==, 100); - g_assert_cmpint (nm_wifi_utils_level_to_quality (255), ==, 100); - - g_assert_cmpint (nm_wifi_utils_level_to_quality (157), ==, 2); - g_assert_cmpint (nm_wifi_utils_level_to_quality (200), ==, 74); - g_assert_cmpint (nm_wifi_utils_level_to_quality (215), ==, 99); -} - -#define _assert_strength_in_range(x) \ - ({ \ - guint32 _x = (x); \ - g_assert_cmpint (_x, >=, 0); \ - g_assert_cmpint (_x, <=, 100); \ - }) - -static void -test_strength_all (void) -{ - int val; - - for (val = -200; val < 300; val++) - _assert_strength_in_range (nm_wifi_utils_level_to_quality (val)); - _assert_strength_in_range (nm_wifi_utils_level_to_quality (G_MININT)); - _assert_strength_in_range (nm_wifi_utils_level_to_quality (G_MAXINT)); - _assert_strength_in_range (nm_wifi_utils_level_to_quality (G_MININT32)); - _assert_strength_in_range (nm_wifi_utils_level_to_quality (G_MAXINT32)); - _assert_strength_in_range (nm_wifi_utils_level_to_quality (G_MININT16)); - _assert_strength_in_range (nm_wifi_utils_level_to_quality (G_MAXINT16)); -} - -/*****************************************************************************/ - -NMTST_DEFINE (); - -int -main (int argc, char **argv) -{ - gsize i; - - nmtst_init_assert_logging (&argc, &argv, "INFO", "DEFAULT"); - - g_test_add_func ("/wifi/lock_bssid", - test_lock_bssid); - - /* Open AP tests; make sure that connections to be completed that have - * various security-related settings already set cause the completion - * to fail. - */ - g_test_add_func ("/wifi/open_ap/empty_connection", - test_open_ap_empty_connection); - g_test_add_data_func ("/wifi/open_ap/leap_connection/1", - (gconstpointer) TRUE, - test_open_ap_leap_connection_1); - g_test_add_data_func ("/wifi/open_ap/leap_connection/1_no_add_wifi", - (gconstpointer) FALSE, - test_open_ap_leap_connection_1); - g_test_add_func ("/wifi/open_ap/leap_connection/2", - test_open_ap_leap_connection_2); - g_test_add_data_func ("/wifi/open_ap/wep_connection_true", - (gconstpointer) TRUE, - test_open_ap_wep_connection); - g_test_add_data_func ("/wifi/open_ap/wep_connection_false", - (gconstpointer) FALSE, - test_open_ap_wep_connection); - - g_test_add_func ("/wifi/open_ap/wpa_psk_connection/1", - test_open_ap_wpa_psk_connection_1); - g_test_add_func ("/wifi/open_ap/wpa_psk_connection/2", - test_open_ap_wpa_psk_connection_2); - g_test_add_func ("/wifi/open_ap/wpa_psk_connection/3", - test_open_ap_wpa_psk_connection_3); - g_test_add_func ("/wifi/open_ap/wpa_psk_connection/4", - test_open_ap_wpa_psk_connection_4); - g_test_add_func ("/wifi/open_ap/wpa_psk_connection/5", - test_open_ap_wpa_psk_connection_5); - - g_test_add_data_func ("/wifi/open_ap/wpa_eap_connection/1", - (gconstpointer) IDX_OPEN, - test_ap_wpa_eap_connection_1); - g_test_add_data_func ("/wifi/open_ap/wpa_eap_connection/2", - (gconstpointer) IDX_OPEN, - test_ap_wpa_eap_connection_2); - g_test_add_data_func ("/wifi/open_ap/wpa_eap_connection/3", - (gconstpointer) IDX_OPEN, - test_ap_wpa_eap_connection_3); - g_test_add_data_func ("/wifi/open_ap/wpa_eap_connection/4", - (gconstpointer) IDX_OPEN, - test_ap_wpa_eap_connection_4); - g_test_add_data_func ("/wifi/open_ap/wpa_eap_connection/5", - (gconstpointer) IDX_OPEN, - test_ap_wpa_eap_connection_5); - - /* WEP AP tests */ - g_test_add_func ("/wifi/priv_ap/empty_connection", - test_priv_ap_empty_connection); - g_test_add_data_func ("/wifi/priv_ap/leap_connection/1", - (gconstpointer) FALSE, - test_priv_ap_leap_connection_1); - g_test_add_func ("/wifi/priv_ap/leap_connection/2", - test_priv_ap_leap_connection_2); - - g_test_add_func ("/wifi/priv_ap/dynamic_wep/1", - test_priv_ap_dynamic_wep_1); - g_test_add_func ("/wifi/priv_ap/dynamic_wep/2", - test_priv_ap_dynamic_wep_2); - g_test_add_func ("/wifi/priv_ap/dynamic_wep/3", - test_priv_ap_dynamic_wep_3); - - g_test_add_func ("/wifi/priv_ap/wpa_psk_connection/1", - test_priv_ap_wpa_psk_connection_1); - g_test_add_func ("/wifi/priv_ap/wpa_psk_connection/2", - test_priv_ap_wpa_psk_connection_2); - g_test_add_func ("/wifi/priv_ap/wpa_psk_connection/3", - test_priv_ap_wpa_psk_connection_3); - g_test_add_func ("/wifi/priv_ap/wpa_psk_connection/4", - test_priv_ap_wpa_psk_connection_4); - g_test_add_func ("/wifi/priv_ap/wpa_psk_connection/5", - test_priv_ap_wpa_psk_connection_5); - - g_test_add_data_func ("/wifi/priv_ap/wpa_eap_connection/1", - (gconstpointer) IDX_PRIV, - test_ap_wpa_eap_connection_1); - g_test_add_data_func ("/wifi/priv_ap/wpa_eap_connection/2", - (gconstpointer) IDX_PRIV, - test_ap_wpa_eap_connection_2); - g_test_add_data_func ("/wifi/priv_ap/wpa_eap_connection/3", - (gconstpointer) IDX_PRIV, - test_ap_wpa_eap_connection_3); - g_test_add_data_func ("/wifi/priv_ap/wpa_eap_connection/4", - (gconstpointer) IDX_PRIV, - test_ap_wpa_eap_connection_4); - g_test_add_data_func ("/wifi/priv_ap/wpa_eap_connection/5", - (gconstpointer) IDX_PRIV, - test_ap_wpa_eap_connection_5); - -#define ADD_FUNC(func) do { \ - char *name_idx = g_strdup_printf ("/wifi/wpa_psk/" G_STRINGIFY (func) "/%zd", i); \ - g_test_add_data_func (name_idx, (gconstpointer) i, func); \ - g_free (name_idx); \ - } while (0) - - /* WPA-PSK tests */ - for (i = IDX_WPA_PSK_PTKIP_GTKIP; i <= IDX_WPA_RSN_PSK_PCCMP_GCCMP; i++) { - ADD_FUNC(test_wpa_ap_empty_connection); - ADD_FUNC(test_wpa_ap_leap_connection_1); - ADD_FUNC(test_wpa_ap_leap_connection_2); - ADD_FUNC(test_wpa_ap_dynamic_wep_connection); - ADD_FUNC(test_wpa_ap_wpa_psk_connection_1); - ADD_FUNC(test_wpa_ap_wpa_psk_connection_2); - ADD_FUNC(test_wpa_ap_wpa_psk_connection_3); - ADD_FUNC(test_wpa_ap_wpa_psk_connection_4); - ADD_FUNC(test_wpa_ap_wpa_psk_connection_5); - ADD_FUNC(test_ap_wpa_eap_connection_1); - ADD_FUNC(test_ap_wpa_eap_connection_2); - ADD_FUNC(test_ap_wpa_eap_connection_3); - ADD_FUNC(test_ap_wpa_eap_connection_4); - ADD_FUNC(test_ap_wpa_eap_connection_5); - } - -#undef ADD_FUNC -#define ADD_FUNC(func) do { \ - char *name_idx = g_strdup_printf ("/wifi/rsn_psk/" G_STRINGIFY (func) "/%zd", i); \ - g_test_add_data_func (name_idx, (gconstpointer) i, func); \ - g_free (name_idx); \ - } while (0) - - /* RSN-PSK tests */ - for (i = IDX_WPA_RSN_PSK_PTKIP_PCCMP_GTKIP; i <= IDX_RSN_PSK_PTKIP_PCCMP_GTKIP; i++) { - ADD_FUNC(test_wpa_ap_empty_connection); - ADD_FUNC(test_wpa_ap_leap_connection_1); - ADD_FUNC(test_wpa_ap_leap_connection_2); - ADD_FUNC(test_wpa_ap_dynamic_wep_connection); - ADD_FUNC(test_wpa_ap_wpa_psk_connection_1); - ADD_FUNC(test_wpa_ap_wpa_psk_connection_2); - ADD_FUNC(test_wpa_ap_wpa_psk_connection_3); - ADD_FUNC(test_wpa_ap_wpa_psk_connection_4); - ADD_FUNC(test_wpa_ap_wpa_psk_connection_5); - ADD_FUNC(test_ap_wpa_eap_connection_1); - ADD_FUNC(test_ap_wpa_eap_connection_2); - ADD_FUNC(test_ap_wpa_eap_connection_3); - ADD_FUNC(test_ap_wpa_eap_connection_4); - ADD_FUNC(test_ap_wpa_eap_connection_5); - } - -#undef ADD_FUNC - - /* Scanned signal strength conversion tests */ - g_test_add_func ("/wifi/strength/dbm", - test_strength_dbm); - g_test_add_func ("/wifi/strength/percent", - test_strength_percent); - g_test_add_func ("/wifi/strength/wext", - test_strength_wext); - g_test_add_func ("/wifi/strength/all", - test_strength_all); - - return g_test_run (); -} -- cgit 1.3.0-6-gf8a5