[glib-networking] proxy/gnome: port to GSimpleProxyResolver



commit f87772cf8d68dc4258af93da790e17eb2ba8bcfa
Author: Dan Winship <danw gnome org>
Date:   Mon Jan 28 11:06:21 2013 -0500

    proxy/gnome: port to GSimpleProxyResolver
    
    Make GProxyResolverGnome a wrapper around GSimpleProxyResolver, and
    remove all the code that is no longer needed as a result.
    
    (It can't be a subclass because of odd GType restrictions involving
    interfaces and dynamic types.)
    
    Also add more tests to proxy/tests/gnome, based on the
    GSimpleProxyResolver tests.

 proxy/gnome/gproxyresolvergnome.c |  369 ++++++++++---------------------------
 proxy/tests/gnome.c               |  166 ++++++++++++++++-
 2 files changed, 259 insertions(+), 276 deletions(-)
---
diff --git a/proxy/gnome/gproxyresolvergnome.c b/proxy/gnome/gproxyresolvergnome.c
index 44e104c..d47c6e5 100644
--- a/proxy/gnome/gproxyresolvergnome.c
+++ b/proxy/gnome/gproxyresolvergnome.c
@@ -51,15 +51,16 @@
 #define GNOME_PROXY_SOCKS_HOST_KEY        "host"
 #define GNOME_PROXY_SOCKS_PORT_KEY        "port"
 
-typedef struct {
-  gchar        *name;
-  gint          length;
-  gushort       port;
-} GProxyResolverGnomeDomain;
+/* We have to has-a GSimpleProxyResolver rather than is-a one,
+ * because a dynamic type cannot reimplement an interface that
+ * its parent also implements... for some reason.
+ */
 
 struct _GProxyResolverGnome {
   GObject parent_instance;
 
+  GProxyResolver *base_resolver;
+
   GSettings *proxy_settings;
   GSettings *http_settings;
   GSettings *https_settings;
@@ -71,17 +72,13 @@ struct _GProxyResolverGnome {
   gchar *autoconfig_url;
   gboolean use_same_proxy;
 
-  GPtrArray *ignore_ips;
-  GProxyResolverGnomeDomain *ignore_domains;
-
-  gchar *http_proxy, *https_proxy;
-  gchar *ftp_proxy, *socks_authority;
-
   GDBusProxy *pacrunner;
 
   GMutex lock;
 };
 
+static GProxyResolverInterface *g_proxy_resolver_gnome_parent_iface;
+
 static void g_proxy_resolver_gnome_iface_init (GProxyResolverInterface *iface);
 
 G_DEFINE_DYNAMIC_TYPE_EXTENDED (GProxyResolverGnome,
@@ -96,27 +93,6 @@ g_proxy_resolver_gnome_class_finalize (GProxyResolverGnomeClass *klass)
 }
 
 static void
-free_settings (GProxyResolverGnome *resolver)
-{
-  int i;
-
-  if (resolver->ignore_ips)
-    g_ptr_array_free (resolver->ignore_ips, TRUE);
-  if (resolver->ignore_domains)
-    {
-      for (i = 0; resolver->ignore_domains[i].name; i++)
-       g_free (resolver->ignore_domains[i].name);
-      g_free (resolver->ignore_domains);
-    }
-
-  g_free (resolver->http_proxy);
-  g_free (resolver->https_proxy);
-  g_free (resolver->ftp_proxy);
-  g_free (resolver->socks_authority);
-  g_free (resolver->autoconfig_url);
-}
-
-static void
 gsettings_changed (GSettings   *settings,
                   const gchar *key,
                   gpointer     user_data)
@@ -159,12 +135,12 @@ g_proxy_resolver_gnome_finalize (GObject *object)
                                            (gpointer)gsettings_changed,
                                            resolver);
       g_object_unref (resolver->socks_settings);
-
-      free_settings (resolver);
     }
 
