aheinecke@404: /* Copyright (C) 2014 by Bundesamt für Sicherheit in der Informationstechnik
aheinecke@404:  * Software engineering by Intevation GmbH
aheinecke@404:  *
aheinecke@404:  * This file is Free Software under the GNU GPL (v>=2)
aheinecke@404:  * and comes with ABSOLUTELY NO WARRANTY!
aheinecke@404:  * See LICENSE.txt for details.
aheinecke@404:  */
aheinecke@240: #include <QString>
aheinecke@240: #include <QTemporaryFile>
aheinecke@240: #include <QDebug>
aheinecke@240: #include "common.h"
aheinecke@240: 
aheinecke@240: QString getRandomDataFile(size_t size, const QDir &inDir)
aheinecke@240: {
aheinecke@240:     QTemporaryFile *tmpfile;
aheinecke@240:     if (inDir != QDir()) {
aheinecke@240:         tmpfile = new QTemporaryFile(inDir.path() + "/downloadertest");
aheinecke@240:     } else {
aheinecke@240:         tmpfile = new QTemporaryFile();
aheinecke@240:     }
aheinecke@240:     tmpfile->setAutoRemove(false);
aheinecke@240:     tmpfile->open();
aheinecke@240:     size_t bufsize = 1024 * 1024;
aheinecke@240:     if (bufsize > size) {
aheinecke@240:         bufsize = size;
aheinecke@240:     }
aheinecke@240:     char buf[bufsize];
aheinecke@240: 
aheinecke@240:     for (size_t i = 0; i < bufsize; i++) {
aheinecke@240:         buf[i] = (char) qrand() % 255;
aheinecke@240:     }
aheinecke@240: 
aheinecke@240:     size_t bytesWritten=0;
aheinecke@240:     int retval = 0;
aheinecke@240:     do {
aheinecke@240:         size_t toWrite = size - bytesWritten;
aheinecke@240:         if (toWrite < bufsize) {
aheinecke@240:             retval = tmpfile->write(buf, toWrite);
aheinecke@240:         } else {
aheinecke@240:             retval = tmpfile->write(buf, bufsize);
aheinecke@240:         }
aheinecke@240:         bytesWritten += retval;
aheinecke@240:     } while (retval != -1 && bytesWritten < size);
aheinecke@240: 
aheinecke@240:     tmpfile->close();
aheinecke@240:     QString ret = tmpfile->fileName();
aheinecke@240:     delete tmpfile;
aheinecke@240:     return ret;
aheinecke@240: }