settings: fix getting default wired connection name with no connections

Due to an error in the loop logic, if there were no connections yet (like
a fresh install or a livecd or whatever) no default name would be returned
and thus ethernet devices wouldn't come up automatically.  Fix that loop
error and add testcases for it.
This commit is contained in:
Dan Williams
2011-03-30 12:02:50 -05:00
parent e12edca3a0
commit 85d364d765
9 changed files with 295 additions and 46 deletions

1
.gitignore vendored
View File

@@ -168,6 +168,7 @@ src/settings/plugins/ifcfg-rh/tests/network-scripts/Test_Write_*
src/settings/plugins/ifcfg-rh/tests/network-scripts/*-Test_Write_*
src/settings/plugins/ifupdown/tests/test-ifupdown
src/settings/plugins/ifnet/tests/check_ifnet
src/settings/tests/test-wired-defname
m4/gtk-doc.m4
m4/intltool.m4

View File

@@ -598,6 +598,7 @@ src/settings/plugins/ifcfg-suse/Makefile
src/settings/plugins/keyfile/Makefile
src/settings/plugins/keyfile/tests/Makefile
src/settings/plugins/keyfile/tests/keyfiles/Makefile
src/settings/tests/Makefile
src/wimax/Makefile
src/backends/Makefile
libnm-util/libnm-util.pc

View File

@@ -27,5 +27,5 @@ src/nm-manager.c
src/nm-netlink-monitor.c
src/settings/plugins/ifcfg-rh/reader.c
src/settings/plugins/ifnet/connection_parser.c
src/settings/nm-settings.c
src/settings/nm-settings-utils.c

View File

@@ -1,4 +1,4 @@
SUBDIRS=plugins
SUBDIRS=plugins . tests
INCLUDES = -I${top_srcdir} \
-I${top_srcdir}/include \
@@ -7,7 +7,20 @@ INCLUDES = -I${top_srcdir} \
-I${top_srcdir}/src \
-I${top_builddir}/marshallers
noinst_LTLIBRARIES = libsettings.la
noinst_LTLIBRARIES = libsettings.la libtest-settings-utils.la
libtest_settings_utils_la_SOURCES = \
nm-settings-utils.c \
nm-settings-utils.h
libtest_settings_utils_la_CPPFLAGS = \
$(DBUS_CFLAGS) \
$(GLIB_CFLAGS)
libtest_settings_utils_la_LIBADD = \
$(top_builddir)/libnm-util/libnm-util.la \
$(DBUS_LIBS) \
$(GLIB_LIBS)
BUILT_SOURCES = \
nm-settings-glue.h \
@@ -31,7 +44,9 @@ libsettings_la_SOURCES = \
nm-agent-manager.c \
nm-agent-manager.h \
nm-secret-agent.c \
nm-secret-agent.h
nm-secret-agent.h \
nm-settings-utils.h \
nm-settings-utils.c
libsettings_la_CPPFLAGS = \
$(DBUS_CFLAGS) \

View File

@@ -0,0 +1,66 @@
/* -*- 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 of the License, 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.
*
* (C) Copyright 2011 Red Hat, Inc.
*/
#include <string.h>
#include <glib.h>
#include <glib/gi18n.h>
#include <nm-connection.h>
#include "nm-settings-utils.h"
char *
nm_settings_utils_get_default_wired_name (GHashTable *connections)
{
GHashTableIter iter;
NMConnection *connection = NULL;
GSList *names = NULL, *niter;
char *cname = NULL;
int i = 0;
/* Build up a list of all existing connection names for dupe checking */
g_hash_table_iter_init (&iter, connections);
while (g_hash_table_iter_next (&iter, NULL, (gpointer) &connection)) {
const char *id;
id = nm_connection_get_id (connection);
g_assert (id);
names = g_slist_append (names, (gpointer) id);
}
/* Find the next available unique connection name */
while (!cname && (i++ < 10000)) {
char *temp;
gboolean found = FALSE;
temp = g_strdup_printf (_("Wired connection %d"), i);
for (niter = names; niter; niter = g_slist_next (niter)) {
if (g_strcmp0 (niter->data, temp) == 0) {
found = TRUE;
g_free (temp);
break;
}
}
if (found == FALSE)
cname = temp;
}
g_slist_free (names);
return cname;
}

