comparison ui/tests/cinstprocesstest.cpp @ 100:8fa273791242

Add test for cinstprocess
author Andre Heinecke <aheinecke@intevation.de>
date Fri, 21 Mar 2014 12:15:29 +0000
parents
children 5ed770c41a73
comparison
equal deleted inserted replaced
99:bc1e6732f43c 100:8fa273791242
1 #include "cinstprocesstest.h"
2 #include "certificatelist.h"
3 #include "errorcodes.h"
4
5 #include <QDebug>
6 #include <QDir>
7 #include <QFile>
8 #include <QProcess>
9 #include <QTest>
10
11 #define RELATIVE_CINST_PATH "../../cinst/cinst"
12
13 QProcess *CinstProcessTest::startCinstProcess() {
14 QProcess *installerProcess = new QProcess();
15 installerProcess->setProgram(RELATIVE_CINST_PATH);
16 installerProcess->start();
17 installerProcess->waitForStarted();
18 return installerProcess;
19 }
20
21 void finishVerify(QProcess *proc, int exitCode) {
22 proc->closeWriteChannel();
23 proc->waitForFinished();
24 QVERIFY(proc->exitStatus() == QProcess::NormalExit);
25 QVERIFY(proc->exitCode() == exitCode);
26 delete proc;
27 }
28
29 void CinstProcessTest::testValidInput() {
30 QProcess* installerProcess = startCinstProcess();
31 QVERIFY(installerProcess->state() == QProcess::Running);
32
33 installerProcess->write("-----BEGIN CERTIFICATE LIST-----\r\n");
34 installerProcess->write(validList.rawData().toLatin1());
35 installerProcess->write("-----END CERTIFICATE LIST-----\r\n");
36
37 foreach (const Certificate &cert, validList.getInstallCertificates()) {
38 installerProcess->write(cert.base64Line().toLatin1());
39 installerProcess->write("\r\n");
40 }
41
42 finishVerify(installerProcess, 0);
43 }
44
45 void CinstProcessTest::initTestCase() {
46 QDir dataDir = QDir(SOURCE_DIR"/data/");
47 QString fileName = dataDir.absoluteFilePath("list-valid-signed.txt");
48 validList = CertificateList(fileName.toLocal8Bit().data());
49 }
50
51 void CinstProcessTest::testNoList() {
52 /* No list */
53 QProcess* installerProcess = startCinstProcess();
54 QVERIFY(installerProcess->state() == QProcess::Running);
55 installerProcess->write("-----BEGIN CERTIFICATE LIST-----\r\n");
56 installerProcess->write("-----END CERTIFICATE LIST-----\r\n");
57
58 foreach (const Certificate &cert, validList.getInstallCertificates()) {
59 installerProcess->write(cert.base64Line().toLatin1());
60 installerProcess->write("\r\n");
61 }
62 finishVerify(installerProcess, ERR_INVALID_INPUT_NO_LIST);
63 }
64
65 void CinstProcessTest::testGarbageInput() {
66 QProcess* installerProcess = startCinstProcess();
67 QVERIFY(installerProcess->state() == QProcess::Running);
68 /* Garbage */
69 installerProcess = startCinstProcess();
70 installerProcess->write("-----BEGIN CERTIFICATE LIST-----\r\n");
71 int retval=0;
72 int bytesWritten=0;
73 do {
74 char garbage[1030];
75 for (int i = 0; i < 1030; i++) {
76 garbage[i] = (char) qrand() % 255;
77 }
78 retval = installerProcess->write(garbage, 1030);
79 bytesWritten += retval;
80 } while (retval != -1 && bytesWritten < 100 *1024 *1024 );
81
82 QVERIFY (bytesWritten < 100 *1024 *1024);
83
84 finishVerify(installerProcess, ERR_TOO_MUCH_INPUT);
85 }
86
87 void CinstProcessTest::testNoInput() {
88 QProcess* installerProcess = startCinstProcess();
89 QVERIFY(installerProcess->state() == QProcess::Running);
90
91 /* Nothing */
92 installerProcess = startCinstProcess();
93 finishVerify(installerProcess, ERR_INVALID_INPUT_NO_LIST);
94 }
95
96
97 void CinstProcessTest::testNoInstructions() {
98 /* No instructions */
99 QProcess* installerProcess = startCinstProcess();
100 QVERIFY(installerProcess->state() == QProcess::Running);
101 installerProcess->write("-----BEGIN CERTIFICATE LIST-----\r\n");
102 installerProcess->write(validList.rawData().toLatin1());
103 installerProcess->write("-----END CERTIFICATE LIST-----\r\n");
104
105 finishVerify(installerProcess, ERR_NO_INSTRUCTIONS);
106 }
107
108 void CinstProcessTest::testInvalidInstruction() {
109 QProcess* installerProcess = startCinstProcess();
110 QVERIFY(installerProcess->state() == QProcess::Running);
111
112 /* foobar as instruction */
113 installerProcess->write("-----BEGIN CERTIFICATE LIST-----\r\n");
114 installerProcess->write(validList.rawData().toLatin1());
115 installerProcess->write("-----END CERTIFICATE LIST-----\r\n");
116
117 installerProcess->write("foobar");
118
119 finishVerify(installerProcess, ERR_INVALID_INSTRUCTIONS);
120 }
121
122 QTEST_GUILESS_MAIN (CinstProcessTest);

http://wald.intevation.org/projects/trustbridge/