Mercurial > thymian
comparison 3rdparty/vmime/tests/parser/headerTest.cpp @ 0:a4671277546c tip
created the repository for the thymian project
| author | ferencd |
|---|---|
| date | Tue, 17 Aug 2021 11:19:54 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:a4671277546c |
|---|---|
| 1 // | |
| 2 // VMime library (http://www.vmime.org) | |
| 3 // Copyright (C) 2002-2013 Vincent Richard <vincent@vmime.org> | |
| 4 // | |
| 5 // This program is free software; you can redistribute it and/or | |
| 6 // modify it under the terms of the GNU General Public License as | |
| 7 // published by the Free Software Foundation; either version 3 of | |
| 8 // the License, or (at your option) any later version. | |
| 9 // | |
| 10 // This program is distributed in the hope that it will be useful, | |
| 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 13 // General Public License for more details. | |
| 14 // | |
| 15 // You should have received a copy of the GNU General Public License along | |
| 16 // with this program; if not, write to the Free Software Foundation, Inc., | |
| 17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
| 18 // | |
| 19 // Linking this library statically or dynamically with other modules is making | |
| 20 // a combined work based on this library. Thus, the terms and conditions of | |
| 21 // the GNU General Public License cover the whole combination. | |
| 22 // | |
| 23 | |
| 24 #include "tests/testUtils.hpp" | |
| 25 | |
| 26 | |
| 27 VMIME_TEST_SUITE_BEGIN(headerTest) | |
| 28 | |
| 29 VMIME_TEST_LIST_BEGIN | |
| 30 VMIME_TEST(testHas1) | |
| 31 VMIME_TEST(testHas2) | |
| 32 | |
| 33 VMIME_TEST(testAppend1) | |
| 34 VMIME_TEST(testAppend2) | |
| 35 | |
| 36 VMIME_TEST(testInsertFieldBefore1) | |
| 37 VMIME_TEST(testInsertFieldBefore2) | |
| 38 | |
| 39 VMIME_TEST(testInsertFieldAfter1) | |
| 40 VMIME_TEST(testInsertFieldAfter2) | |
| 41 | |
| 42 VMIME_TEST(testReplaceField) | |
| 43 | |
| 44 VMIME_TEST(testRemoveField1) | |
| 45 VMIME_TEST(testRemoveField2) | |
| 46 | |
| 47 VMIME_TEST(testRemoveAllFields) | |
| 48 | |
| 49 VMIME_TEST(testgetFieldCount) | |
| 50 | |
| 51 VMIME_TEST(testIsEmpty1) | |
| 52 VMIME_TEST(testIsEmpty2) | |
| 53 | |
| 54 VMIME_TEST(testGetFieldAt) | |
| 55 | |
| 56 VMIME_TEST(testGetFieldList1) | |
| 57 VMIME_TEST(testGetFieldList2) | |
| 58 | |
| 59 VMIME_TEST(testFind1) | |
| 60 | |
| 61 VMIME_TEST(testFindAllFields1) | |
| 62 VMIME_TEST(testFindAllFields2) | |
| 63 VMIME_TEST(testFindAllFields3) | |
| 64 VMIME_TEST_LIST_END | |
| 65 | |
| 66 | |
| 67 static const std::string getFieldValue(const vmime::headerField& field) | |
| 68 { | |
| 69 std::ostringstream oss; | |
| 70 vmime::utility::outputStreamAdapter voss(oss); | |
| 71 field.generate(voss); | |
| 72 | |
| 73 return (oss.str()); | |
| 74 } | |
| 75 | |
| 76 // has function tests | |
| 77 void testHas1() | |
| 78 { | |
| 79 vmime::header hdr; | |
| 80 hdr.parse("From: x\r\nTo: y\r\nTo: z\r\n"); | |
| 81 | |
| 82 bool res = hdr.hasField("Z"); | |
| 83 | |
| 84 VASSERT_EQ("Value", false, res); | |
| 85 } | |
| 86 | |
| 87 void testHas2() | |
| 88 { | |
| 89 vmime::header hdr; | |
| 90 hdr.parse("X: x\r\nTo: y\r\nTo: z\r\n"); | |
| 91 | |
| 92 bool res = hdr.hasField("To"); | |
| 93 | |
| 94 VASSERT_EQ("Value", true, res); | |
| 95 } | |
| 96 | |
| 97 // appendField function tests | |
| 98 void testAppend1() | |
| 99 { | |
| 100 vmime::header hdr; | |
| 101 hdr.parse(""); | |
| 102 | |
| 103 vmime::shared_ptr <vmime::headerField> hf = vmime::headerFieldFactory::getInstance()->create("A", "a"); | |
| 104 hdr.appendField(hf); | |
| 105 | |
| 106 std::vector <vmime::shared_ptr <vmime::headerField> > res = hdr.getFieldList(); | |
| 107 | |
| 108 VASSERT_EQ("Count", static_cast <unsigned int>(1), res.size()); | |
| 109 VASSERT_EQ("First value", "A: a", headerTest::getFieldValue(*res[0])); | |
| 110 } | |
| 111 | |
| 112 void testAppend2() | |
| 113 { | |
| 114 vmime::header hdr; | |
| 115 hdr.parse("A: a\r\n"); | |
| 116 | |
| 117 vmime::shared_ptr <vmime::headerField> hf = vmime::headerFieldFactory::getInstance()->create("B", "b"); | |
| 118 hdr.appendField(hf); | |
| 119 | |
| 120 std::vector <vmime::shared_ptr <vmime::headerField> > res = hdr.getFieldList(); | |
| 121 | |
| 122 VASSERT_EQ("Count", static_cast <unsigned int>(2), res.size()); | |
| 123 VASSERT_EQ("First value", "A: a", headerTest::getFieldValue(*res[0])); | |
| 124 VASSERT_EQ("Second value", "B: b", headerTest::getFieldValue(*res[1])); | |
| 125 } | |
| 126 | |
| 127 // insertFieldBefore | |
| 128 void testInsertFieldBefore1() | |
| 129 { | |
| 130 vmime::header hdr; | |
| 131 hdr.parse("A: a\r\nC: c\r\n"); | |
| 132 | |
| 133 vmime::shared_ptr <vmime::headerField> hf = vmime::headerFieldFactory::getInstance()->create("B", "b"); | |
| 134 hdr.insertFieldBefore(hdr.getField("C"), hf); | |
| 135 | |
| 136 std::vector <vmime::shared_ptr <vmime::headerField> > res = hdr.getFieldList(); | |
| 137 | |
| 138 VASSERT_EQ("Count", static_cast <unsigned int>(3), res.size()); | |
| 139 VASSERT_EQ("First value", "A: a", headerTest::getFieldValue(*res[0])); | |
| 140 VASSERT_EQ("Second value", "B: b", headerTest::getFieldValue(*res[1])); | |
| 141 VASSERT_EQ("Third value", "C: c", headerTest::getFieldValue(*res[2])); | |
| 142 } | |
| 143 | |
| 144 void testInsertFieldBefore2() | |
| 145 { | |
| 146 vmime::header hdr; | |
| 147 hdr.parse("A: a\r\nC: c\r\n"); | |
| 148 | |
| 149 vmime::shared_ptr <vmime::headerField> hf = vmime::headerFieldFactory::getInstance()->create("B", "b"); | |
| 150 hdr.insertFieldBefore(1, hf); | |
| 151 | |
| 152 std::vector <vmime::shared_ptr <vmime::headerField> > res = hdr.getFieldList(); | |
| 153 | |
| 154 VASSERT_EQ("Count", static_cast <unsigned int>(3), res.size()); | |
| 155 VASSERT_EQ("First value", "A: a", headerTest::getFieldValue(*res[0])); | |
| 156 VASSERT_EQ("Second value", "B: b", headerTest::getFieldValue(*res[1])); | |
| 157 VASSERT_EQ("Third value", "C: c", headerTest::getFieldValue(*res[2])); | |
| 158 } | |
| 159 | |
| 160 // insertFieldAfter | |
| 161 void testInsertFieldAfter1() | |
| 162 { | |
| 163 vmime::header hdr; | |
| 164 hdr.parse("A: a\r\nC: c\r\n"); | |
| 165 | |
| 166 vmime::shared_ptr <vmime::headerField> hf = vmime::headerFieldFactory::getInstance()->create("B", "b"); | |
| 167 hdr.insertFieldAfter(hdr.getField("A"), hf); | |
| 168 | |
| 169 std::vector <vmime::shared_ptr <vmime::headerField> > res = hdr.getFieldList(); | |
| 170 | |
| 171 VASSERT_EQ("Count", static_cast <unsigned int>(3), res.size()); | |
| 172 VASSERT_EQ("First value", "A: a", headerTest::getFieldValue(*res[0])); | |
| 173 VASSERT_EQ("Second value", "B: b", headerTest::getFieldValue(*res[1])); | |
| 174 VASSERT_EQ("Third value", "C: c", headerTest::getFieldValue(*res[2])); | |
| 175 } | |
| 176 | |
| 177 void testInsertFieldAfter2() | |
| 178 { | |
| 179 vmime::header hdr; | |
| 180 hdr.parse("A: a\r\nC: c\r\n"); | |
| 181 | |
| 182 vmime::shared_ptr <vmime::headerField> hf = vmime::headerFieldFactory::getInstance()->create("B", "b"); | |
| 183 hdr.insertFieldAfter(0, hf); | |
| 184 | |
| 185 std::vector <vmime::shared_ptr <vmime::headerField> > res = hdr.getFieldList(); | |
| 186 | |
| 187 VASSERT_EQ("Count", static_cast <unsigned int>(3), res.size()); | |
| 188 VASSERT_EQ("First value", "A: a", headerTest::getFieldValue(*res[0])); | |
| 189 VASSERT_EQ("Second value", "B: b", headerTest::getFieldValue(*res[1])); | |
| 190 VASSERT_EQ("Third value", "C: c", headerTest::getFieldValue(*res[2])); | |
| 191 } | |
| 192 | |
| 193 // replaceField | |
| 194 void testReplaceField() | |
| 195 { | |
| 196 vmime::header hdr; | |
| 197 hdr.parse("A: a\r\nB: b\r\nC: c\r\n"); | |
| 198 | |
| 199 vmime::shared_ptr <vmime::headerField> hf = vmime::headerFieldFactory::getInstance()->create("Z", "z"); | |
| 200 hdr.replaceField(hdr.getField("B"), hf); | |
| 201 | |
| 202 std::vector <vmime::shared_ptr <vmime::headerField> > res = hdr.getFieldList(); | |
| 203 | |
| 204 VASSERT_EQ("Count", static_cast <unsigned int>(3), res.size()); | |
| 205 VASSERT_EQ("First value", "A: a", headerTest::getFieldValue(*res[0])); | |
| 206 VASSERT_EQ("Second value", "Z: z", headerTest::getFieldValue(*res[1])); | |
| 207 VASSERT_EQ("Third value", "C: c", headerTest::getFieldValue(*res[2])); | |
| 208 } | |
| 209 | |
| 210 // removeField | |
| 211 void testRemoveField1() | |
| 212 { | |
| 213 vmime::header hdr1, hdr2; | |
| 214 hdr1.parse("A: a\r\nB: b\r\nC: c\r\n"); | |
| 215 hdr2.parse("A: a\r\nB: b\r\nC: c\r\n"); | |
| 216 | |
| 217 hdr1.removeField(hdr1.getField("B")); | |
| 218 hdr2.removeField(1); | |
| 219 | |
| 220 std::vector <vmime::shared_ptr <vmime::headerField> > res1 = hdr1.getFieldList(); | |
| 221 | |
| 222 VASSERT_EQ("Count", static_cast <unsigned int>(2), res1.size()); | |
| 223 VASSERT_EQ("First value", "A: a", headerTest::getFieldValue(*res1[0])); | |
| 224 VASSERT_EQ("Second value", "C: c", headerTest::getFieldValue(*res1[1])); | |
| 225 | |
| 226 std::vector <vmime::shared_ptr <vmime::headerField> > res2 = hdr2.getFieldList(); | |
| 227 | |
| 228 VASSERT_EQ("Count", static_cast <unsigned int>(2), res2.size()); | |
| 229 VASSERT_EQ("First value", "A: a", headerTest::getFieldValue(*res2[0])); | |
| 230 VASSERT_EQ("Second value", "C: c", headerTest::getFieldValue(*res2[1])); | |
| 231 } | |
| 232 | |
| 233 void testRemoveField2() | |
| 234 { | |
| 235 vmime::header hdr1, hdr2; | |
| 236 hdr1.parse("A: a\r\n"); | |
| 237 hdr2.parse("A: a\r\n"); | |
| 238 | |
| 239 hdr1.removeField(hdr1.getField("A")); | |
| 240 hdr2.removeField(0); | |
| 241 | |
| 242 std::vector <vmime::shared_ptr <vmime::headerField> > res1 = hdr1.getFieldList(); | |
| 243 VASSERT_EQ("Count", static_cast <unsigned int>(0), res1.size()); | |
| 244 | |
| 245 std::vector <vmime::shared_ptr <vmime::headerField> > res2 = hdr2.getFieldList(); | |
| 246 VASSERT_EQ("Count", static_cast <unsigned int>(0), res2.size()); | |
| 247 } | |
| 248 | |
| 249 // removeAllFields | |
| 250 void testRemoveAllFields() | |
| 251 { | |
| 252 vmime::header hdr1, hdr2; | |
| 253 hdr1.parse("A: a\r\n"); | |
| 254 hdr2.parse("A: a\r\nB: b\r\n"); | |
| 255 | |
| 256 hdr1.removeAllFields(); | |
| 257 hdr2.removeAllFields(); | |
| 258 | |
| 259 std::vector <vmime::shared_ptr <vmime::headerField> > res1 = hdr1.getFieldList(); | |
| 260 VASSERT_EQ("Count", static_cast <unsigned int>(0), res1.size()); | |
| 261 | |
| 262 std::vector <vmime::shared_ptr <vmime::headerField> > res2 = hdr2.getFieldList(); | |
| 263 VASSERT_EQ("Count", static_cast <unsigned int>(0), res2.size()); | |
| 264 } | |
| 265 | |
| 266 // getFieldCount | |
| 267 void testgetFieldCount() | |
| 268 { | |
| 269 vmime::header hdr; | |
| 270 hdr.parse("A: a\r\nB: b\r\nC: c\r\nD: d\r\n"); | |
| 271 | |
| 272 VASSERT_EQ("Value", 4, hdr.getFieldCount()); | |
| 273 } | |
| 274 | |
| 275 // isEmpty | |
| 276 void testIsEmpty1() | |
| 277 { | |
| 278 vmime::header hdr; | |
| 279 hdr.parse("A: a\r\nB: b\r\nC: c\r\n"); | |
| 280 | |
| 281 VASSERT_EQ("Value", false, hdr.isEmpty()); | |
| 282 } | |
| 283 | |
| 284 void testIsEmpty2() | |
| 285 { | |
| 286 vmime::header hdr; | |
| 287 hdr.parse("\r\n"); | |
| 288 | |
| 289 VASSERT_EQ("Value", true, hdr.isEmpty()); | |
| 290 } | |
| 291 | |
| 292 // getFieldAt | |
| 293 void testGetFieldAt() | |
| 294 { | |
| 295 vmime::header hdr; | |
| 296 hdr.parse("B: b\r\nA: a\r\nC: c\r\n"); | |
| 297 | |
| 298 vmime::shared_ptr <vmime::headerField> res = hdr.getFieldAt(2); | |
| 299 | |
| 300 VASSERT_EQ("Value", "C: c", getFieldValue(*res)); | |
| 301 } | |
| 302 | |
| 303 // getFieldList | |
| 304 void testGetFieldList1() | |
| 305 { | |
| 306 vmime::header hdr; | |
| 307 hdr.parse("A: a\r\nB: b1\r\nC: c\r\nB: b2\r\n"); | |
| 308 | |
| 309 std::vector <vmime::shared_ptr <vmime::headerField> > res = hdr.getFieldList(); | |
| 310 | |
| 311 VASSERT_EQ("Count", static_cast <unsigned int>(4), res.size()); | |
| 312 VASSERT_EQ("First value", "A: a", headerTest::getFieldValue(*res[0])); | |
| 313 VASSERT_EQ("Second value", "B: b1", headerTest::getFieldValue(*res[1])); | |
| 314 VASSERT_EQ("Third value", "C: c", headerTest::getFieldValue(*res[2])); | |
| 315 VASSERT_EQ("Thourth value", "B: b2", headerTest::getFieldValue(*res[3])); | |
| 316 } | |
| 317 | |
| 318 void testGetFieldList2() | |
| 319 { | |
| 320 vmime::header hdr; | |
| 321 hdr.parse("\r\n"); | |
| 322 | |
| 323 std::vector <vmime::shared_ptr <vmime::headerField> > res = hdr.getFieldList(); | |
| 324 | |
| 325 VASSERT_EQ("Count", static_cast <unsigned int>(0), res.size()); | |
| 326 } | |
| 327 | |
| 328 // find function tests | |
| 329 void testFind1() | |
| 330 { | |
| 331 vmime::header hdr; | |
| 332 hdr.parse("A: a\r\nB: b\r\nC: c\r\nB: d\r\n"); | |
| 333 | |
| 334 vmime::shared_ptr <vmime::headerField> res = hdr.findField("B"); | |
| 335 | |
| 336 VASSERT_EQ("Value", "B: b", getFieldValue(*res)); | |
| 337 } | |
| 338 | |
| 339 // getAllByName function tests | |
| 340 void testFindAllFields1() | |
| 341 { | |
| 342 vmime::header hdr; | |
| 343 hdr.parse("A: a1\nC: c1\n"); | |
| 344 | |
| 345 std::vector <vmime::shared_ptr <vmime::headerField> > res = hdr.findAllFields("B"); | |
| 346 | |
| 347 VASSERT_EQ("Count", static_cast <unsigned int>(0), res.size()); | |
| 348 } | |
| 349 | |
| 350 void testFindAllFields2() | |
| 351 { | |
| 352 vmime::header hdr; | |
| 353 hdr.parse("A: a1\nB: b1\nB: b2\nC: c1\n"); | |
| 354 | |
| 355 std::vector <vmime::shared_ptr <vmime::headerField> > res = hdr.findAllFields("B"); | |
| 356 | |
| 357 VASSERT_EQ("Count", static_cast <unsigned int>(2), res.size()); | |
| 358 VASSERT_EQ("First value", "B: b1", headerTest::getFieldValue(*res[0])); | |
| 359 VASSERT_EQ("Second value", "B: b2", headerTest::getFieldValue(*res[1])); | |
| 360 } | |
| 361 | |
| 362 void testFindAllFields3() | |
| 363 { | |
| 364 vmime::header hdr; | |
| 365 hdr.parse("A: a1\nB: b1\nB: b2\nC: c1\nC: c3\nC: c2\n"); | |
| 366 | |
| 367 std::vector <vmime::shared_ptr <vmime::headerField> > res = hdr.findAllFields("C"); | |
| 368 | |
| 369 VASSERT_EQ("Count", static_cast <unsigned int>(3), res.size()); | |
| 370 VASSERT_EQ("First value", "C: c1", headerTest::getFieldValue(*res[0])); | |
| 371 VASSERT_EQ("Second value", "C: c3", headerTest::getFieldValue(*res[1])); | |
| 372 VASSERT_EQ("Second value", "C: c2", headerTest::getFieldValue(*res[2])); | |
| 373 } | |
| 374 | |
| 375 VMIME_TEST_SUITE_END | |
| 376 |
