Hash :
885d3e02
Author :
Date :
2018-01-27T01:05:56
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
/*
* Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/queue.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <zlib.h>
#include "got_error.h"
#include "delta.h"
#include "path.h"
#ifndef nitems
#define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
#endif
struct delta_test {
const char *base;
const char *delta;
size_t delta_len;
const char *expected;
} delta_tests[] = {
/* base len 0, target len 4, append 4 'x' */
{ "", "\x00\x04\x04xxxx", 7, "xxxx" },
/* copy 4 bytes at offset 0 from base, append 4 'x' */
{ "aabbccdd", "\x08\x08\x90\x04\x04xxxx", 9, "aabbxxxx" },
/* copy 4 bytes at offset 4 from base, append 4 'x' */
{ "aabbccdd", "\x08\x08\x91\x04\x04\x04xxxx", 10, "ccddxxxx" },
};
static const struct got_error *
compress_to_file(FILE **outfile, const char *input, size_t inlen)
{
const struct got_error *err = NULL;
z_stream z;
char buf[2048];
char *inbuf;
int ret;
size_t n;
memset(&z, 0, sizeof(z));
z.zalloc = Z_NULL;
z.zfree = Z_NULL;
if (deflateInit(&z, 8) != Z_OK)
return got_error(GOT_ERR_IO);
*outfile = got_opentemp();
if (*outfile == NULL)
return got_error_from_errno();
z.next_in = (Bytef *)input;
z.avail_in = inlen;
z.next_out = buf;
z.avail_out = sizeof(buf);
/* Our output buffer is large enough so one round should be enough. */
ret = deflate(&z, Z_FINISH);
if (ret != Z_STREAM_END || z.avail_out == 0) {
err = got_error(GOT_ERR_COMPRESSION);
goto done;
}
deflateEnd(&z);
n = fwrite(buf, 1, z.avail_out, *outfile);
if (n != z.avail_out)
err = got_ferror(*outfile, GOT_ERR_IO);
done:
if (err) {
fclose(*outfile);
*outfile = NULL;
} else
rewind(*outfile);
return err;
}
static int
delta_combine()
{
const struct got_error *err = NULL;
int i;
FILE *result_file;
result_file = got_opentemp();
if (result_file == NULL)
return 1;
for (i = 0; i < nitems(delta_tests); i++) {
struct delta_test *dt = &delta_tests[i];
FILE *base_file;
char buf[1024];
size_t n, len, result_len;
len = strlen(dt->base);
err = compress_to_file(&base_file, dt->base, len);
if (err)
break;
err = got_delta_apply(base_file, dt->delta, dt->delta_len,
result_file);
fclose(base_file);
if (err)
break;
result_len = strlen(dt->expected);
n = fread(buf, result_len, 1, result_file);
if (n != 1 || strncmp(buf, dt->expected, result_len) != 0) {
err = got_ferror(result_file, GOT_ERR_BAD_DELTA);
break;
}
rewind(result_file);
}
fclose(result_file);
return (err == NULL);
}
#define RUN_TEST(expr, name) \
{ test_ok = (expr); \
printf("test %s %s\n", (name), test_ok ? "ok" : "failed"); \
failure = (failure || !test_ok); }
int
main(int argc, const char *argv[])
{
int test_ok;
int failure = 0;
if (argc != 1) {
fprintf(stderr, "usage: delta_test [REPO_PATH]\n");
return 1;
}
RUN_TEST(delta_combine(), "delta_combine");
return failure ? 1 : 0;
}