[libgda/LIBGDA_4.0] Corrections to gda_connection_string_split()



commit 3c389a3ef18e68c5305e232b2d015cff12bf994d
Author: Vivien Malerba <malerba gnome-db org>
Date:   Mon Oct 18 23:14:47 2010 +0200

    Corrections to gda_connection_string_split()
    
    regarding USERNAME and PASSWORD and added new tests

 libgda/gda-util.c                    |   78 +++++++++++++++------------
 tests/test-connection-string-split.c |   96 ++++++++++++++++++++++++---------
 2 files changed, 114 insertions(+), 60 deletions(-)
---
diff --git a/libgda/gda-util.c b/libgda/gda-util.c
index 0d604b8..e3a63bc 100644
--- a/libgda/gda-util.c
+++ b/libgda/gda-util.c
@@ -1956,7 +1956,7 @@ static char rfc1738_reserved_chars[] =
  * <constant>&quot;%%ab&quot;</constant> where
  * <constant>ab</constant> is the hexadecimal number corresponding to the character.
  *
- * Returns: a new string
+ * Returns: (transfer full): a new string
  */
 gchar *
 gda_rfc1738_encode (const gchar *string)
@@ -2019,7 +2019,7 @@ gda_rfc1738_encode (const gchar *string)
 
 /**
  * gda_rfc1738_decode
- * @string: a string to encode 
+ * @string: a string to decode
  *
  * Decodes @string using the RFC 1738 recommendations: the
  * <constant>&lt;&gt;&quot;#%{}|\^~[]&apos;`;/?:@=&amp;</constant> and space characters are replaced by 
@@ -2216,40 +2216,50 @@ gda_connection_string_split (const gchar *string, gchar **out_cnc_params, gchar
 	if (*out_cnc_params) {
 		gchar *pos;
 
-		pos = g_strrstr (*out_cnc_params, "USERNAME=");
-		if (pos) {
-			for (ptr = pos + 9; ptr && *ptr != '\0' && *ptr != ';'; ptr++);
-			if (ptr != pos + 9)
-				*out_username = g_strndup (pos + 9, ptr - (pos + 9));
-
-			if (*ptr)
-				g_memmove (pos, ptr + 1, strlen (ptr));
-			else
-				*pos = 0;
-			gchar *tmp;
-			gint len;
-			tmp = *out_cnc_params;
-			len = strlen (tmp) - 1;
-			if (tmp [len] == ';')
-				tmp [len] = 0;
+		pos = strstr (*out_cnc_params, "USERNAME=");
+		while (pos) {
+			if (((pos > *out_cnc_params) && (*(pos-1) == ';')) ||
+			    (pos  == *out_cnc_params)) {
+				for (ptr = pos + 9; ptr && *ptr != '\0' && *ptr != ';'; ptr++);
+				if (ptr != pos + 9)
+					*out_username = g_strndup (pos + 9, ptr - (pos + 9));
+				
+				if (*ptr)
+					g_memmove (pos, ptr + 1, strlen (ptr));
+				else
+					*pos = 0;
+				gchar *tmp;
+				gint len;
+				tmp = *out_cnc_params;
+				len = strlen (tmp) - 1;
+				if (tmp [len] == ';')
+					tmp [len] = 0;
+				break;
+			}
+			pos = strstr (pos + 9, "USERNAME=");
 		}
 
-		pos = g_strrstr (*out_cnc_params, "PASSWORD=");
-		if (pos) {
-			for (ptr = pos + 9; ptr && *ptr != '\0' && *ptr != ';'; ptr++);
-			if (ptr != pos + 9)
-				*out_password = g_strndup (pos + 9, ptr - (pos + 9));
-
-			if (*ptr)
-				g_memmove (pos, ptr + 1, strlen (ptr));
-			else
-				*pos = 0;
-			gchar *tmp;
-			gint len;
-			tmp = *out_cnc_params;
-			len = strlen (tmp) - 1;
-			if (tmp [len] == ';')
-				tmp [len] = 0;
+		pos = strstr (*out_cnc_params, "PASSWORD=");
+		while (pos) {
+			if (((pos > *out_cnc_params) && (*(pos-1) == ';')) ||
+			    (pos  == *out_cnc_params)) {
+				for (ptr = pos + 9; ptr && *ptr != '\0' && *ptr != ';'; ptr++);
+				if (ptr != pos + 9)
+					*out_password = g_strndup (pos + 9, ptr - (pos + 9));
+				
+				if (*ptr)
+					g_memmove (pos, ptr + 1, strlen (ptr));
+				else
+					*pos = 0;
+				gchar *tmp;
+				gint len;
+				tmp = *out_cnc_params;
+				len = strlen (tmp) - 1;
+				if (tmp [len] == ';')
+					tmp [len] = 0;
+				break;
+			}
+			pos = strstr (pos +  9, "PASSWORD=");
 		}
 	}
 
diff --git a/tests/test-connection-string-split.c b/tests/test-connection-string-split.c
index 0a3ff69..0f3388e 100644
--- a/tests/test-connection-string-split.c
+++ b/tests/test-connection-string-split.c
@@ -1,54 +1,98 @@
 #include <libgda/libgda.h>
 #include <string.h>
+
+typedef struct {
+	gchar *in_string;
+	gchar *exp_provider;
+	gchar *exp_cnc_params;
+	gchar *exp_user;
+	gchar *exp_pass;
+} ATest;
+
+ATest the_tests[] = {
+	{"PostgreSQL://meme:pass DB_NAME=mydb;HOST=server",
+	 "PostgreSQL", "DB_NAME=mydb;HOST=server",
+	 "meme", "pass"},
+	{"PostgreSQL://meme DB_NAME=mydb;HOST=server;PASSWORD=pass",
+	 "PostgreSQL", "DB_NAME=mydb;HOST=server",
+	 "meme", "pass"},
+	{"PostgreSQL://meme DB_NAME=mydb;PASSWORD=pass;HOST=server",
+	 "PostgreSQL", "DB_NAME=mydb;HOST=server",
+	 "meme", "pass"},
+	{"PostgreSQL://meme PASSWORD=pass;DB_NAME=mydb;HOST=server",
+	 "PostgreSQL", "DB_NAME=mydb;HOST=server",
+	 "meme", "pass"},
+	{"PostgreSQL://DB_NAME=mydb;HOST=server;USERNAME=meme;PASSWORD=pass",
+	 "PostgreSQL", "DB_NAME=mydb;HOST=server",
+	 "meme", "pass"},
+	{"PostgreSQL://DB_NAME=mydb;HOST=server;PASSWORD=pass;USERNAME=meme",
+	 "PostgreSQL", "DB_NAME=mydb;HOST=server",
+	 "meme", "pass"},
+	{"PostgreSQL://DB_NAME=mydb;USERNAME=meme;PASSWORD=pass;HOST=server",
+	 "PostgreSQL", "DB_NAME=mydb;HOST=server",
+	 "meme", "pass"},
+	{"PostgreSQL://PASSWORD=pass;USERNAME=meme;DB_NAME=mydb;HOST=server",
+	 "PostgreSQL", "DB_NAME=mydb;HOST=server",
+	 "meme", "pass"},
+	{"PostgreSQL://:pass USERNAME=meme;DB_NAME=mydb;HOST=server",
+	 "PostgreSQL", "DB_NAME=mydb;HOST=server",
+	 "meme", "pass"},
+	{"PostgreSQL://:pass DB_NAME=mydb;HOST=server;USERNAME=meme",
+	 "PostgreSQL", "DB_NAME=mydb;HOST=server",
+	 "meme", "pass"},
+	{"PORT=5432;ADM_LOGIN=gdauser;HOST=gdatester;ADM_PASSWORD=GdaUser;DB_NAME=testcheckdb",
+	 NULL,
+	 "PORT=5432;ADM_LOGIN=gdauser;HOST=gdatester;ADM_PASSWORD=GdaUser;DB_NAME=testcheckdb",
+	 NULL, NULL},
+	{"PORT=5432;ADM_LOGIN=gdauser;HOST=gdatester;ADM_PASSWORD=GdaUser;DB_NAME=testcheckdb;USERNAME=meme;PASSWORD=pass",
+	 NULL,
+	 "PORT=5432;ADM_LOGIN=gdauser;HOST=gdatester;ADM_PASSWORD=GdaUser;DB_NAME=testcheckdb",
+	 "meme", "pass"},
+	{"PORT=5432;ADM_LOGIN=gdauser;HOST=gdatester;USERNAME=meme;PASSWORD=pass;ADM_PASSWORD=GdaUser;DB_NAME=testcheckdb",
+	 NULL,
+	 "PORT=5432;ADM_LOGIN=gdauser;HOST=gdatester;ADM_PASSWORD=GdaUser;DB_NAME=testcheckdb",
+	 "meme", "pass"}
+};
+
 int
 main (int argc, char *argv[])
 {
 	g_type_init();
 	gda_init ();
 
-	gchar *str[] = {
-	"PostgreSQL://meme:pass DB_NAME=mydb;HOST=server",
-	"PostgreSQL://meme DB_NAME=mydb;HOST=server;PASSWORD=pass",
-	"PostgreSQL://meme DB_NAME=mydb;PASSWORD=pass;HOST=server",
-	"PostgreSQL://meme PASSWORD=pass;DB_NAME=mydb;HOST=server",
-	"PostgreSQL://DB_NAME=mydb;HOST=server;USERNAME=meme;PASSWORD=pass",
-	"PostgreSQL://DB_NAME=mydb;HOST=server;PASSWORD=pass;USERNAME=meme",
-        "PostgreSQL://DB_NAME=mydb;USERNAME=meme;PASSWORD=pass;HOST=server",
-        "PostgreSQL://PASSWORD=pass;USERNAME=meme;DB_NAME=mydb;HOST=server",
-        "PostgreSQL://:pass USERNAME=meme;DB_NAME=mydb;HOST=server",
-        "PostgreSQL://:pass DB_NAME=mydb;HOST=server;USERNAME=meme",
-	NULL};
-
 	gint i;
-	for (i = 0; ;i++) {
-		if (!str[i])
-			break;
+	for (i = 0; i < sizeof (the_tests) / sizeof (ATest); i++) {
+		ATest test = the_tests[i];
 		gchar *cnc_params, *prov, *user, *pass;
-		gda_connection_string_split (str[i], &cnc_params, &prov, &user, &pass);
+		gda_connection_string_split (test.in_string, &cnc_params, &prov, &user, &pass);
 		g_print ("[%s]\n  cnc_params=[%s]\n  prov      =[%s]\n  user      =[%s]\n  pass      =[%s]\n",
-			str[i], cnc_params, prov, user, pass);
-		if (strcmp (cnc_params, "DB_NAME=mydb;HOST=server")) {
+			test.in_string, cnc_params, prov, user, pass);
+		if (g_strcmp0 (cnc_params, test.exp_cnc_params)) {
 			g_print ("Wrong cnc_params result\n");
-			return 1;
+			goto onerror;
 		}
-		if (strcmp (prov, "PostgreSQL")) {
+		if (g_strcmp0 (prov, test.exp_provider)) {
 			g_print ("Wrong provider result\n");
-			return 1;
+			goto onerror;
 		}
-		if (strcmp (user, "meme")) {
+		if (g_strcmp0 (user, test.exp_user)) {
 			g_print ("Wrong username result\n");
-			return 1;
+			goto onerror;
 		}
-		if (strcmp (pass, "pass")) {
+		if (g_strcmp0 (pass, test.exp_pass)) {
 			g_print ("Wrong password result\n");
-			return 1;
+			goto onerror;
 		}
 		g_free (cnc_params);
 		g_free (prov);
 		g_free (user);
 		g_free (pass);
+
 	}
 
 	return 0;
+ onerror:
+	g_print ("Error, aborting\n");
+	return 1;
 }
 



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