ctkLDAPExpreTest.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 <QVariant>
  18. int TestParsing( );
  19. int TestEvaluate( );
  20. //-----------------------------------------------------------------------------
  21. int ctkLDAPExpreTest(int argc, char * argv [] )
  22. {
  23. Q_UNUSED(argc);
  24. Q_UNUSED(argv);
  25. if ( TestParsing( ) != EXIT_SUCCESS )
  26. {
  27. return EXIT_FAILURE;
  28. }
  29. if ( TestEvaluate( ) != EXIT_SUCCESS )
  30. {
  31. return EXIT_FAILURE;
  32. }
  33. return EXIT_SUCCESS;
  34. }
  35. int TestParsing( )
  36. {
  37. // WELL FORMED Expr
  38. try
  39. {
  40. ctkLDAPExpr ldap( "(cn=Babs Jensen)" );
  41. ldap = ctkLDAPExpr( "(!(cn=Tim Howes))" );
  42. ldap = ctkLDAPExpr( "(&(" + PluginConstants::OBJECTCLASS + "=Person)(|(sn=Jensen)(cn=Babs J*)))" );
  43. ldap = ctkLDAPExpr( "(o=univ*of*mich*)" );
  44. ldap = ctkLDAPExpr( "(cn=Babs Jensen)" );
  45. }
  46. catch ( ctkInvalidSyntaxException &e )
  47. {
  48. std::cerr << e.what() << std::endl;
  49. return EXIT_FAILURE;
  50. }
  51. // MALFORMED Expre
  52. try
  53. {
  54. ctkLDAPExpr ldap( "cn=Babs Jensen)" );
  55. return EXIT_FAILURE;
  56. }
  57. catch ( ctkInvalidSyntaxException &e )
  58. {
  59. // Nothing to do
  60. int i = 0;
  61. }
  62. return EXIT_SUCCESS;
  63. }
  64. int TestEvaluate( )
  65. {
  66. // EVALUATE
  67. try
  68. {
  69. ctkLDAPExpr ldap( "(cn=Babs Jensen)" );
  70. ctkDictionary dict;
  71. bool eval = false;
  72. // Several values
  73. dict.insert( "cn", "Babs Jensen" );
  74. dict.insert( "unused", "Jansen" );
  75. eval = ldap.evaluate( dict, true );
  76. if ( !eval )
  77. {
  78. return EXIT_FAILURE;
  79. }
  80. // WILDCARD
  81. ldap = ctkLDAPExpr( "(cn=Babs *)" );
  82. dict.clear();
  83. dict.insert( "cn", "Babs Jensen" );
  84. eval = ldap.evaluate( dict, true );
  85. if ( !eval )
  86. {
  87. return EXIT_FAILURE;
  88. }
  89. // NOT FOUND
  90. ldap = ctkLDAPExpr( "(cn=Babs *)" );
  91. dict.clear();
  92. dict.insert( "unused", "New" );
  93. eval = ldap.evaluate( dict, true );
  94. if ( eval )
  95. {
  96. return EXIT_FAILURE;
  97. }
  98. // QList with integer values
  99. ldap = ctkLDAPExpr( " ( |(cn=Babs *)(sn=1) )" );
  100. dict.clear();
  101. QList<QVariant> list;
  102. list.append( "Babs Jensen" );
  103. list.append( "1" );
  104. dict.insert( "sn", list );
  105. eval = ldap.evaluate( dict, true );
  106. if ( !eval )
  107. {
  108. return EXIT_FAILURE;
  109. }
  110. }
  111. catch ( ctkInvalidSyntaxException &e )
  112. {
  113. std::cerr << e.what() << std::endl;
  114. return EXIT_FAILURE;
  115. }
  116. return EXIT_SUCCESS;
  117. }