bio_comp_test.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright 2022-2025 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. original = OPENSSL_malloc(BUFFER_SIZE);
  75. result = OPENSSL_malloc(BUFFER_SIZE);
  76. if (!TEST_ptr(original) || !TEST_ptr(result))
  77. goto err;
  78. switch (type) {
  79. case 0:
  80. TEST_info("zeros of size %d\n", size);
  81. memset(original, 0, BUFFER_SIZE);
  82. break;
  83. case 1:
  84. TEST_info("ones of size %d\n", size);
  85. memset(original, 1, BUFFER_SIZE);
  86. break;
  87. case 2:
  88. TEST_info("sequential of size %d\n", size);
  89. for (i = 0; i < BUFFER_SIZE; i++)
  90. original[i] = i & 0xFF;
  91. break;
  92. case 3:
  93. TEST_info("random of size %d\n", size);
  94. if (!TEST_int_gt(RAND_bytes(original, BUFFER_SIZE), 0))
  95. goto err;
  96. break;
  97. default:
  98. goto err;
  99. }
  100. if (!TEST_true(do_bio_comp_test(meth, size)))
  101. goto err;
  102. success = 1;
  103. err:
  104. OPENSSL_free(original);
  105. OPENSSL_free(result);
  106. return success;
  107. }
  108. #ifndef OPENSSL_NO_ZSTD
  109. static int test_zstd(int n)
  110. {
  111. return do_bio_comp(BIO_f_zstd(), n);
  112. }
  113. #endif
  114. #ifndef OPENSSL_NO_BROTLI
  115. static int test_brotli(int n)
  116. {
  117. return do_bio_comp(BIO_f_brotli(), n);
  118. }
  119. #endif
  120. #ifndef OPENSSL_NO_ZLIB
  121. static int test_zlib(int n)
  122. {
  123. return do_bio_comp(BIO_f_zlib(), n);
  124. }
  125. #endif
  126. int setup_tests(void)
  127. {
  128. #ifndef OPENSSL_NO_ZLIB
  129. ADD_ALL_TESTS(test_zlib, NUM_SIZES * 4);
  130. #endif
  131. #ifndef OPENSSL_NO_BROTLI
  132. ADD_ALL_TESTS(test_brotli, NUM_SIZES * 4);
  133. #endif
  134. #ifndef OPENSSL_NO_ZSTD
  135. ADD_ALL_TESTS(test_zstd, NUM_SIZES * 4);
  136. #endif
  137. return 1;
  138. }