-  if (resolver->pacrunner)
-    g_object_unref (resolver->pacrunner);
+  g_clear_object (&resolver->base_resolver);
+  g_clear_object (&resolver->pacrunner);
+
+  g_free (resolver->autoconfig_url);
 
   g_mutex_clear (&resolver->lock);
 
@@ -176,6 +152,8 @@ g_proxy_resolver_gnome_init (GProxyResolverGnome *resolver)
 {
   g_mutex_init (&resolver->lock);
 
+  resolver->base_resolver = g_simple_proxy_resolver_new (NULL, NULL);
+
   resolver->proxy_settings = g_settings_new (GNOME_PROXY_SETTINGS_SCHEMA);
   g_signal_connect (resolver->proxy_settings, "changed",
                    G_CALLBACK (gsettings_changed), resolver);
@@ -203,121 +181,60 @@ g_proxy_resolver_gnome_init (GProxyResolverGnome *resolver)
 static void
 update_settings (GProxyResolverGnome *resolver)
 {
+  GSimpleProxyResolver *simple = G_SIMPLE_PROXY_RESOLVER (resolver->base_resolver);
   gchar **ignore_hosts;
-  gchar *host;
+  gchar *host, *http_proxy, *proxy;
   guint port;
-  int i;
 
   resolver->need_update = FALSE;
 
-  free_settings (resolver);
+  g_free (resolver->autoconfig_url);
+  g_simple_proxy_resolver_set_default_proxy (simple, NULL);
+  g_simple_proxy_resolver_set_ignore_hosts (simple, NULL);
+  g_simple_proxy_resolver_set_uri_proxy (simple, "http", NULL);
+  g_simple_proxy_resolver_set_uri_proxy (simple, "https", NULL);
+  g_simple_proxy_resolver_set_uri_proxy (simple, "ftp", NULL);
 
   resolver->mode =
     g_settings_get_enum (resolver->proxy_settings, GNOME_PROXY_MODE_KEY);
   resolver->autoconfig_url =
     g_settings_get_string (resolver->proxy_settings, GNOME_PROXY_AUTOCONFIG_URL_KEY);
-  resolver->use_same_proxy =
-    g_settings_get_boolean (resolver->proxy_settings, GNOME_PROXY_USE_SAME_PROXY_KEY);
 
-  ignore_hosts =
-    g_settings_get_strv (resolver->proxy_settings, GNOME_PROXY_IGNORE_HOSTS_KEY);
-  if (ignore_hosts && ignore_hosts[0])
+  if (resolver->mode == G_DESKTOP_PROXY_MODE_AUTO && !resolver->pacrunner)
     {
-      GPtrArray *ignore_ips;
-      GArray *ignore_domains;
-      gchar *host, *tmp, *colon, *bracket;
-      GInetAddress *iaddr;
-      GInetAddressMask *mask;
-      GProxyResolverGnomeDomain domain;
-      gushort port;
-
-      ignore_ips = g_ptr_array_new_with_free_func (g_object_unref);
-      ignore_domains = g_array_new (TRUE, FALSE, sizeof (GProxyResolverGnomeDomain));
-
-      for (i = 0; ignore_hosts[i]; i++)
-       {
-         host = g_strchomp (ignore_hosts[i]);
-
-         /* See if it's an IP address or IP/length mask */
-         mask = g_inet_address_mask_new_from_string (host, NULL);
-         if (mask)
-           {
-             g_ptr_array_add (ignore_ips, mask);
-             continue;
-           }
-
-         port = 0;
-
-         if (*host == '[')
-           {
-             /* [IPv6]:port */
-             host++;
-             bracket = strchr (host, ']');
-             if (!bracket || !bracket[1] || bracket[1] != ':')
-               goto bad;
-
-             port = strtoul (bracket + 2, &tmp, 10);
-             if (*tmp)
-               goto bad;
-
-             *bracket = '\0';
-           }
-         else
-           {
-             colon = strchr (host, ':');
-             if (colon && !strchr (colon + 1, ':'))
-               {
-                 /* hostname:port or IPv4:port */
-                 port = strtoul (colon + 1, &tmp, 10);
-                 if (*tmp)
-                   goto bad;
-                 *colon = '\0';
-               }
-           }
-
-         iaddr = g_inet_address_new_from_string (host);
-         if (iaddr)
-           g_object_unref (iaddr);
-         else
-           {
-             if (g_str_has_prefix (host, "*."))
-               host += 2;
-             else if (*host == '.')
-               host++;
-           }
-
-         memset (&domain, 0, sizeof (domain));
-         domain.name = g_strdup (host);
-         domain.length = strlen (domain.name);
-         domain.port = port;
-         g_array_append_val (ignore_domains, domain);
-         continue;
-
-       bad:
-         g_warning ("Ignoring invalid ignore_hosts value '%s'", host);
-       }
-
-      if (ignore_ips->len)
-       resolver->ignore_ips = ignore_ips;
-      else
+      GError *error = NULL;
+      resolver->pacrunner =
+       g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
+                                      G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
+                                      G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
+                                      NULL,
+                                      "org.gtk.GLib.PACRunner",
+                                      "/org/gtk/GLib/PACRunner",
+                                      "org.gtk.GLib.PACRunner",
+                                      NULL, &error);
+      if (error)
        {
-         g_ptr_array_free (ignore_ips, TRUE);
-         resolver->ignore_ips = NULL;
+         g_warning ("Could not start proxy autoconfiguration helper:"
+                    "\n    %s\nProxy autoconfiguration will not work",
+                    error->message);
        }
-
-      resolver->ignore_domains = (GProxyResolverGnomeDomain *)
-       g_array_free (ignore_domains, ignore_domains->len == 0);
     }
-  else
+  else if (resolver->mode != G_DESKTOP_PROXY_MODE_AUTO && resolver->pacrunner)
     {
-      resolver->ignore_ips = NULL;
-      resolver->ignore_domains = NULL;
+      g_object_unref (resolver->pacrunner);
+      resolver->pacrunner = NULL;
     }
+
+  if (resolver->mode != G_DESKTOP_PROXY_MODE_MANUAL)
+    return;
+
+  ignore_hosts =
+    g_settings_get_strv (resolver->proxy_settings, GNOME_PROXY_IGNORE_HOSTS_KEY);
+  g_simple_proxy_resolver_set_ignore_hosts (simple, ignore_hosts);
   g_strfreev (ignore_hosts);
 
   host = g_settings_get_string (resolver->http_settings, GNOME_PROXY_HTTP_HOST_KEY);
   port = g_settings_get_int (resolver->http_settings, GNOME_PROXY_HTTP_PORT_KEY);
-
   if (host && *host)
     {
       if (g_settings_get_boolean (resolver->http_settings, GNOME_PROXY_HTTP_USE_AUTH_KEY))
@@ -332,59 +249,56 @@ update_settings (GProxyResolverGnome *resolver)
          enc_password = g_uri_escape_string (password, NULL, TRUE);
          g_free (password);
 
-         resolver->http_proxy = g_strdup_printf ("http://%s:%s %s:%u",
-                                                 enc_user, enc_password,
-                                                 host, port);
+         http_proxy = g_strdup_printf ("http://%s:%s %s:%u",
+                                       enc_user, enc_password,
+                                       host, port);
          g_free (enc_user);
          g_free (enc_password);
        }
       else
-       resolver->http_proxy = g_strdup_printf ("http://%s:%u";, host, port);
+       http_proxy = g_strdup_printf ("http://%s:%u";, host, port);
+
+      g_simple_proxy_resolver_set_uri_proxy (simple, "http", http_proxy);
+      if (g_settings_get_boolean (resolver->proxy_settings, GNOME_PROXY_USE_SAME_PROXY_KEY))
+       g_simple_proxy_resolver_set_default_proxy (simple, http_proxy);
     }
