/src/mozilla-central/xpcom/tests/gtest/TestEscape.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
3 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
4 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | #include "nsEscape.h" |
8 | | #include "gtest/gtest.h" |
9 | | #include "mozilla/ArrayUtils.h" |
10 | | |
11 | | using namespace mozilla; |
12 | | |
13 | | // Testing for failure here would be somewhat hard in automation. Locally you |
14 | | // could use something like ulimit to create a failure. |
15 | | |
16 | | TEST(Escape, FallibleNoEscape) |
17 | 0 | { |
18 | 0 | // Tests the fallible version of NS_EscapeURL works as expected when no |
19 | 0 | // escaping is necessary. |
20 | 0 | nsCString toEscape("data:,Hello%2C%20World!"); |
21 | 0 | nsCString escaped; |
22 | 0 | nsresult rv = NS_EscapeURL(toEscape, esc_OnlyNonASCII, escaped, fallible); |
23 | 0 | EXPECT_EQ(rv, NS_OK); |
24 | 0 | // Nothing should have been escaped, they should be the same string. |
25 | 0 | EXPECT_STREQ(toEscape.BeginReading(), escaped.BeginReading()); |
26 | 0 | // We expect them to point at the same buffer. |
27 | 0 | EXPECT_EQ(toEscape.BeginReading(), escaped.BeginReading()); |
28 | 0 | } |
29 | | |
30 | | TEST(Escape, FallibleEscape) |
31 | 0 | { |
32 | 0 | // Tests the fallible version of NS_EscapeURL works as expected when |
33 | 0 | // escaping is necessary. |
34 | 0 | nsCString toEscape("data:,Hello%2C%20World!\xC4\x9F"); |
35 | 0 | nsCString escaped; |
36 | 0 | nsresult rv = NS_EscapeURL(toEscape, esc_OnlyNonASCII, escaped, fallible); |
37 | 0 | EXPECT_EQ(rv, NS_OK); |
38 | 0 | EXPECT_STRNE(toEscape.BeginReading(), escaped.BeginReading()); |
39 | 0 | const char* const kExpected = "data:,Hello%2C%20World!%C4%9F"; |
40 | 0 | EXPECT_STREQ(escaped.BeginReading(), kExpected); |
41 | 0 | } |
42 | | |
43 | | TEST(Escape, BadEscapeSequences) |
44 | 0 | { |
45 | 0 | { |
46 | 0 | char bad[] = "%s\0fa"; |
47 | 0 |
|
48 | 0 | int32_t count = nsUnescapeCount(bad); |
49 | 0 | EXPECT_EQ(count, 2); |
50 | 0 | EXPECT_STREQ(bad, "%s"); |
51 | 0 | } |
52 | 0 | { |
53 | 0 | char bad[] = "%a"; |
54 | 0 | int32_t count = nsUnescapeCount(bad); |
55 | 0 | EXPECT_EQ(count, 2); |
56 | 0 | EXPECT_STREQ(bad, "%a"); |
57 | 0 | } |
58 | 0 | { |
59 | 0 | char bad[] = "%"; |
60 | 0 | int32_t count = nsUnescapeCount(bad); |
61 | 0 | EXPECT_EQ(count, 1); |
62 | 0 | EXPECT_STREQ(bad, "%"); |
63 | 0 | } |
64 | 0 | { |
65 | 0 | char bad[] = "%s/%s"; |
66 | 0 | int32_t count = nsUnescapeCount(bad); |
67 | 0 | EXPECT_EQ(count, 5); |
68 | 0 | EXPECT_STREQ(bad, "%s/%s"); |
69 | 0 | } |
70 | 0 | } |
71 | | |
72 | | TEST(Escape, nsAppendEscapedHTML) |
73 | 0 | { |
74 | 0 | const char* srcs[] = { |
75 | 0 | "a", |
76 | 0 | "bcdefgh", |
77 | 0 | "<", |
78 | 0 | ">", |
79 | 0 | "&", |
80 | 0 | "\"", |
81 | 0 | "'", |
82 | 0 | "'bad'", |
83 | 0 | "Foo<T>& foo", |
84 | 0 | "'\"&><abc", |
85 | 0 | "", |
86 | 0 | }; |
87 | 0 |
|
88 | 0 | const char* dsts1[] = { |
89 | 0 | "a", |
90 | 0 | "bcdefgh", |
91 | 0 | "<", |
92 | 0 | ">", |
93 | 0 | "&", |
94 | 0 | """, |
95 | 0 | "'", |
96 | 0 | "'bad'", |
97 | 0 | "Foo<T>& foo", |
98 | 0 | "'"&><abc", |
99 | 0 | "", |
100 | 0 | }; |
101 | 0 |
|
102 | 0 | const char* dsts2[] = { |
103 | 0 | "a", |
104 | 0 | "abcdefgh", |
105 | 0 | "abcdefgh<", |
106 | 0 | "abcdefgh<>", |
107 | 0 | "abcdefgh<>&", |
108 | 0 | "abcdefgh<>&"", |
109 | 0 | "abcdefgh<>&"'", |
110 | 0 | "abcdefgh<>&"''bad'", |
111 | 0 | "abcdefgh<>&"''bad'Foo<T>& foo", |
112 | 0 | "abcdefgh<>&"''bad'Foo<T>& foo'"&><abc", |
113 | 0 | "abcdefgh<>&"''bad'Foo<T>& foo'"&><abc", |
114 | 0 | }; |
115 | 0 |
|
116 | 0 | ASSERT_EQ(ArrayLength(srcs), ArrayLength(dsts1)); |
117 | 0 | ASSERT_EQ(ArrayLength(srcs), ArrayLength(dsts2)); |
118 | 0 |
|
119 | 0 | // Test when the destination is empty. |
120 | 0 | for (size_t i = 0; i < ArrayLength(srcs); i++) { |
121 | 0 | nsCString src(srcs[i]); |
122 | 0 | nsCString dst; |
123 | 0 | nsAppendEscapedHTML(src, dst); |
124 | 0 | ASSERT_TRUE(dst.Equals(dsts1[i])); |
125 | 0 | } |
126 | 0 |
|
127 | 0 | // Test when the destination is non-empty. |
128 | 0 | nsCString dst; |
129 | 0 | for (size_t i = 0; i < ArrayLength(srcs); i++) { |
130 | 0 | nsCString src(srcs[i]); |
131 | 0 | nsAppendEscapedHTML(src, dst); |
132 | 0 | ASSERT_TRUE(dst.Equals(dsts2[i])); |
133 | 0 | } |
134 | 0 | } |
135 | | |
136 | | TEST(Escape, EscapeSpaces) |
137 | 0 | { |
138 | 0 | // Tests the fallible version of NS_EscapeURL works as expected when no |
139 | 0 | // escaping is necessary. |
140 | 0 | nsCString toEscape("data:\x0D\x0A spa ces\xC4\x9F"); |
141 | 0 | nsCString escaped; |
142 | 0 | nsresult rv = NS_EscapeURL(toEscape, esc_OnlyNonASCII, escaped, fallible); |
143 | 0 | EXPECT_EQ(rv, NS_OK); |
144 | 0 | // Only non-ASCII and C0 |
145 | 0 | EXPECT_STREQ(escaped.BeginReading(), "data:%0D%0A spa ces%C4%9F"); |
146 | 0 |
|
147 | 0 | escaped.Truncate(); |
148 | 0 | rv = NS_EscapeURL(toEscape, esc_OnlyNonASCII | esc_Spaces, escaped, fallible); |
149 | 0 | EXPECT_EQ(rv, NS_OK); |
150 | 0 | EXPECT_STREQ(escaped.BeginReading(), "data:%0D%0A%20spa%20ces%C4%9F"); |
151 | 0 | } |