Select Git revision
yarrow-test.c 4.61 KiB
#include "testutils.h"
#include "yarrow.h"
#include "knuth-lfib.h"
#include "macros.h"
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Lagged fibonacci sequence as described in Knuth 3.6 */
struct knuth_lfib_ctx lfib;
static int
get_event(FILE *f, struct sha256_ctx *hash,
unsigned *key, unsigned *time)
{
static int t = 0;
uint8_t buf[1];
int c = getc(f);
if (c == EOF)
return 0;
buf[0] = c;
sha256_update(hash, sizeof(buf), buf);
*key = c;
t += (knuth_lfib_get(&lfib) % 10000);
*time = t;
return 1;
}
static FILE *
open_file(const char *name)
{
/* Tries opening the file in $srcdir, if set, otherwise the current
* working directory */
const char *srcdir = getenv("srcdir");
if (srcdir && srcdir[0])
{
/* Leaks this name, but that doesn't matter. */
char *buf = xalloc(strlen(name) + strlen(srcdir) + 10);
sprintf(buf, "%s/%s", srcdir, name);
name = buf;
}
/* Opens the file in text mode. */
return fopen(name, "r");
}
int
test_main(void)
{
FILE *input;
struct yarrow256_ctx yarrow;
struct yarrow_key_event_ctx estimator;
struct yarrow_source sources[2];
struct sha256_ctx output_hash;
struct sha256_ctx input_hash;
uint8_t digest[SHA256_DIGEST_SIZE];