View File

@@ -0,0 +1,26 @@
/* -*- 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 of the License, 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.
*
* (C) Copyright 2011 Red Hat, Inc.
*/
#ifndef NM_SETTINGS_UTILS_H
#define NM_SETTINGS_UTILS_H
#include <glib.h>
char *nm_settings_utils_get_default_wired_name (GHashTable *connections);
#endif /* NM_SETTINGS_UTILS_H */

View File

@@ -34,8 +34,6 @@
#include <dbus/dbus.h>
#include <dbus/dbus-glib-lowlevel.h>
#include <glib/gi18n.h>
#include <NetworkManager.h>
#include <nm-connection.h>
#include <nm-setting-8021x.h>
@@ -67,6 +65,7 @@
#include "nm-session-monitor.h"
#include "plugins/keyfile/plugin.h"
#include "nm-agent-manager.h"
#include "nm-settings-utils.h"
#define CONFIG_KEY_NO_AUTO_DEFAULT "no-auto-default"
@@ -1327,48 +1326,10 @@ default_wired_try_update (NMDefaultWiredConnection *wired,
return TRUE;
}
static char *
find_next_default_wired_name (NMSettings *self)
{
NMSettingsPrivate *priv = NM_SETTINGS_GET_PRIVATE (self);
GHashTableIter iter;
NMConnection *connection = NULL;
GSList *names = NULL, *niter;
char *cname = NULL;
int i = 0;
g_hash_table_iter_init (&iter, priv->connections);
while (g_hash_table_iter_next (&iter, NULL, (gpointer) &connection)) {
const char *id;
id = nm_connection_get_id (connection);
g_assert (id);
names = g_slist_append (names, (gpointer) id);
}
/* Find the next available unique connection name */
while (!cname && (i++ < 10000)) {
char *temp;
gboolean found = FALSE;
temp = g_strdup_printf (_("Wired connection %d"), i);
for (niter = names; niter; niter = g_slist_next (niter)) {
if (g_strcmp0 (niter->data, temp) != 0) {
found = TRUE;
cname = g_strdup (temp);
break;
}
}
g_free (temp);
}
g_slist_free (names);
return cname;
}
void
nm_settings_device_added (NMSettings *self, NMDevice *device)
{
NMSettingsPrivate *priv = NM_SETTINGS_GET_PRIVATE (self);
GByteArray *mac = NULL;
struct ether_addr tmp;
NMDefaultWiredConnection *wired;
@@ -1398,7 +1359,7 @@ nm_settings_device_added (NMSettings *self, NMDevice *device)
if (get_plugin (self, NM_SYSTEM_CONFIG_INTERFACE_CAP_MODIFY_CONNECTIONS))
read_only = FALSE;
defname = find_next_default_wired_name (self);
defname = nm_settings_utils_get_default_wired_name (priv->connections);
wired = nm_default_wired_connection_new (mac, device, defname, read_only);
g_free (defname);
if (!wired)

View File

@@ -0,0 +1,32 @@
INCLUDES = \
-I$(top_srcdir)/include \
-I$(top_srcdir)/libnm-util \
-I$(top_srcdir)/src/settings
noinst_PROGRAMS = \
test-wired-defname
####### wired defname test #######
test_wired_defname_SOURCES = \
test-wired-defname.c
test_wired_defname_CPPFLAGS = \
$(GLIB_CFLAGS) \
$(DBUS_CFLAGS)
test_wired_defname_LDADD = \
$(top_builddir)/libnm-util/libnm-util.la \
$(top_builddir)/src/settings/libtest-settings-utils.la \
$(GLIB_LIBS) \
$(DBUS_LIBS)
###########################################
if WITH_TESTS
check-local: test-wired-defname
$(abs_builddir)/test-wired-defname
endif

View File

@@ -0,0 +1,147 @@
/* -*- 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) 2010 Red Hat, Inc.
*
*/
#include <glib.h>
#include <glib-object.h>
#include <nm-connection.h>
#include <nm-setting-connection.h>
#include "nm-settings-utils.h"
static NMConnection *
_new_connection (const char *id)
{
NMConnection *a;
NMSetting *setting;
a = nm_connection_new ();
setting = nm_setting_connection_new ();
g_object_set (setting, NM_SETTING_CONNECTION_ID, id, NULL);
nm_connection_add_setting (a, setting);
return a;
}
/*******************************************/
static void
test_defname_no_connections (void)
{
GHashTable *hash;
char *name;
hash = g_hash_table_new (g_direct_hash, g_direct_equal);
name = nm_settings_utils_get_default_wired_name (hash);
g_assert_cmpstr (name, ==, "Wired connection 1");
g_hash_table_destroy (hash);
}
/*******************************************/
static void
test_defname_no_conflict (void)
{
GHashTable *hash;
char *name;
hash = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, (GDestroyNotify) g_object_unref);
g_hash_table_insert (hash, "a", _new_connection ("asdfasdfasdfadf"));
g_hash_table_insert (hash, "b", _new_connection ("work wifi"));
g_hash_table_insert (hash, "c", _new_connection ("random gsm connection"));
name = nm_settings_utils_get_default_wired_name (hash);
g_assert_cmpstr (name, ==, "Wired connection 1");
g_hash_table_destroy (hash);
}
/*******************************************/
static void
test_defname_conflict (void)
{
GHashTable *hash;
char *name;
hash = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, (GDestroyNotify) g_object_unref);
g_hash_table_insert (hash, "a", _new_connection ("asdfasdfasdfadf"));
g_hash_table_insert (hash, "b", _new_connection ("Wired connection 1"));
g_hash_table_insert (hash, "c", _new_connection ("random gsm connection"));
name = nm_settings_utils_get_default_wired_name (hash);
g_assert_cmpstr (name, ==, "Wired connection 2");
g_hash_table_destroy (hash);
}
/*******************************************/
static void
test_defname_multiple_conflicts (void)
{
GHashTable *hash;
char *name;
hash = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, (GDestroyNotify) g_object_unref);
g_hash_table_insert (hash, "a", _new_connection ("random gsm connection"));
g_hash_table_insert (hash, "b", _new_connection ("home wifi"));
g_hash_table_insert (hash, "c", _new_connection ("Wired connection 1"));
g_hash_table_insert (hash, "d", _new_connection ("Wired connection 2"));
g_hash_table_insert (hash, "e", _new_connection ("Wired connection 3"));
g_hash_table_insert (hash, "f", _new_connection ("work wifi"));
g_hash_table_insert (hash, "g", _new_connection ("a vpn"));
name = nm_settings_utils_get_default_wired_name (hash);
g_assert_cmpstr (name, ==, "Wired connection 4");
g_hash_table_destroy (hash);
}
/*******************************************/
#if GLIB_CHECK_VERSION(2,25,12)
typedef GTestFixtureFunc TCFunc;
#else
typedef void (*TCFunc)(void);
#endif
#define TESTCASE(t, d) g_test_create_case (#t, 0, d, NULL, (TCFunc) t, NULL)
int main (int argc, char **argv)
{
GTestSuite *suite;
g_type_init ();
g_test_init (&argc, &argv, NULL);
suite = g_test_get_root ();
g_test_suite_add (suite, TESTCASE (test_defname_no_connections, NULL));
g_test_suite_add (suite, TESTCASE (test_defname_no_conflict, NULL));
g_test_suite_add (suite, TESTCASE (test_defname_conflict, NULL));
g_test_suite_add (suite, TESTCASE (test_defname_multiple_conflicts, NULL));
return g_test_run ();
}