summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWaLyong Cho <walyong.cho@samsung.com>2016-11-08 01:52:04 (GMT)
committerWaLyong Cho <walyong.cho@samsung.com>2016-11-08 05:50:36 (GMT)
commit6ca33f19ad13621de873331cda113b729a31fe7c (patch)
treec275e3a57a218ea9571bb3cef0fdfb212af18a1f
parent508125f959419cc6d7dceb500e49189bfc224984 (diff)
downloadlibsystem-6ca33f19ad13621de873331cda113b729a31fe7c.zip
libsystem-6ca33f19ad13621de873331cda113b729a31fe7c.tar.gz
libsystem-6ca33f19ad13621de873331cda113b729a31fe7c.tar.bz2
libsystem: test: add test for exec
Change-Id: I9ae8722ab50ea807100b4dd8a4df67518c023b5b Signed-off-by: WaLyong Cho <walyong.cho@samsung.com>
-rw-r--r--src/Makefile.am9
-rw-r--r--src/test/test-exec.c97
2 files changed, 106 insertions, 0 deletions
diff --git a/src/Makefile.am b/src/Makefile.am
index 61fee7c..e7df956 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -84,6 +84,15 @@ test_truncate_nl_LDADD = \
tests += test-truncate_nl
# ------------------------------------------------------------------------------
+test_exec_SOURCES = \
+ test/test-exec.c
+
+test_exec_LDADD = \
+ libsystem.la
+
+tests += test-exec
+
+# ------------------------------------------------------------------------------
pkgconfiglib_DATA += \
libsystem-sd/libsystem-sd.pc
diff --git a/src/test/test-exec.c b/src/test/test-exec.c
new file mode 100644
index 0000000..c5513cc
--- /dev/null
+++ b/src/test/test-exec.c
@@ -0,0 +1,97 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/*
+ * libsystem
+ *
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <assert.h>
+#include <errno.h>
+
+#include "libsystem/libsystem.h"
+
+#define TEST_EXEC_FILE "/tmp/test-exec"
+
+static void test_do_sleep(int argc, char *argv[]) {
+
+ assert(argc == 2);
+
+ sleep(atoi(argv[1]));
+
+ exit(EXIT_SUCCESS);
+}
+
+static void test_do_fork_exec(int argc, char *argv[]) {
+ char *test_argv[3] = { NULL, NULL, NULL };
+
+ test_argv[0] = argv[0];
+ test_argv[1] = "1";
+
+ assert(do_fork_exec(test_argv, NULL, 500) == -ETIME);
+ assert(do_fork_exec(test_argv, NULL, 1500) == 0);
+}
+
+static void test_do_write_file(int argc, char *argv[]) {
+ char *test1, *test2;
+
+ assert(argc == 3);
+
+ test1 = getenv("TEST1");
+ test2 = getenv("TEST2");
+
+ fprintf(stdout, "%s", argv[1]);
+ fprintf(stdout, "%s", argv[2]);
+ fprintf(stdout, "%s", test1);
+ fprintf(stdout, "%s", test2);
+
+ exit(EXIT_SUCCESS);
+}
+
+static void test_do_fork_exec_redirect(int argc, char *argv[]) {
+ char *test_argv[4] = { NULL, "foo", "bar", NULL };
+ char *test_envp[] = { "TEST1=7", "TEST2=hello", NULL };
+ _cleanup_close_ int fd = -1;
+ _cleanup_free_ char *buf = NULL;
+
+ fd = creat(TEST_EXEC_FILE, 0644);
+ /* Skip if file is not able to be opened. */
+ if (fd < 0) {
+ fprintf(stderr, "Failed to open '" TEST_EXEC_FILE "': %m, skipping\n");
+ return;
+ }
+
+ test_argv[0] = argv[0];
+
+ assert(do_fork_exec_redirect(test_argv, test_envp, 0, fd, EXEC_REDIRECT_ALL) == 0);
+ assert(read_one_line_from_path(TEST_EXEC_FILE, &buf) == 0);
+ assert(strneq(buf, "foobar7hello", 12));
+}
+
+int main(int argc, char *argv[]) {
+ if (argc == 2)
+ test_do_sleep(argc, argv);
+ else if (argc == 3)
+ test_do_write_file(argc, argv);
+
+ test_do_fork_exec(argc, argv);
+ test_do_fork_exec_redirect(argc, argv);
+
+ return 0;
+}