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
random_cb(uv_random_t * req,int status,void * buf,size_t buflen)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
48 random_cb_called++;
49 }
50
51
TEST_IMPL(random_async)52 TEST_IMPL(random_async) {
53 uv_random_t req;
54 uv_loop_t* loop;
55
56 loop = uv_default_loop();
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));
62 ASSERT(0 == random_cb_called);
63
64 ASSERT(0 == uv_run(loop, UV_RUN_DEFAULT));
65 ASSERT(1 == random_cb_called);
66
67 ASSERT(0 == uv_random(loop, &req, scratch, sizeof(scratch), 0, random_cb));
68 ASSERT(1 == random_cb_called);
69
70 ASSERT(0 == uv_run(loop, UV_RUN_DEFAULT));
71 ASSERT(2 == random_cb_called);
72
73 MAKE_VALGRIND_HAPPY();
74 return 0;
75 }
76
77
TEST_IMPL(random_sync)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
92 MAKE_VALGRIND_HAPPY();
93 return 0;
94 }
95