comparison ui/tests/cinstprocesstest.cpp @ 310:f758460ca437

Merged
author Sascha Wilde <wilde@intevation.de>
date Fri, 04 Apr 2014 09:54:19 +0200
parents 9c5e6b142611
children f17226aa2e09
comparison
equal deleted inserted replaced
309:fa37384b86b6 310:f758460ca437
1 #include "cinstprocesstest.h" 1 #include "cinstprocesstest.h"
2 #include "certificatelist.h" 2 #include "certificatelist.h"
3 #include "errorcodes.h" 3 #include "errorcodes.h"
4 #include "common.h"
4 5
5 #include <QDebug> 6 #include <QDebug>
6 #include <QDir> 7 #include <QDir>
7 #include <QFile> 8 #include <QFile>
8 #include <QProcess> 9 #include <QProcess>
9 10
10 #define RELATIVE_CINST_PATH "../../cinst/cinst" 11 #define CINST_PATH_CANDIDATES "../../cinst/cinst" << \
11 12 "cinst" << "../../cinst/cinst.exe" << "cinst.exe";
12 QProcess *CinstProcessTest::startCinstProcess() { 13
14 QProcess *CinstProcessTest::startCinstProcess(const QStringList& args) {
15 QStringList cinstCandidates;
16 cinstCandidates << CINST_PATH_CANDIDATES;
17 QString processPath;
18 foreach (const QString& candidate, cinstCandidates) {
19 QFileInfo fi(candidate);
20 if (fi.isExecutable()) {
21 processPath = candidate;
22 break;
23 }
24 }
25
13 QProcess *installerProcess = new QProcess(); 26 QProcess *installerProcess = new QProcess();
14 installerProcess->setProgram(RELATIVE_CINST_PATH); 27 installerProcess->setArguments(args);
28 installerProcess->setProgram(processPath);
15 installerProcess->start(); 29 installerProcess->start();
16 installerProcess->waitForStarted(); 30 installerProcess->waitForStarted();
17 return installerProcess; 31 return installerProcess;
18 } 32 }
19 33
20 #define VERIFY_PROC_DEBUG(x) \ 34 #define VERIFY_PROC_DEBUG(x) \
21 if (! x ) { \ 35 if (! x ) { \
22 qDebug() << "Stdout:" << proc->readAllStandardOutput(); \ 36 qDebug() << "Stdout:" << proc->readAllStandardOutput(); \
23 qDebug() << "Stderr:" << proc->readAllStandardError(); \ 37 qDebug() << "Stderr:" << proc->readAllStandardError(); \
24 qDebug() << "Exit code: " << proc->exitCode(); \ 38 qDebug() << "Exit code: " << proc->exitCode(); \
39 qDebug() << "Exit status: " << proc->exitStatus(); \
25 } \ 40 } \
26 QVERIFY(x) 41 QVERIFY(x)
27 42
28 void finishVerify(QProcess *proc, int exitCode) { 43 void finishVerify(QProcess *proc, int exitCode) {
29 proc->closeWriteChannel(); 44 proc->closeWriteChannel();
32 VERIFY_PROC_DEBUG(proc->exitCode() == exitCode); 47 VERIFY_PROC_DEBUG(proc->exitCode() == exitCode);
33 delete proc; 48 delete proc;
34 } 49 }
35 50
36 void CinstProcessTest::testValidInput() { 51 void CinstProcessTest::testValidInput() {
37 QProcess* installerProcess = startCinstProcess(); 52 QStringList args;
38 QVERIFY(installerProcess->state() == QProcess::Running); 53 args << "list=" + validListFile.fileName();
39 54
40 installerProcess->write("-----BEGIN CERTIFICATE LIST-----\r\n"); 55 QTemporaryFile instructions;
41 installerProcess->write(validList.rawData().toLatin1()); 56 instructions.open();
42 installerProcess->write("-----END CERTIFICATE LIST-----\r\n"); 57 foreach (const Certificate &cert, validList.getCertificates()) {
43 58 instructions.write(cert.base64Line().toLatin1());
44 foreach (const Certificate &cert, validList.getCertificates()) { 59 instructions.write("\n");
45 installerProcess->write(cert.base64Line().toLatin1()); 60 }
46 installerProcess->write("\r\n"); 61 instructions.close();
47 } 62
48 63 args << "instructions=" + instructions.fileName();
64
65 QProcess* installerProcess = startCinstProcess(args);
49 finishVerify(installerProcess, ERR_NO_ERROR); 66 finishVerify(installerProcess, ERR_NO_ERROR);
50 } 67 }
51 68
52 void CinstProcessTest::initTestCase() { 69 void CinstProcessTest::initTestCase() {
53 QDir dataDir = QDir(SOURCE_DIR"/data/"); 70 QFile valid(":/list-valid-signed.txt");
54 QString fileName = dataDir.absoluteFilePath("list-valid-signed.txt"); 71 valid.open(QIODevice::ReadOnly);
55 validList = CertificateList(fileName.toLocal8Bit().data()); 72 validListFile.open();
73 validListFile.write(valid.readAll());
74 valid.close();
75 validListFile.close();
76 validList = CertificateList(validListFile.fileName().toLocal8Bit().data());
77
78 QVERIFY(validList.isValid());
79
80 QFile invalid(":/list-invalid-signed.txt");
81 invalid.open(QIODevice::ReadOnly);
82 invalidListFile.open();
83 invalidListFile.write(invalid.readAll());
84 invalid.close();
85 invalidListFile.close();
86 invalidList = CertificateList(invalidListFile.fileName().toLocal8Bit().data());
87
88 QVERIFY(!invalidList.isValid());
89
90 QFile other(":/list-valid-other-signature.txt");
91 other.open(QIODevice::ReadOnly);
92 otherListFile.open();
93 otherListFile.write(other.readAll());
94 other.close();
95 otherListFile.close();
96 otherList = CertificateList(otherListFile.fileName().toLocal8Bit().data());
97
98 QVERIFY(!otherList.isValid());
99
100 /* Set HOME or APPDATA so that nss stores are not touched
101 * see nsstest for the real test of that code */
102 #ifdef WIN32
103 QVERIFY(!setenv ("APPDATA", fakeHome.path().toLocal8Bit().constData(), 1));
104 #else
105 QVERIFY(!setenv ("HOME", fakeHome.path().toLocal8Bit().constData(), 1));
106 #endif
56 } 107 }
57 108
58 void CinstProcessTest::testNoList() { 109 void CinstProcessTest::testNoList() {
59 /* No list */ 110 /* No list */
60 QProcess* installerProcess = startCinstProcess(); 111 QTemporaryFile emptyFile;
61 QVERIFY(installerProcess->state() == QProcess::Running); 112 emptyFile.open();
62 installerProcess->write("-----BEGIN CERTIFICATE LIST-----\r\n"); 113 emptyFile.close();
63 installerProcess->write("-----END CERTIFICATE LIST-----\r\n"); 114
64 115 QStringList args;
65 foreach (const Certificate &cert, validList.getCertificates()) { 116 args << "list=" + emptyFile.fileName();
66 installerProcess->write(cert.base64Line().toLatin1()); 117
67 installerProcess->write("\r\n"); 118 QTemporaryFile instructions;
68 } 119 instructions.open();
120 foreach (const Certificate &cert, validList.getCertificates()) {
121 instructions.write(cert.base64Line().toLatin1());
122 instructions.write("\n");
123 }
124 instructions.close();
125
126 args << "instructions=" + instructions.fileName();
127
128 QProcess* installerProcess = startCinstProcess(args);
69 finishVerify(installerProcess, ERR_INVALID_INPUT_NO_LIST); 129 finishVerify(installerProcess, ERR_INVALID_INPUT_NO_LIST);
70 } 130 }
71 131
72 void CinstProcessTest::testGarbageInput() { 132 void CinstProcessTest::testGarbageInput() {
73 QProcess* installerProcess = startCinstProcess(); 133 QStringList args;
74 QVERIFY(installerProcess->state() == QProcess::Running); 134
75 /* Garbage */ 135 QString garbage = getRandomDataFile(21*1024*1024);
76 installerProcess = startCinstProcess(); 136 args << "list=" + garbage;
77 installerProcess->write("-----BEGIN CERTIFICATE LIST-----\r\n"); 137
78 int retval=0; 138 QTemporaryFile instructions;
79 int bytesWritten=0; 139 instructions.open();
80 do { 140 foreach (const Certificate &cert, validList.getCertificates()) {
81 char garbage[1030]; 141 instructions.write(cert.base64Line().toLatin1());
82 for (int i = 0; i < 1030; i++) { 142 instructions.write("\n");
83 garbage[i] = (char) qrand() % 255; 143 }
84 } 144 instructions.close();
85 retval = installerProcess->write(garbage, 1030); 145
86 bytesWritten += retval; 146 args << "instructions=" + instructions.fileName();
87 } while (retval != -1 && bytesWritten < 15 *1024 *1024 ); 147
88 148 QProcess* installerProcess = startCinstProcess(args);
89 finishVerify(installerProcess, ERR_INVALID_INPUT); 149 /* If the following failed there may be leftovers in /tmp */
150 finishVerify(installerProcess, ERR_INVALID_INPUT_NO_LIST);
151 QVERIFY(QFile::remove(garbage));
90 } 152 }
91 153
92 void CinstProcessTest::testNoInput() { 154 void CinstProcessTest::testNoInput() {
93 QProcess* installerProcess = startCinstProcess(); 155 QStringList args;
94 QVERIFY(installerProcess->state() == QProcess::Running); 156 args << "list=foobazbuf";
95 157 args << "instructions=bazbuffoo";
96 /* Nothing */ 158 QProcess* installerProcess;
97 installerProcess = startCinstProcess(); 159 installerProcess = startCinstProcess(args);
98 finishVerify(installerProcess, ERR_INVALID_INPUT_NO_LIST); 160 finishVerify(installerProcess, ERR_INVALID_INPUT_NO_LIST);
99 } 161 }
100 162
101 163
102 void CinstProcessTest::testNoInstructions() { 164 void CinstProcessTest::testNoInstructions() {
103 /* No instructions */ 165 /* No instructions */
104 QProcess* installerProcess = startCinstProcess(); 166 QTemporaryFile emptyFile;
105 QVERIFY(installerProcess->state() == QProcess::Running); 167 emptyFile.open();
106 installerProcess->write("-----BEGIN CERTIFICATE LIST-----\r\n"); 168 emptyFile.close();
107 installerProcess->write(validList.rawData().toLatin1()); 169
108 installerProcess->write("-----END CERTIFICATE LIST-----\r\n"); 170 QStringList args;
109 171 args << "list=" + validListFile.fileName();
172 args << "instructions=" + emptyFile.fileName();
173
174 QProcess* installerProcess = startCinstProcess(args);
110 finishVerify(installerProcess, ERR_NO_INSTRUCTIONS); 175 finishVerify(installerProcess, ERR_NO_INSTRUCTIONS);
111 } 176 }
112 177
113 void CinstProcessTest::testInvalidInstruction() { 178 void CinstProcessTest::testInvalidInstruction() {
114 QProcess* installerProcess = startCinstProcess(); 179 QStringList args;
115 QVERIFY(installerProcess->state() == QProcess::Running); 180 args << "list=" + validListFile.fileName();
116 181
117 installerProcess->write("-----BEGIN CERTIFICATE LIST-----\r\n"); 182 QTemporaryFile instructions;
118 installerProcess->write(validList.rawData().toLatin1()); 183 instructions.open();
119 installerProcess->write("-----END CERTIFICATE LIST-----\r\n"); 184 foreach (const Certificate &cert, validList.getCertificates()) {
120 185 instructions.write(cert.base64Line().toLatin1());
121 installerProcess->write("I:ABCDEF\r\n"); 186 instructions.write("\n");
187 }
188 instructions.write("I:ABCDEF\n");
189 instructions.close();
190
191 args << "instructions=" + instructions.fileName();
192
193 QProcess* installerProcess = startCinstProcess(args);
122 194
123 finishVerify(installerProcess, ERR_INVALID_INSTRUCTIONS); 195 finishVerify(installerProcess, ERR_INVALID_INSTRUCTIONS);
124 } 196 }
125 197
126 void CinstProcessTest::testUninstall() { 198 void CinstProcessTest::testUninstall() {
127 QProcess* installerProcess = startCinstProcess(); 199 QStringList args;
128 QVERIFY(installerProcess->state() == QProcess::Running); 200 args << "list=" + validListFile.fileName();
129 201 args << "instructions=uninstall";
130 installerProcess->write("-----BEGIN CERTIFICATE LIST-----\r\n"); 202
131 installerProcess->write(validList.rawData().toLatin1()); 203 QProcess* installerProcess = startCinstProcess(args);
132 installerProcess->write("-----END CERTIFICATE LIST-----\r\n");
133
134 installerProcess->write("UNINSTALL\r\n");
135 204
136 finishVerify(installerProcess, ERR_NO_ERROR); 205 finishVerify(installerProcess, ERR_NO_ERROR);
137 } 206 }
138 207
139 QTEST_GUILESS_MAIN (CinstProcessTest); 208 QTEST_GUILESS_MAIN (CinstProcessTest);

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