Imported Upstream version 1.5.7
[platform/upstream/libpipeline.git] / tests / pump.c
1 /*
2  * Copyright (C) 2012 Colin Watson.
3  *
4  * This file is part of libpipeline.
5  *
6  * libpipeline is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or (at
9  * your option) any later version.
10  *
11  * libpipeline is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with libpipeline; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
19  * USA.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #  include "config.h"
24 #endif
25
26 #include <stdio.h>
27 #include <unistd.h>
28
29 #include "full-write.h"
30 #include "xalloc.h"
31 #include "xvasprintf.h"
32
33 #include "common.h"
34
35 /* Include private definitions so that we can inspect redirections. */
36 #include "pipeline-private.h"
37
38 const char *program_name = "pump";
39
40 static void assert_files_equal (const char *left, const char *right)
41 {
42         pipeline *diff = pipeline_new_command_args ("diff", "-u", left, right,
43                                                     (void *) 0);
44         ck_assert_int_eq (pipeline_run (diff), 0);
45 }
46
47 START_TEST (test_pump_connect_attaches_correctly)
48 {
49         pipeline *one = pipeline_new ();
50         pipeline *two = pipeline_new ();
51         pipeline *three = pipeline_new ();
52
53         pipeline_connect (one, two, three, (void *) 0);
54         ck_assert_int_eq (one->redirect_out, REDIRECT_FD);
55         ck_assert_int_le (one->want_out, 0);
56         ck_assert_ptr_eq (one->want_outfile, NULL);
57         ck_assert_ptr_eq (two->source, one);
58         ck_assert_int_eq (two->redirect_in, REDIRECT_FD);
59         ck_assert_int_le (two->want_in, 0);
60         ck_assert_ptr_eq (two->want_infile, NULL);
61         ck_assert_ptr_eq (three->source, one);
62         ck_assert_int_eq (three->redirect_in, REDIRECT_FD);
63         ck_assert_int_le (three->want_in, 0);
64         ck_assert_ptr_eq (three->want_infile, NULL);
65
66         pipeline_free (three);
67         pipeline_free (two);
68         pipeline_free (one);
69 }
70 END_TEST
71
72 static void tee_source (void *data _GL_UNUSED)
73 {
74         unsigned char buf[256];
75         int i;
76
77         for (i = 0; i < 256; ++i)
78                 buf[i] = (unsigned char) i;
79
80         for (i = 0; i < 4096; ++i)
81                 full_write (fileno (stdout), buf, 256);
82 }
83
84 START_TEST (test_pump_tee)
85 {
86         pipeline *source, *sink_process, *sink_function;
87         char *process_outfile, *function_outfile;
88
89         source = pipeline_new ();
90         pipeline_command (source, pipecmd_new_function ("source", tee_source,
91                                                         NULL, NULL));
92         sink_process = pipeline_new_command_args ("cat", (void *) 0);
93         process_outfile = xasprintf ("%s/process", temp_dir);
94         pipeline_want_outfile (sink_process, process_outfile);
95         sink_function = pipeline_new ();
96         pipeline_command (sink_function, pipecmd_new_passthrough ());
97         function_outfile = xasprintf ("%s/function", temp_dir);
98         pipeline_want_outfile (sink_function, function_outfile);
99         pipeline_connect (source, sink_process, sink_function, (void *) 0);
100         pipeline_pump (source, sink_process, sink_function, (void *) 0);
101         ck_assert_int_eq (pipeline_wait (source), 0);
102         ck_assert_int_eq (pipeline_wait (sink_process), 0);
103         ck_assert_int_eq (pipeline_wait (sink_function), 0);
104         assert_files_equal (process_outfile, function_outfile);
105
106         free (function_outfile);
107         free (process_outfile);
108         pipeline_free (sink_function);
109         pipeline_free (sink_process);
110         pipeline_free (source);
111 }
112 END_TEST
113
114 static Suite *pump_suite (void)
115 {
116         Suite *s = suite_create ("Pump");
117
118         TEST_CASE (s, pump, connect_attaches_correctly);
119         TEST_CASE_WITH_FIXTURE (s, pump, tee, temp_dir_setup,
120                                 temp_dir_teardown);
121
122         return s;
123 }
124
125 MAIN (pump)