Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
url-coding-test.cc
Go to the documentation of this file.
1 // Copyright 2012 Cloudera Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <iostream>
18 #include <gtest/gtest.h>
19 
20 #include "common/logging.h"
21 #include "util/url-coding.h"
22 
23 #include "common/names.h"
24 
25 namespace impala {
26 
27 // Tests encoding/decoding of input. If expected_encoded is non-empty, the
28 // encoded string is validated against it.
29 void TestUrl(const string& input, const string& expected_encoded, bool hive_compat) {
30  string intermediate;
31  UrlEncode(input, &intermediate, hive_compat);
32  string output;
33  if (!expected_encoded.empty()) {
34  EXPECT_EQ(intermediate, expected_encoded);
35  }
36  EXPECT_TRUE(UrlDecode(intermediate, &output, hive_compat));
37  EXPECT_EQ(input, output);
38 
39  // Convert string to vector and try that also
40  vector<uint8_t> input_vector;
41  input_vector.resize(input.size());
42  memcpy(&input_vector[0], input.c_str(), input.size());
43  string intermediate2;
44  UrlEncode(input_vector, &intermediate2, hive_compat);
45  EXPECT_EQ(intermediate, intermediate2);
46 }
47 
48 void TestBase64(const string& input, const string& expected_encoded) {
49  string intermediate;
50  Base64Encode(input, &intermediate);
51  string output;
52  if (!expected_encoded.empty()) {
53  EXPECT_EQ(intermediate, expected_encoded);
54  }
55  EXPECT_TRUE(Base64Decode(intermediate, &output));
56  EXPECT_EQ(input, output);
57 
58  // Convert string to vector and try that also
59  vector<uint8_t> input_vector;
60  input_vector.resize(input.size());
61  memcpy(&input_vector[0], input.c_str(), input.size());
62  string intermediate2;
63  Base64Encode(input_vector, &intermediate2);
64  EXPECT_EQ(intermediate, intermediate2);
65 }
66 
67 // Test URL encoding. Check that the values that are put in are the
68 // same that come out.
69 TEST(UrlCodingTest, Basic) {
70  string input = "ABCDEFGHIJKLMNOPQRSTUWXYZ1234567890~!@#$%^&*()<>?,./:\";'{}|[]\\_+-=";
71  TestUrl(input, "", false);
72  TestUrl(input, "", true);
73 }
74 
75 TEST(UrlCodingTest, HiveExceptions) {
76  TestUrl(" +", " +", true);
77 }
78 
79 TEST(UrlCodingTest, BlankString) {
80  TestUrl("", "", false);
81  TestUrl("", "", true);
82 }
83 
84 TEST(UrlCodingTest, PathSeparators) {
85  TestUrl("/home/impala/directory/", "%2Fhome%2Fimpala%2Fdirectory%2F", false);
86  TestUrl("/home/impala/directory/", "%2Fhome%2Fimpala%2Fdirectory%2F", true);
87 }
88 
89 TEST(Base64Test, Basic) {
90  TestBase64("a", "YQ==");
91  TestBase64("ab", "YWI=");
92  TestBase64("abc", "YWJj");
93  TestBase64("abcd", "YWJjZA==");
94  TestBase64("abcde", "YWJjZGU=");
95  TestBase64("abcdef", "YWJjZGVm");
96 }
97 
98 TEST(HtmlEscapingTest, Basic) {
99  string before = "<html><body>&amp";
100  stringstream after;
101  EscapeForHtml(before, &after);
102  EXPECT_EQ(after.str(), "&lt;html&gt;&lt;body&gt;&amp;amp");
103 }
104 
105 }
106 
107 int main(int argc, char **argv) {
108  ::testing::InitGoogleTest(&argc, argv);
109  return RUN_ALL_TESTS();
110 }
void TestUrl(const string &input, const string &expected_encoded, bool hive_compat)
TEST(AtomicTest, Basic)
Definition: atomic-test.cc:28
bool UrlDecode(const string &in, string *out, bool hive_compat)
Definition: url-coding.cc:84
static void Base64Encode(const char *in, int in_len, stringstream *out)
Definition: url-coding.cc:110
static void UrlEncode(const char *in, int in_len, string *out, bool hive_compat)
Definition: url-coding.cc:48
void TestBase64(const string &input, const string &expected_encoded)
bool Base64Decode(const string &in, string *out)
Definition: url-coding.cc:155
void EscapeForHtml(const string &in, stringstream *out)
Definition: url-coding.cc:178
int main(int argc, char **argv)