bio_meth_test.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * Copyright 2024 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 <openssl/bio.h>
  10. #include "testutil.h"
  11. static int test_bio_meth(void)
  12. {
  13. int i, ret = 0, id;
  14. BIO_METHOD *meth1 = NULL, *meth2 = NULL, *meth3 = NULL;
  15. BIO *membio = NULL, *bio1 = NULL, *bio2 = NULL, *bio3 = NULL;
  16. id = BIO_get_new_index();
  17. if (!TEST_int_eq(id, BIO_TYPE_START + 1))
  18. goto err;
  19. if (!TEST_ptr(meth1 = BIO_meth_new(id, "Method1"))
  20. || !TEST_ptr(meth2 = BIO_meth_new(BIO_TYPE_NONE, "Method2"))
  21. || !TEST_ptr(meth3 = BIO_meth_new(BIO_TYPE_NONE|BIO_TYPE_FILTER, "Method3"))
  22. || !TEST_ptr(bio1 = BIO_new(meth1))
  23. || !TEST_ptr(bio2 = BIO_new(meth2))
  24. || !TEST_ptr(bio3 = BIO_new(meth3))
  25. || !TEST_ptr(membio = BIO_new(BIO_s_mem())))
  26. goto err;
  27. BIO_set_next(bio3, bio2);
  28. BIO_set_next(bio2, bio1);
  29. BIO_set_next(bio1, membio);
  30. /* Check that we get an error if we exhaust BIO_get_new_index() */
  31. for (i = id + 1; i <= BIO_TYPE_MASK; ++i) {
  32. if (!TEST_int_eq(BIO_get_new_index(), i))
  33. goto err;
  34. }
  35. if (!TEST_int_eq(BIO_get_new_index(), -1))
  36. goto err;
  37. /* test searching works */
  38. if (!TEST_ptr_eq(BIO_find_type(bio3, BIO_TYPE_MEM), membio)
  39. || !TEST_ptr_eq(BIO_find_type(bio3, id), bio1))
  40. goto err;
  41. /* Check searching for BIO_TYPE_NONE returns NULL */
  42. if (!TEST_ptr_null(BIO_find_type(bio3, BIO_TYPE_NONE)))
  43. goto err;
  44. /* Check searching for BIO_TYPE_NONE + BIO_TYPE_FILTER works */
  45. if (!TEST_ptr_eq(BIO_find_type(bio3, BIO_TYPE_FILTER), bio3))
  46. goto err;
  47. ret = 1;
  48. err:
  49. BIO_free(membio);
  50. BIO_free(bio3);
  51. BIO_free(bio2);
  52. BIO_free(bio1);
  53. BIO_meth_free(meth3);
  54. BIO_meth_free(meth2);
  55. BIO_meth_free(meth1);
  56. return ret;
  57. }
  58. int setup_tests(void)
  59. {
  60. ADD_TEST(test_bio_meth);
  61. return 1;
  62. }