1
0

alg_comp.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. const struct {
  22. const char *alg;
  23. const char *inf;
  24. const char *def;
  25. } tests[] = {
  26. { /* RFC 7520 5.9 */
  27. .alg = "DEF",
  28. .inf = "WW91IGNhbiB0cnVzdCB1cyB0byBzdGljayB3aXRoIHlvdSB0aHJvdWdoIHRoaW"
  29. "NrIGFuZCB0aGlu4oCTdG8gdGhlIGJpdHRlciBlbmQuIEFuZCB5b3UgY2FuIHRy"
  30. "dXN0IHVzIHRvIGtlZXAgYW55IHNlY3JldCBvZiB5b3Vyc-KAk2Nsb3NlciB0aG"
  31. "FuIHlvdSBrZWVwIGl0IHlvdXJzZWxmLiBCdXQgeW91IGNhbm5vdCB0cnVzdCB1"
  32. "cyB0byBsZXQgeW91IGZhY2UgdHJvdWJsZSBhbG9uZSwgYW5kIGdvIG9mZiB3aX"
  33. "Rob3V0IGEgd29yZC4gV2UgYXJlIHlvdXIgZnJpZW5kcywgRnJvZG8u",
  34. .def = "bY_BDcIwDEVX-QNU3QEOrIA4pqlDokYxchxVvbEDGzIJbioOSJwc-f___HPjBu"
  35. "8KVFpVtAplVE1-wZo0YjNZo3C7R5v72pV5f5X382VWjYQpqZKAyjziZOr2B7kQ"
  36. "PSy6oZIXUnDYbVKN4jNXi2u0yB7t1qSHTjmMODf9QgvrDzfTIQXnyQRuUya4zI"
  37. "WG3vTOdir0v7BRHFYWq3k1k1A_gSDJqtcBF-GZxw8"
  38. },
  39. {}
  40. };
  41. static void
  42. test(const jose_hook_alg_t *a, bool iter,
  43. const uint8_t *i, size_t il)
  44. {
  45. jose_io_auto_t *b = NULL;
  46. jose_io_auto_t *c = NULL;
  47. jose_io_auto_t *z = NULL;
  48. void *buf1 = NULL;
  49. void *buf2 = NULL;
  50. size_t blen = 0;
  51. size_t clen = 0;
  52. /* Test compression first. */
  53. b = jose_io_malloc(NULL, &buf1, &blen);
  54. assert(b);
  55. z = a->comp.def(a, NULL, b);
  56. assert(z);
  57. if (iter) {
  58. for (size_t j = 0; j < il; j++)
  59. assert(z->feed(z, &i[j], 1));
  60. } else {
  61. assert(z->feed(z, i, il));
  62. }
  63. assert(z->done(z));
  64. /* Test decompression now. */
  65. c = jose_io_malloc(NULL, &buf2, &clen);
  66. assert(b);
  67. z = a->comp.inf(a, NULL, c);
  68. assert(z);
  69. if (iter) {
  70. uint8_t *m = buf1;
  71. for (size_t j = 0; j < blen; j++)
  72. assert(z->feed(z, &m[j], 1));
  73. } else {
  74. assert(z->feed(z, buf1, blen));
  75. }
  76. assert(z->done(z));
  77. /* Compare the final output with the original input. */
  78. assert(clen == il);
  79. assert(memcmp(buf2, i, il) == 0);
  80. }
  81. int
  82. main(int argc, char *argv[])
  83. {
  84. for (size_t i = 0; tests[i].alg; i++) {
  85. const size_t ilen = jose_b64_dec_buf(NULL, strlen(tests[i].inf), NULL, 0);
  86. const size_t dlen = jose_b64_dec_buf(NULL, strlen(tests[i].def), NULL, 0);
  87. const jose_hook_alg_t *a = NULL;
  88. assert(ilen != SIZE_MAX);
  89. assert(dlen != SIZE_MAX);
  90. uint8_t tst_inf[ilen];
  91. uint8_t tst_def[dlen];
  92. assert((a = jose_hook_alg_find(JOSE_HOOK_ALG_KIND_COMP, tests[i].alg)));
  93. assert(jose_b64_dec_buf(tests[i].inf, strlen(tests[i].inf),
  94. tst_inf, sizeof(tst_inf)) == sizeof(tst_inf));
  95. assert(jose_b64_dec_buf(tests[i].def, strlen(tests[i].def),
  96. tst_def, sizeof(tst_def)) == sizeof(tst_def));
  97. test(a, false,
  98. tst_inf, sizeof(tst_inf));
  99. test(a, true,
  100. tst_inf, sizeof(tst_inf));
  101. }
  102. return EXIT_SUCCESS;
  103. }