annotate ui/tests/certlistparsertest.cpp @ 87:be13e5e0a98f

Expand test to cover more functionality from certificate and certificatelist
author Andre Heinecke <aheinecke@intevation.de>
date Thu, 20 Mar 2014 16:23:31 +0000
parents a7b8cb29f1f6
children abc76b750ea8
rev   line source
7
992c0ec57660 Add unit tests make CertificateList work.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
1 #include "certlistparsertest.h"
992c0ec57660 Add unit tests make CertificateList work.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
2 #include "certificatelist.h"
87
be13e5e0a98f Expand test to cover more functionality from certificate and certificatelist
Andre Heinecke <aheinecke@intevation.de>
parents: 50
diff changeset
3 #include "certificate.h"
7
992c0ec57660 Add unit tests make CertificateList work.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
4
992c0ec57660 Add unit tests make CertificateList work.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
5 #include <QDebug>
992c0ec57660 Add unit tests make CertificateList work.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
6
31
37fc66967517 Implement signature verification wiht polarssl
Andre Heinecke <aheinecke@intevation.de>
parents: 26
diff changeset
7 void CertListTest::testValidList()
37fc66967517 Implement signature verification wiht polarssl
Andre Heinecke <aheinecke@intevation.de>
parents: 26
diff changeset
8 {
37fc66967517 Implement signature verification wiht polarssl
Andre Heinecke <aheinecke@intevation.de>
parents: 26
diff changeset
9 const char *fname = "list-valid-signed.txt";
87
be13e5e0a98f Expand test to cover more functionality from certificate and certificatelist
Andre Heinecke <aheinecke@intevation.de>
parents: 50
diff changeset
10 QFile validList(fname);
be13e5e0a98f Expand test to cover more functionality from certificate and certificatelist
Andre Heinecke <aheinecke@intevation.de>
parents: 50
diff changeset
11 validList.open(QIODevice::ReadOnly);
be13e5e0a98f Expand test to cover more functionality from certificate and certificatelist
Andre Heinecke <aheinecke@intevation.de>
parents: 50
diff changeset
12 QString validData = QString::fromLatin1(validList.readAll());
be13e5e0a98f Expand test to cover more functionality from certificate and certificatelist
Andre Heinecke <aheinecke@intevation.de>
parents: 50
diff changeset
13 QStringList instLines;
be13e5e0a98f Expand test to cover more functionality from certificate and certificatelist
Andre Heinecke <aheinecke@intevation.de>
parents: 50
diff changeset
14 QStringList remoLines;
be13e5e0a98f Expand test to cover more functionality from certificate and certificatelist
Andre Heinecke <aheinecke@intevation.de>
parents: 50
diff changeset
15
31
37fc66967517 Implement signature verification wiht polarssl
Andre Heinecke <aheinecke@intevation.de>
parents: 26
diff changeset
16 CertificateList *certList = testWithFile(fname);
87
be13e5e0a98f Expand test to cover more functionality from certificate and certificatelist
Andre Heinecke <aheinecke@intevation.de>
parents: 50
diff changeset
17 QCOMPARE(certList->getStatus(), Valid);
be13e5e0a98f Expand test to cover more functionality from certificate and certificatelist
Andre Heinecke <aheinecke@intevation.de>
parents: 50
diff changeset
18 QVERIFY(certList->isValid());
be13e5e0a98f Expand test to cover more functionality from certificate and certificatelist
Andre Heinecke <aheinecke@intevation.de>
parents: 50
diff changeset
19
be13e5e0a98f Expand test to cover more functionality from certificate and certificatelist
Andre Heinecke <aheinecke@intevation.de>
parents: 50
diff changeset
20 QVERIFY(certList->rawData() == validData.toLatin1());
be13e5e0a98f Expand test to cover more functionality from certificate and certificatelist
Andre Heinecke <aheinecke@intevation.de>
parents: 50
diff changeset
21
be13e5e0a98f Expand test to cover more functionality from certificate and certificatelist
Andre Heinecke <aheinecke@intevation.de>
parents: 50
diff changeset
22 const QList<Certificate> instList = certList->getInstallCertificates();
be13e5e0a98f Expand test to cover more functionality from certificate and certificatelist
Andre Heinecke <aheinecke@intevation.de>
parents: 50
diff changeset
23 const QList<Certificate> remoList = certList->getInstallCertificates();
be13e5e0a98f Expand test to cover more functionality from certificate and certificatelist
Andre Heinecke <aheinecke@intevation.de>
parents: 50
diff changeset
24
be13e5e0a98f Expand test to cover more functionality from certificate and certificatelist
Andre Heinecke <aheinecke@intevation.de>
parents: 50
diff changeset
25 foreach (QString act, validData.split("\r\n")) {
be13e5e0a98f Expand test to cover more functionality from certificate and certificatelist
Andre Heinecke <aheinecke@intevation.de>
parents: 50
diff changeset
26 if (act.startsWith("I:")) {
be13e5e0a98f Expand test to cover more functionality from certificate and certificatelist
Andre Heinecke <aheinecke@intevation.de>
parents: 50
diff changeset
27 instLines << act;
be13e5e0a98f Expand test to cover more functionality from certificate and certificatelist
Andre Heinecke <aheinecke@intevation.de>
parents: 50
diff changeset
28 continue;
be13e5e0a98f Expand test to cover more functionality from certificate and certificatelist
Andre Heinecke <aheinecke@intevation.de>
parents: 50
diff changeset
29 }
be13e5e0a98f Expand test to cover more functionality from certificate and certificatelist
Andre Heinecke <aheinecke@intevation.de>
parents: 50
diff changeset
30 if (act.startsWith("R:")) {
be13e5e0a98f Expand test to cover more functionality from certificate and certificatelist
Andre Heinecke <aheinecke@intevation.de>
parents: 50
diff changeset
31 remoLines << act;
be13e5e0a98f Expand test to cover more functionality from certificate and certificatelist
Andre Heinecke <aheinecke@intevation.de>
parents: 50
diff changeset
32 continue;
be13e5e0a98f Expand test to cover more functionality from certificate and certificatelist
Andre Heinecke <aheinecke@intevation.de>
parents: 50
diff changeset
33 }
be13e5e0a98f Expand test to cover more functionality from certificate and certificatelist
Andre Heinecke <aheinecke@intevation.de>
parents: 50
diff changeset
34 }
be13e5e0a98f Expand test to cover more functionality from certificate and certificatelist
Andre Heinecke <aheinecke@intevation.de>
parents: 50
diff changeset
35
be13e5e0a98f Expand test to cover more functionality from certificate and certificatelist
Andre Heinecke <aheinecke@intevation.de>
parents: 50
diff changeset
36 foreach (const Certificate& cert, instList) {
be13e5e0a98f Expand test to cover more functionality from certificate and certificatelist
Andre Heinecke <aheinecke@intevation.de>
parents: 50
diff changeset
37 QVERIFY(cert.isValid());
be13e5e0a98f Expand test to cover more functionality from certificate and certificatelist
Andre Heinecke <aheinecke@intevation.de>
parents: 50
diff changeset
38 QVERIFY(instLines.contains(cert.base64Line()));
be13e5e0a98f Expand test to cover more functionality from certificate and certificatelist
Andre Heinecke <aheinecke@intevation.de>
parents: 50
diff changeset
39 }
be13e5e0a98f Expand test to cover more functionality from certificate and certificatelist
Andre Heinecke <aheinecke@intevation.de>
parents: 50
diff changeset
40 foreach (const Certificate& cert, remoList) {
be13e5e0a98f Expand test to cover more functionality from certificate and certificatelist
Andre Heinecke <aheinecke@intevation.de>
parents: 50
diff changeset
41 QVERIFY(cert.isValid());
be13e5e0a98f Expand test to cover more functionality from certificate and certificatelist
Andre Heinecke <aheinecke@intevation.de>
parents: 50
diff changeset
42 QVERIFY(remoLines.contains(cert.base64Line()));
be13e5e0a98f Expand test to cover more functionality from certificate and certificatelist
Andre Heinecke <aheinecke@intevation.de>
parents: 50
diff changeset
43 }
be13e5e0a98f Expand test to cover more functionality from certificate and certificatelist
Andre Heinecke <aheinecke@intevation.de>
parents: 50
diff changeset
44
be13e5e0a98f Expand test to cover more functionality from certificate and certificatelist
Andre Heinecke <aheinecke@intevation.de>
parents: 50
diff changeset
45 /* Be variable if test data changes later */
be13e5e0a98f Expand test to cover more functionality from certificate and certificatelist
Andre Heinecke <aheinecke@intevation.de>
parents: 50
diff changeset
46 QVERIFY(instList.size() >= 1);
be13e5e0a98f Expand test to cover more functionality from certificate and certificatelist
Andre Heinecke <aheinecke@intevation.de>
parents: 50
diff changeset
47 QVERIFY(remoList.size() >= 1);
be13e5e0a98f Expand test to cover more functionality from certificate and certificatelist
Andre Heinecke <aheinecke@intevation.de>
parents: 50
diff changeset
48 QVERIFY(instLines.size() == instList.size());
be13e5e0a98f Expand test to cover more functionality from certificate and certificatelist
Andre Heinecke <aheinecke@intevation.de>
parents: 50
diff changeset
49 QVERIFY(remoLines.size() == remoList.size());
be13e5e0a98f Expand test to cover more functionality from certificate and certificatelist
Andre Heinecke <aheinecke@intevation.de>
parents: 50
diff changeset
50
be13e5e0a98f Expand test to cover more functionality from certificate and certificatelist
Andre Heinecke <aheinecke@intevation.de>
parents: 50
diff changeset
51
31
37fc66967517 Implement signature verification wiht polarssl
Andre Heinecke <aheinecke@intevation.de>
parents: 26
diff changeset
52 delete certList;
37fc66967517 Implement signature verification wiht polarssl
Andre Heinecke <aheinecke@intevation.de>
parents: 26
diff changeset
53 }
7
992c0ec57660 Add unit tests make CertificateList work.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
54
992c0ec57660 Add unit tests make CertificateList work.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
55 void CertListTest::testInvalidSig()
992c0ec57660 Add unit tests make CertificateList work.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
56 {
42
6e7ef7e95031 Some more tests and some more test data
Andre Heinecke <aheinecke@intevation.de>
parents: 31
diff changeset
57 const char *fnames[] = {"list-invalid-signed.txt",
6e7ef7e95031 Some more tests and some more test data
Andre Heinecke <aheinecke@intevation.de>
parents: 31
diff changeset
58 "list-valid-other-signature.txt",
6e7ef7e95031 Some more tests and some more test data
Andre Heinecke <aheinecke@intevation.de>
parents: 31
diff changeset
59 "list-valid-sha1-signature.txt",
6e7ef7e95031 Some more tests and some more test data
Andre Heinecke <aheinecke@intevation.de>
parents: 31
diff changeset
60 NULL};
6e7ef7e95031 Some more tests and some more test data
Andre Heinecke <aheinecke@intevation.de>
parents: 31
diff changeset
61 for (int i=0; fnames[i] != NULL; i++) {
6e7ef7e95031 Some more tests and some more test data
Andre Heinecke <aheinecke@intevation.de>
parents: 31
diff changeset
62 CertificateList *certList = testWithFile(fnames[i]);
6e7ef7e95031 Some more tests and some more test data
Andre Heinecke <aheinecke@intevation.de>
parents: 31
diff changeset
63 QCOMPARE (certList->getStatus(), InvalidSignature);
6e7ef7e95031 Some more tests and some more test data
Andre Heinecke <aheinecke@intevation.de>
parents: 31
diff changeset
64 delete certList;
6e7ef7e95031 Some more tests and some more test data
Andre Heinecke <aheinecke@intevation.de>
parents: 31
diff changeset
65 }
7
992c0ec57660 Add unit tests make CertificateList work.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
66 }
992c0ec57660 Add unit tests make CertificateList work.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
67
992c0ec57660 Add unit tests make CertificateList work.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
68 void verifyInvalidFile(const char *fName) {
992c0ec57660 Add unit tests make CertificateList work.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
69 CertificateList *certList = new CertificateList(fName);
992c0ec57660 Add unit tests make CertificateList work.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
70 QVERIFY (certList->getStatus() != Valid);
992c0ec57660 Add unit tests make CertificateList work.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
71 delete certList;
992c0ec57660 Add unit tests make CertificateList work.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
72 }
992c0ec57660 Add unit tests make CertificateList work.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
73
992c0ec57660 Add unit tests make CertificateList work.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
74 void CertListTest::testInvalidFileNames()
992c0ec57660 Add unit tests make CertificateList work.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
75 {
992c0ec57660 Add unit tests make CertificateList work.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
76 verifyInvalidFile("/dev/random");
992c0ec57660 Add unit tests make CertificateList work.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
77 verifyInvalidFile("/tmp/");
992c0ec57660 Add unit tests make CertificateList work.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
78 verifyInvalidFile(NULL);
992c0ec57660 Add unit tests make CertificateList work.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
79 verifyInvalidFile("ä");
992c0ec57660 Add unit tests make CertificateList work.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
80 verifyInvalidFile("💩 ");
992c0ec57660 Add unit tests make CertificateList work.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
81 }
992c0ec57660 Add unit tests make CertificateList work.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
82
42
6e7ef7e95031 Some more tests and some more test data
Andre Heinecke <aheinecke@intevation.de>
parents: 31
diff changeset
83 void CertListTest::testEmptyFile()
6e7ef7e95031 Some more tests and some more test data
Andre Heinecke <aheinecke@intevation.de>
parents: 31
diff changeset
84 {
6e7ef7e95031 Some more tests and some more test data
Andre Heinecke <aheinecke@intevation.de>
parents: 31
diff changeset
85 const char *fname = "empty_file";
6e7ef7e95031 Some more tests and some more test data
Andre Heinecke <aheinecke@intevation.de>
parents: 31
diff changeset
86 CertificateList *certList = testWithFile(fname);
6e7ef7e95031 Some more tests and some more test data
Andre Heinecke <aheinecke@intevation.de>
parents: 31
diff changeset
87 printf("Status :%i\n ",certList->getStatus());
6e7ef7e95031 Some more tests and some more test data
Andre Heinecke <aheinecke@intevation.de>
parents: 31
diff changeset
88 QCOMPARE (certList->getStatus(), UnknownError);
6e7ef7e95031 Some more tests and some more test data
Andre Heinecke <aheinecke@intevation.de>
parents: 31
diff changeset
89 delete certList;
6e7ef7e95031 Some more tests and some more test data
Andre Heinecke <aheinecke@intevation.de>
parents: 31
diff changeset
90 }
6e7ef7e95031 Some more tests and some more test data
Andre Heinecke <aheinecke@intevation.de>
parents: 31
diff changeset
91
7
992c0ec57660 Add unit tests make CertificateList work.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
92 void CertListTest::testGarbage()
992c0ec57660 Add unit tests make CertificateList work.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
93 {
50
a7b8cb29f1f6 Rename random files to garbage to clarify what they contain
Andre Heinecke <aheinecke@intevation.de>
parents: 42
diff changeset
94 const char *fnames[] = {"garbage_500KB",
42
6e7ef7e95031 Some more tests and some more test data
Andre Heinecke <aheinecke@intevation.de>
parents: 31
diff changeset
95 "list-with-null.txt",
6e7ef7e95031 Some more tests and some more test data
Andre Heinecke <aheinecke@intevation.de>
parents: 31
diff changeset
96 NULL};
6e7ef7e95031 Some more tests and some more test data
Andre Heinecke <aheinecke@intevation.de>
parents: 31
diff changeset
97 for (int i=0; fnames[i] != NULL; i++) {
6e7ef7e95031 Some more tests and some more test data
Andre Heinecke <aheinecke@intevation.de>
parents: 31
diff changeset
98 CertificateList *certList = testWithFile(fnames[i]);
6e7ef7e95031 Some more tests and some more test data
Andre Heinecke <aheinecke@intevation.de>
parents: 31
diff changeset
99 QCOMPARE (certList->getStatus(), InvalidFormat);
6e7ef7e95031 Some more tests and some more test data
Andre Heinecke <aheinecke@intevation.de>
parents: 31
diff changeset
100 delete certList;
6e7ef7e95031 Some more tests and some more test data
Andre Heinecke <aheinecke@intevation.de>
parents: 31
diff changeset
101 }
7
992c0ec57660 Add unit tests make CertificateList work.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
102 }
992c0ec57660 Add unit tests make CertificateList work.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
103
992c0ec57660 Add unit tests make CertificateList work.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
104 void CertListTest::testTooLarge()
992c0ec57660 Add unit tests make CertificateList work.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
105 {
50
a7b8cb29f1f6 Rename random files to garbage to clarify what they contain
Andre Heinecke <aheinecke@intevation.de>
parents: 42
diff changeset
106 const char *fname = "garbage_2MB";
7
992c0ec57660 Add unit tests make CertificateList work.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
107 CertificateList *certList = testWithFile(fname);
992c0ec57660 Add unit tests make CertificateList work.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
108 QCOMPARE (certList->getStatus(), TooLarge);
992c0ec57660 Add unit tests make CertificateList work.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
109 QVERIFY (!certList->isValid());
992c0ec57660 Add unit tests make CertificateList work.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
110 delete certList;
992c0ec57660 Add unit tests make CertificateList work.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
111 }
992c0ec57660 Add unit tests make CertificateList work.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
112
992c0ec57660 Add unit tests make CertificateList work.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
113 void CertListTest::benchmarkValid()
992c0ec57660 Add unit tests make CertificateList work.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
114 {
992c0ec57660 Add unit tests make CertificateList work.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
115 const char *fname = "list-valid-signed.txt";
992c0ec57660 Add unit tests make CertificateList work.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
116
9
2ad9a96518e3 Actually parse all elements in the list
Andre Heinecke <aheinecke@intevation.de>
parents: 7
diff changeset
117 QBENCHMARK_ONCE {
7
992c0ec57660 Add unit tests make CertificateList work.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
118 CertificateList *certList = testWithFile(fname);
992c0ec57660 Add unit tests make CertificateList work.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
119 delete certList;
992c0ec57660 Add unit tests make CertificateList work.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
120 }
992c0ec57660 Add unit tests make CertificateList work.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
121 }
992c0ec57660 Add unit tests make CertificateList work.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
122
992c0ec57660 Add unit tests make CertificateList work.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
123 CertificateList* CertListTest::testWithFile(const char *filename)
992c0ec57660 Add unit tests make CertificateList work.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
124 {
992c0ec57660 Add unit tests make CertificateList work.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
125 QDir dataDir = QDir(SOURCE_DIR"/data/");
992c0ec57660 Add unit tests make CertificateList work.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
126 QString fileName = dataDir.absoluteFilePath(filename);
992c0ec57660 Add unit tests make CertificateList work.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
127 return new CertificateList(fileName.toLocal8Bit().data());
992c0ec57660 Add unit tests make CertificateList work.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
128 }
992c0ec57660 Add unit tests make CertificateList work.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
129
992c0ec57660 Add unit tests make CertificateList work.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
130 int main( int argc, char **argv )
992c0ec57660 Add unit tests make CertificateList work.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
131 {
992c0ec57660 Add unit tests make CertificateList work.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
132 CertListTest tc;
992c0ec57660 Add unit tests make CertificateList work.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
133 return QTest::qExec( &tc, argc, argv );
992c0ec57660 Add unit tests make CertificateList work.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
134 }
992c0ec57660 Add unit tests make CertificateList work.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
135
992c0ec57660 Add unit tests make CertificateList work.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
136

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