libsodium: updated to 1.0.10
[tomato.git] / release / src / router / libsodium / test / default / sodium_utils2.c
blobd6dc800509f9a513a43d291426621a8bce467122
2 #include <stdlib.h>
3 #include <sys/types.h>
5 #include <limits.h>
6 #include <signal.h>
8 #define TEST_NAME "sodium_utils2"
9 #include "cmptest.h"
11 #ifdef __SANITIZE_ADDRESS__
12 # warning The sodium_utils2 test is expected to fail with address sanitizer
13 #endif
15 __attribute__ ((noreturn)) static void segv_handler(int sig)
17 (void) sig;
19 printf("Intentional segfault / bus error caught\n");
20 printf("OK\n");
21 #ifdef SIGSEGV
22 signal(SIGSEGV, SIG_DFL);
23 #endif
24 #ifdef SIGBUS
25 signal(SIGBUS, SIG_DFL);
26 #endif
27 #ifdef SIGABRT
28 signal(SIGABRT, SIG_DFL);
29 #endif
30 exit(0);
33 int main(void)
35 void *buf;
36 size_t size;
37 unsigned int i;
39 if (sodium_malloc(SIZE_MAX - 1U) != NULL) {
40 return 1;
42 if (sodium_malloc(0U) == NULL) {
43 return 1;
45 if (sodium_allocarray(SIZE_MAX / 2U + 1U, SIZE_MAX / 2U) != NULL) {
46 return 1;
48 sodium_free(sodium_allocarray(0U, 0U));
49 sodium_free(sodium_allocarray(0U, 1U));
50 sodium_free(sodium_allocarray(1U, 0U));
52 buf = sodium_allocarray(1000U, 50U);
53 memset(buf, 0, 50000U);
54 sodium_free(buf);
56 sodium_free(sodium_malloc(0U));
57 sodium_free(NULL);
58 for (i = 0U; i < 10000U; i++) {
59 size = 1U + randombytes_uniform(100000U);
60 buf = sodium_malloc(size);
61 assert(buf != NULL);
62 memset(buf, i, size);
63 sodium_mprotect_noaccess(buf);
64 sodium_free(buf);
66 printf("OK\n");
68 #ifdef SIGSEGV
69 signal(SIGSEGV, segv_handler);
70 #endif
71 #ifdef SIGBUS
72 signal(SIGBUS, segv_handler);
73 #endif
74 #ifdef SIGABRT
75 signal(SIGABRT, segv_handler);
76 #endif
77 size = 1U + randombytes_uniform(100000U);
78 buf = sodium_malloc(size);
79 assert(buf != NULL);
80 sodium_mprotect_readonly(buf);
81 sodium_mprotect_readwrite(buf);
82 #ifndef __EMSCRIPTEN__
83 sodium_memzero(((unsigned char *)buf) + size, 1U);
84 sodium_mprotect_noaccess(buf);
85 sodium_free(buf);
86 printf("Overflow not caught\n");
87 #endif
88 return 0;