Mercurial > trustbridge
comparison ui/tests/certlistparsertest.cpp @ 202:0861069fd6d0
Fix testing for handling garbage input and generate the data ad hoc.
author | Andre Heinecke <andre.heinecke@intevation.de> |
---|---|
date | Wed, 26 Mar 2014 12:52:53 +0100 |
parents | b0b1375dfd40 |
children | 60c5df8e7980 |
comparison
equal
deleted
inserted
replaced
201:45f6b62f91e7 | 202:0861069fd6d0 |
---|---|
52 QVERIFY(remoLines.size() == remoList.size()); | 52 QVERIFY(remoLines.size() == remoList.size()); |
53 | 53 |
54 Certificate cert; | 54 Certificate cert; |
55 QVERIFY(!cert.isValid()); | 55 QVERIFY(!cert.isValid()); |
56 | 56 |
57 CertificateList *certList2= testWithFile(fname); | |
58 const QList<Certificate> instList2 = certList2->getInstallCertificates(); | |
59 const QList<Certificate> remoList2 = certList2->getRemoveCertificates(); | |
60 QVERIFY(instLines.size() == instList2.size()); | |
61 QVERIFY(remoLines.size() == remoList2.size()); | |
57 | 62 |
58 delete certList; | 63 delete certList; |
59 } | 64 } |
60 | 65 |
61 void CertListTest::testInvalidSig() | 66 void CertListTest::testInvalidSig() |
95 delete certList; | 100 delete certList; |
96 } | 101 } |
97 | 102 |
98 void CertListTest::testGarbage() | 103 void CertListTest::testGarbage() |
99 { | 104 { |
100 const char *fnames[] = {"garbage_500KB", | 105 const char *fname = "list-with-null.txt"; |
101 "list-with-null.txt", | 106 QString fname2 = getRandomDataFile(200); |
102 NULL}; | 107 CertificateList *certList = testWithFile(fname); |
103 for (int i=0; fnames[i] != NULL; i++) { | 108 QCOMPARE (certList->getStatus(), InvalidFormat); |
104 CertificateList *certList = testWithFile(fnames[i]); | 109 delete certList; |
105 QCOMPARE (certList->getStatus(), InvalidFormat); | 110 certList = testWithFile(fname2.toLocal8Bit().constData()); |
106 delete certList; | 111 QVERIFY(QFile::remove(fname2)); |
112 QCOMPARE (certList->getStatus(), InvalidFormat); | |
113 delete certList; | |
114 } | |
115 | |
116 QString CertListTest::getRandomDataFile(size_t size) | |
117 { | |
118 QTemporaryFile tmpfile; | |
119 tmpfile.setAutoRemove(false); | |
120 tmpfile.open(); | |
121 size_t bufsize = 1024 * 1024; | |
122 if (bufsize > size) { | |
123 bufsize = size; | |
107 } | 124 } |
125 char buf[bufsize]; | |
126 | |
127 for (size_t i = 0; i < bufsize; i++) { | |
128 buf[i] = (char) qrand() % 255; | |
129 } | |
130 | |
131 size_t bytesWritten=0; | |
132 int retval = 0; | |
133 do { | |
134 size_t toWrite = size - bytesWritten; | |
135 if (toWrite < bufsize) { | |
136 retval = tmpfile.write(buf, toWrite); | |
137 } else { | |
138 retval = tmpfile.write(buf, bufsize); | |
139 } | |
140 bytesWritten += retval; | |
141 } while (retval != -1 && bytesWritten < size); | |
142 | |
143 tmpfile.close(); | |
144 return tmpfile.fileName(); | |
108 } | 145 } |
109 | 146 |
110 void CertListTest::testTooLarge() | 147 void CertListTest::testTooLarge() |
111 { | 148 { |
112 const char *fname = "garbage_2MB"; | 149 QString fname = getRandomDataFile(MAX_LINE_LENGTH * MAX_LINES + 1); |
113 CertificateList *certList = testWithFile(fname); | 150 CertificateList *certList = testWithFile(fname.toLocal8Bit().constData()); |
114 QCOMPARE (certList->getStatus(), TooLarge); | 151 QVERIFY(QFile::remove(fname)); |
115 QVERIFY (!certList->isValid()); | 152 QCOMPARE(certList->getStatus(), TooLarge); |
153 QVERIFY(!certList->isValid()); | |
116 delete certList; | 154 delete certList; |
117 } | 155 } |
118 | 156 |
119 void CertListTest::benchmarkValid() | 157 void CertListTest::benchmarkValid() |
120 { | 158 { |