1
mirror of https://github.com/jedisct1/libsodium.git synced 2024-12-20 10:37:24 -07:00
libsodium/test/default/cmptest.h

135 lines
2.2 KiB
C
Raw Normal View History

2013-01-20 16:25:22 -07:00
#ifndef __CMPTEST_H__
#define __CMPTEST_H__
#include <assert.h>
2013-01-20 16:25:22 -07:00
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
2013-01-20 16:25:22 -07:00
#include "sodium.h"
#include "quirks.h"
#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"
#define TEST_NAME_OUT TEST_SRCDIR "/" TEST_NAME ".exp"
2013-01-20 16:25:22 -07:00
#ifdef HAVE_ARC4RANDOM
# undef rand
# define rand(X) arc4random(X)
#endif
int xmain(void);
2017-08-04 14:08:22 -07:00
#ifdef BENCHMARKS
# include <sys/time.h>
# ifndef ITERATIONS
# define ITERATIONS 128
# endif
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;
}
randombytes_set_implementation(&randombytes_salsa20_implementation);
ts_start = now();
for (i = 0; i < ITERATIONS; i++) {
if (xmain() != 0) {
abort();
}
}
ts_end = now();
printf("%llu\n", 1000000ULL * (ts_end - ts_start) / ITERATIONS);
2017-08-04 14:08:22 -07:00
return 0;
}
#define printf(...) do { } while(0)
#elif !defined(BROWSER_TESTS)
2013-01-20 16:25:22 -07:00
FILE *fp_res;
int main(void)
{
FILE *fp_out;
int c;
if ((fp_res = fopen(TEST_NAME_RES, "w+")) == NULL) {
perror("fopen(" TEST_NAME_RES ")");
return 99;
}
if (sodium_init() != 0) {
return 99;
}
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) {
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);
return 0;
}
#undef printf
2013-01-20 16:25:22 -07:00
#define printf(...) fprintf(fp_res, __VA_ARGS__)
#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