OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2015 Eyeo GmbH |
| 4 * |
| 5 * Adblock Plus is free software: you can redistribute it and/or modify |
| 6 * it under the terms of the GNU General Public License version 3 as |
| 7 * published by the Free Software Foundation. |
| 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 14 * You should have received a copy of the GNU General Public License |
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 16 */ |
| 17 |
| 18 #include <cstdint> |
| 19 #include <gtest/gtest.h> |
| 20 #include "../src/shared/Utils.h" |
| 21 |
| 22 namespace |
| 23 { |
| 24 struct ParameterHandler |
| 25 { |
| 26 bool operator()(const std::wstring& parameter, const std::wstring& value) |
| 27 { |
| 28 parameters.emplace_back(parameter, value); |
| 29 return true; |
| 30 } |
| 31 struct Parameter |
| 32 { |
| 33 Parameter(const std::wstring& name, const std::wstring& value) |
| 34 : name(name), value(value) |
| 35 { |
| 36 } |
| 37 std::wstring name; |
| 38 std::wstring value; |
| 39 }; |
| 40 std::vector<Parameter> parameters; |
| 41 }; |
| 42 } |
| 43 |
| 44 TEST(ForEachQueryStringParameterTest, EmptyParameterHandler) |
| 45 { |
| 46 ASSERT_THROW(ForEachQueryStringParameter(L"some=query&string", |
| 47 std::function<bool(const std::wstring&, const std::wstring&)>()), std::bad_f
unction_call); |
| 48 } |
| 49 |
| 50 TEST(ForEachQueryStringParameterTest, EmptyQueryString) |
| 51 { |
| 52 ParameterHandler parameterHandler; |
| 53 ForEachQueryStringParameter(L"", std::ref(parameterHandler)); |
| 54 EXPECT_EQ(0, parameterHandler.parameters.size()); |
| 55 } |
| 56 |
| 57 TEST(ForEachQueryStringParameterTest, QueryStringWithOneNormalParameter) |
| 58 { |
| 59 ParameterHandler parameterHandler; |
| 60 ForEachQueryStringParameter(L"param=value", std::ref(parameterHandler)); |
| 61 ASSERT_EQ(1, parameterHandler.parameters.size()); |
| 62 EXPECT_EQ(L"param", parameterHandler.parameters[0].name); |
| 63 EXPECT_EQ(L"value", parameterHandler.parameters[0].value); |
| 64 } |
| 65 |
| 66 TEST(ForEachQueryStringParameterTest, QueryStringWithSeveralNormalParameters) |
| 67 { |
| 68 ParameterHandler parameterHandler; |
| 69 ForEachQueryStringParameter(L"param1=value1¶m2=value2¶m3=value3", std:
:ref(parameterHandler)); |
| 70 ASSERT_EQ(3, parameterHandler.parameters.size()); |
| 71 EXPECT_EQ(L"param1", parameterHandler.parameters[0].name); |
| 72 EXPECT_EQ(L"value1", parameterHandler.parameters[0].value); |
| 73 EXPECT_EQ(L"param2", parameterHandler.parameters[1].name); |
| 74 EXPECT_EQ(L"value2", parameterHandler.parameters[1].value); |
| 75 EXPECT_EQ(L"param3", parameterHandler.parameters[2].name); |
| 76 EXPECT_EQ(L"value3", parameterHandler.parameters[2].value); |
| 77 } |
| 78 |
| 79 TEST(ForEachQueryStringParameterTest, QueryStringWithEmptyParameterAtTheEnd) |
| 80 { |
| 81 ParameterHandler parameterHandler; |
| 82 ForEachQueryStringParameter(L"param=value&", std::ref(parameterHandler)); |
| 83 ASSERT_EQ(2, parameterHandler.parameters.size()); |
| 84 EXPECT_EQ(L"param", parameterHandler.parameters[0].name); |
| 85 EXPECT_EQ(L"value", parameterHandler.parameters[0].value); |
| 86 EXPECT_EQ(L"", parameterHandler.parameters[1].name); |
| 87 EXPECT_EQ(L"", parameterHandler.parameters[1].value); |
| 88 } |
| 89 |
| 90 TEST(ForEachQueryStringParameterTest, QueryStringWithEmptyParameterAtTheBeginnin
g) |
| 91 { |
| 92 ParameterHandler parameterHandler; |
| 93 ForEachQueryStringParameter(L"¶m=value", std::ref(parameterHandler)); |
| 94 ASSERT_EQ(2, parameterHandler.parameters.size()); |
| 95 EXPECT_EQ(L"", parameterHandler.parameters[0].name); |
| 96 EXPECT_EQ(L"", parameterHandler.parameters[0].value); |
| 97 EXPECT_EQ(L"param", parameterHandler.parameters[1].name); |
| 98 EXPECT_EQ(L"value", parameterHandler.parameters[1].value); |
| 99 } |
| 100 |
| 101 TEST(ForEachQueryStringParameterTest, ParameterWithoutAssignSign) |
| 102 { |
| 103 ParameterHandler parameterHandler; |
| 104 ForEachQueryStringParameter(L"param", std::ref(parameterHandler)); |
| 105 ASSERT_EQ(1, parameterHandler.parameters.size()); |
| 106 EXPECT_EQ(L"param", parameterHandler.parameters[0].name); |
| 107 EXPECT_EQ(L"", parameterHandler.parameters[0].value); |
| 108 } |
| 109 |
| 110 TEST(ForEachQueryStringParameterTest, ParametersWithoutAssignSign) |
| 111 { |
| 112 ParameterHandler parameterHandler; |
| 113 ForEachQueryStringParameter(L"param1¶m2¶m3", std::ref(parameterHandler
)); |
| 114 ASSERT_EQ(3, parameterHandler.parameters.size()); |
| 115 EXPECT_EQ(L"param1", parameterHandler.parameters[0].name); |
| 116 EXPECT_EQ(L"", parameterHandler.parameters[0].value); |
| 117 EXPECT_EQ(L"param2", parameterHandler.parameters[1].name); |
| 118 EXPECT_EQ(L"", parameterHandler.parameters[1].value); |
| 119 EXPECT_EQ(L"param3", parameterHandler.parameters[2].name); |
| 120 EXPECT_EQ(L"", parameterHandler.parameters[2].value); |
| 121 } |
| 122 |
| 123 TEST(ForEachQueryStringParameterTest, ParameterWithSeveralAssignSigns) |
| 124 { |
| 125 ParameterHandler parameterHandler; |
| 126 ForEachQueryStringParameter(L"param1=value1.1=value1.2", std::ref(parameterHan
dler)); |
| 127 ASSERT_EQ(1, parameterHandler.parameters.size()); |
| 128 EXPECT_EQ(L"param1", parameterHandler.parameters[0].name); |
| 129 EXPECT_EQ(L"value1.1=value1.2", parameterHandler.parameters[0].value); |
| 130 } |
| 131 |
| 132 TEST(ForEachQueryStringParameterTest, ParameterWithEmptyValue) |
| 133 { |
| 134 ParameterHandler parameterHandler; |
| 135 ForEachQueryStringParameter(L"param1=", std::ref(parameterHandler)); |
| 136 ASSERT_EQ(1, parameterHandler.parameters.size()); |
| 137 EXPECT_EQ(L"param1", parameterHandler.parameters[0].name); |
| 138 EXPECT_EQ(L"", parameterHandler.parameters[0].value); |
| 139 } |
| 140 |
| 141 TEST(ForEachQueryStringParameterTest, StopProcessingWhenHandlerReturnsFalse) |
| 142 { |
| 143 struct LimitedParameterHandler : ParameterHandler |
| 144 { |
| 145 LimitedParameterHandler(uint32_t limit) |
| 146 : callCounter(0), limit(limit) |
| 147 { |
| 148 } |
| 149 bool operator()(const std::wstring& parameter, const std::wstring& value) |
| 150 { |
| 151 ++callCounter; |
| 152 return ParameterHandler::operator()(parameter, value) && shouldContinue(); |
| 153 } |
| 154 bool shouldContinue() const |
| 155 { |
| 156 return parameters.size() < limit; |
| 157 } |
| 158 uint32_t callCounter; |
| 159 uint32_t limit; |
| 160 } parameterHandler(2); |
| 161 ForEachQueryStringParameter(L"param1=value1¶m2=value2¶m3=value3", std:
:ref(parameterHandler)); |
| 162 EXPECT_EQ(2, parameterHandler.callCounter); |
| 163 ASSERT_EQ(2, parameterHandler.parameters.size()); |
| 164 EXPECT_EQ(L"param1", parameterHandler.parameters[0].name); |
| 165 EXPECT_EQ(L"value1", parameterHandler.parameters[0].value); |
| 166 EXPECT_EQ(L"param2", parameterHandler.parameters[1].name); |
| 167 EXPECT_EQ(L"value2", parameterHandler.parameters[1].value); |
| 168 } |
| 169 |
| 170 TEST(ForEachQueryStringParameterTest, ParameterWithoutName) |
| 171 { |
| 172 ParameterHandler parameterHandler; |
| 173 ForEachQueryStringParameter(L"=param", std::ref(parameterHandler)); |
| 174 ASSERT_EQ(1, parameterHandler.parameters.size()); |
| 175 EXPECT_EQ(L"", parameterHandler.parameters[0].name); |
| 176 EXPECT_EQ(L"param", parameterHandler.parameters[0].value); |
| 177 } |
| 178 |
| 179 TEST(ForEachQueryStringParameterTest, ParametersWithoutName) |
| 180 { |
| 181 ParameterHandler parameterHandler; |
| 182 ForEachQueryStringParameter(L"=param1&=param2&=param3", std::ref(parameterHand
ler)); |
| 183 ASSERT_EQ(3, parameterHandler.parameters.size()); |
| 184 EXPECT_EQ(L"", parameterHandler.parameters[0].name); |
| 185 EXPECT_EQ(L"param1", parameterHandler.parameters[0].value); |
| 186 EXPECT_EQ(L"", parameterHandler.parameters[1].name); |
| 187 EXPECT_EQ(L"param2", parameterHandler.parameters[1].value); |
| 188 EXPECT_EQ(L"", parameterHandler.parameters[2].name); |
| 189 EXPECT_EQ(L"param3", parameterHandler.parameters[2].value); |
| 190 } |
OLD | NEW |