+  else
+    http_proxy = NULL;
   g_free (host);
 
   host = g_settings_get_string (resolver->https_settings, GNOME_PROXY_HTTPS_HOST_KEY);
   port = g_settings_get_int (resolver->https_settings, GNOME_PROXY_HTTPS_PORT_KEY);
   if (host && *host)
-    resolver->https_proxy = g_strdup_printf ("http://%s:%u";, host, port);
-  g_free (host);
-
-  host = g_settings_get_string (resolver->ftp_settings, GNOME_PROXY_FTP_HOST_KEY);
-  port = g_settings_get_int (resolver->ftp_settings, GNOME_PROXY_FTP_PORT_KEY);
-  if (host && *host)
-    resolver->ftp_proxy = g_strdup_printf ("ftp://%s:%u";, host, port);
+    {
+      proxy = g_strdup_printf ("http://%s:%u";, host, port);
+      g_simple_proxy_resolver_set_uri_proxy (simple, "https", proxy);
+      g_free (proxy);
+    }
+  else if (http_proxy)
+    g_simple_proxy_resolver_set_uri_proxy (simple, "https", http_proxy);
   g_free (host);
 
   host = g_settings_get_string (resolver->socks_settings, GNOME_PROXY_SOCKS_HOST_KEY);
   port = g_settings_get_int (resolver->socks_settings, GNOME_PROXY_SOCKS_PORT_KEY);
   if (host && *host)
