LCOV - code coverage report
Current view: top level - glib/gio/tests - simple-proxy.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 131 131 100.0 %
Date: 2024-04-23 05:16:05 Functions: 5 5 100.0 %
Branches: 6 6 100.0 %

           Branch data     Line data    Source code
       1                 :            : /* GStaticProxyResolver tests
       2                 :            :  *
       3                 :            :  * Copyright 2011, 2013 Red Hat, Inc.
       4                 :            :  *
       5                 :            :  * SPDX-License-Identifier: LGPL-2.1-or-later
       6                 :            :  *
       7                 :            :  * This library is free software; you can redistribute it and/or
       8                 :            :  * modify it under the terms of the GNU Lesser General Public
       9                 :            :  * License as published by the Free Software Foundation; either
      10                 :            :  * version 2.1 of the License, or (at your option) any later version.
      11                 :            :  *
      12                 :            :  * This library is distributed in the hope that it will be useful,
      13                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15                 :            :  * Lesser General Public License for more details.
      16                 :            :  *
      17                 :            :  * You should have received a copy of the GNU Lesser General
      18                 :            :  * Public License along with this library; if not, see
      19                 :            :  * <http://www.gnu.org/licenses/>.
      20                 :            :  */
      21                 :            : 
      22                 :            : #include <gio/gio.h>
      23                 :            : 
      24                 :            : static void
      25                 :          2 : async_result_cb (GObject      *obj,
      26                 :            :                  GAsyncResult *result,
      27                 :            :                  gpointer      user_data)
      28                 :            : {
      29                 :          2 :   GAsyncResult **result_out = user_data;
      30                 :          2 :   *result_out = g_object_ref (result);
      31                 :          2 : }
      32                 :            : 
      33                 :            : static void
      34                 :          1 : test_uris (void)
      35                 :            : {
      36                 :            :   GProxyResolver *resolver;
      37                 :          1 :   const gchar *ignore_hosts[2] = { "127.0.0.1", NULL };
      38                 :            :   gchar **proxies;
      39                 :          1 :   GError *error = NULL;
      40                 :            :   const gchar *uri;
      41                 :          1 :   gchar *str = NULL;
      42                 :          1 :   GAsyncResult *result = NULL;
      43                 :            : 
      44                 :            :   /* Valid URI. */
      45                 :          1 :   uri = "http://%E0%B4%A8%E0%B4%B2:80/";
      46                 :          1 :   resolver = g_simple_proxy_resolver_new (NULL, (char **) ignore_hosts);
      47                 :            : 
      48                 :          1 :   proxies = g_proxy_resolver_lookup (resolver, uri, NULL, &error);
      49                 :          1 :   g_assert_no_error (error);
      50                 :          1 :   g_strfreev (proxies);
      51                 :            : 
      52                 :          1 :   g_proxy_resolver_lookup_async (resolver, uri, NULL, async_result_cb, &result);
      53         [ +  + ]:          2 :   while (result == NULL)
      54                 :          1 :     g_main_context_iteration (NULL, TRUE);
      55                 :          1 :   proxies = g_proxy_resolver_lookup_finish (resolver, result, &error);
      56                 :          1 :   g_assert_no_error (error);
      57                 :          1 :   g_strfreev (proxies);
      58                 :          1 :   g_clear_object (&result);
      59                 :            : 
      60                 :          1 :   g_object_unref (resolver);
      61                 :            : 
      62                 :            :   /* Invalid URI. */
      63                 :          1 :   uri = "%E0%B4%A8%E0%B4%B2";
      64                 :          1 :   str = g_strdup_printf ("Invalid URI ā€˜%sā€™", uri);
      65                 :          1 :   resolver = g_simple_proxy_resolver_new (NULL, (char **) ignore_hosts);
      66                 :            : 
      67                 :          1 :   proxies = g_proxy_resolver_lookup (resolver, uri, NULL, &error);
      68                 :          1 :   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
      69                 :          1 :   g_assert_cmpstr (error->message, ==, str);
      70                 :          1 :   g_clear_error (&error);
      71                 :          1 :   g_assert_null (proxies);
      72                 :          1 :   g_clear_object (&result);
      73                 :            : 
      74                 :          1 :   g_proxy_resolver_lookup_async (resolver, uri, NULL, async_result_cb, &result);
      75         [ +  + ]:          2 :   while (result == NULL)
      76                 :          1 :     g_main_context_iteration (NULL, TRUE);
      77                 :          1 :   proxies = g_proxy_resolver_lookup_finish (resolver, result, &error);
      78                 :          1 :   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
      79                 :          1 :   g_assert_cmpstr (error->message, ==, str);
      80                 :          1 :   g_clear_error (&error);
      81                 :          1 :   g_assert_null (proxies);
      82                 :          1 :   g_object_unref (result);
      83                 :            : 
      84                 :          1 :   g_object_unref (resolver);
      85                 :          1 :   g_free (str);
      86                 :            : 
      87                 :          1 :   resolver = g_simple_proxy_resolver_new ("default://", (char **) ignore_hosts);
      88                 :          1 :   g_simple_proxy_resolver_set_uri_proxy (G_SIMPLE_PROXY_RESOLVER (resolver),
      89                 :            :                                          "http", "http://proxy.example.com");
      90                 :          1 :   g_simple_proxy_resolver_set_uri_proxy (G_SIMPLE_PROXY_RESOLVER (resolver),
      91                 :            :                                          "ftp", "ftp://proxy.example.com");
      92                 :            : 
      93                 :          1 :   proxies = g_proxy_resolver_lookup (resolver, "http://one.example.com/",
      94                 :            :                                      NULL, &error);
      95                 :          1 :   g_assert_no_error (error);
      96                 :          1 :   g_assert_cmpint (g_strv_length (proxies), ==, 1);
      97                 :          1 :   g_assert_cmpstr (proxies[0], ==, "http://proxy.example.com");
      98                 :          1 :   g_strfreev (proxies);
      99                 :            : 
     100                 :          1 :   proxies = g_proxy_resolver_lookup (resolver, "HTTP://uppercase.example.com/",
     101                 :            :                                      NULL, &error);
     102                 :          1 :   g_assert_no_error (error);
     103                 :          1 :   g_assert_cmpint (g_strv_length (proxies), ==, 1);
     104                 :          1 :   g_assert_cmpstr (proxies[0], ==, "http://proxy.example.com");
     105                 :          1 :   g_strfreev (proxies);
     106                 :            : 
     107                 :          1 :   proxies = g_proxy_resolver_lookup (resolver, "htt://missing-letter.example.com/",
     108                 :            :                                      NULL, &error);
     109                 :          1 :   g_assert_no_error (error);
     110                 :          1 :   g_assert_cmpint (g_strv_length (proxies), ==, 1);
     111                 :          1 :   g_assert_cmpstr (proxies[0], ==, "default://");
     112                 :          1 :   g_strfreev (proxies);
     113                 :            : 
     114                 :          1 :   proxies = g_proxy_resolver_lookup (resolver, "https://extra-letter.example.com/",
     115                 :            :                                      NULL, &error);
     116                 :          1 :   g_assert_no_error (error);
     117                 :          1 :   g_assert_cmpint (g_strv_length (proxies), ==, 1);
     118                 :          1 :   g_assert_cmpstr (proxies[0], ==, "default://");
     119                 :          1 :   g_strfreev (proxies);
     120                 :            : 
     121                 :          1 :   proxies = g_proxy_resolver_lookup (resolver, "ftp://five.example.com/",
     122                 :            :                                      NULL, &error);
     123                 :          1 :   g_assert_no_error (error);
     124                 :          1 :   g_assert_cmpint (g_strv_length (proxies), ==, 1);
     125                 :          1 :   g_assert_cmpstr (proxies[0], ==, "ftp://proxy.example.com");
     126                 :          1 :   g_strfreev (proxies);
     127                 :            : 
     128                 :          1 :   proxies = g_proxy_resolver_lookup (resolver, "http://127.0.0.1/",
     129                 :            :                                      NULL, &error);
     130                 :          1 :   g_assert_no_error (error);
     131                 :          1 :   g_assert_cmpint (g_strv_length (proxies), ==, 1);
     132                 :          1 :   g_assert_cmpstr (proxies[0], ==, "direct://");
     133                 :          1 :   g_strfreev (proxies);
     134                 :            : 
     135                 :          1 :   g_object_unref (resolver);
     136                 :          1 : }
     137                 :            : 
     138                 :            : static void
     139                 :          1 : test_socks (void)
     140                 :            : {
     141                 :            :   GProxyResolver *resolver;
     142                 :          1 :   const gchar *ignore_hosts[2] = { "127.0.0.1", NULL };
     143                 :            :   gchar **proxies;
     144                 :          1 :   GError *error = NULL;
     145                 :            : 
     146                 :          1 :   resolver = g_simple_proxy_resolver_new ("socks://proxy.example.com", (char **) ignore_hosts);
     147                 :            : 
     148                 :          1 :   proxies = g_proxy_resolver_lookup (resolver, "http://one.example.com/",
     149                 :            :                                      NULL, &error);
     150                 :          1 :   g_assert_no_error (error);
     151                 :          1 :   g_assert_cmpint (g_strv_length (proxies), ==, 3);
     152                 :          1 :   g_assert_cmpstr (proxies[0], ==, "socks5://proxy.example.com");
     153                 :          1 :   g_assert_cmpstr (proxies[1], ==, "socks4a://proxy.example.com");
     154                 :          1 :   g_assert_cmpstr (proxies[2], ==, "socks4://proxy.example.com");
     155                 :          1 :   g_strfreev (proxies);
     156                 :            : 
     157                 :          1 :   proxies = g_proxy_resolver_lookup (resolver, "http://127.0.0.1/",
     158                 :            :                                      NULL, &error);
     159                 :          1 :   g_assert_no_error (error);
     160                 :          1 :   g_assert_cmpint (g_strv_length (proxies), ==, 1);
     161                 :          1 :   g_assert_cmpstr (proxies[0], ==, "direct://");
     162                 :          1 :   g_strfreev (proxies);
     163                 :            : 
     164                 :          1 :   g_object_unref (resolver);
     165                 :            : 
     166                 :          1 :   resolver = g_simple_proxy_resolver_new ("default-proxy://", (char **) ignore_hosts);
     167                 :          1 :   g_simple_proxy_resolver_set_uri_proxy (G_SIMPLE_PROXY_RESOLVER (resolver),
     168                 :            :                                          "http", "socks://proxy.example.com");
     169                 :            : 
     170                 :          1 :   proxies = g_proxy_resolver_lookup (resolver, "http://one.example.com/",
     171                 :            :                                      NULL, &error);
     172                 :          1 :   g_assert_no_error (error);
     173                 :          1 :   g_assert_cmpint (g_strv_length (proxies), ==, 3);
     174                 :          1 :   g_assert_cmpstr (proxies[0], ==, "socks5://proxy.example.com");
     175                 :          1 :   g_assert_cmpstr (proxies[1], ==, "socks4a://proxy.example.com");
     176                 :          1 :   g_assert_cmpstr (proxies[2], ==, "socks4://proxy.example.com");
     177                 :          1 :   g_strfreev (proxies);
     178                 :            : 
     179                 :          1 :   proxies = g_proxy_resolver_lookup (resolver, "ftp://two.example.com/",
     180                 :            :                                      NULL, &error);
     181                 :          1 :   g_assert_no_error (error);
     182                 :          1 :   g_assert_cmpint (g_strv_length (proxies), ==, 1);
     183                 :          1 :   g_assert_cmpstr (proxies[0], ==, "default-proxy://");
     184                 :          1 :   g_strfreev (proxies);
     185                 :            : 
     186                 :          1 :   proxies = g_proxy_resolver_lookup (resolver, "http://127.0.0.1/",
     187                 :            :                                      NULL, &error);
     188                 :          1 :   g_assert_no_error (error);
     189                 :          1 :   g_assert_cmpint (g_strv_length (proxies), ==, 1);
     190                 :          1 :   g_assert_cmpstr (proxies[0], ==, "direct://");
     191                 :          1 :   g_strfreev (proxies);
     192                 :            : 
     193                 :          1 :   g_object_unref (resolver);
     194                 :          1 : }
     195                 :            : 
     196                 :            : static const char *ignore_hosts[] = {
     197                 :            :   ".bbb.xx",
     198                 :            :   "*.ccc.xx",
     199                 :            :   "ddd.xx",
     200                 :            :   "*.eee.xx:8000",
     201                 :            :   "127.0.0.0/24",
     202                 :            :   "10.0.0.1:8000",
     203                 :            :   "::1",
     204                 :            :   "fe80::/10",
     205                 :            :   NULL
     206                 :            : };
     207                 :            : 
     208                 :            : static const struct {
     209                 :            :   const char *uri;
     210                 :            :   const char *proxy;
     211                 :            : } ignore_tests[] = {
     212                 :            :   { "http://aaa.xx/",                  "http://localhost:8080" },
     213                 :            :   { "http://aaa.xx:8000/",             "http://localhost:8080" },
     214                 :            :   { "http://www.aaa.xx/",              "http://localhost:8080" },
     215                 :            :   { "http://www.aaa.xx:8000/",         "http://localhost:8080" },
     216                 :            :   { "https://aaa.xx/",                 "http://localhost:8080" },
     217                 :            :   { "http://bbb.xx/",                  "direct://" },
     218                 :            :   { "http://www.bbb.xx/",              "direct://" },
     219                 :            :   { "http://bbb.xx:8000/",             "direct://" },
     220                 :            :   { "http://www.bbb.xx:8000/",         "direct://" },
     221                 :            :   { "https://bbb.xx/",                 "direct://" },
     222                 :            :   { "http://nobbb.xx/",          "http://localhost:8080" },
     223                 :            :   { "http://www.nobbb.xx/",      "http://localhost:8080" },
     224                 :            :   { "http://nobbb.xx:8000/",     "http://localhost:8080" },
     225                 :            :   { "http://www.nobbb.xx:8000/", "http://localhost:8080" },
     226                 :            :   { "https://nobbb.xx/",         "http://localhost:8080" },
     227                 :            :   { "http://ccc.xx/",                  "direct://" },
     228                 :            :   { "http://www.ccc.xx/",              "direct://" },
     229                 :            :   { "http://ccc.xx:8000/",             "direct://" },
     230                 :            :   { "http://www.ccc.xx:8000/",         "direct://" },
     231                 :            :   { "https://ccc.xx/",                 "direct://" },
     232                 :            :   { "http://ddd.xx/",                  "direct://" },
     233                 :            :   { "http://ddd.xx:8000/",             "direct://" },
     234                 :            :   { "http://www.ddd.xx/",              "direct://" },
     235                 :            :   { "http://www.ddd.xx:8000/",         "direct://" },
     236                 :            :   { "https://ddd.xx/",                 "direct://" },
     237                 :            :   { "http://eee.xx/",                  "http://localhost:8080" },
     238                 :            :   { "http://eee.xx:8000/",             "direct://" },
     239                 :            :   { "http://www.eee.xx/",              "http://localhost:8080" },
     240                 :            :   { "http://www.eee.xx:8000/",         "direct://" },
     241                 :            :   { "https://eee.xx/",                 "http://localhost:8080" },
     242                 :            :   { "http://1.2.3.4/",                 "http://localhost:8080" },
     243                 :            :   { "http://127.0.0.1/",               "direct://" },
     244                 :            :   { "http://127.0.0.2/",               "direct://" },
     245                 :            :   { "http://127.0.0.255/",             "direct://" },
     246                 :            :   { "http://127.0.1.0/",               "http://localhost:8080" },
     247                 :            :   { "http://10.0.0.1/",                "http://localhost:8080" },
     248                 :            :   { "http://10.0.0.1:8000/",           "direct://" },
     249                 :            :   { "http://[::1]/",                   "direct://" },
     250                 :            :   { "http://[::1]:80/",                "direct://" },
     251                 :            :   { "http://[::1:1]/",                 "http://localhost:8080" },
     252                 :            :   { "http://[::1:1]:80/",              "http://localhost:8080" },
     253                 :            :   { "http://[fe80::1]/",               "direct://" },
     254                 :            :   { "http://[fe80::1]:80/",            "direct://" },
     255                 :            :   { "http://[fec0::1]/",               "http://localhost:8080" },
     256                 :            :   { "http://[fec0::1]:80/",            "http://localhost:8080" }
     257                 :            : };
     258                 :            : static const int n_ignore_tests = G_N_ELEMENTS (ignore_tests);
     259                 :            : 
     260                 :            : static void
     261                 :          1 : test_ignore (void)
     262                 :            : {
     263                 :            :   GProxyResolver *resolver;
     264                 :          1 :   GError *error = NULL;
     265                 :            :   char **proxies;
     266                 :            :   int i;
     267                 :            : 
     268                 :          1 :   resolver = g_simple_proxy_resolver_new ("http://localhost:8080",
     269                 :            :                                           (char **)ignore_hosts);
     270                 :            : 
     271         [ +  + ]:         46 :   for (i = 0; i < n_ignore_tests; i++)
     272                 :            :     {
     273                 :         45 :       proxies = g_proxy_resolver_lookup (resolver, ignore_tests[i].uri,
     274                 :            :                                          NULL, &error);
     275                 :         45 :       g_assert_no_error (error);
     276                 :            : 
     277                 :         45 :       g_assert_cmpstr (proxies[0], ==, ignore_tests[i].proxy);
     278                 :         45 :       g_strfreev (proxies);
     279                 :            :     }
     280                 :            : 
     281                 :          1 :   g_object_unref (resolver);
     282                 :          1 : }
     283                 :            : 
     284                 :            : int
     285                 :          1 : main (int   argc,
     286                 :            :       char *argv[])
     287                 :            : {
     288                 :          1 :   g_test_init (&argc, &argv, NULL);
     289                 :            : 
     290                 :          1 :   g_test_add_func ("/static-proxy/uri", test_uris);
     291                 :          1 :   g_test_add_func ("/static-proxy/socks", test_socks);
     292                 :          1 :   g_test_add_func ("/static-proxy/ignore", test_ignore);
     293                 :            : 
     294                 :          1 :   return g_test_run();
     295                 :            : }

Generated by: LCOV version 1.14