tizen 2.4 release
[framework/web/wrt-commons.git] / tests / test / test_process_pipe.cpp
1 /*
2  * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /*
17  * @file        test_process_pipe.cpp
18  * @author      Tomasz Iwanek (t.iwanek@samsung.com)
19  * @version     1.0
20  * @brief       This file is the implementation file of ProcessPipe tests
21  */
22
23 #include <dpl/test/test_runner.h>
24 #include <dpl/test/process_pipe.h>
25 #include <dpl/binary_queue.h>
26 #include <dpl/log/wrt_log.h>
27
28 #include <cstring>
29
30 using namespace DPL;
31
32 RUNNER_TEST_GROUP_INIT(DPL)
33
34 namespace {
35 void readAll(ProcessPipe & npp, BinaryQueue & result)
36 {
37     do
38     {
39         BinaryQueueAutoPtr dataptr = npp.Read(4096);
40
41         RUNNER_ASSERT_MSG(dataptr.get() != NULL, "Cannot read from pipe subprocess");
42
43         WrtLogD("Size: %i", dataptr->Size());
44
45         if(dataptr->Empty()) break;
46         result.AppendMoveFrom(*dataptr);
47     }
48     while(true);
49 }
50 }
51
52 RUNNER_TEST(ProcessPipe_echo)
53 {
54     ProcessPipe npp;
55     npp.Open("echo -e \"Test echo text\\nAnd new line\"");
56     BinaryQueue result;
57     readAll(npp, result);
58     npp.Close();
59
60     char buffer[100] = "";
61     result.FlattenConsume(buffer, std::min(result.Size(), sizeof(buffer)));
62
63     RUNNER_ASSERT_MSG(strcmp(buffer, "Test echo text\nAnd new line\n") == 0, "Echoed text in not equal");
64 }
65
66 RUNNER_TEST(ProcessPipe_double_open)
67 {
68     ProcessPipe npp;
69     npp.Open("echo  \"Test \"");
70     Try
71     {
72         npp.Open("echo \"Test\"");
73     }
74     Catch(DPL::ProcessPipe::Exception::DoubleOpen)
75     {
76         npp.Close();
77         return;
78     }
79     npp.Close();
80     RUNNER_ASSERT_MSG(false, "DoubleOpen not thrown");
81 }
82
83 RUNNER_TEST(ProcessPipe_double_close)
84 {
85     ProcessPipe npp;
86     npp.Open("echo  \"Test invalid\"");
87     npp.Close();
88     Try
89     {
90         npp.Close();
91     }
92     Catch(DPL::Exception)
93     {
94         RUNNER_ASSERT_MSG(false, "Second Close throws exception");
95     }
96 }
97
98 RUNNER_TEST(ProcessPipe_pipeerror_off)
99 {
100     ProcessPipe npp(ProcessPipe::PipeErrorPolicy::OFF);
101     npp.Open("ls /nonexistingdirectory");
102     BinaryQueue result;
103     readAll(npp, result); //TODO: fix this test
104     npp.Close();
105 }
106
107 RUNNER_TEST(ProcessPipe_pipeerror_pipe)
108 {
109     //ls output dependent...
110     ProcessPipe npp(ProcessPipe::PipeErrorPolicy::PIPE);
111     npp.Open("ls /nonexistingdirectory");
112     BinaryQueue result;
113     readAll(npp, result);
114     npp.Close();
115     char buffer[100] = "";
116     result.FlattenConsume(buffer, std::min(result.Size(), sizeof(buffer)));
117
118     RUNNER_ASSERT_MSG(strcmp(buffer, "ls: cannot access /nonexistingdirectory: No such file or directory\n") == 0, "Ls error text in not equal");
119 }