• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 #include "src/core/ext/filters/client_channel/uri_parser.h"
20 
21 #include <string.h>
22 
23 #include <grpc/grpc.h>
24 #include <grpc/support/log.h>
25 
26 #include "src/core/lib/iomgr/exec_ctx.h"
27 #include "test/core/util/test_config.h"
28 
test_succeeds(const char * uri_text,const char * scheme,const char * authority,const char * path,const char * query,const char * fragment)29 static void test_succeeds(const char* uri_text, const char* scheme,
30                           const char* authority, const char* path,
31                           const char* query, const char* fragment) {
32   grpc_core::ExecCtx exec_ctx;
33   grpc_uri* uri = grpc_uri_parse(uri_text, 0);
34   GPR_ASSERT(uri);
35   GPR_ASSERT(0 == strcmp(scheme, uri->scheme));
36   GPR_ASSERT(0 == strcmp(authority, uri->authority));
37   GPR_ASSERT(0 == strcmp(path, uri->path));
38   GPR_ASSERT(0 == strcmp(query, uri->query));
39   GPR_ASSERT(0 == strcmp(fragment, uri->fragment));
40 
41   grpc_uri_destroy(uri);
42 }
43 
test_fails(const char * uri_text)44 static void test_fails(const char* uri_text) {
45   grpc_core::ExecCtx exec_ctx;
46   GPR_ASSERT(nullptr == grpc_uri_parse(uri_text, 0));
47 }
48 
test_query_parts()49 static void test_query_parts() {
50   {
51     grpc_core::ExecCtx exec_ctx;
52     const char* uri_text = "http://foo/path?a&b=B&c=&#frag";
53     grpc_uri* uri = grpc_uri_parse(uri_text, 0);
54     GPR_ASSERT(uri);
55 
56     GPR_ASSERT(0 == strcmp("http", uri->scheme));
57     GPR_ASSERT(0 == strcmp("foo", uri->authority));
58     GPR_ASSERT(0 == strcmp("/path", uri->path));
59     GPR_ASSERT(0 == strcmp("a&b=B&c=&", uri->query));
60     GPR_ASSERT(4 == uri->num_query_parts);
61 
62     GPR_ASSERT(0 == strcmp("a", uri->query_parts[0]));
63     GPR_ASSERT(nullptr == uri->query_parts_values[0]);
64 
65     GPR_ASSERT(0 == strcmp("b", uri->query_parts[1]));
66     GPR_ASSERT(0 == strcmp("B", uri->query_parts_values[1]));
67 
68     GPR_ASSERT(0 == strcmp("c", uri->query_parts[2]));
69     GPR_ASSERT(0 == strcmp("", uri->query_parts_values[2]));
70 
71     GPR_ASSERT(0 == strcmp("", uri->query_parts[3]));
72     GPR_ASSERT(nullptr == uri->query_parts_values[3]);
73 
74     GPR_ASSERT(nullptr == grpc_uri_get_query_arg(uri, "a"));
75     GPR_ASSERT(0 == strcmp("B", grpc_uri_get_query_arg(uri, "b")));
76     GPR_ASSERT(0 == strcmp("", grpc_uri_get_query_arg(uri, "c")));
77     GPR_ASSERT(nullptr == grpc_uri_get_query_arg(uri, ""));
78 
79     GPR_ASSERT(0 == strcmp("frag", uri->fragment));
80 
81     grpc_uri_destroy(uri);
82   }
83   {
84     /* test the current behavior of multiple query part values */
85     grpc_core::ExecCtx exec_ctx;
86     const char* uri_text = "http://auth/path?foo=bar=baz&foobar==";
87     grpc_uri* uri = grpc_uri_parse(uri_text, 0);
88     GPR_ASSERT(uri);
89 
90     GPR_ASSERT(0 == strcmp("http", uri->scheme));
91     GPR_ASSERT(0 == strcmp("auth", uri->authority));
92     GPR_ASSERT(0 == strcmp("/path", uri->path));
93     GPR_ASSERT(0 == strcmp("foo=bar=baz&foobar==", uri->query));
94     GPR_ASSERT(2 == uri->num_query_parts);
95 
96     GPR_ASSERT(0 == strcmp("bar", grpc_uri_get_query_arg(uri, "foo")));
97     GPR_ASSERT(0 == strcmp("", grpc_uri_get_query_arg(uri, "foobar")));
98 
99     grpc_uri_destroy(uri);
100   }
101   {
102     /* empty query */
103     grpc_core::ExecCtx exec_ctx;
104     const char* uri_text = "http://foo/path";
105     grpc_uri* uri = grpc_uri_parse(uri_text, 0);
106     GPR_ASSERT(uri);
107 
108     GPR_ASSERT(0 == strcmp("http", uri->scheme));
109     GPR_ASSERT(0 == strcmp("foo", uri->authority));
110     GPR_ASSERT(0 == strcmp("/path", uri->path));
111     GPR_ASSERT(0 == strcmp("", uri->query));
112     GPR_ASSERT(0 == uri->num_query_parts);
113     GPR_ASSERT(nullptr == uri->query_parts);
114     GPR_ASSERT(nullptr == uri->query_parts_values);
115     GPR_ASSERT(0 == strcmp("", uri->fragment));
116 
117     grpc_uri_destroy(uri);
118   }
119 }
120 
main(int argc,char ** argv)121 int main(int argc, char** argv) {
122   grpc_test_init(argc, argv);
123   grpc_init();
124   test_succeeds("http://www.google.com", "http", "www.google.com", "", "", "");
125   test_succeeds("dns:///foo", "dns", "", "/foo", "", "");
126   test_succeeds("http://www.google.com:90", "http", "www.google.com:90", "", "",
127                 "");
128   test_succeeds("a192.4-df:foo.coom", "a192.4-df", "", "foo.coom", "", "");
129   test_succeeds("a+b:foo.coom", "a+b", "", "foo.coom", "", "");
130   test_succeeds("zookeeper://127.0.0.1:2181/foo/bar", "zookeeper",
131                 "127.0.0.1:2181", "/foo/bar", "", "");
132   test_succeeds("http://www.google.com?yay-i'm-using-queries", "http",
133                 "www.google.com", "", "yay-i'm-using-queries", "");
134   test_succeeds("dns:foo.com#fragment-all-the-things", "dns", "", "foo.com", "",
135                 "fragment-all-the-things");
136   test_succeeds("http:?legit", "http", "", "", "legit", "");
137   test_succeeds("unix:#this-is-ok-too", "unix", "", "", "", "this-is-ok-too");
138   test_succeeds("http:?legit#twice", "http", "", "", "legit", "twice");
139   test_succeeds("http://foo?bar#lol?", "http", "foo", "", "bar", "lol?");
140   test_succeeds("http://foo?bar#lol?/", "http", "foo", "", "bar", "lol?/");
141   test_succeeds("ipv6:[2001:db8::1%252]:12345", "ipv6", "",
142                 "[2001:db8::1%2]:12345", "", "");
143 
144   test_fails("xyz");
145   test_fails("http:?dangling-pct-%0");
146   test_fails("http://foo?[bar]");
147   test_fails("http://foo?x[bar]");
148   test_fails("http://foo?bar#lol#");
149 
150   test_query_parts();
151   grpc_shutdown();
152   return 0;
153 }
154