1
0

alg_comp.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /* vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80: */
  2. /*
  3. * Copyright 2017 Red Hat, Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. #include "../lib/hooks.h"
  18. #include <jose/jose.h>
  19. #include <assert.h>
  20. #include <string.h>
  21. #include <stdlib.h>
  22. static int g_high_compression_tested = 0;
  23. static int g_low_compression_tested = 0;
  24. const struct {
  25. const char *alg;
  26. const char *inf;
  27. const char *def;
  28. } tests[] = {
  29. { /* RFC 7520 5.9 */
  30. .alg = "DEF",
  31. .inf = "WW91IGNhbiB0cnVzdCB1cyB0byBzdGljayB3aXRoIHlvdSB0aHJvdWdoIHRoaW"
  32. "NrIGFuZCB0aGlu4oCTdG8gdGhlIGJpdHRlciBlbmQuIEFuZCB5b3UgY2FuIHRy"
  33. "dXN0IHVzIHRvIGtlZXAgYW55IHNlY3JldCBvZiB5b3Vyc-KAk2Nsb3NlciB0aG"
  34. "FuIHlvdSBrZWVwIGl0IHlvdXJzZWxmLiBCdXQgeW91IGNhbm5vdCB0cnVzdCB1"
  35. "cyB0byBsZXQgeW91IGZhY2UgdHJvdWJsZSBhbG9uZSwgYW5kIGdvIG9mZiB3aX"
  36. "Rob3V0IGEgd29yZC4gV2UgYXJlIHlvdXIgZnJpZW5kcywgRnJvZG8u",
  37. .def = "bY_BDcIwDEVX-QNU3QEOrIA4pqlDokYxchxVvbEDGzIJbioOSJwc-f___HPjBu"
  38. "8KVFpVtAplVE1-wZo0YjNZo3C7R5v72pV5f5X382VWjYQpqZKAyjziZOr2B7kQ"
  39. "PSy6oZIXUnDYbVKN4jNXi2u0yB7t1qSHTjmMODf9QgvrDzfTIQXnyQRuUya4zI"
  40. "WG3vTOdir0v7BRHFYWq3k1k1A_gSDJqtcBF-GZxw8"
  41. },
  42. {}
  43. };
  44. const uint32_t long_string_tests[] = {
  45. 2000, 200000, 10000000, 0
  46. };
  47. static uint8_t* get_random_string(uint32_t length)
  48. {
  49. assert(length);
  50. uint8_t* c = (uint8_t*)malloc(length*sizeof(uint8_t));
  51. assert(c);
  52. for (uint32_t i=0; i<length; i++) {
  53. c[i] = 'A' + (random() % 26);
  54. }
  55. return c;
  56. }
  57. static void
  58. test_long_string(size_t inputlen) {
  59. jose_io_auto_t *b = NULL;
  60. jose_io_auto_t *c = NULL;
  61. jose_io_auto_t *z = NULL;
  62. void *buf1 = NULL;
  63. void *buf2 = NULL;
  64. size_t blen = 0;
  65. size_t clen = 0;
  66. const jose_hook_alg_t *a = jose_hook_alg_find(JOSE_HOOK_ALG_KIND_COMP, "DEF");
  67. uint8_t* str = get_random_string(inputlen);
  68. /* Test compression first. */
  69. b = jose_io_malloc(NULL, &buf1, &blen);
  70. assert(b);
  71. z = a->comp.def(a, NULL, b);
  72. assert(z);
  73. assert(z->feed(z, str, inputlen));
  74. assert(z->done(z));
  75. /* Test decompression now */
  76. c = jose_io_malloc(NULL, &buf2, &clen);
  77. assert(b);
  78. z = a->comp.inf(a, NULL, c);
  79. assert(z);
  80. /* If length>MAX_COMPRESSED_SIZE, it must fail due to high decompression size */
  81. if(blen > MAX_COMPRESSED_SIZE) {
  82. assert(!z->feed(z, buf1, blen));
  83. g_high_compression_tested = 1;
  84. } else {
  85. assert(z->feed(z, buf1, blen));
  86. g_low_compression_tested = 1;
  87. /* Compare the final output with the original input. */
  88. assert(clen == inputlen);
  89. assert(memcmp(buf2, str, inputlen) == 0);
  90. }
  91. assert(z->done(z));
  92. free(str);
  93. }
  94. static void
  95. test(const jose_hook_alg_t *a, bool iter,
  96. const uint8_t *i, size_t il)
  97. {
  98. jose_io_auto_t *b = NULL;
  99. jose_io_auto_t *c = NULL;
  100. jose_io_auto_t *z = NULL;
  101. void *buf1 = NULL;
  102. void *buf2 = NULL;
  103. size_t blen = 0;
  104. size_t clen = 0;
  105. /* Test compression first. */
  106. b = jose_io_malloc(NULL, &buf1, &blen);
  107. assert(b);
  108. z = a->comp.def(a, NULL, b);
  109. assert(z);
  110. if (iter) {
  111. for (size_t j = 0; j < il; j++)
  112. assert(z->feed(z, &i[j], 1));
  113. } else {
  114. assert(z->feed(z, i, il));
  115. }
  116. assert(z->done(z));
  117. /* Test decompression now. */
  118. c = jose_io_malloc(NULL, &buf2, &clen);
  119. assert(b);
  120. z = a->comp.inf(a, NULL, c);
  121. assert(z);
  122. if (iter) {
  123. uint8_t *m = buf1;
  124. for (size_t j = 0; j < blen; j++)
  125. assert(z->feed(z, &m[j], 1));
  126. } else {
  127. assert(z->feed(z, buf1, blen));
  128. }
  129. assert(z->done(z));
  130. /* Compare the final output with the original input. */
  131. assert(clen == il);
  132. assert(memcmp(buf2, i, il) == 0);
  133. }
  134. int
  135. main(int argc, char *argv[])
  136. {
  137. for (size_t i = 0; tests[i].alg; i++) {
  138. const size_t ilen = jose_b64_dec_buf(NULL, strlen(tests[i].inf), NULL, 0);
  139. const size_t dlen = jose_b64_dec_buf(NULL, strlen(tests[i].def), NULL, 0);
  140. const jose_hook_alg_t *a = NULL;
  141. assert(ilen != SIZE_MAX);
  142. assert(dlen != SIZE_MAX);
  143. uint8_t tst_inf[ilen];
  144. uint8_t tst_def[dlen];
  145. assert((a = jose_hook_alg_find(JOSE_HOOK_ALG_KIND_COMP, tests[i].alg)));
  146. assert(jose_b64_dec_buf(tests[i].inf, strlen(tests[i].inf),
  147. tst_inf, sizeof(tst_inf)) == sizeof(tst_inf));
  148. assert(jose_b64_dec_buf(tests[i].def, strlen(tests[i].def),
  149. tst_def, sizeof(tst_def)) == sizeof(tst_def));
  150. test(a, false,
  151. tst_inf, sizeof(tst_inf));
  152. test(a, true,
  153. tst_inf, sizeof(tst_inf));
  154. }
  155. for (size_t i = 0; long_string_tests[i]; i++) {
  156. test_long_string(long_string_tests[i]);
  157. }
  158. assert(1 == g_high_compression_tested);
  159. assert(1 == g_low_compression_tested);
  160. return EXIT_SUCCESS;
  161. }