New test.
[mono.git] / eglib / test / spawn.c
1 #include <glib.h>
2 #include <string.h>
3 #include <stdio.h>
4 #include <unistd.h>
5 #include "test.h"
6
7 RESULT
8 test_spawn_sync ()
9 {
10         gchar *out;
11         gchar *err;
12         gint status;
13         GError *error = NULL;
14
15         if (!g_spawn_command_line_sync ("ls", &out, &err, &status, &error))
16                 return FAILED ("Error executing 'ls'");
17
18         if (status != 0)
19                 return FAILED ("Status is %d", status);
20
21         if (out == NULL || strlen (out) == 0)
22                 return FAILED ("Didn't get any output from ls!?");
23
24         g_free (out);
25         g_free (err);
26         return OK;
27 }
28
29 RESULT
30 test_spawn_async ()
31 {
32         /*
33 gboolean
34 g_spawn_async_with_pipes (const gchar *working_directory,
35                         gchar **argv,
36                         gchar **envp,
37                         GSpawnFlags flags,
38                         GSpawnChildSetupFunc child_setup,
39                         gpointer user_data,
40                         GPid *child_pid,
41                         gint *standard_input,
42                         gint *standard_output,
43                         gint *standard_error,
44                         GError **error) */
45         char *argv [15];
46         int stdout_fd = -1;
47         char buffer [512];
48         pid_t child_pid = 0;
49
50         memset (argv, 0, 15 * sizeof (char *));
51         argv [0] = "ls";
52         if (!g_spawn_async_with_pipes (NULL, argv, NULL, 0, NULL, NULL, &child_pid, NULL, &stdout_fd, NULL, NULL))
53                 return FAILED ("1 Failed to run ls");
54         if (child_pid == 0)
55                 return FAILED ("2 child pid not returned");
56         if (stdout_fd == -1)
57                 return FAILED ("3 out fd is -1");
58
59         while (read (stdout_fd, buffer, 512) > 0);
60         close (stdout_fd);
61         printf ("Child pid: %d\n", child_pid);
62
63         return OK;
64 }
65
66 static Test spawn_tests [] = {
67         {"g_shell_spawn_sync", test_spawn_sync},
68         {"g_shell_spawn_async_with_pipes", test_spawn_async},
69         {NULL, NULL}
70 };
71
72 DEFINE_TEST_GROUP_INIT(spawn_tests_init, spawn_tests)
73