/**
 * testutils: Implementations of random useful functions for writing
 * test cases.
 */

#include "testutils.h"
#include <sha1.h>

#include <stdlib.h>

void
fail(const str & err)
{
  fatal << "FAILURE: " << err;
}

void
failCBE(pStat s)
{
  warn << "Error callback: pStat=" << s << "\n";
  fail("Unexpected error callback.");
}

blockFingerprint
randomFingerprint()
{
  blockFingerprint f;
  for (int i = 0; i < sha1::hashsize; i++) {
    f.fp[i] = (u_int8_t) random();
  }
  return f;
}

blockAddress
randomAddress()
{
  return (blockAddress) random();
}

str
readFile(const char *fname)
{
  FILE *fp = fopen(fname, "rb");
  char cbuf[1024];
  strbuf buf;

  while (!feof(fp)) {
    int len = fread(cbuf, 1, sizeof(cbuf), fp);
    buf << str(cbuf, len);
  }

  fclose(fp);
  return str(buf);
}
