1
0

test_base32.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #include <stddef.h>
  2. #include <stdint.h>
  3. #include "types.h"
  4. #include "base32.h"
  5. #include <string.h>
  6. #include <assert.h>
  7. #include <stdio.h>
  8. #include <sodium/randombytes.h>
  9. struct texttestcase {
  10. const char *in;
  11. const char *out;
  12. const char *rev;
  13. } tests0[] = {
  14. {"", "", ""},
  15. {"f", "my", "f"},
  16. {"fo", "mzxq", "fo"},
  17. {"foo", "mzxw6", "foo"},
  18. {"foob", "mzxw6yq", "foob"},
  19. {"fooba", "mzxw6ytb", "fooba"},
  20. {"foobar", "mzxw6ytboi", "foobar"},
  21. };
  22. /*
  23. r:0, mask:FF
  24. --
  25. r:2, mask:C0
  26. f -- f
  27. r:3, mask:F0
  28. fo -- fo
  29. r:4, mask:80
  30. foo -- foo
  31. r:5, mask:E0
  32. foob -- foob
  33. r:5, mask:FF
  34. fooba -- fooba
  35. r:7, mask:C0
  36. foobar -- foobar
  37. */
  38. /*
  39. struct masktestcase {
  40. const char *src;
  41. u8 mask;
  42. } tests1[] = {
  43. {"", 0x00},
  44. {"a", 0x00},
  45. {"ab", 0x00},
  46. {"abc", 0x00},
  47. {"abcd", 0x00},
  48. {"abcde", 0x00},
  49. {"abcdef", 0x00},
  50. {"abcdefg", 0x00},
  51. {"abcdefgh", 0x00},
  52. {"abcdefghi", 0x00},
  53. {"abcdefghij", 0x00},
  54. };
  55. */
  56. int main(void)
  57. {
  58. char buf[1024], buf2[1024], mask;
  59. size_t r;
  60. for (size_t i = 0; i < sizeof(tests0)/sizeof(tests0[0]); ++i) {
  61. base32_to(buf, (const u8 *)tests0[i].in, strlen(tests0[i].in));
  62. assert(strcmp(buf, tests0[i].out) == 0);
  63. r = base32_from((u8 *)buf2, (u8 *)&mask, buf);
  64. buf2[r] = 0;
  65. if (r > 0) {
  66. assert((buf2[r-1] & ~mask) == 0);
  67. }
  68. //fprintf(stderr, "r:%d, mask:%02X\n", (int)r, ((unsigned int)mask) & 0xFF);
  69. //assert(r == strlen(buf2));
  70. //assert(r == strlen(tests0[i].rev));
  71. //fprintf(stderr, "%s -- %s\n", buf2, tests0[i].rev);
  72. assert(strcmp(buf2, tests0[i].rev) == 0);
  73. }
  74. //randombytes_buf(buf, 128);
  75. //base32_to(buf2, (const u8 *)buf, 128);
  76. //fprintf(stderr, ">%s\n", buf2);
  77. return 0;
  78. }