2013-01-20 18:25:22 -05:00
|
|
|
|
|
|
|
#ifndef __CMPTEST_H__
|
|
|
|
#define __CMPTEST_H__
|
|
|
|
|
2017-12-06 07:17:57 -05:00
|
|
|
#ifdef NDEBUG
|
|
|
|
#/**/undef/**/ NDEBUG
|
|
|
|
#endif
|
|
|
|
|
2014-09-13 17:11:12 -04:00
|
|
|
#include <assert.h>
|
2017-12-16 07:08:55 -05:00
|
|
|
#include <errno.h>
|
2017-12-16 07:08:34 -05:00
|
|
|
#include <limits.h>
|
2013-01-20 18:25:22 -05:00
|
|
|
#include <stdio.h>
|
2014-09-13 17:11:12 -04:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2013-01-20 18:25:22 -05:00
|
|
|
|
2013-04-18 08:57:41 -04:00
|
|
|
#include "sodium.h"
|
2015-01-23 04:41:28 -05:00
|
|
|
#include "quirks.h"
|
2013-04-18 08:57:41 -04:00
|
|
|
|
2014-11-22 16:46:09 -05:00
|
|
|
#ifdef __EMSCRIPTEN__
|
|
|
|
# undef TEST_SRCDIR
|
|
|
|
# define TEST_SRCDIR "/test-data"
|
|
|
|
#endif
|
2014-04-14 18:52:55 -04:00
|
|
|
#ifndef TEST_SRCDIR
|
|
|
|
# define TEST_SRCDIR "."
|
|
|
|
#endif
|
|
|
|
|
2013-01-20 18:25:22 -05:00
|
|
|
#define TEST_NAME_RES TEST_NAME ".res"
|
2013-04-27 14:43:26 -04:00
|
|
|
#define TEST_NAME_OUT TEST_SRCDIR "/" TEST_NAME ".exp"
|
2013-01-20 18:25:22 -05:00
|
|
|
|
2013-10-06 18:51:45 -04:00
|
|
|
#ifdef HAVE_ARC4RANDOM
|
|
|
|
# undef rand
|
|
|
|
# define rand(X) arc4random(X)
|
|
|
|
#endif
|
|
|
|
|
2015-12-06 19:44:33 -05:00
|
|
|
int xmain(void);
|
|
|
|
|
2017-08-04 17:08:22 -04:00
|
|
|
#ifdef BENCHMARKS
|
|
|
|
|
|
|
|
# include <sys/time.h>
|
|
|
|
|
2017-08-04 17:16:42 -04:00
|
|
|
# ifndef ITERATIONS
|
|
|
|
# define ITERATIONS 128
|
|
|
|
# endif
|
2017-08-04 17:08:22 -04: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-20 20:02:39 -05:00
|
|
|
#ifndef __EMSCRIPTEN__
|
2017-08-04 17:08:22 -04:00
|
|
|
randombytes_set_implementation(&randombytes_salsa20_implementation);
|
2017-12-20 20:02:39 -05:00
|
|
|
#endif
|
2017-08-04 17:08:22 -04:00
|
|
|
ts_start = now();
|
|
|
|
for (i = 0; i < ITERATIONS; i++) {
|
|
|
|
if (xmain() != 0) {
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ts_end = now();
|
2017-08-04 17:30:30 -04:00
|
|
|
printf("%llu\n", 1000000ULL * (ts_end - ts_start) / ITERATIONS);
|
2017-08-04 17:08:22 -04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-12-16 15:00:44 -05:00
|
|
|
#undef printf
|
2017-08-04 17:08:22 -04:00
|
|
|
#define printf(...) do { } while(0)
|
|
|
|
|
|
|
|
#elif !defined(BROWSER_TESTS)
|
2015-12-06 19:44:33 -05:00
|
|
|
|
2017-12-21 12:46:08 -05:00
|
|
|
static FILE *fp_res;
|
2013-01-20 18:25:22 -05:00
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
|
|
|
FILE *fp_out;
|
|
|
|
int c;
|
|
|
|
|
|
|
|
if ((fp_res = fopen(TEST_NAME_RES, "w+")) == NULL) {
|
|
|
|
perror("fopen(" TEST_NAME_RES ")");
|
|
|
|
return 99;
|
|
|
|
}
|
2013-04-18 13:14:10 -04:00
|
|
|
if (sodium_init() != 0) {
|
|
|
|
return 99;
|
|
|
|
}
|
2013-12-31 12:37:05 -05:00
|
|
|
if (xmain() != 0) {
|
|
|
|
return 99;
|
|
|
|
}
|
2013-01-20 18:25:22 -05:00
|
|
|
rewind(fp_res);
|
|
|
|
if ((fp_out = fopen(TEST_NAME_OUT, "r")) == NULL) {
|
2013-04-21 09:15:25 -04:00
|
|
|
perror("fopen(" TEST_NAME_OUT ")");
|
2013-01-20 18:25:22 -05:00
|
|
|
return 99;
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
if ((c = fgetc(fp_res)) != fgetc(fp_out)) {
|
|
|
|
return 99;
|
|
|
|
}
|
|
|
|
} while (c != EOF);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-04-26 02:25:39 -04:00
|
|
|
#undef printf
|
2013-01-20 18:25:22 -05:00
|
|
|
#define printf(...) fprintf(fp_res, __VA_ARGS__)
|
2015-12-06 19:44:33 -05: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 18:25:22 -05:00
|
|
|
#define main xmain
|
|
|
|
|
|
|
|
#endif
|