[network-manager-vpnc] import: try to convert current locale-encoded files to UTF-8 (bgo #547973)



commit 5210b274772aa2193515a6098166d46c04c90c9c
Author: Dan Williams <dcbw redhat com>
Date:   Fri Sep 11 17:24:05 2009 -0700

    import: try to convert current locale-encoded files to UTF-8 (bgo #547973)

 properties/pcf-file.c                 |   20 +++++++++++++++-
 properties/tests/pcf/Makefile.am      |    3 +-
 properties/tests/pcf/iso885915.pcf    |   38 +++++++++++++++++++++++++++++++++
 properties/tests/test-import-export.c |   38 +++++++++++++++++++++++++++++++++
 4 files changed, 96 insertions(+), 3 deletions(-)
---
diff --git a/properties/pcf-file.c b/properties/pcf-file.c
index 76f6761..ed8523c 100644
--- a/properties/pcf-file.c
+++ b/properties/pcf-file.c
@@ -63,13 +63,29 @@ pcf_file_load (const char *fname)
 
     line = 0;
     while (!feof (fo)) {
-        char ln[256], *s, *e;
+        char ln[1024]; /* 4x what we think to allow for possible UTF-8 conversion */
+        char *s, *e;
         
-        if (!(fgets (ln, sizeof (ln), fo)))
+        if (!(fgets (ln, sizeof (ln) / 4, fo)))
             break;
 
         line++;
 
+		if (!g_utf8_validate (ln, -1, NULL)) {
+			char *tmp;
+			GError *error = NULL;
+
+			tmp = g_locale_to_utf8 (ln, -1, NULL, NULL, &error);
+			if (error) {
+				/* ignore the error; leave 'ln' alone.  We tried. */
+				g_error_free (error);
+			} else {
+				g_assert (tmp);
+				strcpy (ln, tmp);  /* update ln with the UTF-8 safe text */
+			}
+			g_free (tmp);
+		}
+
         s = ln + strspn (ln, " \t");
         s[strcspn (s, "\r\n")] = 0;
 
diff --git a/properties/tests/pcf/Makefile.am b/properties/tests/pcf/Makefile.am
index 0596fc9..4193363 100644
--- a/properties/tests/pcf/Makefile.am
+++ b/properties/tests/pcf/Makefile.am
@@ -2,5 +2,6 @@ EXTRA_DIST = \
 	basic.pcf \
 	everything-via-vpn.pcf \
 	no-natt.pcf \
-	always-ask.pcf
+	always-ask.pcf \
+	iso885915.pcf
 
diff --git a/properties/tests/pcf/iso885915.pcf b/properties/tests/pcf/iso885915.pcf
new file mode 100644
index 0000000..325ff2c
--- /dev/null
+++ b/properties/tests/pcf/iso885915.pcf
@@ -0,0 +1,38 @@
+[main]
+Description=Att äta en ko
+!Host=10.20.30.40
+!AuthType=1
+!GroupName=blahblah
+!GroupPwd=my-group-password
+!enc_GroupPwd=
+EnableISPConnect=0
+ISPConnectType=0
+ISPConnect=
+ISPCommand=
+Username=bsmith
+SaveUserPassword=1
+UserPassword=my-user-password
+enc_UserPassword=
+!NTDomain=COMPANY
+!EnableBackup=0
+!BackupServer=
+!EnableMSLogon=1
+!MSLogonType=0
+!EnableNat=1
+!TunnelingMode=0
+!TcpTunnelingPort=10000
+CertStore=0
+CertName=
+CertPath=
+CertSubjectName=
+CertSerialHash=00000000000000000000000000000000
+SendCertChain=0
+VerifyCertDN=
+DHGroup=2
+ForceKeepAlives=1
+PeerTimeout=90
+!EnableLocalLAN=1
+!EnableSplitDNS=1
+ISPPhonebook=
+X-NM-Routes=10.0.0.0/8 172.16.0.0/16
+
diff --git a/properties/tests/test-import-export.c b/properties/tests/test-import-export.c
index 9d0bf97..12884ef 100644
--- a/properties/tests/test-import-export.c
+++ b/properties/tests/test-import-export.c
@@ -23,6 +23,7 @@
 #include <dbus/dbus-glib.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
+#include <locale.h>
 
 #include <nm-utils.h>
 #include <nm-setting-connection.h>
@@ -427,6 +428,42 @@ test_always_ask (NMVpnPluginUiInterface *plugin, const char *dir)
 	g_free (pcf);
 }
 
+static void
+test_non_utf8_import (NMVpnPluginUiInterface *plugin, const char *dir)
+{
+	NMConnection *connection;
+	NMSettingConnection *s_con;
+	NMSettingVPN *s_vpn;
+	const char *expected_id = "Att äta en ko";
+	const char *charset = NULL;
+
+	/* Change charset to ISO-8859-15 to match iso885915.pcf */
+	g_get_charset (&charset);
+	setlocale (LC_ALL, "de_DE euro");
+	connection = get_basic_connection ("non-utf8-import", plugin, dir, "iso885915.pcf");
+	setlocale (LC_ALL, charset);
+
+	ASSERT (connection != NULL, "non-utf8-import", "failed to import connection");
+
+	/* Connection setting */
+	s_con = (NMSettingConnection *) nm_connection_get_setting (connection, NM_TYPE_SETTING_CONNECTION);
+	ASSERT (s_con != NULL,
+	        "non-utf8-import", "missing 'connection' setting");
+
+	ASSERT (strcmp (nm_setting_connection_get_id (s_con), expected_id) == 0,
+	        "non-utf8-import", "unexpected connection ID");
+
+	ASSERT (nm_setting_connection_get_uuid (s_con) == NULL,
+	        "non-utf8-import", "unexpected valid UUID");
+
+	/* VPN setting */
+	s_vpn = (NMSettingVPN *) nm_connection_get_setting (connection, NM_TYPE_SETTING_VPN);
+	ASSERT (s_vpn != NULL,
+	        "non-utf8-import", "missing 'vpn' setting");
+
+	g_object_unref (connection);
+}
+
 int main (int argc, char **argv)
 {
 	GError *error = NULL;
@@ -454,6 +491,7 @@ int main (int argc, char **argv)
 	test_everything_via_vpn (plugin, argv[1]);
 	test_no_natt (plugin, argv[1]);
 	test_always_ask (plugin, argv[1]);
+	test_non_utf8_import (plugin, argv[1]);
 
 	test_basic_export (plugin, argv[1]);
 



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