-    resolver->socks_authority = g_strdup_printf ("%s:%u", host, port);
-  g_free (host);
-
-  if (resolver->mode == G_DESKTOP_PROXY_MODE_AUTO && !resolver->pacrunner)
     {
-      GError *error = NULL;
-      resolver->pacrunner =
-       g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
-                                      G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
-                                      G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
-                                      NULL,
-                                      "org.gtk.GLib.PACRunner",
-                                      "/org/gtk/GLib/PACRunner",
-                                      "org.gtk.GLib.PACRunner",
-                                      NULL, &error);
-      if (error)
-       {
-         g_warning ("Could not start proxy autoconfiguration helper:"
-                    "\n    %s\nProxy autoconfiguration will not work",
-                    error->message);
-       }
+      proxy = g_strdup_printf ("socks://%s:%u", host, port);
+      g_simple_proxy_resolver_set_default_proxy (simple, proxy);
+      g_free (proxy);
     }
-  else if (resolver->mode != G_DESKTOP_PROXY_MODE_AUTO && resolver->pacrunner)
+  g_free (host);
+
+  g_free (http_proxy);
+
+  host = g_settings_get_string (resolver->ftp_settings, GNOME_PROXY_FTP_HOST_KEY);
+  port = g_settings_get_int (resolver->ftp_settings, GNOME_PROXY_FTP_PORT_KEY);
+  if (host && *host)
     {
-      g_object_unref (resolver->pacrunner);
-      resolver->pacrunner = NULL;
+      proxy = g_strdup_printf ("ftp://%s:%u";, host, port);
+      g_simple_proxy_resolver_set_uri_proxy (simple, "ftp", proxy);
+      g_free (proxy);
     }
+  g_free (host);
 }
 
 static gboolean
@@ -393,64 +307,6 @@ g_proxy_resolver_gnome_is_supported (GProxyResolver *object)
   return !g_strcmp0 (g_getenv ("DESKTOP_SESSION"), "gnome");
 }
 
