test_auth.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright (C) 2009 Mamadou Diop.
  3. *
  4. * Contact: Mamadou Diop <diopmamadou(at)doubango.org>
  5. *
  6. * This file is part of Open Source Doubango Framework.
  7. *
  8. * DOUBANGO is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * DOUBANGO is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with DOUBANGO.
  20. *
  21. */
  22. #ifndef _TEST_AUTH_H_
  23. #define _TEST_AUTH_H_
  24. struct auth_basic_msg {
  25. const char* userid;
  26. const char* password;
  27. const char* xres;
  28. };
  29. struct auth_basic_msg auth_basic_msgs[] = {
  30. { "Aladdin"/*Ali baba*/, "open sesame", "QWxhZGRpbjpvcGVuIHNlc2FtZQ==" },
  31. };
  32. void test_basic_auth()
  33. {
  34. char *response = tsk_null;
  35. size_t i, size;
  36. for(i=0; i<sizeof(auth_basic_msgs)/sizeof(struct auth_basic_msg); i++) {
  37. size = thttp_auth_basic_response(auth_basic_msgs[i].userid, auth_basic_msgs[i].password, &response);
  38. if(tsk_striequals(auth_basic_msgs[i].xres, response)) {
  39. TSK_DEBUG_INFO("[HTTP_BASIC-%d] ==> OK", i);
  40. }
  41. else {
  42. TSK_DEBUG_INFO("[HTTP_BASIC-%d] ==> NOK", i);
  43. }
  44. TSK_FREE(response);
  45. }
  46. }
  47. //========================================================================
  48. struct auth_ws {
  49. const char* key;
  50. const char* xres;
  51. };
  52. struct auth_ws auth_ws_msgs[] = {
  53. { "dGhlIHNhbXBsZSBub25jZQ==", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo=" },
  54. { "x3JJHMbDL1EzLkh9GBhXDw==", "HSmrc0sMlYUkAGmm5OPpG2HaGWk=" },
  55. };
  56. void test_ws_auth()
  57. {
  58. thttp_auth_ws_keystring_t response = {0};
  59. size_t i, size;
  60. for(i=0; i<sizeof(auth_ws_msgs)/sizeof(struct auth_ws); i++) {
  61. size = thttp_auth_ws_response(auth_ws_msgs[i].key, &response);
  62. if(tsk_striequals(auth_ws_msgs[i].xres, response)) {
  63. TSK_DEBUG_INFO("[WS_AUTH-%d] ==> OK", i);
  64. }
  65. else {
  66. TSK_DEBUG_INFO("[WS_AUTH-%d] ==> NOK", i);
  67. }
  68. }
  69. }
  70. //========================================================================
  71. struct auth_digest_msg {
  72. const char* username;
  73. const char* password;
  74. const char* realm;
  75. const char* nonce;
  76. const char* method;
  77. const char* uri;
  78. const char* qop;
  79. const char* nc;
  80. const char* cnonce;
  81. const char* opaque;
  82. const char* entitybody;
  83. const char* response;
  84. };
  85. struct auth_digest_msg auth_digest_msgs[] = {
  86. {
  87. "Mufasa",
  88. "Circle Of Life",
  89. "testrealm@host.com",
  90. "dcd98b7102dd2f0e8b11d0f600bfb0c093",
  91. "GET",
  92. "/dir/index.html",
  93. "auth",
  94. "00000001",
  95. "0a4f113b",
  96. "5ccc069c403ebaf9f0171e9517f40e41",
  97. 0,
  98. "6629fae49393a05397450978507c4ef1"
  99. }
  100. ,
  101. };
  102. void test_digest_auth()
  103. {
  104. tsk_md5string_t response, ha1, ha2;
  105. size_t i;
  106. for(i=0; i<sizeof(auth_digest_msgs)/sizeof(struct auth_digest_msg); i++) {
  107. tsk_buffer_t* entitybody;
  108. /* HA1 */
  109. thttp_auth_digest_HA1(auth_digest_msgs[i].username,
  110. auth_digest_msgs[i].realm,
  111. auth_digest_msgs[i].password,
  112. &ha1);
  113. /* HA2 */
  114. entitybody = tsk_buffer_create(auth_digest_msgs[i].entitybody, strlen(auth_digest_msgs[i].entitybody));
  115. thttp_auth_digest_HA2(auth_digest_msgs[i].method,
  116. auth_digest_msgs[i].uri,
  117. entitybody,
  118. auth_digest_msgs[i].qop,
  119. &ha2);
  120. TSK_OBJECT_SAFE_FREE(entitybody);
  121. /* RESPONSE */
  122. thttp_auth_digest_response(&ha1,
  123. auth_digest_msgs[i].nonce,
  124. auth_digest_msgs[i].nc,
  125. auth_digest_msgs[i].cnonce,
  126. auth_digest_msgs[i].qop,
  127. &ha2,
  128. &response);
  129. if(tsk_striequals(auth_digest_msgs[i].response, response)) {
  130. TSK_DEBUG_INFO("[HTTP_DIGEST-%d] ==> OK", i);
  131. }
  132. else {
  133. TSK_DEBUG_INFO("[HTTP_DIGEST-%d] ==> NOK", i);
  134. }
  135. }
  136. }
  137. #endif /* _TEST_AUTH_H_ */