summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiuseppe Scrivano <gscrivan@redhat.com>2015-08-11 11:26:14 +0200
committerGiuseppe Scrivano <gscrivan@redhat.com>2015-08-12 07:55:50 +0200
commit3861b79efd876cd6116700e8e94776cd1ef23219 (patch)
treeb1d022d3b47ece864c2f3674f422318df44c1f36
parent3c152fb38bb1fe36ee5bdb544e194b8b11152780 (diff)
downloadostree-3861b79efd876cd6116700e8e94776cd1ef23219.tar.gz
tests: add tests for LZMA compressor and decompressor
Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
-rw-r--r--Makefile-tests.am7
-rw-r--r--tests/test-lzma.c95
2 files changed, 101 insertions, 1 deletions
diff --git a/Makefile-tests.am b/Makefile-tests.am
index 94dd79f5..6f82477b 100644
--- a/Makefile-tests.am
+++ b/Makefile-tests.am
@@ -129,7 +129,7 @@ noinst_PROGRAMS += tests/test-rollsum
TESTS = tests/test-varint tests/test-ot-unix-utils tests/test-bsdiff tests/test-mutable-tree \
tests/test-keyfile-utils tests/test-ot-opt-utils tests/test-ot-tool-util \
- tests/test-gpg-verify-result tests/test-checksum
+ tests/test-gpg-verify-result tests/test-checksum tests/test-lzma
check_PROGRAMS = $(TESTS)
TESTS_ENVIRONMENT = \
@@ -168,6 +168,11 @@ tests_test_ot_opt_utils_LDADD = $(TESTS_LDADD)
tests_test_ot_tool_util_CFLAGS = $(TESTS_CFLAGS)
tests_test_ot_tool_util_LDADD = $(TESTS_LDADD)
+tests_test_lzma_SOURCES = src/libostree/ostree-lzma-common.c src/libostree/ostree-lzma-compressor.c \
+ src/libostree/ostree-lzma-decompressor.c tests/test-lzma.c
+tests_test_lzma_CFLAGS = $(TESTS_CFLAGS)
+tests_test_lzma_LDADD = $(TESTS_LDADD)
+
tests_test_gpg_verify_result_SOURCES = \
src/libostree/ostree-gpg-verify-result-private.h \
tests/test-gpg-verify-result.c
diff --git a/tests/test-lzma.c b/tests/test-lzma.c
new file mode 100644
index 00000000..b2ca87d7
--- /dev/null
+++ b/tests/test-lzma.c
@@ -0,0 +1,95 @@
+/* -*- 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 "libglnx.h"
+#include <glib.h>
+#include <stdlib.h>
+#include <gio/gio.h>
+#include <string.h>
+#include "ostree-lzma-compressor.h"
+#include "ostree-lzma-decompressor.h"
+#include <gio/gunixoutputstream.h>
+#include <gio/gmemoryoutputstream.h>
+
+static void
+helper_test_compress_decompress (const char *data, gssize data_size)
+{
+ GError *error = NULL;
+ g_autoptr(GOutputStream) out_compress = g_memory_output_stream_new_resizable ();
+ g_autoptr(GOutputStream) out_decompress = NULL;
+ g_autoptr(GInputStream) in_compress = g_memory_input_stream_new_from_data (data, data_size, NULL);
+ g_autoptr(GConverter) compressor = (GConverter*)_ostree_lzma_compressor_new (NULL);
+ g_autoptr(GInputStream) in_decompress = NULL;
+ g_autoptr(GConverter) decompressor = NULL;
+
+ {
+ gssize n_bytes_written = g_output_stream_splice (out_compress, in_compress,
+ G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET | G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE,
+ NULL, &error);
+ g_assert_cmpint (n_bytes_written, >, 0);
+ g_assert_no_error (error);
+ }
+
+ in_decompress = g_memory_input_stream_new_from_bytes (g_memory_output_stream_steal_as_bytes (G_MEMORY_OUTPUT_STREAM (out_compress)));
+ out_decompress = g_memory_output_stream_new_resizable ();
+
+ decompressor = (GConverter*)_ostree_lzma_decompressor_new ();
+ {
+ gssize n_bytes_written = g_output_stream_splice (out_decompress, in_decompress,
+ G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET | G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE,
+ NULL, &error);
+ g_assert_cmpint (n_bytes_written, >, 0);
+ g_assert_no_error (error);
+ }
+
+ g_assert_cmpint (g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (out_decompress)), ==, data_size);
+ {
+ gpointer new_data = g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (out_decompress));
+ g_assert_cmpint (memcmp (new_data, data, data_size), ==, 0);
+ }
+
+}
+
+static void
+test_lzma_compress_decompress (void)
+{
+ gssize i;
+#define BUFFER_SIZE (4096 + 1)
+ char buffer[BUFFER_SIZE];
+ srandom (1);
+ for (i = 0; i < BUFFER_SIZE; i++)
+ buffer[i] = random();
+
+ for (i = 2; i <= BUFFER_SIZE; i *= 2)
+ {
+ helper_test_compress_decompress (buffer, i - 1);
+ helper_test_compress_decompress (buffer, i);
+ helper_test_compress_decompress (buffer, i + 1);
+ }
+}
+
+int main (int argc, char **argv)
+{
+ g_test_init (&argc, &argv, NULL);
+ g_test_add_func ("/lzma/same-char-string", test_lzma_compress_decompress);
+
+ return g_test_run();
+}