annotate ui/tests/common.cpp @ 245:fbd74e2370de

Error out, when base64 decode results in empty data.
author Sascha Wilde <wilde@intevation.de>
date Fri, 28 Mar 2014 19:30:35 +0100
parents c05e126b0b9e
children 17e1c8f37d72
rev   line source
240
c05e126b0b9e Fix downloadertest and factor out generating file input.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
1 #include <QString>
c05e126b0b9e Fix downloadertest and factor out generating file input.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
2 #include <QTemporaryFile>
c05e126b0b9e Fix downloadertest and factor out generating file input.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
3 #include <QDebug>
c05e126b0b9e Fix downloadertest and factor out generating file input.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
4 #include "common.h"
c05e126b0b9e Fix downloadertest and factor out generating file input.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
5
c05e126b0b9e Fix downloadertest and factor out generating file input.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
6 QString getRandomDataFile(size_t size, const QDir &inDir)
c05e126b0b9e Fix downloadertest and factor out generating file input.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
7 {
c05e126b0b9e Fix downloadertest and factor out generating file input.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
8 QTemporaryFile *tmpfile;
c05e126b0b9e Fix downloadertest and factor out generating file input.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
9 if (inDir != QDir()) {
c05e126b0b9e Fix downloadertest and factor out generating file input.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
10 tmpfile = new QTemporaryFile(inDir.path() + "/downloadertest");
c05e126b0b9e Fix downloadertest and factor out generating file input.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
11 } else {
c05e126b0b9e Fix downloadertest and factor out generating file input.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
12 tmpfile = new QTemporaryFile();
c05e126b0b9e Fix downloadertest and factor out generating file input.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
13 }
c05e126b0b9e Fix downloadertest and factor out generating file input.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
14 tmpfile->setAutoRemove(false);
c05e126b0b9e Fix downloadertest and factor out generating file input.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
15 tmpfile->open();
c05e126b0b9e Fix downloadertest and factor out generating file input.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
16 size_t bufsize = 1024 * 1024;
c05e126b0b9e Fix downloadertest and factor out generating file input.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
17 if (bufsize > size) {
c05e126b0b9e Fix downloadertest and factor out generating file input.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
18 bufsize = size;
c05e126b0b9e Fix downloadertest and factor out generating file input.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
19 }
c05e126b0b9e Fix downloadertest and factor out generating file input.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
20 char buf[bufsize];
c05e126b0b9e Fix downloadertest and factor out generating file input.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
21
c05e126b0b9e Fix downloadertest and factor out generating file input.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
22 for (size_t i = 0; i < bufsize; i++) {
c05e126b0b9e Fix downloadertest and factor out generating file input.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
23 buf[i] = (char) qrand() % 255;
c05e126b0b9e Fix downloadertest and factor out generating file input.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
24 }
c05e126b0b9e Fix downloadertest and factor out generating file input.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
25
c05e126b0b9e Fix downloadertest and factor out generating file input.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
26 size_t bytesWritten=0;
c05e126b0b9e Fix downloadertest and factor out generating file input.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
27 int retval = 0;
c05e126b0b9e Fix downloadertest and factor out generating file input.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
28 do {
c05e126b0b9e Fix downloadertest and factor out generating file input.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
29 size_t toWrite = size - bytesWritten;
c05e126b0b9e Fix downloadertest and factor out generating file input.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
30 if (toWrite < bufsize) {
c05e126b0b9e Fix downloadertest and factor out generating file input.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
31 retval = tmpfile->write(buf, toWrite);
c05e126b0b9e Fix downloadertest and factor out generating file input.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
32 } else {
c05e126b0b9e Fix downloadertest and factor out generating file input.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
33 retval = tmpfile->write(buf, bufsize);
c05e126b0b9e Fix downloadertest and factor out generating file input.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
34 }
c05e126b0b9e Fix downloadertest and factor out generating file input.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
35 bytesWritten += retval;
c05e126b0b9e Fix downloadertest and factor out generating file input.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
36 } while (retval != -1 && bytesWritten < size);
c05e126b0b9e Fix downloadertest and factor out generating file input.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
37
c05e126b0b9e Fix downloadertest and factor out generating file input.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
38 tmpfile->close();
c05e126b0b9e Fix downloadertest and factor out generating file input.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
39 QString ret = tmpfile->fileName();
c05e126b0b9e Fix downloadertest and factor out generating file input.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
40 delete tmpfile;
c05e126b0b9e Fix downloadertest and factor out generating file input.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
41 return ret;
c05e126b0b9e Fix downloadertest and factor out generating file input.
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
42 }

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