test-random.c
Go to the documentation of this file.
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 
22 #include "uv.h"
23 #include "task.h"
24 
25 #include <string.h>
26 
27 static char scratch[256];
28 static int random_cb_called;
29 
30 
31 static void random_cb(uv_random_t* req, int status, void* buf, size_t buflen) {
32  char zero[sizeof(scratch)];
33 
34  memset(zero, 0, sizeof(zero));
35 
36  ASSERT(0 == status);
37  ASSERT(buf == (void*) scratch);
38 
39  if (random_cb_called == 0) {
40  ASSERT(buflen == 0);
41  ASSERT(0 == memcmp(scratch, zero, sizeof(zero)));
42  } else {
43  ASSERT(buflen == sizeof(scratch));
44  /* Buy a lottery ticket if you manage to trip this assertion. */
45  ASSERT(0 != memcmp(scratch, zero, sizeof(zero)));
46  }
47 
49 }
50 
51 
52 TEST_IMPL(random_async) {
54  uv_loop_t* loop;
55 
57  ASSERT(UV_EINVAL == uv_random(loop, &req, scratch, sizeof(scratch), -1,
58  random_cb));
59  ASSERT(UV_E2BIG == uv_random(loop, &req, scratch, -1, -1, random_cb));
60 
61  ASSERT(0 == uv_random(loop, &req, scratch, 0, 0, random_cb));
63 
66 
67  ASSERT(0 == uv_random(loop, &req, scratch, sizeof(scratch), 0, random_cb));
69 
72 
74  return 0;
75 }
76 
77 
78 TEST_IMPL(random_sync) {
79  char zero[256];
80  char buf[256];
81 
82  ASSERT(UV_EINVAL == uv_random(NULL, NULL, buf, sizeof(buf), -1, NULL));
83  ASSERT(UV_E2BIG == uv_random(NULL, NULL, buf, -1, -1, NULL));
84 
85  memset(buf, 0, sizeof(buf));
86  ASSERT(0 == uv_random(NULL, NULL, buf, sizeof(buf), 0, NULL));
87 
88  /* Buy a lottery ticket if you manage to trip this assertion. */
89  memset(zero, 0, sizeof(zero));
90  ASSERT(0 != memcmp(buf, zero, sizeof(zero)));
91 
93  return 0;
94 }
async_greeter_server_with_graceful_shutdown.loop
loop
Definition: async_greeter_server_with_graceful_shutdown.py:59
task.h
memset
return memset(p, 0, total)
string.h
buf
voidpf void * buf
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
ASSERT
#define ASSERT(expr)
Definition: task.h:102
status
absl::Status status
Definition: rls.cc:251
uv_run
UV_EXTERN int uv_run(uv_loop_t *, uv_run_mode mode)
Definition: unix/core.c:361
uv_random_s
Definition: uv.h:1631
uv_default_loop
UV_EXTERN uv_loop_t * uv_default_loop(void)
Definition: uv-common.c:733
req
static uv_connect_t req
Definition: test-connection-fail.c:30
UV_RUN_DEFAULT
@ UV_RUN_DEFAULT
Definition: uv.h:254
random_cb
static void random_cb(uv_random_t *req, int status, void *buf, size_t buflen)
Definition: test-random.c:31
TEST_IMPL
TEST_IMPL(random_async)
Definition: test-random.c:52
uv.h
MAKE_VALGRIND_HAPPY
#define MAKE_VALGRIND_HAPPY()
Definition: task.h:229
uv_random
UV_EXTERN int uv_random(uv_loop_t *loop, uv_random_t *req, void *buf, size_t buflen, unsigned flags, uv_random_cb cb)
Definition: libuv/src/random.c:94
scratch
static char scratch[256]
Definition: test-random.c:27
uv_loop_s
Definition: uv.h:1767
random_cb_called
static int random_cb_called
Definition: test-random.c:28


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:30