-static gboolean
-ignore_host (GProxyResolverGnome *resolver,
-            const gchar         *host,
-            gushort              port)
-{
-  gchar *ascii_host = NULL;
-  gboolean ignore = FALSE;
-  gint i, length, offset;
-
-  if (resolver->ignore_ips)
-    {
-      GInetAddress *iaddr;
-
-      iaddr = g_inet_address_new_from_string (host);
-      if (iaddr)
-       {
-         for (i = 0; i < resolver->ignore_ips->len; i++)
-           {
-             GInetAddressMask *mask = resolver->ignore_ips->pdata[i];
-
-             if (g_inet_address_mask_matches (mask, iaddr))
-               {
-                 ignore = TRUE;
-                 break;
-               }
-           }
-
-         g_object_unref (iaddr);
-         if (ignore)
-           return TRUE;
-       }
-    }
-
-  if (g_hostname_is_non_ascii (host))
-    host = ascii_host = g_hostname_to_ascii (host);
-  length = strlen (host);
-
-  if (resolver->ignore_domains)
-    {
-      for (i = 0; resolver->ignore_domains[i].length; i++)
-       {
-         GProxyResolverGnomeDomain *domain = &resolver->ignore_domains[i];
-
-         offset = length - domain->length;
-         if ((domain->port == 0 || domain->port == port) &&
-             (offset == 0 || (offset > 0 && host[offset - 1] == '.')) &&
-             (g_ascii_strcasecmp (domain->name, host + offset) == 0))
-           {
-             ignore = TRUE;
-             break;
-           }
-       }
-    }
-
-  g_free (ascii_host);
-  return ignore;
-}
-
 static inline gchar **
 make_proxies (const gchar *proxy)
 {
@@ -477,9 +333,7 @@ g_proxy_resolver_gnome_lookup_internal (GProxyResolverGnome   *resolver,
                                        GCancellable          *cancellable,
                                        GError               **error)
 {
-  GSocketConnectable *addr = NULL;
-  const gchar *scheme = NULL, *host = NULL;
-  gushort port;
+  gchar **proxies = NULL;
 
   *out_proxies = NULL;
   *out_pacrunner = NULL;
@@ -489,67 +343,32 @@ g_proxy_resolver_gnome_lookup_internal (GProxyResolverGnome   *resolver,
   if (resolver->need_update)
     update_settings (resolver);
 
-  if (resolver->mode == G_DESKTOP_PROXY_MODE_NONE)
-    {
-      *out_proxies = make_proxies ("direct://");
-      goto done;
-    }
-
-  /* FIXME: use guri when it lands... */
-  addr = g_network_address_parse_uri (uri, 0, error);
-  if (!addr)
+  proxies = g_proxy_resolver_lookup (resolver->base_resolver,
+                                    uri, cancellable, error);
+  if (!proxies)
     goto done;
-  scheme = g_network_address_get_scheme (G_NETWORK_ADDRESS (addr));
-  host = g_network_address_get_hostname (G_NETWORK_ADDRESS (addr));
-  port = g_network_address_get_port (G_NETWORK_ADDRESS (addr));
 
-  if (ignore_host (resolver, host, port))
-    {
-      *out_proxies = make_proxies ("direct://");
-      goto done;
-    }
+  /* Parent class does ignore-host handling */
+  if (!strcmp (proxies[0], "direct://") && !proxies[1])
+    goto done;
 
   if (resolver->pacrunner)
     {
+      g_clear_pointer (&proxies, g_strfreev);
       *out_pacrunner = g_object_ref (resolver->pacrunner);
       *out_autoconfig_url = g_strdup (resolver->autoconfig_url);
       goto done;
     }
-  else if (resolver->ftp_proxy &&
-          (!strcmp (scheme, "ftp") || !strcmp (scheme, "ftps")))
-    {
-      *out_proxies = make_proxies (resolver->ftp_proxy);
-    }
-  else if (resolver->https_proxy && !strcmp (scheme, "https"))
-    {
-      *out_proxies = make_proxies (resolver->https_proxy);
-    }
-  else if (resolver->http_proxy &&
-      (!strcmp (scheme, "http") || !strcmp (scheme, "https")))
-    {
-      *out_proxies = make_proxies (resolver->http_proxy);
-    }
-  else if (resolver->socks_authority)
-    {
-      *out_proxies = g_new (gchar *, 4);
-      *out_proxies[0] = g_strdup_printf ("socks5://%s", resolver->socks_authority);
-      *out_proxies[1] = g_strdup_printf ("socks4a://%s", resolver->socks_authority);
-      *out_proxies[2] = g_strdup_printf ("socks4://%s", resolver->socks_authority);
-      *out_proxies[3] = NULL;
-    }
-  else if (resolver->use_same_proxy && resolver->http_proxy)
-    {
-      *out_proxies = make_proxies (resolver->http_proxy);
-    }
-  else
-    *out_proxies = make_proxies ("direct://");
 
-done:
-  if (addr)
-    g_object_unref (addr);
+ done:
   g_mutex_unlock (&resolver->lock);
 
-  if (*out_proxies || *out_pacrunner)
+  if (proxies)
+    {
+      *out_proxies = proxies;
+      return TRUE;
+    }
+  else if (*out_pacrunner)
     return TRUE;
   else
     return FALSE;
@@ -686,6 +505,8 @@ g_proxy_resolver_gnome_class_init (GProxyResolverGnomeClass *resolver_class)
 static void
 g_proxy_resolver_gnome_iface_init (GProxyResolverInterface *iface)
 {
+  g_proxy_resolver_gnome_parent_iface = g_type_interface_peek_parent (iface);
+
   iface->is_supported = g_proxy_resolver_gnome_is_supported;
   iface->lookup = g_proxy_resolver_gnome_lookup;
   iface->lookup_async = g_proxy_resolver_gnome_lookup_async;
diff --git a/proxy/tests/gnome.c b/proxy/tests/gnome.c
index 431c449..69f43ac 100644
--- a/proxy/tests/gnome.c
+++ b/proxy/tests/gnome.c
@@ -45,6 +45,159 @@
 #define GNOME_PROXY_SOCKS_HOST_KEY        "host"
 #define GNOME_PROXY_SOCKS_PORT_KEY        "port"
 
+static void
+reset_proxy_settings (gpointer      fixture,
+                     gconstpointer user_data)
+{
+  GSettings *settings, *child;
+
+  settings = g_settings_new (GNOME_PROXY_SETTINGS_SCHEMA);
+  g_settings_reset (settings, GNOME_PROXY_MODE_KEY);
+  g_settings_reset (settings, GNOME_PROXY_USE_SAME_PROXY_KEY);
+
+  child = g_settings_get_child (settings, GNOME_PROXY_HTTP_CHILD_SCHEMA);
+  g_settings_reset (child, GNOME_PROXY_HTTP_HOST_KEY);
+  g_settings_reset (child, GNOME_PROXY_HTTP_PORT_KEY);
+  g_object_unref (child);
+
+  child = g_settings_get_child (settings, GNOME_PROXY_HTTPS_CHILD_SCHEMA);
+  g_settings_reset (child, GNOME_PROXY_HTTPS_HOST_KEY);
+  g_settings_reset (child, GNOME_PROXY_HTTPS_PORT_KEY);
+  g_object_unref (child);
+
+  child = g_settings_get_child (settings, GNOME_PROXY_FTP_CHILD_SCHEMA);
+  g_settings_reset (child, GNOME_PROXY_FTP_HOST_KEY);
+  g_settings_reset (child, GNOME_PROXY_FTP_PORT_KEY);
+  g_object_unref (child);
+
+  child = g_settings_get_child (settings, GNOME_PROXY_SOCKS_CHILD_SCHEMA);
+  g_settings_reset (child, GNOME_PROXY_SOCKS_HOST_KEY);
+  g_settings_reset (child, GNOME_PROXY_SOCKS_PORT_KEY);
+  g_object_unref (child);
+
+  g_object_unref (settings);
+}
+
+static void
+test_proxy_uri (gpointer      fixture,
+               gconstpointer user_data)
+{
+  GSettings *settings, *child;
+  GProxyResolver *resolver;
+  gchar **proxies;
+  GError *error = NULL;
+
+  settings = g_settings_new (GNOME_PROXY_SETTINGS_SCHEMA);
+  g_settings_set_enum (settings, GNOME_PROXY_MODE_KEY, G_DESKTOP_PROXY_MODE_MANUAL);
+  g_settings_set_boolean (settings, GNOME_PROXY_USE_SAME_PROXY_KEY, TRUE);
+
+  child = g_settings_get_child (settings, GNOME_PROXY_HTTP_CHILD_SCHEMA);
+  g_settings_set_string (child, GNOME_PROXY_HTTP_HOST_KEY, "proxy.example.com");
+  g_settings_set_int (child, GNOME_PROXY_HTTP_PORT_KEY, 8080);
+  g_object_unref (child);
+
+  child = g_settings_get_child (settings, GNOME_PROXY_HTTPS_CHILD_SCHEMA);
+  g_settings_set_string (child, GNOME_PROXY_HTTPS_HOST_KEY, "proxy-s.example.com");
+  g_settings_set_int (child, GNOME_PROXY_HTTPS_PORT_KEY, 7070);
+  g_object_unref (child);
+
+  child = g_settings_get_child (settings, GNOME_PROXY_FTP_CHILD_SCHEMA);
+  g_settings_set_string (child, GNOME_PROXY_FTP_HOST_KEY, "proxy-f.example.com");
+  g_settings_set_int (child, GNOME_PROXY_FTP_PORT_KEY, 6060);
+  g_object_unref (child);
+
+  g_object_unref (settings);
+
+  resolver = g_proxy_resolver_get_default ();
+
+  proxies = g_proxy_resolver_lookup (resolver, "http://one.example.com/";,
+                                     NULL, &error);
+  g_assert_no_error (error);
+  g_assert_cmpint (g_strv_length (proxies), ==, 1);
+  g_assert_cmpstr (proxies[0], ==, "http://proxy.example.com:8080";);
+  g_strfreev (proxies);
+
+  proxies = g_proxy_resolver_lookup (resolver, "HTTPS://uppercase.example.com/",
+                                     NULL, &error);
+  g_assert_no_error (error);
+  g_assert_cmpint (g_strv_length (proxies), ==, 1);
+  g_assert_cmpstr (proxies[0], ==, "http://proxy-s.example.com:7070";);
+  g_strfreev (proxies);
+
+  /* Because we set use_same_proxy = TRUE, unknown protocols will use
+   * the http proxy by default.
+   */
+  proxies = g_proxy_resolver_lookup (resolver, "htt://missing-letter.example.com/",
+                                     NULL, &error);
+  g_assert_no_error (error);
+  g_assert_cmpint (g_strv_length (proxies), ==, 1);
+  g_assert_cmpstr (proxies[0], ==, "http://proxy.example.com:8080";);
+  g_strfreev (proxies);
+
+  proxies = g_proxy_resolver_lookup (resolver, "ftps://extra-letter.example.com/",
+                                     NULL, &error);
+  g_assert_no_error (error);
+  g_assert_cmpint (g_strv_length (proxies), ==, 1);
+  g_assert_cmpstr (proxies[0], ==, "http://proxy.example.com:8080";);
+  g_strfreev (proxies);
+
+  proxies = g_proxy_resolver_lookup (resolver, "ftp://five.example.com/";,
+                                     NULL, &error);
+  g_assert_no_error (error);
+  g_assert_cmpint (g_strv_length (proxies), ==, 1);
+  g_assert_cmpstr (proxies[0], ==, "ftp://proxy-f.example.com:6060";);
+  g_strfreev (proxies);
+}
+
+static void
+test_proxy_socks (gpointer      fixture,
+                 gconstpointer user_data)
+{
+  GSettings *settings, *child;
+  GProxyResolver *resolver;
+  const gchar *ignore_hosts[2] = { "127.0.0.1", NULL };
+  gchar **proxies;
+  GError *error = NULL;
+
+  settings = g_settings_new (GNOME_PROXY_SETTINGS_SCHEMA);
+  g_settings_set_enum (settings, GNOME_PROXY_MODE_KEY, G_DESKTOP_PROXY_MODE_MANUAL);
+  g_settings_set (settings, GNOME_PROXY_IGNORE_HOSTS_KEY,
+                 "@as", g_variant_new_strv (ignore_hosts, -1));
+
+  child = g_settings_get_child (settings, GNOME_PROXY_SOCKS_CHILD_SCHEMA);
+  g_settings_set_string (child, GNOME_PROXY_SOCKS_HOST_KEY, "proxy.example.com");
+  g_settings_set_int (child, GNOME_PROXY_SOCKS_PORT_KEY, 1234);
+  g_object_unref (child);
+  g_object_unref (settings);
+
+  resolver = g_proxy_resolver_get_default ();
+
+  proxies = g_proxy_resolver_lookup (resolver, "http://one.example.com/";,
+                                     NULL, &error);
+  g_assert_no_error (error);
+  g_assert_cmpint (g_strv_length (proxies), ==, 3);
+  g_assert_cmpstr (proxies[0], ==, "socks5://proxy.example.com:1234");
+  g_assert_cmpstr (proxies[1], ==, "socks4a://proxy.example.com:1234");
+  g_assert_cmpstr (proxies[2], ==, "socks4://proxy.example.com:1234");
+  g_strfreev (proxies);
+
+  proxies = g_proxy_resolver_lookup (resolver, "wednesday://two.example.com/",
+                                     NULL, &error);
+  g_assert_no_error (error);
+  g_assert_cmpint (g_strv_length (proxies), ==, 3);
+  g_assert_cmpstr (proxies[0], ==, "socks5://proxy.example.com:1234");
+  g_assert_cmpstr (proxies[1], ==, "socks4a://proxy.example.com:1234");
+  g_assert_cmpstr (proxies[2], ==, "socks4://proxy.example.com:1234");
+  g_strfreev (proxies);
+
+  proxies = g_proxy_resolver_lookup (resolver, "http://127.0.0.1/";,
+                                     NULL, &error);
+  g_assert_no_error (error);
+  g_assert_cmpint (g_strv_length (proxies), ==, 1);
+  g_assert_cmpstr (proxies[0], ==, "direct://");
+  g_strfreev (proxies);
+}
+
 static const char *ignore_hosts[] = {
   ".bbb.xx",
   "*.ccc.xx",
@@ -110,7 +263,8 @@ static const struct {
 static const int n_ignore_tests = G_N_ELEMENTS (ignore_tests);
 
 static void
-test_proxy_ignore (void)
+test_proxy_ignore (gpointer      fixture,
+                  gconstpointer user_data)
 {
   GSettings *settings, *http;
   GProxyResolver *resolver;
@@ -127,6 +281,9 @@ test_proxy_ignore (void)
   g_settings_set_string (http, GNOME_PROXY_HTTP_HOST_KEY, "localhost");
   g_settings_set_int (http, GNOME_PROXY_HTTP_PORT_KEY, 8080);
 
+  g_object_unref (http);
+  g_object_unref (settings);
+
   resolver = g_proxy_resolver_get_default ();
 
   for (i = 0; i < n_ignore_tests; i++)
@@ -151,7 +308,12 @@ main (int   argc,
   g_setenv ("GSETTINGS_BACKEND", "memory", TRUE);
   g_setenv ("DESKTOP_SESSION", "gnome", TRUE);
 
-  g_test_add_func ("/proxy/gnome/ignore", test_proxy_ignore);
+  g_test_add_vtable ("/proxy/gnome/uri", 0, NULL,
+                    reset_proxy_settings, test_proxy_uri, NULL);
+  g_test_add_vtable ("/proxy/gnome/socks", 0, NULL,
+                    reset_proxy_settings, test_proxy_socks, NULL);
+  g_test_add_vtable ("/proxy/gnome/ignore", 0, NULL,
+                    reset_proxy_settings, test_proxy_ignore, NULL);
 
   return g_test_run();
 }


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