| 1 | // RUN: %clangxx -O0 -g %s -o %t && %run %t 2>&1 | FileCheck %s |
| 2 | // |
| 3 | // UNSUPPORTED: target={{.*(linux|solaris).*}} |
| 4 | |
| 5 | #include <cstdlib> |
| 6 | #include <ctime> |
| 7 | #include <cstdio> |
| 8 | #include <inttypes.h> |
| 9 | |
| 10 | void print_buf(unsigned char *buf, size_t buflen) { |
| 11 | printf(format: "buf '" ); |
| 12 | for (auto i = 0; i < buflen; i ++) |
| 13 | printf(format: "%" PRIx8, buf[i]); |
| 14 | printf(format: "'\n" ); |
| 15 | } |
| 16 | |
| 17 | void test_seed() { |
| 18 | #ifdef __NetBSD__ |
| 19 | time_t now = ::time(nullptr); |
| 20 | arc4random_addrandom((unsigned char *)&now, sizeof(now)); |
| 21 | #endif |
| 22 | } |
| 23 | |
| 24 | void test_arc4random() { |
| 25 | printf(format: "test_arc4random\n" ); |
| 26 | auto i = arc4random(); |
| 27 | print_buf(buf: (unsigned char *)&i, buflen: sizeof(i)); |
| 28 | } |
| 29 | |
| 30 | void test_arc4random_uniform() { |
| 31 | printf(format: "test_arc4random_uniform\n" ); |
| 32 | auto i = arc4random_uniform(1024); |
| 33 | print_buf(buf: (unsigned char *)&i, buflen: sizeof(i)); |
| 34 | } |
| 35 | |
| 36 | void test_arc4random_buf10() { |
| 37 | printf(format: "test_arc4random_buf10\n" ); |
| 38 | char buf[10]; |
| 39 | #ifdef __NetBSD__ |
| 40 | arc4random_stir(); |
| 41 | #endif |
| 42 | arc4random_buf(buf, sizeof(buf)); |
| 43 | print_buf(buf: (unsigned char *)buf, buflen: sizeof(buf)); |
| 44 | } |
| 45 | |
| 46 | void test_arc4random_buf256() { |
| 47 | printf(format: "test_arc4random_buf256\n" ); |
| 48 | char buf[256]; |
| 49 | #ifdef __NetBSD__ |
| 50 | arc4random_stir(); |
| 51 | #endif |
| 52 | arc4random_buf(buf, sizeof(buf)); |
| 53 | print_buf(buf: (unsigned char *)buf, buflen: sizeof(buf)); |
| 54 | } |
| 55 | |
| 56 | int main(void) { |
| 57 | test_seed(); |
| 58 | test_arc4random(); |
| 59 | test_arc4random_uniform(); |
| 60 | test_arc4random_buf10(); |
| 61 | test_arc4random_buf256(); |
| 62 | return 0; |
| 63 | // CHECK: test_arc4random |
| 64 | // CHECK: buf '{{.*}}' |
| 65 | // CHECK: test_arc4random_uniform |
| 66 | // CHECK: buf '{{.*}}' |
| 67 | // CHECK: test_arc4random_buf10 |
| 68 | // CHECK: buf '{{.*}}' |
| 69 | // CHECK: test_arc4random_buf256 |
| 70 | // CHECK: buf '{{.*}}' |
| 71 | } |
| 72 | |