network-manager-vpnc r21 - in branches/NETWORKMANAGER_0_7: . properties properties/tests



Author: dcbw
Date: Fri Feb  6 00:01:52 2009
New Revision: 21
URL: http://svn.gnome.org/viewvc/network-manager-vpnc?rev=21&view=rev

Log:
2009-02-05  Dan Williams  <dcbw redhat com>

	* configure.in
		- Add configure-time option for running test cases on 'make check'

	* properties/pcf-file.c
	  properties/pcf-file.h
		- (pcf_file_lookup_string, pcf_file_lookup_bool, pcf_file_lookup_int):
			add helper functions to look up specific value types

	* properties/nm-vpnc.c
		- (import): use new value types helper functions; fix interpretation of
			"EnableNat" and ensure default is Cisco NAT traversal; import
			"DHGroup"
		- (export): export "DHGroup" and "SaveUserPassword"; fix export of
			static routes

	* properties/tests/*
	  nm-test-helpers.h
	  Makefile.am
		- Add testcases for .pcf file import/export



Added:
   branches/NETWORKMANAGER_0_7/nm-test-helpers.h
      - copied unchanged from r19, /trunk/nm-test-helpers.h
   branches/NETWORKMANAGER_0_7/properties/tests/
      - copied from r19, /trunk/properties/tests/
Modified:
   branches/NETWORKMANAGER_0_7/ChangeLog
   branches/NETWORKMANAGER_0_7/Makefile.am
   branches/NETWORKMANAGER_0_7/configure.in
   branches/NETWORKMANAGER_0_7/properties/Makefile.am
   branches/NETWORKMANAGER_0_7/properties/nm-vpnc.c
   branches/NETWORKMANAGER_0_7/properties/pcf-file.c
   branches/NETWORKMANAGER_0_7/properties/pcf-file.h

Modified: branches/NETWORKMANAGER_0_7/Makefile.am
==============================================================================
--- branches/NETWORKMANAGER_0_7/Makefile.am	(original)
+++ branches/NETWORKMANAGER_0_7/Makefile.am	Fri Feb  6 00:01:52 2009
@@ -31,7 +31,8 @@
              $(icon_DATA)         \
              intltool-extract.in  \
              intltool-merge.in    \
-             intltool-update.in
+             intltool-update.in \
+             nm-test-helpers.h
 
 CLEANFILES = $(nmvpnservice_DATA) $(desktop_DATA) *~
 DISTCLEANFILES = intltool-extract intltool-merge intltool-update

Modified: branches/NETWORKMANAGER_0_7/configure.in
==============================================================================
--- branches/NETWORKMANAGER_0_7/configure.in	(original)
+++ branches/NETWORKMANAGER_0_7/configure.in	Fri Feb  6 00:01:52 2009
@@ -115,11 +115,27 @@
 	AC_MSG_RESULT(no)
 fi
 
+dnl
+dnl Tests
+dnl
+AC_ARG_WITH(tests, AC_HELP_STRING([--with-tests], [Build NetworkManager tests]))
+AM_CONDITIONAL(WITH_TESTS, test "x$with_tests" = "xyes")
+case $with_tests in
+    yes)
+        with_tests=yes
+        ;;
+    *)
+        with_tests=no
+        ;;
+esac
+
 AC_OUTPUT([
 Makefile
 src/Makefile
 common-gnome/Makefile
 auth-dialog/Makefile
 properties/Makefile
+properties/tests/Makefile
+properties/tests/pcf/Makefile
 po/Makefile.in
 ])

Modified: branches/NETWORKMANAGER_0_7/properties/Makefile.am
==============================================================================
--- branches/NETWORKMANAGER_0_7/properties/Makefile.am	(original)
+++ branches/NETWORKMANAGER_0_7/properties/Makefile.am	Fri Feb  6 00:01:52 2009
@@ -1,3 +1,5 @@
+SUBDIRS=. tests
+
 INCLUDES = -I${top_srcdir}
 
 plugindir = $(libdir)/NetworkManager
@@ -31,6 +33,7 @@
 	$(GTK_LIBS) \
 	$(GCONF_LIBS) \
 	$(NM_UTILS_LIBS) \
+	$(GNOMEKEYRING_LIBS) \
 	$(top_builddir)/common-gnome/libnm-vpnc-common-gnome.la
 
 libnm_vpnc_properties_la_LDFLAGS = \

Modified: branches/NETWORKMANAGER_0_7/properties/nm-vpnc.c
==============================================================================
--- branches/NETWORKMANAGER_0_7/properties/nm-vpnc.c	(original)
+++ branches/NETWORKMANAGER_0_7/properties/nm-vpnc.c	Fri Feb  6 00:01:52 2009
@@ -974,9 +974,9 @@
 	NMSettingVPN *s_vpn;
 	GHashTable *pcf;
 	const char *buf;
-	gboolean have_value;
+	gboolean bool_value;
 	NMSettingIP4Config *s_ip4;
-	long int val;
+	gint val;
 
 	pcf = pcf_file_load (path);
 	if (!pcf) {
@@ -997,7 +997,7 @@
 	nm_connection_add_setting (connection, NM_SETTING (s_ip4));
 
 	/* Connection name */
