ctkLDAPExpreTest.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) 2010 Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.commontk.org/LICENSE
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. // CTK includes
  15. #include "ctkLDAPExpr.h"
  16. #include <iostream>
  17. #include <cstdlib>
  18. #include <QVariant>
  19. int TestParsing( );
  20. int TestEvaluate( );
  21. //-----------------------------------------------------------------------------
  22. int ctkLDAPExpreTest(int argc, char * argv [] )
  23. {
  24. Q_UNUSED(argc);
  25. Q_UNUSED(argv);
  26. if ( TestParsing( ) != EXIT_SUCCESS )
  27. {
  28. return EXIT_FAILURE;
  29. }
  30. if ( TestEvaluate( ) != EXIT_SUCCESS )
  31. {
  32. return EXIT_FAILURE;
  33. }
  34. return EXIT_SUCCESS;
  35. }
  36. int TestParsing( )
  37. {
  38. // WELL FORMED Expr
  39. try
  40. {
  41. ctkLDAPExpr ldap( "(cn=Babs Jensen)" );
  42. ldap = ctkLDAPExpr( "(!(cn=Tim Howes))" );
  43. ldap = ctkLDAPExpr( "(&(" + ctkPluginConstants::OBJECTCLASS + "=Person)(|(sn=Jensen)(cn=Babs J*)))" );
  44. ldap = ctkLDAPExpr( "(o=univ*of*mich*)" );
  45. ldap = ctkLDAPExpr( "(cn=Babs Jensen)" );
  46. }
  47. catch ( ctkInvalidSyntaxException &e )
  48. {
  49. std::cerr << e.what() << std::endl;
  50. return EXIT_FAILURE;
  51. }
  52. // MALFORMED Expre
  53. try
  54. {
  55. ctkLDAPExpr ldap( "cn=Babs Jensen)" );
  56. return EXIT_FAILURE;
  57. }
  58. catch ( ctkInvalidSyntaxException &e )
  59. {
  60. // Nothing to do
  61. int i = 0;
  62. }
  63. return EXIT_SUCCESS;
  64. }
  65. int TestEvaluate( )
  66. {
  67. // EVALUATE
  68. try
  69. {
  70. ctkLDAPExpr ldap( "(cn=Babs Jensen)" );
  71. ctkDictionary dict;
  72. bool eval = false;
  73. // Several values
  74. dict.insert( "cn", "Babs Jensen" );
  75. dict.insert( "unused", "Jansen" );
  76. eval = ldap.evaluate( dict, true );
  77. if ( !eval )
  78. {
  79. return EXIT_FAILURE;
  80. }
  81. // WILDCARD
  82. ldap = ctkLDAPExpr( "(cn=Babs *)" );
  83. dict.clear();
  84. dict.insert( "cn", "Babs Jensen" );
  85. eval = ldap.evaluate( dict, true );
  86. if ( !eval )
  87. {
  88. return EXIT_FAILURE;
  89. }
  90. // NOT FOUND
  91. ldap = ctkLDAPExpr( "(cn=Babs *)" );
  92. dict.clear();
  93. dict.insert( "unused", "New" );
  94. eval = ldap.evaluate( dict, true );
  95. if ( eval )
  96. {
  97. return EXIT_FAILURE;
  98. }
  99. // QList with integer values
  100. ldap = ctkLDAPExpr( " ( |(cn=Babs *)(sn=1) )" );
  101. dict.clear();
  102. QList<QVariant> list;
  103. list.append( "Babs Jensen" );
  104. list.append( "1" );
  105. dict.insert( "sn", list );
  106. eval = ldap.evaluate( dict, true );
  107. if ( !eval )
  108. {
  109. return EXIT_FAILURE;
  110. }
  111. }
  112. catch ( ctkInvalidSyntaxException &e )
  113. {
  114. std::cerr << e.what() << std::endl;
  115. return EXIT_FAILURE;
  116. }
  117. return EXIT_SUCCESS;
  118. }