mirror of
https://gitlab.freedesktop.org/NetworkManager/NetworkManager.git
synced 2026-01-04 14:30:18 +01:00
libnm-util: don't allow blank or NULL VPN items or secrets (rh #532084)
Weren't supposed to be allowed anyway; fix that and add a testcase for it.
This commit is contained in:
parent
82011dff04
commit
df32cfbfd8
3 changed files with 177 additions and 1 deletions
|
|
@ -133,6 +133,10 @@ nm_setting_vpn_add_data_item (NMSettingVPN *setting,
|
|||
const char *item)
|
||||
{
|
||||
g_return_if_fail (NM_IS_SETTING_VPN (setting));
|
||||
g_return_if_fail (key != NULL);
|
||||
g_return_if_fail (strlen (key) > 0);
|
||||
g_return_if_fail (item != NULL);
|
||||
g_return_if_fail (strlen (item) > 0);
|
||||
|
||||
g_hash_table_insert (NM_SETTING_VPN_GET_PRIVATE (setting)->data,
|
||||
g_strdup (key), g_strdup (item));
|
||||
|
|
@ -171,6 +175,10 @@ nm_setting_vpn_add_secret (NMSettingVPN *setting,
|
|||
const char *secret)
|
||||
{
|
||||
g_return_if_fail (NM_IS_SETTING_VPN (setting));
|
||||
g_return_if_fail (key != NULL);
|
||||
g_return_if_fail (strlen (key) > 0);
|
||||
g_return_if_fail (secret != NULL);
|
||||
g_return_if_fail (strlen (secret) > 0);
|
||||
|
||||
g_hash_table_insert (NM_SETTING_VPN_GET_PRIVATE (setting)->secrets,
|
||||
g_strdup (key), g_strdup (secret));
|
||||
|
|
|
|||
|
|
@ -4,7 +4,11 @@ INCLUDES = \
|
|||
-I$(top_srcdir)/include \
|
||||
-I$(top_srcdir)/libnm-util
|
||||
|
||||
noinst_PROGRAMS = test-settings-defaults test-crypto test-need-secrets
|
||||
noinst_PROGRAMS = \
|
||||
test-settings-defaults \
|
||||
test-crypto \
|
||||
test-need-secrets \
|
||||
test-general
|
||||
|
||||
test_settings_defaults_SOURCES = \
|
||||
test-settings-defaults.c
|
||||
|
|
@ -42,11 +46,24 @@ test_need_secrets_LDADD = \
|
|||
$(GLIB_LIBS) \
|
||||
$(DBUS_LIBS)
|
||||
|
||||
test_general_SOURCES = \
|
||||
test-general.c
|
||||
|
||||
test_general_CPPFLAGS = \
|
||||
$(GLIB_CFLAGS) \
|
||||
$(DBUS_CFLAGS)
|
||||
|
||||
test_general_LDADD = \
|
||||
$(top_builddir)/libnm-util/libnm-util.la \
|
||||
$(GLIB_LIBS) \
|
||||
$(DBUS_LIBS)
|
||||
|
||||
if WITH_TESTS
|
||||
|
||||
check-local: test-settings-defaults test-crypto test-need-secrets
|
||||
$(abs_builddir)/test-settings-defaults
|
||||
$(abs_builddir)/test-need-secrets
|
||||
$(abs_builddir)/test-general
|
||||
|
||||
# Cert with 8 bytes of tail padding
|
||||
$(abs_builddir)/test-crypto \
|
||||
|
|
|
|||
151
libnm-util/tests/test-general.c
Normal file
151
libnm-util/tests/test-general.c
Normal file
|
|
@ -0,0 +1,151 @@
|
|||
/* -*- 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) 2008 - 2009 Red Hat, Inc.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <glib.h>
|
||||
#include <dbus/dbus-glib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "nm-test-helpers.h"
|
||||
#include <nm-utils.h>
|
||||
|
||||
#include "nm-setting-connection.h"
|
||||
#include "nm-setting-vpn.h"
|
||||
|
||||
|
||||
static void
|
||||
vpn_check_func (const char *key, const char *value, gpointer user_data)
|
||||
{
|
||||
const char *test = user_data;
|
||||
|
||||
if (!strcmp (key, "foobar1")) {
|
||||
ASSERT (strcmp (value, "blahblah1") == 0,
|
||||
test, "unexpected vpn item '%s' / '%s'", key, value);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!strcmp (key, "foobar2")) {
|
||||
ASSERT (strcmp (value, "blahblah2") == 0,
|
||||
test, "unexpected vpn item '%s' / '%s'", key, value);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!strcmp (key, "foobar3")) {
|
||||
ASSERT (strcmp (value, "blahblah3") == 0,
|
||||
test, "unexpected vpn item '%s' / '%s'", key, value);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!strcmp (key, "foobar4")) {
|
||||
ASSERT (strcmp (value, "blahblah4") == 0,
|
||||
test, "unexpected vpn item '%s' / '%s'", key, value);
|
||||
return;
|
||||
}
|
||||
|
||||
ASSERT (FALSE, test, "unexpected vpn item '%s'", key);
|
||||
}
|
||||
|
||||
static void
|
||||
vpn_check_empty_func (const char *key, const char *value, gpointer user_data)
|
||||
{
|
||||
const char *test = user_data;
|
||||
|
||||
/* We don't expect any values */
|
||||
ASSERT (FALSE, test, "unexpected vpn item '%s'", key);
|
||||
}
|
||||
|
||||
static void
|
||||
test_setting_vpn_items (void)
|
||||
{
|
||||
NMSettingVPN *s_vpn;
|
||||
|
||||
s_vpn = (NMSettingVPN *) nm_setting_vpn_new ();
|
||||
ASSERT (s_vpn != NULL,
|
||||
"vpn-items",
|
||||
"error creating vpn setting");
|
||||
|
||||
nm_setting_vpn_add_data_item (s_vpn, "foobar1", "blahblah1");
|
||||
nm_setting_vpn_add_data_item (s_vpn, "foobar2", "blahblah2");
|
||||
nm_setting_vpn_add_data_item (s_vpn, "foobar3", "blahblah3");
|
||||
nm_setting_vpn_add_data_item (s_vpn, "foobar4", "blahblah4");
|
||||
|
||||
/* Ensure that added values are all present */
|
||||
nm_setting_vpn_foreach_data_item (s_vpn, vpn_check_func, "vpn-data");
|
||||
nm_setting_vpn_remove_data_item (s_vpn, "foobar1");
|
||||
nm_setting_vpn_remove_data_item (s_vpn, "foobar2");
|
||||
nm_setting_vpn_remove_data_item (s_vpn, "foobar3");
|
||||
nm_setting_vpn_remove_data_item (s_vpn, "foobar4");
|
||||
|
||||
nm_setting_vpn_add_secret (s_vpn, "foobar1", "blahblah1");
|
||||
nm_setting_vpn_add_secret (s_vpn, "foobar2", "blahblah2");
|
||||
nm_setting_vpn_add_secret (s_vpn, "foobar3", "blahblah3");
|
||||
nm_setting_vpn_add_secret (s_vpn, "foobar4", "blahblah4");
|
||||
|
||||
/* Ensure that added values are all present */
|
||||
nm_setting_vpn_foreach_secret (s_vpn, vpn_check_func, "vpn-secrets");
|
||||
nm_setting_vpn_remove_secret (s_vpn, "foobar1");
|
||||
nm_setting_vpn_remove_secret (s_vpn, "foobar2");
|
||||
nm_setting_vpn_remove_secret (s_vpn, "foobar3");
|
||||
nm_setting_vpn_remove_secret (s_vpn, "foobar4");
|
||||
|
||||
/* Try to add some blank values and make sure they are rejected */
|
||||
nm_setting_vpn_add_data_item (s_vpn, NULL, NULL);
|
||||
nm_setting_vpn_add_data_item (s_vpn, "", "");
|
||||
nm_setting_vpn_add_data_item (s_vpn, "foobar1", NULL);
|
||||
nm_setting_vpn_add_data_item (s_vpn, "foobar1", "");
|
||||
nm_setting_vpn_add_data_item (s_vpn, NULL, "blahblah1");
|
||||
nm_setting_vpn_add_data_item (s_vpn, "", "blahblah1");
|
||||
|
||||
nm_setting_vpn_foreach_data_item (s_vpn, vpn_check_empty_func, "vpn-data-empty");
|
||||
|
||||
/* Try to add some blank secrets and make sure they are rejected */
|
||||
nm_setting_vpn_add_secret (s_vpn, NULL, NULL);
|
||||
nm_setting_vpn_add_secret (s_vpn, "", "");
|
||||
nm_setting_vpn_add_secret (s_vpn, "foobar1", NULL);
|
||||
nm_setting_vpn_add_secret (s_vpn, "foobar1", "");
|
||||
nm_setting_vpn_add_secret (s_vpn, NULL, "blahblah1");
|
||||
nm_setting_vpn_add_secret (s_vpn, "", "blahblah1");
|
||||
|
||||
nm_setting_vpn_foreach_secret (s_vpn, vpn_check_empty_func, "vpn-secrets-empty");
|
||||
|
||||
g_object_unref (s_vpn);
|
||||
}
|
||||
|
||||
int main (int argc, char **argv)
|
||||
{
|
||||
GError *error = NULL;
|
||||
DBusGConnection *bus;
|
||||
char *base;
|
||||
|
||||
g_type_init ();
|
||||
bus = dbus_g_bus_get (DBUS_BUS_SESSION, NULL);
|
||||
|
||||
if (!nm_utils_init (&error))
|
||||
FAIL ("nm-utils-init", "failed to initialize libnm-util: %s", error->message);
|
||||
|
||||
/* The tests */
|
||||
test_setting_vpn_items ();
|
||||
|
||||
base = g_path_get_basename (argv[0]);
|
||||
fprintf (stdout, "%s: SUCCESS\n", base);
|
||||
g_free (base);
|
||||
return 0;
|
||||
}
|
||||
|
||||
Loading…
Add table
Reference in a new issue