bio_core_test.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright 2021-2023 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 <string.h>
  10. #include <openssl/bio.h>
  11. #include "testutil.h"
  12. struct ossl_core_bio_st {
  13. int dummy;
  14. BIO *bio;
  15. };
  16. static int tst_bio_core_read_ex(OSSL_CORE_BIO *bio, char *data, size_t data_len,
  17. size_t *bytes_read)
  18. {
  19. return BIO_read_ex(bio->bio, data, data_len, bytes_read);
  20. }
  21. static int tst_bio_core_write_ex(OSSL_CORE_BIO *bio, const char *data,
  22. size_t data_len, size_t *written)
  23. {
  24. return BIO_write_ex(bio->bio, data, data_len, written);
  25. }
  26. static int tst_bio_core_gets(OSSL_CORE_BIO *bio, char *buf, int size)
  27. {
  28. return BIO_gets(bio->bio, buf, size);
  29. }
  30. static int tst_bio_core_puts(OSSL_CORE_BIO *bio, const char *str)
  31. {
  32. return BIO_puts(bio->bio, str);
  33. }
  34. static long tst_bio_core_ctrl(OSSL_CORE_BIO *bio, int cmd, long num, void *ptr)
  35. {
  36. return BIO_ctrl(bio->bio, cmd, num, ptr);
  37. }
  38. static int tst_bio_core_up_ref(OSSL_CORE_BIO *bio)
  39. {
  40. return BIO_up_ref(bio->bio);
  41. }
  42. static int tst_bio_core_free(OSSL_CORE_BIO *bio)
  43. {
  44. return BIO_free(bio->bio);
  45. }
  46. static const OSSL_DISPATCH biocbs[] = {
  47. { OSSL_FUNC_BIO_READ_EX, (void (*)(void))tst_bio_core_read_ex },
  48. { OSSL_FUNC_BIO_WRITE_EX, (void (*)(void))tst_bio_core_write_ex },
  49. { OSSL_FUNC_BIO_GETS, (void (*)(void))tst_bio_core_gets },
  50. { OSSL_FUNC_BIO_PUTS, (void (*)(void))tst_bio_core_puts },
  51. { OSSL_FUNC_BIO_CTRL, (void (*)(void))tst_bio_core_ctrl },
  52. { OSSL_FUNC_BIO_UP_REF, (void (*)(void))tst_bio_core_up_ref },
  53. { OSSL_FUNC_BIO_FREE, (void (*)(void))tst_bio_core_free },
  54. OSSL_DISPATCH_END
  55. };
  56. static int test_bio_core(void)
  57. {
  58. BIO *cbio = NULL, *cbiobad = NULL;
  59. OSSL_LIB_CTX *libctx = OSSL_LIB_CTX_new_from_dispatch(NULL, biocbs);
  60. int testresult = 0;
  61. OSSL_CORE_BIO corebio;
  62. const char *msg = "Hello world";
  63. char buf[80];
  64. corebio.bio = BIO_new(BIO_s_mem());
  65. if (!TEST_ptr(corebio.bio)
  66. || !TEST_ptr(libctx)
  67. /*
  68. * Attempting to create a corebio in a libctx that was not
  69. * created via OSSL_LIB_CTX_new_from_dispatch() should fail.
  70. */
  71. || !TEST_ptr_null((cbiobad = BIO_new_from_core_bio(NULL, &corebio)))
  72. || !TEST_ptr((cbio = BIO_new_from_core_bio(libctx, &corebio))))
  73. goto err;
  74. if (!TEST_int_gt(BIO_puts(corebio.bio, msg), 0)
  75. /* Test a ctrl via BIO_eof */
  76. || !TEST_false(BIO_eof(cbio))
  77. || !TEST_int_gt(BIO_gets(cbio, buf, sizeof(buf)), 0)
  78. || !TEST_true(BIO_eof(cbio))
  79. || !TEST_str_eq(buf, msg))
  80. goto err;
  81. buf[0] = '\0';
  82. if (!TEST_int_gt(BIO_write(cbio, msg, strlen(msg) + 1), 0)
  83. || !TEST_int_gt(BIO_read(cbio, buf, sizeof(buf)), 0)
  84. || !TEST_str_eq(buf, msg))
  85. goto err;
  86. testresult = 1;
  87. err:
  88. BIO_free(cbiobad);
  89. BIO_free(cbio);
  90. BIO_free(corebio.bio);
  91. OSSL_LIB_CTX_free(libctx);
  92. return testresult;
  93. }
  94. int setup_tests(void)
  95. {
  96. if (!test_skip_common_options()) {
  97. TEST_error("Error parsing test options\n");
  98. return 0;
  99. }
  100. ADD_TEST(test_bio_core);
  101. return 1;
  102. }