Permalink
Browse files

Add a test for archive_cmdline.

  • Loading branch information...
1 parent 25169e1 commit ccf9948469c000148bd05c3046899e579193dcbe @ggcueroad ggcueroad committed Oct 11, 2012
Showing with 127 additions and 0 deletions.
  1. +1 −0 Makefile.am
  2. +2 −0 libarchive/archive_cmdline_private.h
  3. +1 −0 libarchive/test/CMakeLists.txt
  4. +123 −0 libarchive/test/test_archive_cmdline.c
View
@@ -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 \
@@ -26,8 +26,10 @@
*/
#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
@@ -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
@@ -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));
+}

0 comments on commit ccf9948

Please sign in to comment.