/Users/deen/code/yugabyte-db/src/yb/util/url-coding-test.cc
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. 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, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | // |
18 | | // The following only applies to changes made to this file as part of YugaByte development. |
19 | | // |
20 | | // Portions Copyright (c) YugaByte, Inc. |
21 | | // |
22 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
23 | | // in compliance with the License. You may obtain a copy of the License at |
24 | | // |
25 | | // http://www.apache.org/licenses/LICENSE-2.0 |
26 | | // |
27 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
28 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
29 | | // or implied. See the License for the specific language governing permissions and limitations |
30 | | // under the License. |
31 | | // |
32 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
33 | | // you may not use this file except in compliance with the License. |
34 | | // You may obtain a copy of the License at |
35 | | // |
36 | | // http://www.apache.org/licenses/LICENSE-2.0 |
37 | | // |
38 | | // Unless required by applicable law or agreed to in writing, software |
39 | | // distributed under the License is distributed on an "AS IS" BASIS, |
40 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
41 | | // See the License for the specific language governing permissions and |
42 | | // limitations under the License. |
43 | | |
44 | | #include <gtest/gtest.h> |
45 | | #include "yb/util/url-coding.h" |
46 | | |
47 | | using namespace std; // NOLINT(*) |
48 | | |
49 | | namespace yb { |
50 | | |
51 | | // Tests encoding/decoding of input. If expected_encoded is non-empty, the |
52 | | // encoded string is validated against it. |
53 | 7 | void TestUrl(const string& input, const string& expected_encoded, bool hive_compat) { |
54 | 7 | string intermediate; |
55 | 7 | UrlEncode(input, &intermediate, hive_compat); |
56 | 7 | string output; |
57 | 7 | if (!expected_encoded.empty()) { |
58 | 3 | EXPECT_EQ(expected_encoded, intermediate); |
59 | 3 | } |
60 | 7 | EXPECT_TRUE(UrlDecode(intermediate, &output, hive_compat)); |
61 | 7 | EXPECT_EQ(input, output); |
62 | | |
63 | | // Convert string to vector and try that also |
64 | 7 | vector<uint8_t> input_vector; |
65 | 7 | input_vector.resize(input.size()); |
66 | 7 | if (!input.empty()) { |
67 | 5 | memcpy(&input_vector[0], input.c_str(), input.size()); |
68 | 5 | } |
69 | 7 | string intermediate2; |
70 | 7 | UrlEncode(input_vector, &intermediate2, hive_compat); |
71 | 7 | EXPECT_EQ(intermediate, intermediate2); |
72 | 7 | } |
73 | | |
74 | 6 | void TestBase64(const string& input, const string& expected_encoded) { |
75 | 6 | string intermediate; |
76 | 6 | Base64Encode(input, &intermediate); |
77 | 6 | string output; |
78 | 6 | if (!expected_encoded.empty()) { |
79 | 6 | EXPECT_EQ(intermediate, expected_encoded); |
80 | 6 | } |
81 | 6 | EXPECT_TRUE(Base64Decode(intermediate, &output)); |
82 | 6 | EXPECT_EQ(input, output); |
83 | | |
84 | | // Convert string to vector and try that also |
85 | 6 | vector<uint8_t> input_vector; |
86 | 6 | input_vector.resize(input.size()); |
87 | 6 | memcpy(&input_vector[0], input.c_str(), input.size()); |
88 | 6 | string intermediate2; |
89 | 6 | Base64Encode(input_vector, &intermediate2); |
90 | 6 | EXPECT_EQ(intermediate, intermediate2); |
91 | 6 | } |
92 | | |
93 | | // Test URL encoding. Check that the values that are put in are the |
94 | | // same that come out. |
95 | 1 | TEST(UrlCodingTest, Basic) { |
96 | 1 | string input = "ABCDEFGHIJKLMNOPQRSTUWXYZ1234567890~!@#$%^&*()<>?,./:\";'{}|[]\\_+-="; |
97 | 1 | TestUrl(input, "", false); |
98 | 1 | TestUrl(input, "", true); |
99 | 1 | } |
100 | | |
101 | 1 | TEST(UrlCodingTest, HiveExceptions) { |
102 | 1 | TestUrl(" +", " +", true); |
103 | 1 | } |
104 | | |
105 | 1 | TEST(UrlCodingTest, BlankString) { |
106 | 1 | TestUrl("", "", false); |
107 | 1 | TestUrl("", "", true); |
108 | 1 | } |
109 | | |
110 | 1 | TEST(UrlCodingTest, PathSeparators) { |
111 | 1 | TestUrl("/home/impala/directory/", "%2Fhome%2Fimpala%2Fdirectory%2F", false); |
112 | 1 | TestUrl("/home/impala/directory/", "%2Fhome%2Fimpala%2Fdirectory%2F", true); |
113 | 1 | } |
114 | | |
115 | 1 | TEST(Base64Test, Basic) { |
116 | 1 | TestBase64("a", "YQ=="); |
117 | 1 | TestBase64("ab", "YWI="); |
118 | 1 | TestBase64("abc", "YWJj"); |
119 | 1 | TestBase64("abcd", "YWJjZA=="); |
120 | 1 | TestBase64("abcde", "YWJjZGU="); |
121 | 1 | TestBase64("abcdef", "YWJjZGVm"); |
122 | 1 | } |
123 | | |
124 | 1 | TEST(HtmlEscapingTest, Basic) { |
125 | 1 | string before = "<html><body>&"; |
126 | 1 | stringstream after; |
127 | 1 | EscapeForHtml(before, &after); |
128 | 1 | EXPECT_EQ(after.str(), "<html><body>&amp"); |
129 | 1 | } |
130 | | |
131 | | } // namespace yb |