test-random.c 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /* Copyright libuv contributors. All rights reserved.
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to
  5. * deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  7. * sell copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  19. * IN THE SOFTWARE.
  20. */
  21. #include "uv.h"
  22. #include "task.h"
  23. #include <string.h>
  24. static char scratch[256];
  25. static int random_cb_called;
  26. static void random_cb(uv_random_t* req, int status, void* buf, size_t buflen) {
  27. char zero[sizeof(scratch)];
  28. memset(zero, 0, sizeof(zero));
  29. ASSERT(0 == status);
  30. ASSERT(buf == (void*) scratch);
  31. if (random_cb_called == 0) {
  32. ASSERT(buflen == 0);
  33. ASSERT(0 == memcmp(scratch, zero, sizeof(zero)));
  34. } else {
  35. ASSERT(buflen == sizeof(scratch));
  36. /* Buy a lottery ticket if you manage to trip this assertion. */
  37. ASSERT(0 != memcmp(scratch, zero, sizeof(zero)));
  38. }
  39. random_cb_called++;
  40. }
  41. TEST_IMPL(random_async) {
  42. uv_random_t req;
  43. uv_loop_t* loop;
  44. loop = uv_default_loop();
  45. ASSERT(UV_EINVAL == uv_random(loop, &req, scratch, sizeof(scratch), -1,
  46. random_cb));
  47. ASSERT(UV_E2BIG == uv_random(loop, &req, scratch, -1, -1, random_cb));
  48. ASSERT(0 == uv_random(loop, &req, scratch, 0, 0, random_cb));
  49. ASSERT(0 == random_cb_called);
  50. ASSERT(0 == uv_run(loop, UV_RUN_DEFAULT));
  51. ASSERT(1 == random_cb_called);
  52. ASSERT(0 == uv_random(loop, &req, scratch, sizeof(scratch), 0, random_cb));
  53. ASSERT(1 == random_cb_called);
  54. ASSERT(0 == uv_run(loop, UV_RUN_DEFAULT));
  55. ASSERT(2 == random_cb_called);
  56. MAKE_VALGRIND_HAPPY();
  57. return 0;
  58. }
  59. TEST_IMPL(random_sync) {
  60. char zero[256];
  61. char buf[256];
  62. ASSERT(UV_EINVAL == uv_random(NULL, NULL, buf, sizeof(buf), -1, NULL));
  63. ASSERT(UV_E2BIG == uv_random(NULL, NULL, buf, -1, -1, NULL));
  64. memset(buf, 0, sizeof(buf));
  65. ASSERT(0 == uv_random(NULL, NULL, buf, sizeof(buf), 0, NULL));
  66. /* Buy a lottery ticket if you manage to trip this assertion. */
  67. memset(zero, 0, sizeof(zero));
  68. ASSERT(0 != memcmp(buf, zero, sizeof(zero)));
  69. MAKE_VALGRIND_HAPPY();
  70. return 0;
  71. }