bio_comp_test.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <openssl/evp.h>
  12. #include <openssl/bio.h>
  13. #include <openssl/rand.h>
  14. #include <openssl/comp.h>
  15. #include "testutil.h"
  16. #include "testutil/output.h"
  17. #include "testutil/tu_local.h"
  18. #define COMPRESS 1
  19. #define EXPAND 0
  20. #define BUFFER_SIZE 32 * 1024
  21. #define NUM_SIZES 4
  22. static int sizes[NUM_SIZES] = { 64, 512, 2048, 16 * 1024 };
  23. /* using global buffers */
  24. static unsigned char *original = NULL;
  25. static unsigned char *result = NULL;
  26. /*
  27. * For compression:
  28. * the write operation compresses
  29. * the read operation decompresses
  30. */
  31. static int do_bio_comp_test(const BIO_METHOD *meth, size_t size)
  32. {
  33. BIO *bcomp = NULL;
  34. BIO *bmem = NULL;
  35. BIO *bexp = NULL;
  36. int osize;
  37. int rsize;
  38. int ret = 0;
  39. /* Compress */
  40. if (!TEST_ptr(meth))
  41. goto err;
  42. if (!TEST_ptr(bcomp = BIO_new(meth)))
  43. goto err;
  44. if (!TEST_ptr(bmem = BIO_new(BIO_s_mem())))
  45. goto err;
  46. BIO_push(bcomp, bmem);
  47. osize = BIO_write(bcomp, original, size);
  48. if (!TEST_int_eq(osize, size)
  49. || !TEST_true(BIO_flush(bcomp)))
  50. goto err;
  51. BIO_free(bcomp);
  52. bcomp = NULL;
  53. /* decompress */
  54. if (!TEST_ptr(bexp = BIO_new(meth)))
  55. goto err;
  56. BIO_push(bexp, bmem);
  57. rsize = BIO_read(bexp, result, size);
  58. if (!TEST_int_eq(size, rsize)
  59. || !TEST_mem_eq(original, osize, result, rsize))
  60. goto err;
  61. ret = 1;
  62. err:
  63. BIO_free(bexp);
  64. BIO_free(bcomp);
  65. BIO_free(bmem);
  66. return ret;
  67. }
  68. static int do_bio_comp(const BIO_METHOD *meth, int n)
  69. {
  70. int i;
  71. int success = 0;
  72. int size = sizes[n % 4];
  73. int type = n / 4;
  74. if (!TEST_ptr(original = OPENSSL_malloc(BUFFER_SIZE))
  75. || !TEST_ptr(result = OPENSSL_malloc(BUFFER_SIZE)))
  76. goto err;
  77. switch (type) {
  78. case 0:
  79. TEST_info("zeros of size %d\n", size);
  80. memset(original, 0, BUFFER_SIZE);
  81. break;
  82. case 1:
  83. TEST_info("ones of size %d\n", size);
  84. memset(original, 1, BUFFER_SIZE);
  85. break;
  86. case 2:
  87. TEST_info("sequential of size %d\n", size);
  88. for (i = 0; i < BUFFER_SIZE; i++)
  89. original[i] = i & 0xFF;
  90. break;
  91. case 3:
  92. TEST_info("random of size %d\n", size);
  93. if (!TEST_int_gt(RAND_bytes(original, BUFFER_SIZE), 0))
  94. goto err;
  95. break;
  96. default:
  97. goto err;
  98. }
  99. if (!TEST_true(do_bio_comp_test(meth, size)))
  100. goto err;
  101. success = 1;
  102. err:
  103. OPENSSL_free(original);
  104. OPENSSL_free(result);
  105. return success;
  106. }
  107. #ifndef OPENSSL_NO_ZSTD
  108. static int test_zstd(int n)
  109. {
  110. return do_bio_comp(BIO_f_zstd(), n);
  111. }
  112. #endif
  113. #ifndef OPENSSL_NO_BROTLI
  114. static int test_brotli(int n)
  115. {
  116. return do_bio_comp(BIO_f_brotli(), n);
  117. }
  118. #endif
  119. #ifndef OPENSSL_NO_ZLIB
  120. static int test_zlib(int n)
  121. {
  122. return do_bio_comp(BIO_f_zlib(), n);
  123. }
  124. #endif
  125. int setup_tests(void)
  126. {
  127. #ifndef OPENSSL_NO_ZLIB
  128. ADD_ALL_TESTS(test_zlib, NUM_SIZES * 4);
  129. #endif
  130. #ifndef OPENSSL_NO_BROTLI
  131. ADD_ALL_TESTS(test_brotli, NUM_SIZES * 4);
  132. #endif
  133. #ifndef OPENSSL_NO_ZSTD
  134. ADD_ALL_TESTS(test_zstd, NUM_SIZES * 4);
  135. #endif
  136. return 1;
  137. }