1 /* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3  *     Copyright 2012 Couchbase, Inc.
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 #include "config.h"
18 #include <gtest/gtest.h>
19 #include <libcouchbase/couchbase.h>
20 #include <strcodecs/strcodecs.h>
21 
22 class UrlEncoding : public ::testing::Test
23 {
TEST_F(SListTests,testBasic)24 };
25 
26 using lcb::strcodecs::urlencode;
27 using lcb::strcodecs::urldecode;
28 
29 TEST_F(UrlEncoding, plainTextTests)
30 {
31     std::string input("abcdef");
32     std::string exp("abcdef");
33     std::string out;
34     ASSERT_TRUE(urlencode(input.begin(), input.end(), out));
35     ASSERT_EQ(exp, out);
36 }
37 
38 TEST_F(UrlEncoding, plainTextWithSlashTests)
39 {
40     std::string input("a/b/c/d/e/f/g/h/i/j");
41     std::string out;
42     ASSERT_TRUE(urlencode(input.begin(), input.end(), out));
43     ASSERT_EQ(input, out);
44 }
45 
46 TEST_F(UrlEncoding, plainTextWithSpaceTests)
47 {
48     std::string out;
49     std::string input("a b c d e f g");
50     std::string exp("a%20b%20c%20d%20e%20f%20g");
51     ASSERT_TRUE(urlencode(input.begin(), input.end(), out));
52     ASSERT_EQ(exp, out);
53 }
54 
55 TEST_F(UrlEncoding, encodedTextWithPlusAsApaceTests)
56 {
57     std::string input("a+b+c+d+e+g+h");
58     std::string out;
59     ASSERT_TRUE(urlencode(input.begin(), input.end(), out));
60     ASSERT_EQ(input, out);
61 }
62 
TEST_F(SListTests,testBasicIter)63 TEST_F(UrlEncoding, encodedTextWithPlusAndHexAsApaceTests)
64 {
65     std::string input("a+b%20c%20d+e+g+h");
66     std::string out;
67     ASSERT_TRUE(urlencode(input.begin(), input.end(), out));
68     ASSERT_EQ(input, out);
69 }
70 
71 TEST_F(UrlEncoding, mixedLegalTextTests)
72 {
73     std::string input("a/b/c/d/e f g+32%20");
74     std::string exp("a/b/c/d/e%20f%20g+32%20");
75     std::string out;
76 
77     ASSERT_TRUE(urlencode(input.begin(), input.end(), out));
78     ASSERT_EQ(exp, out);
79 }
80 
81 TEST_F(UrlEncoding, mixedIllegalEncodingTextTests)
82 {
83     std::string input("a+ ");
84     std::string out;
85     ASSERT_FALSE(urlencode(input.begin(), input.end(), out));
86 }
87 
88 TEST_F(UrlEncoding, internationalTest)
89 {
fillDynamicSlist(sllist_root * root,my_elem ** ptrs,int nptrs)90     std::string input("_design/beer/_view/all?startkey=\"\xc3\xb8l\"");
91     std::string exp("_design/beer/_view/all?startkey=%22%C3%B8l%22");
92     std::string out;
93     ASSERT_TRUE(urlencode(input.begin(), input.end(), out));
94     ASSERT_EQ(exp, out);
95 }
96 
97 TEST_F(UrlEncoding, internationalEncodedTest)
98 {
99     std::string input("_design/beer/_view/all?startkey=%22%C3%B8l%22");
100     std::string exp("_design/beer/_view/all?startkey=%22%C3%B8l%22");
101     std::string out;
102     ASSERT_TRUE(urlencode(input.begin(), input.end(), out));
103     ASSERT_EQ(exp, out);
TEST_F(SListTests,testExtendedIter)104 }
105 
106 TEST_F(UrlEncoding, testDecode)
107 {
108     char obuf[4096];
109 
110     ASSERT_TRUE(urldecode("%22", obuf)) << "Single character";
111     ASSERT_STREQ("\x22", obuf);
112 
113     ASSERT_TRUE(urldecode("Hello World", obuf)) << "No pct encode";
114     ASSERT_STREQ("Hello World", obuf);
115 
116     ASSERT_TRUE(urldecode("Hello%20World", obuf));
117     ASSERT_STREQ("Hello World", obuf);
118 
119     ASSERT_TRUE(urldecode("%2Ffoo%2Fbar%2Fbaz%2F", obuf));
120     ASSERT_STREQ("/foo/bar/baz/", obuf);
121 
122     ASSERT_TRUE(urldecode("%01%02%03%04", obuf)) << "Multiple octets";
123     ASSERT_STREQ("\x01\x02\x03\x04", obuf);
124 
125     ASSERT_TRUE(urldecode("%FFFF", obuf)) << "Recognize only first two hex digits";
126     // Split the hex literal so we don't confuse the preprocessor
127     ASSERT_STREQ("\xff" "FF", obuf);
128 
129     // Error tests
130     ASSERT_FALSE(urldecode("%", obuf));
131     ASSERT_FALSE(urldecode("%RR", obuf)) << "Invalid hex digits";
132 }
133