Blame test/test-random.c

Packit Service e08953
/* Copyright libuv contributors. All rights reserved.
Packit Service e08953
 *
Packit Service e08953
 * Permission is hereby granted, free of charge, to any person obtaining a copy
Packit Service e08953
 * of this software and associated documentation files (the "Software"), to
Packit Service e08953
 * deal in the Software without restriction, including without limitation the
Packit Service e08953
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
Packit Service e08953
 * sell copies of the Software, and to permit persons to whom the Software is
Packit Service e08953
 * furnished to do so, subject to the following conditions:
Packit Service e08953
 *
Packit Service e08953
 * The above copyright notice and this permission notice shall be included in
Packit Service e08953
 * all copies or substantial portions of the Software.
Packit Service e08953
 *
Packit Service e08953
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Packit Service e08953
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Packit Service e08953
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Packit Service e08953
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Packit Service e08953
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
Packit Service e08953
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
Packit Service e08953
 * IN THE SOFTWARE.
Packit Service e08953
 */
Packit Service e08953
Packit Service e08953
#include "uv.h"
Packit Service e08953
#include "task.h"
Packit Service e08953
Packit Service e08953
#include <string.h>
Packit Service e08953
Packit Service e08953
static char scratch[256];
Packit Service e08953
static int random_cb_called;
Packit Service e08953
Packit Service e08953
Packit Service e08953
static void random_cb(uv_random_t* req, int status, void* buf, size_t buflen) {
Packit Service e08953
  char zero[sizeof(scratch)];
Packit Service e08953
Packit Service e08953
  memset(zero, 0, sizeof(zero));
Packit Service e08953
Packit Service e08953
  ASSERT(0 == status);
Packit Service e08953
  ASSERT(buf == (void*) scratch);
Packit Service e08953
Packit Service e08953
  if (random_cb_called == 0) {
Packit Service e08953
    ASSERT(buflen == 0);
Packit Service e08953
    ASSERT(0 == memcmp(scratch, zero, sizeof(zero)));
Packit Service e08953
  } else {
Packit Service e08953
    ASSERT(buflen == sizeof(scratch));
Packit Service e08953
    /* Buy a lottery ticket if you manage to trip this assertion. */
Packit Service e08953
    ASSERT(0 != memcmp(scratch, zero, sizeof(zero)));
Packit Service e08953
  }
Packit Service e08953
Packit Service e08953
  random_cb_called++;
Packit Service e08953
}
Packit Service e08953
Packit Service e08953
Packit Service e08953
TEST_IMPL(random_async) {
Packit Service e08953
  uv_random_t req;
Packit Service e08953
  uv_loop_t* loop;
Packit Service e08953
Packit Service e08953
  loop = uv_default_loop();
Packit Service e08953
  ASSERT(UV_EINVAL == uv_random(loop, &req, scratch, sizeof(scratch), -1,
Packit Service e08953
                                random_cb));
Packit Service e08953
  ASSERT(UV_E2BIG == uv_random(loop, &req, scratch, -1, -1, random_cb));
Packit Service e08953
Packit Service e08953
  ASSERT(0 == uv_random(loop, &req, scratch, 0, 0, random_cb));
Packit Service e08953
  ASSERT(0 == random_cb_called);
Packit Service e08953
Packit Service e08953
  ASSERT(0 == uv_run(loop, UV_RUN_DEFAULT));
Packit Service e08953
  ASSERT(1 == random_cb_called);
Packit Service e08953
Packit Service e08953
  ASSERT(0 == uv_random(loop, &req, scratch, sizeof(scratch), 0, random_cb));
Packit Service e08953
  ASSERT(1 == random_cb_called);
Packit Service e08953
Packit Service e08953
  ASSERT(0 == uv_run(loop, UV_RUN_DEFAULT));
Packit Service e08953
  ASSERT(2 == random_cb_called);
Packit Service e08953
Packit Service e08953
  MAKE_VALGRIND_HAPPY();
Packit Service e08953
  return 0;
Packit Service e08953
}
Packit Service e08953
Packit Service e08953
Packit Service e08953
TEST_IMPL(random_sync) {
Packit Service e08953
  char zero[256];
Packit Service e08953
  char buf[256];
Packit Service e08953
Packit Service e08953
  ASSERT(UV_EINVAL == uv_random(NULL, NULL, buf, sizeof(buf), -1, NULL));
Packit Service e08953
  ASSERT(UV_E2BIG == uv_random(NULL, NULL, buf, -1, -1, NULL));
Packit Service e08953
Packit Service e08953
  memset(buf, 0, sizeof(buf));
Packit Service e08953
  ASSERT(0 == uv_random(NULL, NULL, buf, sizeof(buf), 0, NULL));
Packit Service e08953
Packit Service e08953
  /* Buy a lottery ticket if you manage to trip this assertion. */
Packit Service e08953
  memset(zero, 0, sizeof(zero));
Packit Service e08953
  ASSERT(0 != memcmp(buf, zero, sizeof(zero)));
Packit Service e08953
Packit Service e08953
  MAKE_VALGRIND_HAPPY();
Packit Service e08953
  return 0;
Packit Service e08953
}