summaryrefslogtreecommitdiff
path: root/tests/test-bsdiff.c
blob: 4a9ac6cbcc18217a2c3d518ddcdec8e22f22a4cd (plain)
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
/*
 * Copyright (C) 2015 Red Hat, Inc.
 *
 * SPDX-License-Identifier: LGPL-2.0+
 *
 * 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, see <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#include "libglnx.h"
#include "bsdiff/bsdiff.h"
#include "bsdiff/bspatch.h"
#include <glib.h>
#include <stdlib.h>
#include <gio/gio.h>
#include <string.h>

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;
  g_autofree guint8 *old = g_new (guint8, OLD_SIZE);
  g_autofree guint8 *new = g_new (guint8, NEW_SIZE);
  g_autofree guint8 *new_generated = g_new0 (guint8, NEW_SIZE);
  g_autoptr(GOutputStream) out = g_memory_output_stream_new_resizable ();
  g_autoptr(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_assert (g_output_stream_close (out, NULL, NULL));

  /* Now generate NEW_GENERATED from OLD and OUT.  */
  { g_autoptr(GBytes) bytes = g_memory_output_stream_steal_as_bytes (G_MEMORY_OUTPUT_STREAM (out));
    in = g_memory_input_stream_new_from_bytes (bytes);
  }
  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();
}