diff --git a/Makefile-tests.am b/Makefile-tests.am index 3f6f2dea..49cca8ce 100644 --- a/Makefile-tests.am +++ b/Makefile-tests.am @@ -104,7 +104,7 @@ endif endif # "make check" do not depend from --enable-installed-tests -check_PROGRAMS = tests/test-rollsum tests/test-varint tests/test-ot-unix-utils +check_PROGRAMS = tests/test-rollsum tests/test-varint tests/test-ot-unix-utils tests/test-bsdiff tests_test_ot_unix_utils_CFLAGS = $(ostree_bin_shared_cflags) $(OT_INTERNAL_GIO_UNIX_CFLAGS) tests_test_ot_unix_utils_LDADD = $(ostree_bin_shared_ldadd) $(OT_INTERNAL_GIO_UNIX_LIBS) @@ -117,4 +117,8 @@ tests_test_varint_SOURCES = src/libostree/ostree-varint.c tests/test-varint.c tests_test_varint_CFLAGS = $(ostree_bin_shared_cflags) $(OT_INTERNAL_GIO_UNIX_CFLAGS) tests_test_varint_LDADD = $(ostree_bin_shared_ldadd) $(OT_INTERNAL_GIO_UNIX_LIBS) -TESTS = tests/test-varint tests/test-ot-unix-utils +tests_test_bsdiff_SOURCES = tests/test-bsdiff.c bsdiff/bsdiff.c bsdiff/bspatch.c +tests_test_bsdiff_CFLAGS = $(ostree_bin_shared_cflags) $(OT_INTERNAL_GIO_UNIX_CFLAGS) +tests_test_bsdiff_LDADD = $(ostree_bin_shared_ldadd) $(OT_INTERNAL_GIO_UNIX_LIBS) + +TESTS = tests/test-varint tests/test-ot-unix-utils tests/test-bsdiff diff --git a/tests/test-bsdiff.c b/tests/test-bsdiff.c new file mode 100644 index 00000000..17155321 --- /dev/null +++ b/tests/test-bsdiff.c @@ -0,0 +1,106 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- + * + * Copyright (C) 2015 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#include "config.h" + +#include "libgsystem.h" +#include "bsdiff/bsdiff.h" +#include "bsdiff/bspatch.h" +#include +#include +#include +#include + +static int +bzpatch_read (const struct bspatch_stream* stream, void* buffer, int length) +{ + GInputStream *in = stream->opaque; + if (length && ! g_input_stream_read (in, + buffer, + length, + NULL, + NULL)) + return -1; + + return 0; +} + +static int +bzdiff_write (struct bsdiff_stream* stream, const void* buffer, int size) +{ + GOutputStream *out = stream->opaque; + if (! g_output_stream_write (out, + buffer, + size, + NULL, + NULL)) + return -1; + + return 0; +} + +static void +test_bsdiff (void) +{ +#define OLD_SIZE 512 +#define NEW_SIZE (512+24) + + struct bsdiff_stream bsdiff_stream; + struct bspatch_stream bspatch_stream; + int i; + gs_free guint8 *old = g_new (guint8, OLD_SIZE); + gs_free guint8 *new = g_new (guint8, NEW_SIZE); + gs_free guint8 *new_generated = g_new0 (guint8, NEW_SIZE); + gs_unref_object GOutputStream *out = g_memory_output_stream_new_resizable (); + gs_unref_object GInputStream *in = NULL; + + new[0] = 'A'; + for (i = 0; i < OLD_SIZE; i++) + { + old[i] = i; + new[i + 1] = old[i]; + } + for (i = OLD_SIZE + 1; i < NEW_SIZE; i++) + new[i] = i; + + bsdiff_stream.malloc = malloc; + bsdiff_stream.free = free; + bsdiff_stream.write = bzdiff_write; + bsdiff_stream.opaque = out; + g_assert_cmpint (bsdiff (old, OLD_SIZE, new, NEW_SIZE, &bsdiff_stream), ==, 0); + + g_output_stream_close (out, NULL, NULL); + + /* Now generate NEW_GENERATED from OLD and OUT. */ + in = g_memory_input_stream_new_from_bytes (g_memory_output_stream_steal_as_bytes (G_MEMORY_OUTPUT_STREAM (out))); + bspatch_stream.read = bzpatch_read; + bspatch_stream.opaque = in; + + g_assert_cmpint (bspatch (old, OLD_SIZE, new_generated, NEW_SIZE, &bspatch_stream), ==, 0); + + g_assert_cmpint (memcmp (new, new_generated, NEW_SIZE), ==, 0); +} + +int main (int argc, char **argv) +{ + g_test_init (&argc, &argv, NULL); + g_test_add_func ("/bsdiff", test_bsdiff); + return g_test_run(); +}