1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the test suite of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29#include <QtCore/QCoreApplication>
30#include <QtCore/QDeadlineTimer>
31#include <QtCore/QProcess>
32#include <QtCore/QTemporaryFile>
33#include <QtCore/QThread>
34
35#include <stdlib.h>
36
37static bool waitForDoneFileWritten(const QString &filePath, int msecs = 30000)
38{
39 QDeadlineTimer t(msecs);
40 do {
41 QThread::msleep(250);
42 QFile file(filePath);
43 if (!file.open(flags: QIODevice::ReadOnly))
44 continue;
45 if (file.readAll() == "That's all folks!")
46 return true;
47 } while (!t.hasExpired());
48 return false;
49}
50
51int main(int argc, char **argv)
52{
53 QCoreApplication app(argc, argv);
54
55 if (argc < 4)
56 return 13;
57
58 QProcess process;
59
60 QProcess::ProcessChannelMode mode = (QProcess::ProcessChannelMode)atoi(nptr: argv[1]);
61 process.setProcessChannelMode(mode);
62 if (process.processChannelMode() != mode)
63 return 1;
64
65 QProcess::InputChannelMode inmode = (QProcess::InputChannelMode)atoi(nptr: argv[2]);
66 process.setInputChannelMode(inmode);
67 if (process.inputChannelMode() != inmode)
68 return 11;
69
70 if (atoi(nptr: argv[3])) {
71 QTemporaryFile doneFile("testForwarding_XXXXXX.txt");
72 if (!doneFile.open())
73 return 12;
74 doneFile.close();
75
76 process.setProgram("testForwardingHelper/testForwardingHelper");
77 process.setArguments(QStringList(doneFile.fileName()));
78 if (!process.startDetached())
79 return 13;
80 if (!waitForDoneFileWritten(filePath: doneFile.fileName()))
81 return 14;
82 } else {
83 process.start(command: "testProcessEcho2/testProcessEcho2");
84
85 if (!process.waitForStarted(msecs: 5000))
86 return 2;
87
88 if (inmode == QProcess::ManagedInputChannel && process.write(data: "forwarded") != 9)
89 return 3;
90
91 process.closeWriteChannel();
92 if (!process.waitForFinished(msecs: 5000))
93 return 4;
94
95 if ((mode == QProcess::ForwardedOutputChannel || mode == QProcess::ForwardedChannels)
96 && !process.readAllStandardOutput().isEmpty())
97 return 5;
98 if ((mode == QProcess::ForwardedErrorChannel || mode == QProcess::ForwardedChannels)
99 && !process.readAllStandardError().isEmpty())
100 return 6;
101 }
102 return 0;
103}
104

source code of qtbase/tests/auto/corelib/io/qprocess/testForwarding/main.cpp