#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#undef NDEBUG
#include <assert.h>
#include "NETL2.h"
#include "../common.h"
#include "../zstdseek.h"
#include "../types.h"
#include "../scaffold.h"
#include "../hash.h"
#include "../vertex.h"
#include "../atom.h"
#include "../hunk.h"
#include "../mbuf.h"
#include "../change.h"
static void
test_changefileoffsets()
{
struct mbuf ch = {
.len = NETL2LEN,
.buf = NETL2,
};
struct offsets off = { 0 };
int err;
err = changefileoffsets(&ch, &off);
assert(err == CHANGEFILE_OK);
assert(off.version == VERSION);
assert(off.hashed_len == 985);
assert(off.unhashed_off == 803);
assert(off.unhashed_len == 104);
assert(off.contents_off == 945);
assert(off.contents_len == 120);
}
static void
test_changefileoffsetsbad()
{
struct mbuf ch = { .len = 55, .buf = NULL };
int err;
struct offsets off = { 0 };
err = changefileoffsets(&ch, &off);
assert(err == CHANGEFILE_TOOSHORT);
}
static void
test_changefileoffsetsversion()
{
struct mbuf ch = {
.len = NETL2LEN,
.buf = NETL2,
};
struct offsets off = { 0 };
int err;
ch.buf[0] = 5;
err = changefileoffsets(&ch, &off);
assert(err == CHANGEFILE_UNSUPPORTEDVERSION);
}
static void
test_decodehashed()
{
struct hashed hashed = { 0 };
struct changeheader *header;
struct author *author;
usize datalen = 747;
usize hashedlen = 985;
u8 buf[hashedlen];
enum error ret;
usize err;
u8 expectedhash[32] = { 0x69, 0x26, 0xbd, 0x37, 0xbb, 0x93, 0xa2, 0x5a,
0xae, 0x27, 0x9e, 0x8c, 0xaf, 0x13, 0x08, 0x32,
0xbd, 0xca, 0x78, 0x9c, 0x84, 0xc4, 0x49, 0x79,
0xa6, 0x83, 0xc3, 0x8a, 0x3d, 0x0e, 0xa9, 0x10 };
err = zstdseek_decompress(buf, hashedlen, NETL2 + 56, datalen);
assert(err > 0);
ret = change_decodehashed(buf, hashedlen, expectedhash, &hashed);
assert(ret == CHANGEFILE_OK);
assert(hashed.version == VERSION);
header = &hashed.header;
assert(strcmp(header->message, "Makefile: add check target") == 0);
assert(header->description == NULL);
assert(strcmp(header->timestamp, "2022-10-31T12:34:27.238139355Z") == 0
);
assert(header->authors.len == 1);
author = &header->authors.map[0];
assert(author->len == 1);
assert(strcmp(author->entries[0].key, "key") == 0);
assert(strcmp(author->entries[0].value,
"AFgzZkD8ARgC21gkkCvxPV5HbL7YDVzgYEhbAxQG7UQY") == 0);
assert(hashed.dependencies.len == 1);
assert(hashed.extraknown.len == 4);
assert(hashed.hunks.len == 2);
hashedfree(&hashed);
}
int
main()
{
test_changefileoffsets();
test_changefileoffsetsbad();
test_changefileoffsetsversion();
test_decodehashed();
}