bio_core_test.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright 2021 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 const OSSL_DISPATCH biocbs[] = {
  39. { OSSL_FUNC_BIO_READ_EX, (void (*)(void))tst_bio_core_read_ex },
  40. { OSSL_FUNC_BIO_WRITE_EX, (void (*)(void))tst_bio_core_write_ex },
  41. { OSSL_FUNC_BIO_GETS, (void (*)(void))tst_bio_core_gets },
  42. { OSSL_FUNC_BIO_PUTS, (void (*)(void))tst_bio_core_puts },
  43. { OSSL_FUNC_BIO_CTRL, (void (*)(void))tst_bio_core_ctrl },
  44. { 0, NULL }
  45. };
  46. static int test_bio_core(void)
  47. {
  48. BIO *cbio = NULL, *cbiobad = NULL;
  49. OSSL_LIB_CTX *libctx = OSSL_LIB_CTX_new_from_dispatch(NULL, biocbs);
  50. int testresult = 0;
  51. OSSL_CORE_BIO corebio;
  52. const char *msg = "Hello world";
  53. char buf[80];
  54. corebio.bio = BIO_new(BIO_s_mem());
  55. if (!TEST_ptr(corebio.bio)
  56. || !TEST_ptr(libctx)
  57. /*
  58. * Attempting to create a corebio in a libctx that was not
  59. * created via OSSL_LIB_CTX_new_from_dispatch() should fail.
  60. */
  61. || !TEST_ptr_null((cbiobad = BIO_new_from_core_bio(NULL, &corebio)))
  62. || !TEST_ptr((cbio = BIO_new_from_core_bio(libctx, &corebio))))
  63. goto err;
  64. if (!TEST_int_gt(BIO_puts(corebio.bio, msg), 0)
  65. /* Test a ctrl via BIO_eof */
  66. || !TEST_false(BIO_eof(cbio))
  67. || !TEST_int_gt(BIO_gets(cbio, buf, sizeof(buf)), 0)
  68. || !TEST_true(BIO_eof(cbio))
  69. || !TEST_str_eq(buf, msg))
  70. goto err;
  71. buf[0] = '\0';
  72. if (!TEST_int_gt(BIO_write(cbio, msg, strlen(msg) + 1), 0)
  73. || !TEST_int_gt(BIO_read(cbio, buf, sizeof(buf)), 0)
  74. || !TEST_str_eq(buf, msg))
  75. goto err;
  76. testresult = 1;
  77. err:
  78. BIO_free(cbiobad);
  79. BIO_free(cbio);
  80. BIO_free(corebio.bio);
  81. OSSL_LIB_CTX_free(libctx);
  82. return testresult;
  83. }
  84. int setup_tests(void)
  85. {
  86. if (!test_skip_common_options()) {
  87. TEST_error("Error parsing test options\n");
  88. return 0;
  89. }
  90. ADD_TEST(test_bio_core);
  91. return 1;
  92. }