alg_comp.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. typedef typeof(((jose_hook_alg_t *) NULL)->comp.inf) comp_func_t;
  42. static void
  43. test(const jose_hook_alg_t *a, comp_func_t func, bool iter,
  44. const uint8_t *i, size_t il,
  45. const uint8_t *o, size_t ol)
  46. {
  47. jose_io_auto_t *b = NULL;
  48. jose_io_auto_t *z = NULL;
  49. void *buf = NULL;
  50. size_t len = 0;
  51. b = jose_io_malloc(NULL, &buf, &len);
  52. assert(b);
  53. z = func(a, NULL, b);
  54. assert(z);
  55. if (iter) {
  56. for (size_t j = 0; j < il; j++)
  57. assert(z->feed(z, &i[j], 1));
  58. } else {
  59. assert(z->feed(z, i, il));
  60. }
  61. assert(z->done(z));
  62. assert(len == ol);
  63. assert(memcmp(buf, o, ol) == 0);
  64. }
  65. int
  66. main(int argc, char *argv[])
  67. {
  68. for (size_t i = 0; tests[i].alg; i++) {
  69. const size_t ilen = jose_b64_dec_buf(NULL, strlen(tests[i].inf), NULL, 0);
  70. const size_t dlen = jose_b64_dec_buf(NULL, strlen(tests[i].def), NULL, 0);
  71. const jose_hook_alg_t *a = NULL;
  72. assert(ilen != SIZE_MAX);
  73. assert(dlen != SIZE_MAX);
  74. uint8_t tst_inf[ilen];
  75. uint8_t tst_def[dlen];
  76. assert((a = jose_hook_alg_find(JOSE_HOOK_ALG_KIND_COMP, tests[i].alg)));
  77. assert(jose_b64_dec_buf(tests[i].inf, strlen(tests[i].inf),
  78. tst_inf, sizeof(tst_inf)) == sizeof(tst_inf));
  79. assert(jose_b64_dec_buf(tests[i].def, strlen(tests[i].def),
  80. tst_def, sizeof(tst_def)) == sizeof(tst_def));
  81. test(a, a->comp.def, false,
  82. tst_inf, sizeof(tst_inf),
  83. tst_def, sizeof(tst_def));
  84. test(a, a->comp.inf, false,
  85. tst_def, sizeof(tst_def),
  86. tst_inf, sizeof(tst_inf));
  87. test(a, a->comp.def, true,
  88. tst_inf, sizeof(tst_inf),
  89. tst_def, sizeof(tst_def));
  90. test(a, a->comp.inf, true,
  91. tst_def, sizeof(tst_def),
  92. tst_inf, sizeof(tst_inf));
  93. }
  94. return EXIT_SUCCESS;
  95. }