xref: /haiku/src/tests/add-ons/kernel/drivers/random/random_test.cpp (revision 02354704729d38c3b078c696adc1bbbd33cbcf72)
1 #include <errno.h>
2 #include <fcntl.h>
3 #include <stdint.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <unistd.h>
8 
9 
10 static const char* kRandomDevice = "/dev/urandom";
11 
12 
13 int
14 main()
15 {
16 	int fd = open(kRandomDevice, O_RDONLY);
17 	if (fd < 0) {
18 		fprintf(stderr, "Error: Failed to open \"%s\": %s", kRandomDevice,
19 			strerror(errno));
20 		exit(1);
21 	}
22 
23 	uint8_t buffer[16];
24 	ssize_t bytesRead = read(fd, buffer, sizeof(buffer));
25 	if (bytesRead < 0) {
26 		fprintf(stderr, "Error: Failed to read from random device: %s",
27 			strerror(errno));
28 		exit(1);
29 	}
30 
31 	printf("Read %d bytes from random device: ", (int)bytesRead);
32 	for (int i = 0; i < bytesRead; i++)
33 		printf("%02x", buffer[i]);
34 	printf("\n");
35 
36 	return 0;
37 }
38 
39