-	if ((buf = pcf_file_lookup_value (pcf, "main", "Description")))
+	if (pcf_file_lookup_string (pcf, "main", "Description", &buf))
 		g_object_set (s_con, NM_SETTING_CONNECTION_ID, buf, NULL);
 	else {
 		g_set_error (error, 0, 0, "does not look like a %s VPN connection (parse failed)",
@@ -1007,7 +1007,7 @@
 	}
 
 	/* Gateway */
-	if ((buf = pcf_file_lookup_value (pcf, "main", "Host")))
+	if (pcf_file_lookup_string (pcf, "main", "Host", &buf))
 		nm_setting_vpn_add_data_item (s_vpn, NM_VPNC_KEY_GATEWAY, buf);
 	else {
 		g_set_error (error, 0, 0, "does not look like a %s VPN connection (no Host)",
@@ -1017,7 +1017,7 @@
 	}
 
 	/* Group name */
-	if ((buf = pcf_file_lookup_value (pcf, "main", "GroupName")))
+	if (pcf_file_lookup_string (pcf, "main", "GroupName", &buf))
 		nm_setting_vpn_add_data_item (s_vpn, NM_VPNC_KEY_ID, buf);
 	else {
 		g_set_error (error, 0, 0, "does not look like a %s VPN connection (no GroupName)",
@@ -1028,37 +1028,25 @@
 
 	/* Optional settings */
 
-	buf = pcf_file_lookup_value (pcf, "main", "UserName");
-	have_value = buf == NULL ? FALSE : strlen (buf) > 0;
-	if (have_value)
+	if (pcf_file_lookup_string (pcf, "main", "UserName", &buf))
 		nm_setting_vpn_add_data_item (s_vpn, NM_VPNC_KEY_XAUTH_USER, buf);
 
-	buf = pcf_file_lookup_value (pcf, "main", "UserPassword");
-	have_value = buf == NULL ? FALSE : strlen (buf) > 0;
-	if (have_value)
+	if (pcf_file_lookup_string (pcf, "main", "UserPassword", &buf))
 		nm_setting_vpn_add_secret (s_vpn, NM_VPNC_KEY_XAUTH_PASSWORD, buf);
 
-	buf = pcf_file_lookup_value (pcf, "main", "SaveUserPassword");
-	have_value = buf == NULL ? FALSE : strlen (buf) > 0;
-	if (have_value) {
-		errno = 0;
-		val = strtol (buf, NULL, 10);
-		if ((errno == 0) && (val == 1)) {
+	if (pcf_file_lookup_bool (pcf, "main", "SaveUserPassword", &bool_value)) {
+		if (bool_value) {
 			nm_setting_vpn_add_data_item (s_vpn,
 			                              NM_VPNC_KEY_XAUTH_PASSWORD_TYPE,
 			                              NM_VPNC_PW_TYPE_SAVE);
 		}
 	}
 
-	buf = pcf_file_lookup_value (pcf, "main", "GroupPwd");
-	have_value = buf == NULL ? FALSE : strlen (buf) > 0;
-	if (have_value)
+	if (pcf_file_lookup_string (pcf, "main", "GroupPwd", &buf))
 		nm_setting_vpn_add_secret (s_vpn, NM_VPNC_KEY_SECRET, buf);
 	else {
 		/* Handle encrypted passwords */
-		buf = pcf_file_lookup_value (pcf, "main", "enc_GroupPwd");
-		have_value = buf == NULL ? FALSE : strlen (buf) > 0;
-		if (have_value) {
+		if (pcf_file_lookup_string (pcf, "main", "enc_GroupPwd", &buf)) {
 			char *decrypted;
 
 			decrypted = decrypt_cisco_key (buf);
@@ -1070,50 +1058,55 @@
 		}
 	}
 
-	buf = pcf_file_lookup_value (pcf, "main", "NTDomain");
-	have_value = buf == NULL ? FALSE : strlen (buf) > 0;
-	if (have_value)
+	if (pcf_file_lookup_string (pcf, "main", "NTDomain", &buf))
 		nm_setting_vpn_add_data_item (s_vpn, NM_VPNC_KEY_DOMAIN, buf);
 
-	buf = pcf_file_lookup_value (pcf, "main", "SingleDES");
-	have_value = (buf == NULL ? FALSE : strcmp (buf, "0") != 0);
-	if (have_value)
-		nm_setting_vpn_add_data_item (s_vpn, NM_VPNC_KEY_SINGLE_DES, "yes");
+	if (pcf_file_lookup_bool (pcf, "main", "SingleDES", &bool_value)) {
+		if (bool_value)
+			nm_setting_vpn_add_data_item (s_vpn, NM_VPNC_KEY_SINGLE_DES, "yes");
+	}
 
 	/* Default is enabled, only disabled if explicit EnableNat=0 exists */
-	buf = pcf_file_lookup_value (pcf, "main", "EnableNat");
-	have_value = (buf ? strncmp (buf, "0", 1) == 0 : FALSE);
-	if (have_value)
-		nm_setting_vpn_add_data_item (s_vpn, NM_VPNC_KEY_NAT_TRAVERSAL_MODE, NM_VPNC_NATT_MODE_NATT);
-
-	if ((buf = pcf_file_lookup_value (pcf, "main", "PeerTimeout"))) {
-		errno = 0;
-		val = strtol (buf, NULL, 10);
-		if ((errno == 0) && ((val == 0) || ((val >= 10) && (val <= 86400)))) {
+	if (pcf_file_lookup_bool (pcf, "main", "EnableNat", &bool_value)) {
+		if (!bool_value) {
+			nm_setting_vpn_add_data_item (s_vpn,
+			                              NM_VPNC_KEY_NAT_TRAVERSAL_MODE,
+			                              NM_VPNC_NATT_MODE_NONE);
+		}
+	}
+
+	/* Default to Cisco UDP */
+	if (!nm_setting_vpn_get_data_item (s_vpn, NM_VPNC_KEY_NAT_TRAVERSAL_MODE)) {
+		nm_setting_vpn_add_data_item (s_vpn,
+		                              NM_VPNC_KEY_NAT_TRAVERSAL_MODE,
+		                              NM_VPNC_NATT_MODE_CISCO);
+	}
+
+	if (pcf_file_lookup_int (pcf, "main", "PeerTimeout", &val)) {
+		if ((val == 0) || ((val >= 10) && (val <= 86400))) {
 			char *tmp = g_strdup_printf ("%d", (gint) val);
 			nm_setting_vpn_add_data_item (s_vpn, NM_VPNC_KEY_DPD_IDLE_TIMEOUT, tmp);
 			g_free (tmp);
 		}
 	}
 
-	buf = pcf_file_lookup_value (pcf, "main", "EnableLocalLAN");
-	have_value = buf == NULL ? FALSE : strlen (buf) > 0;
-	if (have_value) {
-		errno = 0;
-		val = strtol (buf, NULL, 10);
-		if ((errno == 0) && (val == 1))
+	if (pcf_file_lookup_bool (pcf, "main", "EnableLocalLAN", &bool_value)) {
+		if (bool_value)
 			g_object_set (s_ip4, NM_SETTING_IP4_CONFIG_NEVER_DEFAULT, TRUE, NULL);
 	}
 
-	buf = pcf_file_lookup_value (pcf, "main", "X-NM-Routes");
-	have_value = buf == NULL ? FALSE : strlen (buf) > 0;
-	if (have_value)
+	if (pcf_file_lookup_string (pcf, "main", "DHGroup", &buf)) {
+		if (!strcmp (buf, "1") || !strcmp (buf, "2") || !strcmp (buf, "5"))
+			nm_setting_vpn_add_data_item (s_vpn, NM_VPNC_KEY_DHGROUP, buf);
+	}
+
+	if (pcf_file_lookup_string (pcf, "main", "X-NM-Routes", &buf))
 		add_routes (s_ip4, buf);
 
-	if ((buf = pcf_file_lookup_value (pcf, "main", "TunnelingMode"))) {
+	if (pcf_file_lookup_int (pcf, "main", "TunnelingMode", &val)) {
 		/* If applicable, put up warning that TCP tunneling will be disabled */
 
-		if (strncmp (buf, "1", 1) == 0) {
+		if (val == 1) {
 			GtkWidget *dialog;
 			char *basename;
 
@@ -1152,8 +1145,11 @@
 	const char *username = NULL;
 	const char *domain = NULL;
 	const char *peertimeout = NULL;
+	const char *dhgroup = NULL;
 	GString *routes = NULL;
 	gboolean success = FALSE;
+	guint32 routes_count = 0;
+	gboolean save_password = FALSE;
 
 	s_con = NM_SETTING_CONNECTION (nm_connection_get_setting (connection, NM_TYPE_SETTING_CONNECTION));
 	s_ip4 = (NMSettingIP4Config *) nm_connection_get_setting (connection, NM_TYPE_SETTING_IP4_CONFIG);
@@ -1188,7 +1184,7 @@
 
 	value = nm_setting_vpn_get_data_item (s_vpn, NM_VPNC_KEY_DOMAIN);
 	if (value && strlen (value))
-		domain =  value;
+		domain = value;
 
 	value = nm_setting_vpn_get_data_item (s_vpn, NM_VPNC_KEY_SINGLE_DES);
 	if (value && !strcmp (value, "yes"))
@@ -1202,7 +1198,17 @@
 	if (value && strlen (value))
 		peertimeout = value;
 
-	routes = g_string_new ("");
+	value = nm_setting_vpn_get_data_item (s_vpn, NM_VPNC_KEY_DHGROUP);
+	if (value && strlen (value))
+		dhgroup = value;
+
+	value = nm_setting_vpn_get_data_item (s_vpn, NM_VPNC_KEY_XAUTH_PASSWORD_TYPE);
+	if (value && strlen (value)) {
+		if (!strcmp (value, NM_VPNC_PW_TYPE_SAVE))
+			save_password = TRUE;
+	}
+
+	routes = g_string_new ("X-NM-Routes=");
 	if (s_ip4 && nm_setting_ip4_config_get_num_routes (s_ip4)) {
 		int i;
 
@@ -1211,14 +1217,20 @@
 			char str_addr[INET_ADDRSTRLEN + 1];
 			struct in_addr num_addr;
 
-			if (routes->len)
+			if (routes_count)
 				g_string_append_c (routes, ' ');
 
 			num_addr.s_addr = nm_ip4_route_get_dest (route);
 			if (inet_ntop (AF_INET, &num_addr, &str_addr[0], INET_ADDRSTRLEN + 1))
 				g_string_append_printf (routes, "%s/%d", str_addr, nm_ip4_route_get_prefix (route));
+
+			routes_count++;
 		}
 	}
+	if (!routes_count) {
+		g_string_free (routes, TRUE);
+		routes = NULL;
+	}
 
 	fprintf (f, 
 		 "[main]\n"
@@ -1232,7 +1244,7 @@
 		 "ISPConnect=\n"
 		 "ISPCommand=\n"
 		 "Username=%s\n"
-		 "SaveUserPassword=0\n"
+		 "SaveUserPassword=%s\n"
 		 "EnableBackup=0\n"
 		 "BackupServer=\n"
 		 "EnableNat=%s\n"
@@ -1241,7 +1253,7 @@
 		 "CertPath=\n"
 		 "CertSubjectName=\n"
 		 "CertSerialHash=\n"
-		 "DHGroup=2\n"
+		 "DHGroup=%s\n"
 		 "ForceKeepAlives=0\n"
 		 "enc_GroupPwd=\n"
 		 "UserPassword=\n"
@@ -1258,16 +1270,18 @@
 		 "EnableSplitDNS=1\n"
 		 "SingleDES=%s\n"
 		 "SPPhonebook=\n"
-		 "%s",
-		 /* Description */ nm_setting_connection_get_id (s_con),
-		 /* Host */        gateway,
-		 /* GroupName */   groupname,
-		 /* Username */    username != NULL ? username : "",
-		 /* EnableNat */   enablenat ? "1" : "0",
-		 /* NTDomain */    domain != NULL ? domain : "",
-		 /* PeerTimeout */ peertimeout != NULL ? peertimeout : "0",
-		 /* SingleDES */   singledes ? "1" : "0",
-		 /* X-NM-Routes */ routes->str ? routes->str : "");
+		 "%s\n",
+		 /* Description */   nm_setting_connection_get_id (s_con),
+		 /* Host */          gateway,
+		 /* GroupName */     groupname,
+		 /* Username */      username != NULL ? username : "",
+		 /* Save Password */ save_password ? "1" : "0",
+		 /* EnableNat */     enablenat ? "1" : "0",
+		 /* DHGroup */       dhgroup != NULL ? dhgroup : "2",
+		 /* NTDomain */      domain != NULL ? domain : "",
+		 /* PeerTimeout */   peertimeout != NULL ? peertimeout : "0",
+		 /* SingleDES */     singledes ? "1" : "0",
+		 /* X-NM-Routes */   (routes && routes->str) ? routes->str : "");
 
 	success = TRUE;
 

Modified: branches/NETWORKMANAGER_0_7/properties/pcf-file.c
==============================================================================
--- branches/NETWORKMANAGER_0_7/properties/pcf-file.c	(original)
+++ branches/NETWORKMANAGER_0_7/properties/pcf-file.c	Fri Feb  6 00:01:52 2009
@@ -23,6 +23,7 @@
 #include <string.h>
 #include <errno.h>
 #include <ctype.h>
+#include <stdlib.h>
 
 #include "pcf-file.h"
 
@@ -92,6 +93,7 @@
 			g_hash_table_insert (pcf, g_utf8_strdown (s+1, -1), group);
         } else {
 			PcfEntry *entry;
+			char *key;
 
             /* Normal assignment */
             if (!(e = strchr (s, '='))) {
@@ -111,13 +113,15 @@
 			entry->value = g_strdup (e);
 
 			if (*s == '!') {
-				entry->key = g_utf8_strdown (s+1, -1);
+				key = g_utf8_strdown (s+1, -1);
 				entry->read_only = TRUE;
 			} else {
-				entry->key = g_utf8_strdown (s, -1);
+				key = g_utf8_strdown (s, -1);
 				entry->read_only = FALSE;
 			}
 
+			entry->key = g_strdup (g_strstrip (key));
+			g_free (key);
 			g_hash_table_insert (group, entry->key, entry);
         }
     }
@@ -139,8 +143,8 @@
 
 PcfEntry *
 pcf_file_lookup (GHashTable *pcf_file,
-				 const char *group,
-				 const char *key)
+                 const char *group,
+                 const char *key)
 {
 	gpointer section;
 	PcfEntry *entry = NULL;
@@ -164,16 +168,94 @@
 	return entry;
 }
 
-const char *
-pcf_file_lookup_value (GHashTable *pcf_file,
-					   const char *group,
-					   const char *key)
+gboolean
+pcf_file_lookup_string (GHashTable *pcf_file,
+                        const char *group,
+                        const char *key,
+                        const char **value)
 {
 	PcfEntry *entry;
 
+	g_return_val_if_fail (pcf_file != NULL, FALSE);
+	g_return_val_if_fail (group != NULL, FALSE);
+	g_return_val_if_fail (key != NULL, FALSE);
+	g_return_val_if_fail (value != NULL, FALSE);
+
+	*value = NULL;
 	entry = pcf_file_lookup (pcf_file, group, key);
-	if (entry)
-		return entry->value;
+	if (!entry || !entry->value || !strlen (entry->value))
+		return FALSE;
+
+	*value = entry->value;
+	return TRUE;
+}
+
+gboolean
+pcf_file_lookup_bool (GHashTable *pcf_file,
+                      const char *group,
+                      const char *key,
+                      gboolean *value)
+{
+	const char *buf = NULL;
+	gboolean success = FALSE;
+
+	g_return_val_if_fail (pcf_file != NULL, FALSE);
+	g_return_val_if_fail (group != NULL, FALSE);
+	g_return_val_if_fail (key != NULL, FALSE);
+	g_return_val_if_fail (value != NULL, FALSE);
+
+	*value = FALSE;
+	if (!pcf_file_lookup_string (pcf_file, group, key, &buf))
+		return FALSE;
+
+	if (strlen (buf) == 1) {
+		if (strcmp (buf, "1") == 0) {
+			*value = TRUE;
+			success = TRUE;
+		} else if (strcmp (buf, "0") == 0) {
+			*value = FALSE;
+			success = TRUE;
+		}
+	} else {
+		if (   !strncasecmp (buf, "yes", 3)
+		    || !strncasecmp (buf, "true", 4)) {
+			*value = TRUE;
+			success = TRUE;
+		} else if (   !strncasecmp (buf, "no", 2)
+		           || !strncasecmp (buf, "false", 5)) {
+			*value = FALSE;
+			success = TRUE;
+		}
+	}
 
-	return NULL;
+	return success;
 }
+
+gboolean
+pcf_file_lookup_int (GHashTable *pcf_file,
+                     const char *group,
+                     const char *key,
+                     gint *value)
+{
+	const char *buf = NULL;
+	long int tmp;
+
+	g_return_val_if_fail (pcf_file != NULL, FALSE);
+	g_return_val_if_fail (group != NULL, FALSE);
+	g_return_val_if_fail (key != NULL, FALSE);
+	g_return_val_if_fail (value != NULL, FALSE);
+
+	*value = 0;
+	if (!pcf_file_lookup_string (pcf_file, group, key, &buf))
+		return FALSE;
+
+	errno = 0;
+	tmp = strtol (buf, NULL, 10);
+	if ((errno == 0) && (tmp > G_MININT) && (tmp < G_MAXINT)) {
+		*value = (gint) tmp;
+		return TRUE;
+	}
+
+	return FALSE;
+}
+

Modified: branches/NETWORKMANAGER_0_7/properties/pcf-file.h
==============================================================================
--- branches/NETWORKMANAGER_0_7/properties/pcf-file.h	(original)
+++ branches/NETWORKMANAGER_0_7/properties/pcf-file.h	Fri Feb  6 00:01:52 2009
@@ -34,11 +34,23 @@
 
 GHashTable  *pcf_file_load        (const char *fname);
 PcfEntry    *pcf_file_lookup      (GHashTable *pcf_file,
-								   const char *group,
-								   const char *key);
+                                   const char *group,
+                                   const char *key);
 
-const char *pcf_file_lookup_value (GHashTable *pcf_file,
-								   const char *group,
-								   const char *key);
+gboolean pcf_file_lookup_string (GHashTable *pcf_file,
+                                 const char *group,
+                                 const char *key,
+                                 const char **value);
+
+gboolean pcf_file_lookup_bool (GHashTable *pcf_file,
+                               const char *group,
+                               const char *key,
+                               gboolean *value);
+
+gboolean pcf_file_lookup_int (GHashTable *pcf_file,
+                              const char *group,
+                              const char *key,
+                              gint *value);
 
 #endif /* PCF_FILE_H */
+



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]