/
test_mythsystem.h
187 lines (164 loc) · 5.84 KB
/
test_mythsystem.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
* Class TestMythSystem
*
* Copyright (C) Daniel Kristjansson 2013
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <QtTest/QtTest>
#include <QTemporaryFile>
#include <QDateTime>
#include <iostream>
using namespace std;
#include "mythlogging_extra.h"
#include "debugloghandler.h"
#include "mythcorecontext.h"
#include "mythsystem.h"
static DebugLogHandler *console_dbg(void)
{
return DebugLogHandler::Get("ConsoleLogHandler");
}
class TestMythSystem: public QObject
{
Q_OBJECT
private slots:
// called at the beginning of these sets of tests
void initTestCase(void)
{
gCoreContext = new MythCoreContext("bin_version", NULL);
DebugLogHandler::AddReplacement("ConsoleLogHandler");
myth_logging::initialize_logging(
VB_GENERAL, LOG_INFO, kNoFacility, /*use_threads*/false,
/*logfile*/ QString(), /*logprefix*/QString());
}
// called at the end of these sets of tests
void cleanupTestCase(void)
{
ShutdownMythSystem();
}
// called before each test case
void init(void)
{
}
// called after each test case
void cleanup(void)
{
}
void constructed_command_is_run(void)
{
MythSystem cmd(QString("echo %1").arg(__FUNCTION__), kMSStdOut);
cmd.Run();
int ret = cmd.Wait();
QVERIFY(ret == 0);
QVERIFY(QString(cmd.ReadAll()).contains(__FUNCTION__));
}
void exit_code_works(void)
{
MythSystem cmd("exit 34", kMSNone | kMSRunShell);
cmd.Run();
int ret = cmd.Wait();
QVERIFY(ret == 34);
}
// TODO kMSDontBlockInputDevs -- avoid blocking LIRC & Joystick Menu
// TODO kMSDontDisableDrawing -- avoid disabling UI drawing
// tests kMSRunBackground -- run child in the background
void run_in_background_works(void)
{
MythSystem cmd("sleep 1", kMSRunBackground);
QDateTime before = QDateTime::currentDateTime();
cmd.Run();
(void) cmd.Wait();
QDateTime after = QDateTime::currentDateTime();
QVERIFY(before.msecsTo(after) < 500);
}
// TODO kMSProcessEvents -- process events while waiting
// TODO kMSInUi -- the parent is in the UI
#if 0 /* FIXME currently this blocks forever :( */
// kMSStdIn -- allow access to stdin
void stdin_works(void)
{
QTemporaryFile tempfile;
tempfile.open();
QByteArray in = QString(__FUNCTION__).toLatin1();
MythSystem cmd(QString("cat - > %1").arg(tempfile.fileName()),
kMSStdIn);
cmd.Write(in);
cmd.Run();
int ret = cmd.Wait();
QVERIFY(ret == 0);
QByteArray out = tempfile.readAll();
QVERIFY(QString(out).contains(QString(in)));
}
#endif
// kMSStdOut -- allow access to stdout
void stdout_works(void)
{
MythSystem cmd(QString("echo %1").arg(__FUNCTION__), kMSStdOut);
cmd.Run();
int ret = cmd.Wait();
QVERIFY(ret == 0);
QVERIFY(QString(cmd.ReadAll()).contains(__FUNCTION__));
}
// kMSStdErr -- allow access to stderr
void stderr_works(void)
{
MythSystem cmd(QString("echo %1 >&2").arg(__FUNCTION__),
kMSRunShell | kMSStdErr);
cmd.Run();
int ret = cmd.Wait();
QVERIFY(ret == 0);
QVERIFY(QString(cmd.ReadAllErr()).contains(__FUNCTION__));
}
// TODO kMSBuffered -- buffer the IO channels
// kMSRunShell -- run process through shell
void shell_used_when_requested(void)
{
MythSystem cmd("if [ x != y ] ; then echo X ; else echo Y ; fi",
kMSRunShell | kMSStdOut);
cmd.Run();
(void) cmd.Wait();
QVERIFY(QString(cmd.ReadAll()).contains("X"));
}
void shell_not_used_when_not_requested(void)
{
MythSystem cmd("if [ x != y ] ; then echo X ; else echo Y ; fi",
kMSStdOut);
cmd.Run();
(void) cmd.Wait();
QVERIFY(!QString(cmd.ReadAll()).contains("X"));
}
// no need to test kMSNoRunShell, it is a no-op
// TODO delete flag
// kMSNoRunShell -- do NOT run process through shell
// kMSAnonLog -- anonymize the logs
void logs_anonymized_when_requested(void)
{
console_dbg()->Clear();
MythSystem cmd(QString("echo %1").arg(__FUNCTION__), kMSAnonLog);
cmd.Run();
(void) cmd.Wait();
DebugLogHandlerEntry l = console_dbg()->LastEntry(kHandleLog);
QVERIFY(!l.entry().GetMessage().contains(__FUNCTION__));
QVERIFY(!cmd.GetLogCmd().contains(__FUNCTION__));
}
// TODO flags to test
// TODO kMSAbortOnJump -- abort this process on a jumppoint
// TODO kMSSetPGID -- set the process group id
// TODO kMSAutoCleanup -- automatically delete if backgrounded
// TODO kMSLowExitVal -- allow exit values 0-127 only
// TODO kMSDisableUDPListener -- disable MythMessage UDP listener
// for the duration of application.
// TODO kMSPropagateLogs -- add arguments for MythTV log propagation
};