bio_callback_test.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright 2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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/bio.h>
  12. #include "testutil.h"
  13. #define MAXCOUNT 5
  14. static int my_param_count;
  15. static BIO *my_param_b[MAXCOUNT];
  16. static int my_param_oper[MAXCOUNT];
  17. static const char *my_param_argp[MAXCOUNT];
  18. static int my_param_argi[MAXCOUNT];
  19. static long my_param_argl[MAXCOUNT];
  20. static long my_param_ret[MAXCOUNT];
  21. static long my_bio_callback(BIO *b, int oper, const char *argp, int argi,
  22. long argl, long ret)
  23. {
  24. if (my_param_count >= MAXCOUNT)
  25. return -1;
  26. my_param_b[my_param_count] = b;
  27. my_param_oper[my_param_count] = oper;
  28. my_param_argp[my_param_count] = argp;
  29. my_param_argi[my_param_count] = argi;
  30. my_param_argl[my_param_count] = argl;
  31. my_param_ret[my_param_count] = ret;
  32. my_param_count++;
  33. return ret;
  34. }
  35. static int test_bio_callback(void)
  36. {
  37. int ok = 0;
  38. BIO *bio;
  39. int i;
  40. char *test1 = "test";
  41. char *test2 = "hello";
  42. my_param_count = 0;
  43. bio = BIO_new(BIO_s_mem());
  44. if (bio == NULL)
  45. goto err;
  46. BIO_set_callback(bio, my_bio_callback);
  47. i = BIO_write(bio, test1, 4);
  48. if (!TEST_int_eq(i, 4)
  49. || !TEST_int_eq(my_param_count, 2)
  50. || !TEST_ptr_eq(my_param_b[0], bio)
  51. || !TEST_int_eq(my_param_oper[0], BIO_CB_WRITE)
  52. || !TEST_ptr_eq(my_param_argp[0], test1)
  53. || !TEST_int_eq(my_param_argi[0], 4)
  54. || !TEST_long_eq(my_param_argl[0], 0L)
  55. || !TEST_long_eq(my_param_ret[0], 1L)
  56. || !TEST_ptr_eq(my_param_b[1], bio)
  57. || !TEST_int_eq(my_param_oper[1], BIO_CB_WRITE | BIO_CB_RETURN)
  58. || !TEST_ptr_eq(my_param_argp[1], test1)
  59. || !TEST_int_eq(my_param_argi[1], 4)
  60. || !TEST_long_eq(my_param_argl[1], 0L)
  61. || !TEST_long_eq(my_param_ret[1], 4L))
  62. goto err;
  63. i = BIO_puts(bio, test2);
  64. if (!TEST_int_eq(i, 5)
  65. || !TEST_int_eq(my_param_count, 4)
  66. || !TEST_ptr_eq(my_param_b[2], bio)
  67. || !TEST_int_eq(my_param_oper[2], BIO_CB_PUTS)
  68. || !TEST_ptr_eq(my_param_argp[2], test2)
  69. || !TEST_int_eq(my_param_argi[2], 0)
  70. || !TEST_long_eq(my_param_argl[2], 0L)
  71. || !TEST_long_eq(my_param_ret[2], 1L)
  72. || !TEST_ptr_eq(my_param_b[3], bio)
  73. || !TEST_int_eq(my_param_oper[3], BIO_CB_PUTS | BIO_CB_RETURN)
  74. || !TEST_ptr_eq(my_param_argp[3], test2)
  75. || !TEST_int_eq(my_param_argi[3], 0)
  76. || !TEST_long_eq(my_param_argl[3], 0L)
  77. || !TEST_long_eq(my_param_ret[3], 5L))
  78. goto err;
  79. i = BIO_free(bio);
  80. if (!TEST_int_eq(i, 1)
  81. || !TEST_int_eq(my_param_count, 5)
  82. || !TEST_ptr_eq(my_param_b[4], bio)
  83. || !TEST_int_eq(my_param_oper[4], BIO_CB_FREE)
  84. || !TEST_ptr_eq(my_param_argp[4], NULL)
  85. || !TEST_int_eq(my_param_argi[4], 0)
  86. || !TEST_long_eq(my_param_argl[4], 0L)
  87. || !TEST_long_eq(my_param_ret[4], 1L))
  88. goto finish;
  89. ok = 1;
  90. goto finish;
  91. err:
  92. BIO_free(bio);
  93. finish:
  94. /* This helps finding memory leaks with ASAN */
  95. memset(my_param_b, 0, sizeof(my_param_b));
  96. memset(my_param_argp, 0, sizeof(my_param_argp));
  97. return ok;
  98. }
  99. int setup_tests(void)
  100. {
  101. ADD_TEST(test_bio_callback);
  102. return 1;
  103. }