Branch data Line data Source code
1 : : /* Copyright 2006-2009 Savarese Software Research Corporation
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 : : * https://www.savarese.com/software/ApacheLicense-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 : :
16 : : #include <ssrc/wispers/utility/WebStrings.h>
17 : :
18 : : #define BOOST_TEST_MODULE WebStringsTest
19 : : #include <boost/test/unit_test.hpp>
20 : :
21 : : using namespace NS_SSRC_WSPR_UTILITY;
22 : :
23 [ + - + - ]: 3 : BOOST_AUTO_TEST_CASE(test_escape_javascript) {
24 [ + - ]: 2 : const string src(" \"&'/;<>\\\n ");
25 [ + - + - ]: 2 : const string expected("\\x22\\x26\\x27\\x2f\\x3b\\x3c\\x3e\\x5c\\x0a");
26 [ + - + - ]: 2 : const string && result = escape_javascript(src);
27 : :
28 [ + - + - : 1 : BOOST_CHECK_EQUAL(result, expected);
+ - + - +
- - + ]
29 : 1 : }
30 : :
31 [ + - + - ]: 3 : BOOST_AUTO_TEST_CASE(test_escape_url) {
32 [ + - ]: 2 : const string src("$&+,/:;=?@ \"\nFOO<>#%{}|\\^~[]`'");
33 : : const string expected("%24%26%2b%2c%2f%3a%3b%3d%3f%40%20%22%0aFOO%3c%3e%23"
34 [ + - + - ]: 2 : "%25%7b%7d%7c%5c%5e%7e%5b%5d%60%27");
35 [ + - + - ]: 2 : const string && result = escape_url(src);
36 : :
37 [ + - + - : 1 : BOOST_CHECK_EQUAL(result, expected);
+ - + - +
- - + ]
38 : 1 : }
39 : :
40 [ + - + - ]: 3 : BOOST_AUTO_TEST_CASE(test_unescape_url) {
41 [ + - ]: 2 : const string expected("jklmno");
42 [ + - + - ]: 2 : string str("%6a%6B%6c%6D%6e%6f");
43 [ + - ]: 1 : unescape_url(str);
44 [ + - + - : 1 : BOOST_CHECK_EQUAL(str, expected);
+ - + - +
- - + ]
45 : 1 : }
46 : :
47 [ + - + - ]: 3 : BOOST_AUTO_TEST_CASE(test_escape_html) {
48 [ + - ]: 2 : const string src(" &<>\n\"' ");
49 [ + - + - ]: 2 : const string expected("&<>\n"'");
50 [ + - + - ]: 2 : const string && result = escape_html(src);
51 : :
52 [ + - + - : 1 : BOOST_CHECK_EQUAL(result, expected);
+ - + - +
- - + ]
53 : 1 : }
54 : :
55 [ + - + - ]: 3 : BOOST_AUTO_TEST_CASE(test_strip_html) {
56 [ + - ]: 2 : const string src("<html>\n<head><title>FOO & BAR</title></head>\n<body><p>This is a test. © </p></body></html>");
57 [ + - + - ]: 2 : const string expected("FOO & BAR\nThis is a test. ©");
58 [ + - + - ]: 2 : const string && result = strip_html(src);
59 : :
60 [ + - + - : 1 : BOOST_CHECK_EQUAL(result, expected);
+ - + - +
- - + ]
61 : :
62 [ + - + - ]: 2 : const string comment_src("foo<!-- <foo>blah</foo> -->bar");
63 [ + - + - ]: 2 : const string comment_expected("foobar");
64 [ + - + - ]: 2 : const string && comment_result = strip_html(comment_src);
65 : :
66 [ + - + - : 1 : BOOST_CHECK_EQUAL(comment_result, comment_expected);
+ - + - +
- - + ]
67 : 1 : }
68 : :
69 [ + - + - ]: 3 : BOOST_AUTO_TEST_CASE(test_strip_html_and_unescape) {
70 [ + - ]: 2 : const string src("<html>\n<head><title>FOO & BAR</title><!--<title>FOO & BAR</title>--></head>\n<body><p>This is a test. ©©© " </p></body></html> bar ");
71 [ + - + - ]: 2 : string expected("FOO & BAR\nThis is a test. ");
72 [ + - + - ]: 2 : const string && result = strip_html_and_unescape(src);
73 : :
74 [ + - ]: 1 : expected.append(3, 169);
75 [ + - ]: 1 : expected.append(" \" bar");
76 : :
77 [ + - + - : 1 : BOOST_CHECK_EQUAL(result, expected);
+ - + - +
- - + ]
78 : 1 : }
79 : :
80 [ + - + - ]: 3 : BOOST_AUTO_TEST_CASE(test_wrap_lines) {
81 : : struct test_value {
82 : : const char * const text;
83 : : const char * const expected;
84 : : const unsigned int max_length;
85 : : };
86 : : const test_value values[] = {
87 : : { "01234567890 01234 012345 0123",
88 : : "01234567890\n01234\n012345\n0123", 10 },
89 : : { "01234567890\n01234\n012345\n0123\n",
90 : : "01234567890\n01234\n012345\n0123\n", 10 },
91 : : { "0 1 2 3 4 5 6 7 8 9 0",
92 : : "0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n0", 0 },
93 : : { "01234567890", "01234567890", 0 }
94 : 1 : };
95 : 1 : const unsigned int num_values = sizeof(values) / sizeof(test_value);
96 : :
97 [ + + ]: 5 : for(unsigned int i = 0; i < num_values; ++i) {
98 [ + - ]: 8 : string text(values[i].text);
99 [ + - + - ]: 8 : string expected(values[i].expected);
100 : 4 : const unsigned int max_length(values[i].max_length);
101 : :
102 [ + - + - : 4 : wrap_lines(&text[0], text.size(), max_length);
+ - ]
103 : :
104 [ + - + - : 4 : BOOST_CHECK_EQUAL(text, expected);
+ - + - +
- - + ]
105 : : }
106 : 1 : }
107 : :
108 [ + - + - ]: 3 : BOOST_AUTO_TEST_CASE(test_html_title_and_body) {
109 [ + - ]: 2 : const string src0("<html><head><title>FOO</TITLE>");
110 [ + - + - ]: 2 : const string src1("<html><body>bar</BOdy></html>");
111 [ + - + - ]: 2 : const string src2("<html><head><title>FOO</TITLE></head><body>bar</body></html>");
112 : :
113 : : title_body_type tb =
114 [ + - + - : 1 : html_title_and_body(src0.c_str(), src0.c_str() + src0.size());
+ - + - ]
115 : :
116 [ + - + - : 1 : BOOST_CHECK(std::get<0>(tb) != 0);
+ - + - +
- + - + -
+ - - + ]
117 [ + - + - : 1 : BOOST_CHECK(std::get<1>(tb) == 3);
+ - + - +
- + - + -
+ - - + ]
118 [ + - + - : 1 : BOOST_CHECK(std::get<2>(tb) == 0);
+ - + - +
- + - + -
+ - - + ]
119 [ + - + - : 1 : BOOST_CHECK(std::get<3>(tb) == 0);
+ - + - +
- + - + -
+ - - + ]
120 : :
121 [ + - + - : 1 : BOOST_CHECK(std::memcmp("FOO", std::get<0>(tb), std::get<1>(tb)) == 0);
+ - + - +
- + - + -
+ - + - -
+ ]
122 : :
123 [ + - + - : 1 : tb = html_title_and_body(src1.c_str(), src1.c_str() + src1.size());
+ - + - +
- ]
124 : :
125 [ + - + - : 1 : BOOST_CHECK(std::get<0>(tb) == 0);
+ - + - +
- + - + -
+ - - + ]
126 [ + - + - : 1 : BOOST_CHECK(std::get<1>(tb) == 0);
+ - + - +
- + - + -
+ - - + ]
127 [ + - + - : 1 : BOOST_CHECK(std::get<2>(tb) != 0);
+ - + - +
- + - + -
+ - - + ]
128 [ + - + - : 1 : BOOST_CHECK(std::get<3>(tb) == 3);
+ - + - +
- + - + -
+ - - + ]
129 : :
130 [ + - + - : 1 : BOOST_CHECK(std::memcmp("bar", std::get<2>(tb), std::get<3>(tb)) == 0);
+ - + - +
- + - + -
+ - + - -
+ ]
131 : :
132 [ + - + - : 1 : tb = html_title_and_body(src2.c_str(), src2.c_str() + src2.size());
+ - + - +
- ]
133 : :
134 [ + - + - : 1 : BOOST_CHECK(std::get<0>(tb) != 0);
+ - + - +
- + - + -
+ - - + ]
135 [ + - + - : 1 : BOOST_CHECK(std::get<1>(tb) == 3);
+ - + - +
- + - + -
+ - - + ]
136 [ + - + - : 1 : BOOST_CHECK(std::get<2>(tb) != 0);
+ - + - +
- + - + -
+ - - + ]
137 [ + - + - : 1 : BOOST_CHECK(std::get<3>(tb) == 3);
+ - + - +
- + - + -
+ - - + ]
138 : :
139 [ + - + - : 1 : BOOST_CHECK(std::memcmp("FOO", std::get<0>(tb), std::get<1>(tb)) == 0);
+ - + - +
- + - + -
+ - + - -
+ ]
140 [ + - + - : 1 : BOOST_CHECK(std::memcmp("bar", std::get<2>(tb), std::get<3>(tb)) == 0);
+ - + - +
- + - + -
+ - + - -
+ ]
141 [ + - + - ]: 4 : }
|