]> git.ipfire.org Git - thirdparty/libarchive.git/commitdiff
Add a test for archive_cmdline.
authorMichihiro NAKAJIMA <ggcueroad@gmail.com>
Thu, 11 Oct 2012 10:51:52 +0000 (19:51 +0900)
committerMichihiro NAKAJIMA <ggcueroad@gmail.com>
Thu, 11 Oct 2012 10:52:04 +0000 (19:52 +0900)
Makefile.am
libarchive/archive_cmdline_private.h
libarchive/test/CMakeLists.txt
libarchive/test/test_archive_cmdline.c [new file with mode: 0644]

index 74770b845447d0143beb23a0d3925f7859a2eeb1..1f8d6efff82f32150c240ce1cd2ca5897fe9b50e 100644 (file)
@@ -279,6 +279,7 @@ libarchive_test_SOURCES=                                    \
        libarchive/test/test_acl_posix1e.c                      \
        libarchive/test/test_archive_api_feature.c              \
        libarchive/test/test_archive_clear_error.c              \
+       libarchive/test/test_archive_cmdline.c                  \
        libarchive/test/test_archive_crypto.c                   \
        libarchive/test/test_archive_getdate.c                  \
        libarchive/test/test_archive_match_owner.c              \
index 85fba1e11e5cb20c3f345e545dc74df64521a53b..4e409e814817a189958552411429f1ba459a09f2 100644 (file)
  */
 
 #ifndef __LIBARCHIVE_BUILD
+#ifndef __LIBARCHIVE_TEST
 #error This header is only to be used internally to libarchive.
 #endif
+#endif
 
 #ifndef ARCHIVE_CMDLINE_PRIVATE_H
 #define ARCHIVE_CMDLINE_PRIVATE_H
index e6471c714a6aed371472a68426592fce8fce81b4..a9fd1ba24b80d0455cc0679a3a2ded49b544f681 100644 (file)
@@ -16,6 +16,7 @@ IF(ENABLE_TEST)
     test_acl_posix1e.c
     test_archive_api_feature.c
     test_archive_clear_error.c
+    test_archive_cmdline.c
     test_archive_crypto.c
     test_archive_getdate.c
     test_archive_match_owner.c
diff --git a/libarchive/test/test_archive_cmdline.c b/libarchive/test/test_archive_cmdline.c
new file mode 100644 (file)
index 0000000..963454d
--- /dev/null
@@ -0,0 +1,123 @@
+/*-
+ * Copyright (c) 2012 Michihiro NAKAJIMA
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "test.h"
+__FBSDID("$FreeBSD$");
+
+#define __LIBARCHIVE_TEST
+#include "archive_cmdline_private.h"
+
+DEFINE_TEST(test_archive_cmdline)
+{
+       struct archive_cmdline *cl;
+
+       /* Command name only. */
+       assert((cl = __archive_cmdline_allocate()) != NULL);
+       assertEqualInt(ARCHIVE_OK, __archive_cmdline_parse(cl, "gzip"));
+       assertEqualInt(1, cl->argc);
+       assertEqualString("gzip", cl->path);
+       assertEqualString("gzip", cl->argv[0]);
+       assertEqualInt(ARCHIVE_OK, __archive_cmdline_free(cl));
+
+       assert((cl = __archive_cmdline_allocate()) != NULL);
+       assertEqualInt(ARCHIVE_OK, __archive_cmdline_parse(cl, "gzip "));
+       assertEqualInt(1, cl->argc);
+       failure("path should not include a space character");
+       assertEqualString("gzip", cl->path);
+       failure("arg0 should not include a space character");
+       assertEqualString("gzip", cl->argv[0]);
+       assertEqualInt(ARCHIVE_OK, __archive_cmdline_free(cl));
+
+       assert((cl = __archive_cmdline_allocate()) != NULL);
+       assertEqualInt(ARCHIVE_OK, __archive_cmdline_parse(cl,
+           "/usr/bin/gzip "));
+       assertEqualInt(1, cl->argc);
+       failure("path should be a full path");
+       assertEqualString("/usr/bin/gzip", cl->path);
+       failure("arg0 should not be a full path");
+       assertEqualString("gzip", cl->argv[0]);
+       assertEqualInt(ARCHIVE_OK, __archive_cmdline_free(cl));
+
+       /* A command line includes space characer. */
+       assert((cl = __archive_cmdline_allocate()) != NULL);
+       assertEqualInt(ARCHIVE_OK, __archive_cmdline_parse(cl, "\"gzip \""));
+       assertEqualInt(1, cl->argc);
+       failure("path should include a space character");
+       assertEqualString("gzip ", cl->path);
+       failure("arg0 should include a space character");
+       assertEqualString("gzip ", cl->argv[0]);
+       assertEqualInt(ARCHIVE_OK, __archive_cmdline_free(cl));
+
+       /* A command line includes space characer: pattern 2.*/
+       assert((cl = __archive_cmdline_allocate()) != NULL);
+       assertEqualInt(ARCHIVE_OK, __archive_cmdline_parse(cl, "\"gzip \"x"));
+       assertEqualInt(1, cl->argc);
+       failure("path should include a space character");
+       assertEqualString("gzip x", cl->path);
+       failure("arg0 should include a space character");
+       assertEqualString("gzip x", cl->argv[0]);
+       assertEqualInt(ARCHIVE_OK, __archive_cmdline_free(cl));
+
+       /* A command line includes space characer: pattern 3.*/
+       assert((cl = __archive_cmdline_allocate()) != NULL);
+       assertEqualInt(ARCHIVE_OK, __archive_cmdline_parse(cl,
+           "\"gzip \"x\" s \""));
+       assertEqualInt(1, cl->argc);
+       failure("path should include a space character");
+       assertEqualString("gzip x s ", cl->path);
+       failure("arg0 should include a space character");
+       assertEqualString("gzip x s ", cl->argv[0]);
+       assertEqualInt(ARCHIVE_OK, __archive_cmdline_free(cl));
+
+       /* A command line includes space characer: pattern 4.*/
+       assert((cl = __archive_cmdline_allocate()) != NULL);
+       assertEqualInt(ARCHIVE_OK, __archive_cmdline_parse(cl,
+           "\"gzip\\\" \""));
+       assertEqualInt(1, cl->argc);
+       failure("path should include a space character");
+       assertEqualString("gzip\" ", cl->path);
+       failure("arg0 should include a space character");
+       assertEqualString("gzip\" ", cl->argv[0]);
+       assertEqualInt(ARCHIVE_OK, __archive_cmdline_free(cl));
+
+       /* A command name with a argument. */
+       assert((cl = __archive_cmdline_allocate()) != NULL);
+       assertEqualInt(ARCHIVE_OK, __archive_cmdline_parse(cl, "gzip -d"));
+       assertEqualInt(2, cl->argc);
+       assertEqualString("gzip", cl->path);
+       assertEqualString("gzip", cl->argv[0]);
+       assertEqualString("-d", cl->argv[1]);
+       assertEqualInt(ARCHIVE_OK, __archive_cmdline_free(cl));
+
+       /* A command name with two arguments. */
+       assert((cl = __archive_cmdline_allocate()) != NULL);
+       assertEqualInt(ARCHIVE_OK, __archive_cmdline_parse(cl, "gzip -d -q"));
+       assertEqualInt(3, cl->argc);
+       assertEqualString("gzip", cl->path);
+       assertEqualString("gzip", cl->argv[0]);
+       assertEqualString("-d", cl->argv[1]);
+       assertEqualString("-q", cl->argv[2]);
+       assertEqualInt(ARCHIVE_OK, __archive_cmdline_free(cl));
+}