2013-01-20 16:25:22 -07:00
|
|
|
|
|
|
|
#ifndef __CMPTEST_H__
|
|
|
|
#define __CMPTEST_H__
|
|
|
|
|
2017-12-06 05:17:57 -07:00
|
|
|
#ifdef NDEBUG
|
|
|
|
#/**/undef/**/ NDEBUG
|
|
|
|
#endif
|
|
|
|
|
2014-09-13 14:11:12 -07:00
|
|
|
#include <assert.h>
|
2017-12-16 05:08:55 -07:00
|
|
|
#include <errno.h>
|
2017-12-16 05:08:34 -07:00
|
|
|
#include <limits.h>
|
2013-01-20 16:25:22 -07:00
|
|
|
#include <stdio.h>
|
2014-09-13 14:11:12 -07:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2013-01-20 16:25:22 -07:00
|
|
|
|
2013-04-18 05:57:41 -07:00
|
|
|
#include "sodium.h"
|
2015-01-23 02:41:28 -07:00
|
|
|
#include "quirks.h"
|
2013-04-18 05:57:41 -07:00
|
|
|
|
2014-11-22 14:46:09 -07:00
|
|
|
#ifdef __EMSCRIPTEN__
|
|
|
|
# undef TEST_SRCDIR
|
|
|
|
# define TEST_SRCDIR "/test-data"
|
|
|
|
#endif
|
2014-04-14 15:52:55 -07:00
|
|
|
#ifndef TEST_SRCDIR
|
|
|
|
# define TEST_SRCDIR "."
|
|
|
|
#endif
|
|
|
|
|
2013-01-20 16:25:22 -07:00
|
|
|
#define TEST_NAME_RES TEST_NAME ".res"
|
2013-04-27 11:43:26 -07:00
|
|
|
#define TEST_NAME_OUT TEST_SRCDIR "/" TEST_NAME ".exp"
|
2013-01-20 16:25:22 -07:00
|
|
|
|
2013-10-06 15:51:45 -07:00
|
|
|
#ifdef HAVE_ARC4RANDOM
|
|
|
|
# undef rand
|
|
|
|
# define rand(X) arc4random(X)
|
|
|
|
#endif
|
|
|
|
|
2015-12-06 17:44:33 -07:00
|
|
|
int xmain(void);
|
|
|
|
|
2019-01-03 01:49:33 -07:00
|
|
|
static unsigned char *guard_page;
|
2018-12-24 07:02:59 -07:00
|
|
|
|
2017-08-04 14:08:22 -07:00
|
|
|
#ifdef BENCHMARKS
|
|
|
|
|
|
|
|
# include <sys/time.h>
|
|
|
|
|
2017-08-04 14:16:42 -07:00
|
|
|
# ifndef ITERATIONS
|
|
|
|
# define ITERATIONS 128
|
|
|
|
# endif
|
2017-08-04 14:08:22 -07:00
|
|
|
|
2017-12-16 05:05:49 -07:00
|
|
|
struct {
|
|
|
|
void *pnt;
|
|
|
|
size_t size;
|
|
|
|
} mempool[1024];
|
|
|
|
|
|
|
|
static size_t mempool_idx;
|
|
|
|
|
|
|
|
static __attribute__((malloc)) void *mempool_alloc(size_t size)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
if (size >= (size_t) 0x80000000 - (size_t) 0x00000fff) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
size = (size + (size_t) 0x00000fff) & ~ (size_t) 0x00000fff;
|
|
|
|
for (i = 0U; i < mempool_idx; i++) {
|
|
|
|
if (mempool[i].size >= (size | (size_t) 0x80000000)) {
|
|
|
|
mempool[i].size &= ~ (size_t) 0x80000000;
|
|
|
|
return mempool[i].pnt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (mempool_idx >= sizeof mempool / sizeof mempool[0]) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
mempool[mempool_idx].size = size;
|
2017-12-16 05:12:07 -07:00
|
|
|
return (mempool[mempool_idx++].pnt = (void *) malloc(size));
|
2017-12-16 05:05:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void mempool_free(void *pnt)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
for (i = 0U; i < mempool_idx; i++) {
|
|
|
|
if (mempool[i].pnt == pnt) {
|
|
|
|
if ((mempool[i].size & (size_t) 0x80000000) != (size_t) 0x0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
mempool[i].size |= (size_t) 0x80000000;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
static __attribute__((malloc)) void *mempool_allocarray(size_t count, size_t size)
|
|
|
|
{
|
|
|
|
if (count > (size_t) 0U && size >= (size_t) SIZE_MAX / count) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return mempool_alloc(count * size);
|
|
|
|
}
|
|
|
|
|
2017-12-16 06:51:11 -07:00
|
|
|
static int mempool_free_all(void)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
for (i = 0U; i < mempool_idx; i++) {
|
|
|
|
if ((mempool[i].size & (size_t) 0x80000000) == (size_t) 0x0) {
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
free(mempool[i].pnt);
|
|
|
|
mempool[i].pnt = NULL;
|
|
|
|
}
|
|
|
|
mempool_idx = (size_t) 0U;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-12-16 05:05:49 -07:00
|
|
|
#define sodium_malloc(X) mempool_alloc(X)
|
|
|
|
#define sodium_free(X) mempool_free(X)
|
|
|
|
#define sodium_allocarray(X, Y) mempool_allocarray((X), (Y))
|
|
|
|
|
2017-08-04 14:08:22 -07:00
|
|
|
static unsigned long long now(void)
|
|
|
|
{
|
|
|
|
struct timeval tp;
|
|
|
|
unsigned long long now;
|
|
|
|
|
|
|
|
if (gettimeofday(&tp, NULL) != 0) {
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
now = ((unsigned long long) tp.tv_sec * 1000000ULL) +
|
|
|
|
(unsigned long long) tp.tv_usec;
|
|
|
|
|
|
|
|
return now;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
|
|
|
unsigned long long ts_start;
|
|
|
|
unsigned long long ts_end;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
if (sodium_init() != 0) {
|
|
|
|
return 99;
|
|
|
|
}
|
2017-12-21 10:46:29 -07:00
|
|
|
|
2017-12-20 18:02:39 -07:00
|
|
|
#ifndef __EMSCRIPTEN__
|
2017-08-04 14:08:22 -07:00
|
|
|
randombytes_set_implementation(&randombytes_salsa20_implementation);
|
2017-12-20 18:02:39 -07:00
|
|
|
#endif
|
2017-08-04 14:08:22 -07:00
|
|
|
ts_start = now();
|
|
|
|
for (i = 0; i < ITERATIONS; i++) {
|
|
|
|
if (xmain() != 0) {
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ts_end = now();
|
2017-08-04 14:30:30 -07:00
|
|
|
printf("%llu\n", 1000000ULL * (ts_end - ts_start) / ITERATIONS);
|
2017-12-16 06:51:11 -07:00
|
|
|
if (mempool_free_all() != 0) {
|
|
|
|
fprintf(stderr, "** memory leaks detected **\n");
|
|
|
|
return 99;
|
|
|
|
}
|
2017-08-04 14:08:22 -07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-12-16 13:00:44 -07:00
|
|
|
#undef printf
|
2017-08-04 14:08:22 -07:00
|
|
|
#define printf(...) do { } while(0)
|
|
|
|
|
|
|
|
#elif !defined(BROWSER_TESTS)
|
2015-12-06 17:44:33 -07:00
|
|
|
|
2017-12-21 10:27:29 -07:00
|
|
|
static FILE *fp_res;
|
2013-01-20 16:25:22 -07:00
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
2019-01-03 01:49:33 -07:00
|
|
|
FILE *fp_out;
|
|
|
|
unsigned char *_guard_page;
|
|
|
|
int c;
|
2013-01-20 16:25:22 -07:00
|
|
|
|
|
|
|
if ((fp_res = fopen(TEST_NAME_RES, "w+")) == NULL) {
|
|
|
|
perror("fopen(" TEST_NAME_RES ")");
|
|
|
|
return 99;
|
|
|
|
}
|
2013-04-18 10:14:10 -07:00
|
|
|
if (sodium_init() != 0) {
|
|
|
|
return 99;
|
|
|
|
}
|
2019-01-03 01:49:33 -07:00
|
|
|
if ((_guard_page = (unsigned char *) sodium_malloc(0)) == NULL) {
|
2018-12-24 07:02:59 -07:00
|
|
|
perror("sodium_malloc()");
|
|
|
|
return 99;
|
|
|
|
}
|
2019-01-03 01:49:33 -07:00
|
|
|
guard_page = _guard_page + 1;
|
2013-12-31 10:37:05 -07:00
|
|
|
if (xmain() != 0) {
|
|
|
|
return 99;
|
|
|
|
}
|
2013-01-20 16:25:22 -07:00
|
|
|
rewind(fp_res);
|
|
|
|
if ((fp_out = fopen(TEST_NAME_OUT, "r")) == NULL) {
|
2013-04-21 06:15:25 -07:00
|
|
|
perror("fopen(" TEST_NAME_OUT ")");
|
2013-01-20 16:25:22 -07:00
|
|
|
return 99;
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
if ((c = fgetc(fp_res)) != fgetc(fp_out)) {
|
|
|
|
return 99;
|
|
|
|
}
|
|
|
|
} while (c != EOF);
|
2018-12-24 07:02:59 -07:00
|
|
|
sodium_free(_guard_page);
|
2013-01-20 16:25:22 -07:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-04-25 23:25:39 -07:00
|
|
|
#undef printf
|
2013-01-20 16:25:22 -07:00
|
|
|
#define printf(...) fprintf(fp_res, __VA_ARGS__)
|
2015-12-06 17:44:33 -07:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
|
|
|
if (sodium_init() != 0) {
|
|
|
|
return 99;
|
|
|
|
}
|
|
|
|
if (xmain() != 0) {
|
|
|
|
return 99;
|
|
|
|
}
|
|
|
|
printf("--- SUCCESS ---\n");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2013-01-20 16:25:22 -07:00
|
|
|
#define main xmain
|
|
|
|
|
|
|
|
#endif
|