test_base64.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 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_BASE64_H_
  23. #define _TEST_BASE64_H_
  24. struct b64_msg {
  25. const char* ascii;
  26. const char* base64;
  27. };
  28. struct b64_msg b64_msgs[] = {
  29. { "", "" },
  30. { "f", "Zg==" },
  31. { "fo", "Zm8=" },
  32. { "foo", "Zm9v" },
  33. { "foob", "Zm9vYg==" },
  34. { "fooba", "Zm9vYmE=" },
  35. { "foobar", "Zm9vYmFy" },
  36. { "Aladdin:open sesame", "QWxhZGRpbjpvcGVuIHNlc2FtZQ=="},
  37. { "Doubango Project", "RG91YmFuZ28gUHJvamVjdA=="},
  38. { "Open Source Doubango Framework", "T3BlbiBTb3VyY2UgRG91YmFuZ28gRnJhbWV3b3Jr"},
  39. {
  40. "* Copyright (C) 2009 Mamadou Diop."
  41. "*"
  42. "* Contact: Mamadou Diop <diopmamadou(at)doubango.org>"
  43. "*"
  44. "* This file is part of Open Source Doubango Framework."
  45. "*"
  46. "* DOUBANGO is free software: you can redistribute it and/or modify"
  47. "* it under the terms of the GNU General Public License as published by"
  48. "* the Free Software Foundation, either version 3 of the License, or"
  49. "* (at your option) any later version."
  50. "*"
  51. "* DOUBANGO is distributed in the hope that it will be useful,"
  52. "* but WITHOUT ANY WARRANTY; without even the implied warranty of"
  53. "* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the"
  54. "* GNU General Public License for more details."
  55. "*"
  56. "* You should have received a copy of the GNU General Public License"
  57. "* along with DOUBANGO."
  58. ,
  59. "KiBDb3B5cmlnaHQgKEMpIDIwMDkgTWFtYWRvdSBEaW9wLioqIENvbnRhY3Q6IE1hbW"
  60. "Fkb3UgRGlvcCA8ZGlvcG1hbWFkb3UoYXQpZG91YmFuZ28ub3JnPioqIFRoaXMgZmls"
  61. "ZSBpcyBwYXJ0IG9mIE9wZW4gU291cmNlIERvdWJhbmdvIEZyYW1ld29yay4qKiBET1"
  62. "VCQU5HTyBpcyBmcmVlIHNvZnR3YXJlOiB5b3UgY2FuIHJlZGlzdHJpYnV0ZSBpdCBh"
  63. "bmQvb3IgbW9kaWZ5KiBpdCB1bmRlciB0aGUgdGVybXMgb2YgdGhlIEdOVSBHZW5lcm"
  64. "FsIFB1YmxpYyBMaWNlbnNlIGFzIHB1Ymxpc2hlZCBieSogdGhlIEZyZWUgU29mdHdh"
  65. "cmUgRm91bmRhdGlvbiwgZWl0aGVyIHZlcnNpb24gMyBvZiB0aGUgTGljZW5zZSwgb3"
  66. "IqIChhdCB5b3VyIG9wdGlvbikgYW55IGxhdGVyIHZlcnNpb24uKiogRE9VQkFOR08g"
  67. "aXMgZGlzdHJpYnV0ZWQgaW4gdGhlIGhvcGUgdGhhdCBpdCB3aWxsIGJlIHVzZWZ1bC"
  68. "wqIGJ1dCBXSVRIT1VUIEFOWSBXQVJSQU5UWTsgd2l0aG91dCBldmVuIHRoZSBpbXBs"
  69. "aWVkIHdhcnJhbnR5IG9mKiBNRVJDSEFOVEFCSUxJVFkgb3IgRklUTkVTUyBGT1IgQS"
  70. "BQQVJUSUNVTEFSIFBVUlBPU0UuICBTZWUgdGhlKiBHTlUgR2VuZXJhbCBQdWJsaWMg"
  71. "TGljZW5zZSBmb3IgbW9yZSBkZXRhaWxzLioqIFlvdSBzaG91bGQgaGF2ZSByZWNlaX"
  72. "ZlZCBhIGNvcHkgb2YgdGhlIEdOVSBHZW5lcmFsIFB1YmxpYyBMaWNlbnNlKiBhbG9u"
  73. "ZyB3aXRoIERPVUJBTkdPLg=="
  74. }
  75. };
  76. void test_base64()
  77. {
  78. size_t i, size;
  79. char *output_e = 0, *output_d = 0;
  80. for(i=0; i<sizeof(b64_msgs)/sizeof(struct b64_msg); i++) {
  81. /*===========
  82. * Encoding
  83. */
  84. size = tsk_base64_encode((const uint8_t*)b64_msgs[i].ascii, strlen(b64_msgs[i].ascii), &output_e);
  85. if(tsk_striequals(b64_msgs[i].base64, output_e)) {
  86. TSK_DEBUG_INFO("[BASE64-%d encoding] ==> OK", i);
  87. }
  88. else {
  89. TSK_DEBUG_INFO("[BASE64-%d encoding] ==> NOK", i);
  90. }
  91. TSK_FREE(output_e);
  92. /*===========
  93. * Decoding
  94. */
  95. size = tsk_base64_decode((const uint8_t*)b64_msgs[i].base64, strlen(b64_msgs[i].base64), &output_d);
  96. if(tsk_striequals(b64_msgs[i].ascii, output_d)) {
  97. TSK_DEBUG_INFO("[BASE64-%d decoding] ==> OK", i);
  98. }
  99. else {
  100. TSK_DEBUG_INFO("[BASE64-%d decoding] ==> NOK", i);
  101. }
  102. TSK_FREE(output_d);
  103. }
  104. }
  105. #endif /* _TEST_BASE64_H_ */