util_stp_filter_parser.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  1. /*
  2. * Copyright Andrey Semashev 2007 - 2015.
  3. * Distributed under the Boost Software License, Version 1.0.
  4. * (See accompanying file LICENSE_1_0.txt or copy at
  5. * http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. /*!
  8. * \file setup_filter_parser.cpp
  9. * \author Andrey Semashev
  10. * \date 24.08.2013
  11. *
  12. * \brief This header contains tests for the filter parser.
  13. */
  14. #define BOOST_TEST_MODULE setup_filter_parser
  15. #include <string>
  16. #include <boost/test/unit_test.hpp>
  17. #include <boost/log/utility/setup/filter_parser.hpp>
  18. #if !defined(BOOST_LOG_WITHOUT_SETTINGS_PARSERS) && !defined(BOOST_LOG_WITHOUT_DEFAULT_FACTORIES)
  19. #include <boost/smart_ptr/shared_ptr.hpp>
  20. #include <boost/log/exceptions.hpp>
  21. #include <boost/log/attributes/constant.hpp>
  22. #include <boost/log/attributes/attribute_set.hpp>
  23. #include <boost/log/attributes/attribute_value_set.hpp>
  24. #include <boost/log/expressions/filter.hpp>
  25. namespace logging = boost::log;
  26. namespace attrs = logging::attributes;
  27. typedef logging::attribute_set attr_set;
  28. typedef logging::attribute_value_set attr_values;
  29. // Tests for attribute presence check
  30. BOOST_AUTO_TEST_CASE(attr_presence)
  31. {
  32. attrs::constant< int > attr1(10);
  33. attr_set set1, set2, set3;
  34. attr_values values1(set1, set2, set3);
  35. values1.freeze();
  36. set1["MyAttr"] = attr1;
  37. attr_values values2(set1, set2, set3);
  38. values2.freeze();
  39. {
  40. logging::filter f = logging::parse_filter("%MyAttr%");
  41. BOOST_CHECK(!f(values1));
  42. BOOST_CHECK(f(values2));
  43. }
  44. {
  45. logging::filter f = logging::parse_filter(" % MyAttr % ");
  46. BOOST_CHECK(!f(values1));
  47. BOOST_CHECK(f(values2));
  48. }
  49. }
  50. // Tests for integer relation filter
  51. BOOST_AUTO_TEST_CASE(int_relation)
  52. {
  53. attrs::constant< int > attr1(10);
  54. attrs::constant< long > attr2(20);
  55. attrs::constant< int > attr3(-2);
  56. attr_set set1, set2, set3;
  57. attr_values values1(set1, set2, set3);
  58. values1.freeze();
  59. set1["MyAttr"] = attr1;
  60. attr_values values2(set1, set2, set3);
  61. values2.freeze();
  62. set1["MyAttr"] = attr2;
  63. attr_values values3(set1, set2, set3);
  64. values3.freeze();
  65. set1["MyAttr"] = attr3;
  66. attr_values values4(set1, set2, set3);
  67. values4.freeze();
  68. {
  69. logging::filter f = logging::parse_filter("%MyAttr% = 10");
  70. BOOST_CHECK(!f(values1));
  71. BOOST_CHECK(f(values2));
  72. BOOST_CHECK(!f(values3));
  73. BOOST_CHECK(!f(values4));
  74. }
  75. {
  76. logging::filter f = logging::parse_filter("%MyAttr% != 10");
  77. BOOST_CHECK(!f(values1));
  78. BOOST_CHECK(!f(values2));
  79. BOOST_CHECK(f(values3));
  80. BOOST_CHECK(f(values4));
  81. }
  82. {
  83. logging::filter f = logging::parse_filter("%MyAttr% < 20");
  84. BOOST_CHECK(!f(values1));
  85. BOOST_CHECK(f(values2));
  86. BOOST_CHECK(!f(values3));
  87. BOOST_CHECK(f(values4));
  88. }
  89. {
  90. logging::filter f = logging::parse_filter("%MyAttr% < -7");
  91. BOOST_CHECK(!f(values1));
  92. BOOST_CHECK(!f(values2));
  93. BOOST_CHECK(!f(values3));
  94. BOOST_CHECK(!f(values4));
  95. }
  96. {
  97. logging::filter f = logging::parse_filter("%MyAttr% > 10");
  98. BOOST_CHECK(!f(values1));
  99. BOOST_CHECK(!f(values2));
  100. BOOST_CHECK(f(values3));
  101. BOOST_CHECK(!f(values4));
  102. }
  103. {
  104. logging::filter f = logging::parse_filter("%MyAttr% > -5");
  105. BOOST_CHECK(!f(values1));
  106. BOOST_CHECK(f(values2));
  107. BOOST_CHECK(f(values3));
  108. BOOST_CHECK(f(values4));
  109. }
  110. {
  111. logging::filter f = logging::parse_filter("%MyAttr% <= 20");
  112. BOOST_CHECK(!f(values1));
  113. BOOST_CHECK(f(values2));
  114. BOOST_CHECK(f(values3));
  115. BOOST_CHECK(f(values4));
  116. }
  117. {
  118. logging::filter f = logging::parse_filter("%MyAttr% >= 20");
  119. BOOST_CHECK(!f(values1));
  120. BOOST_CHECK(!f(values2));
  121. BOOST_CHECK(f(values3));
  122. BOOST_CHECK(!f(values4));
  123. }
  124. }
  125. // Tests for floating point relation filter
  126. BOOST_AUTO_TEST_CASE(fp_relation)
  127. {
  128. attrs::constant< float > attr1(2.5f);
  129. attrs::constant< float > attr2(8.8f);
  130. attrs::constant< double > attr3(-9.1);
  131. attrs::constant< float > attr4(0.0f);
  132. attr_set set1, set2, set3;
  133. attr_values values1(set1, set2, set3);
  134. values1.freeze();
  135. set1["MyAttr"] = attr1;
  136. attr_values values2(set1, set2, set3);
  137. values2.freeze();
  138. set1["MyAttr"] = attr2;
  139. attr_values values3(set1, set2, set3);
  140. values3.freeze();
  141. set1["MyAttr"] = attr3;
  142. attr_values values4(set1, set2, set3);
  143. values4.freeze();
  144. set1["MyAttr"] = attr4;
  145. attr_values values5(set1, set2, set3);
  146. values5.freeze();
  147. {
  148. logging::filter f = logging::parse_filter("%MyAttr% = 10.3");
  149. BOOST_CHECK(!f(values1));
  150. BOOST_CHECK(!f(values2));
  151. BOOST_CHECK(!f(values3));
  152. BOOST_CHECK(!f(values4));
  153. BOOST_CHECK(!f(values5));
  154. }
  155. {
  156. logging::filter f = logging::parse_filter("%MyAttr% != 10");
  157. BOOST_CHECK(!f(values1));
  158. BOOST_CHECK(f(values2));
  159. BOOST_CHECK(f(values3));
  160. BOOST_CHECK(f(values4));
  161. BOOST_CHECK(f(values5));
  162. }
  163. {
  164. logging::filter f = logging::parse_filter("%MyAttr% < 5.5");
  165. BOOST_CHECK(!f(values1));
  166. BOOST_CHECK(f(values2));
  167. BOOST_CHECK(!f(values3));
  168. BOOST_CHECK(f(values4));
  169. BOOST_CHECK(f(values5));
  170. }
  171. {
  172. logging::filter f = logging::parse_filter("%MyAttr% < -7");
  173. BOOST_CHECK(!f(values1));
  174. BOOST_CHECK(!f(values2));
  175. BOOST_CHECK(!f(values3));
  176. BOOST_CHECK(f(values4));
  177. BOOST_CHECK(!f(values5));
  178. }
  179. {
  180. logging::filter f = logging::parse_filter("%MyAttr% > 5.6");
  181. BOOST_CHECK(!f(values1));
  182. BOOST_CHECK(!f(values2));
  183. BOOST_CHECK(f(values3));
  184. BOOST_CHECK(!f(values4));
  185. BOOST_CHECK(!f(values5));
  186. }
  187. {
  188. logging::filter f = logging::parse_filter("%MyAttr% > -5");
  189. BOOST_CHECK(!f(values1));
  190. BOOST_CHECK(f(values2));
  191. BOOST_CHECK(f(values3));
  192. BOOST_CHECK(!f(values4));
  193. BOOST_CHECK(f(values5));
  194. }
  195. {
  196. logging::filter f = logging::parse_filter("%MyAttr% <= 20");
  197. BOOST_CHECK(!f(values1));
  198. BOOST_CHECK(f(values2));
  199. BOOST_CHECK(f(values3));
  200. BOOST_CHECK(f(values4));
  201. BOOST_CHECK(f(values5));
  202. }
  203. {
  204. logging::filter f = logging::parse_filter("%MyAttr% >= 20");
  205. BOOST_CHECK(!f(values1));
  206. BOOST_CHECK(!f(values2));
  207. BOOST_CHECK(!f(values3));
  208. BOOST_CHECK(!f(values4));
  209. BOOST_CHECK(!f(values5));
  210. }
  211. }
  212. // Tests for string relation filter
  213. BOOST_AUTO_TEST_CASE(string_relation)
  214. {
  215. attrs::constant< std::string > attr1("hello");
  216. attr_set set1, set2, set3;
  217. attr_values values1(set1, set2, set3);
  218. values1.freeze();
  219. set1["MyStr"] = attr1;
  220. attr_values values2(set1, set2, set3);
  221. values2.freeze();
  222. {
  223. logging::filter f = logging::parse_filter("%MyStr% = hello");
  224. BOOST_CHECK(!f(values1));
  225. BOOST_CHECK(f(values2));
  226. }
  227. {
  228. logging::filter f = logging::parse_filter("%MyStr% = \"hello\"");
  229. BOOST_CHECK(!f(values1));
  230. BOOST_CHECK(f(values2));
  231. }
  232. {
  233. logging::filter f = logging::parse_filter(" % MyStr % = \"hello\" ");
  234. BOOST_CHECK(!f(values1));
  235. BOOST_CHECK(f(values2));
  236. }
  237. {
  238. logging::filter f = logging::parse_filter("%MyStr% = \" hello\"");
  239. BOOST_CHECK(!f(values1));
  240. BOOST_CHECK(!f(values2));
  241. }
  242. {
  243. logging::filter f = logging::parse_filter("%MyStr% = \"hello \"");
  244. BOOST_CHECK(!f(values1));
  245. BOOST_CHECK(!f(values2));
  246. }
  247. {
  248. logging::filter f = logging::parse_filter("%MyStr% = \"world\"");
  249. BOOST_CHECK(!f(values1));
  250. BOOST_CHECK(!f(values2));
  251. }
  252. {
  253. logging::filter f = logging::parse_filter("%MyStr% = \"Hello\"");
  254. BOOST_CHECK(!f(values1));
  255. BOOST_CHECK(!f(values2));
  256. }
  257. {
  258. logging::filter f = logging::parse_filter("%MyStr% != hello");
  259. BOOST_CHECK(!f(values1));
  260. BOOST_CHECK(!f(values2));
  261. }
  262. {
  263. logging::filter f = logging::parse_filter("%MyStr% != world");
  264. BOOST_CHECK(!f(values1));
  265. BOOST_CHECK(f(values2));
  266. }
  267. {
  268. logging::filter f = logging::parse_filter("%MyStr% < world");
  269. BOOST_CHECK(!f(values1));
  270. BOOST_CHECK(f(values2));
  271. }
  272. {
  273. logging::filter f = logging::parse_filter("%MyStr% > world");
  274. BOOST_CHECK(!f(values1));
  275. BOOST_CHECK(!f(values2));
  276. }
  277. // Check that strings that look like numbers can still be used in filters
  278. attrs::constant< std::string > attr2("55");
  279. set1["MyStr"] = attr2;
  280. attr_values values3(set1, set2, set3);
  281. values3.freeze();
  282. {
  283. logging::filter f = logging::parse_filter("%MyStr% = \"55\"");
  284. BOOST_CHECK(f(values3));
  285. }
  286. {
  287. logging::filter f = logging::parse_filter("%MyStr% < \"555\"");
  288. BOOST_CHECK(f(values3));
  289. }
  290. {
  291. logging::filter f = logging::parse_filter("%MyStr% > \"44\"");
  292. BOOST_CHECK(f(values3));
  293. }
  294. }
  295. // Tests for multiple expression filter
  296. BOOST_AUTO_TEST_CASE(multi_expression)
  297. {
  298. attrs::constant< int > attr1(10);
  299. attrs::constant< int > attr2(20);
  300. attrs::constant< std::string > attr3("hello");
  301. attrs::constant< std::string > attr4("world");
  302. attr_set set1, set2, set3;
  303. attr_values values1(set1, set2, set3);
  304. values1.freeze();
  305. set1["MyAttr"] = attr1;
  306. attr_values values2(set1, set2, set3);
  307. values2.freeze();
  308. set1["MyAttr"] = attr2;
  309. attr_values values3(set1, set2, set3);
  310. values3.freeze();
  311. set1["MyStr"] = attr3;
  312. attr_values values4(set1, set2, set3);
  313. values4.freeze();
  314. set1["MyStr"] = attr4;
  315. attr_values values5(set1, set2, set3);
  316. values5.freeze();
  317. {
  318. logging::filter f = logging::parse_filter("%MyAttr% = 10 & %MyStr% = \"hello\"");
  319. BOOST_CHECK(!f(values1));
  320. BOOST_CHECK(!f(values2));
  321. BOOST_CHECK(!f(values3));
  322. BOOST_CHECK(!f(values4));
  323. BOOST_CHECK(!f(values5));
  324. }
  325. {
  326. logging::filter f = logging::parse_filter("%MyAttr% > 10 & %MyStr% = \"hello\"");
  327. BOOST_CHECK(!f(values1));
  328. BOOST_CHECK(!f(values2));
  329. BOOST_CHECK(!f(values3));
  330. BOOST_CHECK(f(values4));
  331. BOOST_CHECK(!f(values5));
  332. }
  333. {
  334. logging::filter f = logging::parse_filter("%MyAttr% > 10 and %MyStr% = \"hello\"");
  335. BOOST_CHECK(!f(values1));
  336. BOOST_CHECK(!f(values2));
  337. BOOST_CHECK(!f(values3));
  338. BOOST_CHECK(f(values4));
  339. BOOST_CHECK(!f(values5));
  340. }
  341. {
  342. logging::filter f = logging::parse_filter("%MyAttr% = 10 | %MyStr% = \"world\"");
  343. BOOST_CHECK(!f(values1));
  344. BOOST_CHECK(f(values2));
  345. BOOST_CHECK(!f(values3));
  346. BOOST_CHECK(!f(values4));
  347. BOOST_CHECK(f(values5));
  348. }
  349. {
  350. logging::filter f = logging::parse_filter("%MyAttr% > 10 | %MyStr% = \"world\"");
  351. BOOST_CHECK(!f(values1));
  352. BOOST_CHECK(!f(values2));
  353. BOOST_CHECK(f(values3));
  354. BOOST_CHECK(f(values4));
  355. BOOST_CHECK(f(values5));
  356. }
  357. {
  358. logging::filter f = logging::parse_filter("%MyAttr% = 10 or %MyStr% = \"world\"");
  359. BOOST_CHECK(!f(values1));
  360. BOOST_CHECK(f(values2));
  361. BOOST_CHECK(!f(values3));
  362. BOOST_CHECK(!f(values4));
  363. BOOST_CHECK(f(values5));
  364. }
  365. {
  366. logging::filter f = logging::parse_filter("%MyAttr% > 0 & %MyAttr% < 20 | %MyStr% = \"hello\"");
  367. BOOST_CHECK(!f(values1));
  368. BOOST_CHECK(f(values2));
  369. BOOST_CHECK(!f(values3));
  370. BOOST_CHECK(f(values4));
  371. BOOST_CHECK(!f(values5));
  372. }
  373. }
  374. // Tests for negation
  375. BOOST_AUTO_TEST_CASE(negation)
  376. {
  377. attrs::constant< int > attr1(10);
  378. attrs::constant< int > attr2(20);
  379. attrs::constant< std::string > attr3("hello");
  380. attr_set set1, set2, set3;
  381. attr_values values1(set1, set2, set3);
  382. values1.freeze();
  383. set1["MyAttr"] = attr1;
  384. attr_values values2(set1, set2, set3);
  385. values2.freeze();
  386. set1["MyAttr"] = attr2;
  387. attr_values values3(set1, set2, set3);
  388. values3.freeze();
  389. set1["MyStr"] = attr3;
  390. attr_values values4(set1, set2, set3);
  391. values4.freeze();
  392. // Test with presence filter
  393. {
  394. logging::filter f = logging::parse_filter("!%MyAttr%");
  395. BOOST_CHECK(f(values1));
  396. BOOST_CHECK(!f(values2));
  397. }
  398. {
  399. logging::filter f = logging::parse_filter(" ! % MyAttr % ");
  400. BOOST_CHECK(f(values1));
  401. BOOST_CHECK(!f(values2));
  402. }
  403. {
  404. logging::filter f = logging::parse_filter("not %MyAttr%");
  405. BOOST_CHECK(f(values1));
  406. BOOST_CHECK(!f(values2));
  407. }
  408. {
  409. logging::filter f = logging::parse_filter("!!%MyAttr%");
  410. BOOST_CHECK(!f(values1));
  411. BOOST_CHECK(f(values2));
  412. }
  413. // Test with relations
  414. {
  415. logging::filter f = logging::parse_filter("!(%MyAttr% = 10)");
  416. BOOST_CHECK(f(values1));
  417. BOOST_CHECK(!f(values2));
  418. BOOST_CHECK(f(values3));
  419. }
  420. {
  421. logging::filter f = logging::parse_filter("not ( %MyAttr% = 10 ) ");
  422. BOOST_CHECK(f(values1));
  423. BOOST_CHECK(!f(values2));
  424. BOOST_CHECK(f(values3));
  425. }
  426. {
  427. logging::filter f = logging::parse_filter("!(%MyAttr% < 20)");
  428. BOOST_CHECK(f(values1));
  429. BOOST_CHECK(!f(values2));
  430. BOOST_CHECK(f(values3));
  431. }
  432. // Test with multiple subexpressions
  433. {
  434. logging::filter f = logging::parse_filter("!(%MyAttr% = 20 & %MyStr% = hello)");
  435. BOOST_CHECK(f(values1));
  436. BOOST_CHECK(f(values2));
  437. BOOST_CHECK(f(values3));
  438. BOOST_CHECK(!f(values4));
  439. }
  440. {
  441. logging::filter f = logging::parse_filter("!(%MyAttr% = 10 | %MyStr% = hello)");
  442. BOOST_CHECK(f(values1));
  443. BOOST_CHECK(!f(values2));
  444. BOOST_CHECK(f(values3));
  445. BOOST_CHECK(!f(values4));
  446. }
  447. }
  448. // Tests for begins_with relation filter
  449. BOOST_AUTO_TEST_CASE(begins_with_relation)
  450. {
  451. attrs::constant< std::string > attr1("abcdABCD");
  452. attr_set set1, set2, set3;
  453. set1["MyStr"] = attr1;
  454. attr_values values1(set1, set2, set3);
  455. values1.freeze();
  456. {
  457. logging::filter f = logging::parse_filter("%MyStr% begins_with \"abcd\"");
  458. BOOST_CHECK(f(values1));
  459. }
  460. {
  461. logging::filter f = logging::parse_filter("%MyStr% begins_with \"ABCD\"");
  462. BOOST_CHECK(!f(values1));
  463. }
  464. {
  465. logging::filter f = logging::parse_filter("%MyStr% begins_with \"efgh\"");
  466. BOOST_CHECK(!f(values1));
  467. }
  468. }
  469. // Tests for ends_with relation filter
  470. BOOST_AUTO_TEST_CASE(ends_with_relation)
  471. {
  472. attrs::constant< std::string > attr1("abcdABCD");
  473. attr_set set1, set2, set3;
  474. set1["MyStr"] = attr1;
  475. attr_values values1(set1, set2, set3);
  476. values1.freeze();
  477. {
  478. logging::filter f = logging::parse_filter("%MyStr% ends_with \"abcd\"");
  479. BOOST_CHECK(!f(values1));
  480. }
  481. {
  482. logging::filter f = logging::parse_filter("%MyStr% ends_with \"ABCD\"");
  483. BOOST_CHECK(f(values1));
  484. }
  485. {
  486. logging::filter f = logging::parse_filter("%MyStr% ends_with \"efgh\"");
  487. BOOST_CHECK(!f(values1));
  488. }
  489. }
  490. // Tests for contains relation filter
  491. BOOST_AUTO_TEST_CASE(contains_relation)
  492. {
  493. attrs::constant< std::string > attr1("abcdABCD");
  494. attr_set set1, set2, set3;
  495. set1["MyStr"] = attr1;
  496. attr_values values1(set1, set2, set3);
  497. values1.freeze();
  498. {
  499. logging::filter f = logging::parse_filter("%MyStr% contains \"abcd\"");
  500. BOOST_CHECK(f(values1));
  501. }
  502. {
  503. logging::filter f = logging::parse_filter("%MyStr% contains \"ABCD\"");
  504. BOOST_CHECK(f(values1));
  505. }
  506. {
  507. logging::filter f = logging::parse_filter("%MyStr% contains \"cdAB\"");
  508. BOOST_CHECK(f(values1));
  509. }
  510. {
  511. logging::filter f = logging::parse_filter("%MyStr% contains \"efgh\"");
  512. BOOST_CHECK(!f(values1));
  513. }
  514. }
  515. // Tests for regex matching relation filter
  516. BOOST_AUTO_TEST_CASE(matches_relation)
  517. {
  518. attrs::constant< std::string > attr1("hello");
  519. attrs::constant< std::string > attr2("127.0.0.1");
  520. attr_set set1, set2, set3;
  521. set1["MyStr"] = attr1;
  522. set1["MyIP"] = attr2;
  523. attr_values values1(set1, set2, set3);
  524. values1.freeze();
  525. {
  526. logging::filter f = logging::parse_filter("%MyStr% matches \"h.*\"");
  527. BOOST_CHECK(f(values1));
  528. }
  529. {
  530. logging::filter f = logging::parse_filter("%MyStr% matches \"w.*\"");
  531. BOOST_CHECK(!f(values1));
  532. }
  533. {
  534. logging::filter f = logging::parse_filter("%MyStr% matches \"\\\\d*\"");
  535. BOOST_CHECK(!f(values1));
  536. }
  537. {
  538. logging::filter f = logging::parse_filter("%MyStr% matches \"[a-z]*\"");
  539. BOOST_CHECK(f(values1));
  540. }
  541. {
  542. logging::filter f = logging::parse_filter("%MyIP% matches \"\\\\d*\\\\.\\\\d*\\\\.\\\\d*\\\\.\\\\d*\"");
  543. BOOST_CHECK(f(values1));
  544. }
  545. }
  546. namespace {
  547. class test_filter_factory :
  548. public logging::filter_factory< char >
  549. {
  550. private:
  551. typedef logging::filter_factory< char > base_type;
  552. public:
  553. enum relation_type
  554. {
  555. custom,
  556. exists,
  557. equality,
  558. inequality,
  559. less,
  560. greater,
  561. less_or_equal,
  562. greater_or_equal
  563. };
  564. typedef base_type::string_type string_type;
  565. public:
  566. explicit test_filter_factory(logging::attribute_name const& name) : m_name(name), m_rel(custom), m_called(false)
  567. {
  568. }
  569. void expect_relation(relation_type rel, string_type const& arg)
  570. {
  571. m_rel = rel;
  572. m_arg = arg;
  573. m_custom_rel.clear();
  574. }
  575. void expect_relation(string_type const& rel, string_type const& arg)
  576. {
  577. m_rel = custom;
  578. m_arg = arg;
  579. m_custom_rel = rel;
  580. }
  581. void check_called()
  582. {
  583. BOOST_CHECK(m_called);
  584. m_called = false;
  585. }
  586. logging::filter on_exists_test(logging::attribute_name const& name)
  587. {
  588. BOOST_CHECK_EQUAL(m_name, name);
  589. BOOST_CHECK_EQUAL(m_rel, exists);
  590. m_called = true;
  591. return logging::filter();
  592. }
  593. logging::filter on_equality_relation(logging::attribute_name const& name, string_type const& arg)
  594. {
  595. BOOST_CHECK_EQUAL(m_name, name);
  596. BOOST_CHECK_EQUAL(m_rel, equality);
  597. BOOST_CHECK_EQUAL(m_arg, arg);
  598. m_called = true;
  599. return logging::filter();
  600. }
  601. logging::filter on_inequality_relation(logging::attribute_name const& name, string_type const& arg)
  602. {
  603. BOOST_CHECK_EQUAL(m_name, name);
  604. BOOST_CHECK_EQUAL(m_rel, inequality);
  605. BOOST_CHECK_EQUAL(m_arg, arg);
  606. m_called = true;
  607. return logging::filter();
  608. }
  609. logging::filter on_less_relation(logging::attribute_name const& name, string_type const& arg)
  610. {
  611. BOOST_CHECK_EQUAL(m_name, name);
  612. BOOST_CHECK_EQUAL(m_rel, less);
  613. BOOST_CHECK_EQUAL(m_arg, arg);
  614. m_called = true;
  615. return logging::filter();
  616. }
  617. logging::filter on_greater_relation(logging::attribute_name const& name, string_type const& arg)
  618. {
  619. BOOST_CHECK_EQUAL(m_name, name);
  620. BOOST_CHECK_EQUAL(m_rel, greater);
  621. BOOST_CHECK_EQUAL(m_arg, arg);
  622. m_called = true;
  623. return logging::filter();
  624. }
  625. logging::filter on_less_or_equal_relation(logging::attribute_name const& name, string_type const& arg)
  626. {
  627. BOOST_CHECK_EQUAL(m_name, name);
  628. BOOST_CHECK_EQUAL(m_rel, less_or_equal);
  629. BOOST_CHECK_EQUAL(m_arg, arg);
  630. m_called = true;
  631. return logging::filter();
  632. }
  633. logging::filter on_greater_or_equal_relation(logging::attribute_name const& name, string_type const& arg)
  634. {
  635. BOOST_CHECK_EQUAL(m_name, name);
  636. BOOST_CHECK_EQUAL(m_rel, greater_or_equal);
  637. BOOST_CHECK_EQUAL(m_arg, arg);
  638. m_called = true;
  639. return logging::filter();
  640. }
  641. logging::filter on_custom_relation(logging::attribute_name const& name, string_type const& rel, string_type const& arg)
  642. {
  643. BOOST_CHECK_EQUAL(m_name, name);
  644. BOOST_CHECK_EQUAL(m_rel, custom);
  645. BOOST_CHECK_EQUAL(m_custom_rel, rel);
  646. BOOST_CHECK_EQUAL(m_arg, arg);
  647. m_called = true;
  648. return logging::filter();
  649. }
  650. private:
  651. logging::attribute_name m_name;
  652. relation_type m_rel;
  653. string_type m_arg;
  654. string_type m_custom_rel;
  655. bool m_called;
  656. };
  657. } // namespace
  658. // Tests for filter factory
  659. BOOST_AUTO_TEST_CASE(filter_factory)
  660. {
  661. logging::attribute_name attr_name("MyCustomAttr");
  662. boost::shared_ptr< test_filter_factory > factory(new test_filter_factory(attr_name));
  663. logging::register_filter_factory(attr_name, factory);
  664. BOOST_TEST_CHECKPOINT("filter_factory::exists");
  665. factory->expect_relation(test_filter_factory::exists, "");
  666. logging::parse_filter("%MyCustomAttr%");
  667. factory->check_called();
  668. BOOST_TEST_CHECKPOINT("filter_factory::equality");
  669. factory->expect_relation(test_filter_factory::equality, "15");
  670. logging::parse_filter("%MyCustomAttr% = 15");
  671. factory->check_called();
  672. BOOST_TEST_CHECKPOINT("filter_factory::equality");
  673. factory->expect_relation(test_filter_factory::equality, "hello");
  674. logging::parse_filter("%MyCustomAttr% = hello");
  675. factory->check_called();
  676. BOOST_TEST_CHECKPOINT("filter_factory::equality");
  677. factory->expect_relation(test_filter_factory::equality, "hello");
  678. logging::parse_filter("%MyCustomAttr% = \"hello\"");
  679. factory->check_called();
  680. BOOST_TEST_CHECKPOINT("filter_factory::equality");
  681. factory->expect_relation(test_filter_factory::equality, "hello\nworld");
  682. logging::parse_filter("%MyCustomAttr% = \"hello\\nworld\"");
  683. factory->check_called();
  684. BOOST_TEST_CHECKPOINT("filter_factory::inequality");
  685. factory->expect_relation(test_filter_factory::inequality, "hello");
  686. logging::parse_filter("%MyCustomAttr% != \"hello\"");
  687. factory->check_called();
  688. BOOST_TEST_CHECKPOINT("filter_factory::less");
  689. factory->expect_relation(test_filter_factory::less, "hello");
  690. logging::parse_filter("%MyCustomAttr% < \"hello\"");
  691. factory->check_called();
  692. BOOST_TEST_CHECKPOINT("filter_factory::greater");
  693. factory->expect_relation(test_filter_factory::greater, "hello");
  694. logging::parse_filter("%MyCustomAttr% > \"hello\"");
  695. factory->check_called();
  696. BOOST_TEST_CHECKPOINT("filter_factory::less_or_equal");
  697. factory->expect_relation(test_filter_factory::less_or_equal, "hello");
  698. logging::parse_filter("%MyCustomAttr% <= \"hello\"");
  699. factory->check_called();
  700. BOOST_TEST_CHECKPOINT("filter_factory::greater_or_equal");
  701. factory->expect_relation(test_filter_factory::greater_or_equal, "hello");
  702. logging::parse_filter("%MyCustomAttr% >= \"hello\"");
  703. factory->check_called();
  704. BOOST_TEST_CHECKPOINT("filter_factory::custom");
  705. factory->expect_relation("my_relation", "hello");
  706. logging::parse_filter("%MyCustomAttr% my_relation \"hello\"");
  707. factory->check_called();
  708. }
  709. // Tests for invalid filters
  710. BOOST_AUTO_TEST_CASE(invalid)
  711. {
  712. BOOST_CHECK_THROW(logging::parse_filter("%MyStr"), logging::parse_error);
  713. BOOST_CHECK_THROW(logging::parse_filter("MyStr%"), logging::parse_error);
  714. BOOST_CHECK_THROW(logging::parse_filter("%MyStr% abcd"), logging::parse_error);
  715. BOOST_CHECK_THROW(logging::parse_filter("(%MyStr%"), logging::parse_error);
  716. BOOST_CHECK_THROW(logging::parse_filter("%MyStr%)"), logging::parse_error);
  717. BOOST_CHECK_THROW(logging::parse_filter("%%"), logging::parse_error);
  718. BOOST_CHECK_THROW(logging::parse_filter("%"), logging::parse_error);
  719. BOOST_CHECK_THROW(logging::parse_filter("!"), logging::parse_error);
  720. BOOST_CHECK_THROW(logging::parse_filter("!()"), logging::parse_error);
  721. BOOST_CHECK_THROW(logging::parse_filter("\"xxx\" == %MyStr%"), logging::parse_error);
  722. BOOST_CHECK_THROW(logging::parse_filter("%MyStr% == \"xxx"), logging::parse_error);
  723. BOOST_CHECK_THROW(logging::parse_filter("%MyStr% === \"xxx\""), logging::parse_error);
  724. BOOST_CHECK_THROW(logging::parse_filter("%MyStr% ! \"xxx\""), logging::parse_error);
  725. BOOST_CHECK_THROW(logging::parse_filter("%MyStr% %MyStr2%"), logging::parse_error);
  726. }
  727. #endif // !defined(BOOST_LOG_WITHOUT_SETTINGS_PARSERS) && !defined(BOOST_LOG_WITHOUT_DEFAULT_FACTORIES)