2
* Copyright 2016 Canonical Ltd.
4
* This program is free software: you can redistribute it and/or modify it
5
* under the terms of the GNU General Public License version 3, as published
6
* by the Free Software Foundation.
8
* This program is distributed in the hope that it will be useful, but
9
* WITHOUT ANY WARRANTY; without even the implied warranties of
10
* MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11
* PURPOSE. See the GNU General Public License for more details.
13
* You should have received a copy of the GNU General Public License along
14
* with this program. If not, see <http://www.gnu.org/licenses/>.
17
* Charles Kerr <charles.kerr@canonical.com>
20
#include "tests/utils/dummy-file.h"
22
#include <QCryptographicHash>
26
#include <QTemporaryFile>
29
DummyFile::create(const QDir& dir, qint64 filesize)
31
// NB we want to exercise long filenames, but this cutoff length is arbitrary
32
static constexpr int MAX_BASENAME_LEN {200};
33
int filename_len = qrand() % MAX_BASENAME_LEN;
35
for (int i=0; i<filename_len; ++i)
36
basename += ('a' + char(qrand() % ('z'-'a')));
37
basename += QStringLiteral("-XXXXXX");
38
auto template_name = dir.absoluteFilePath(basename);
40
// fill the file with noise
41
QTemporaryFile f(template_name);
42
f.setAutoRemove(false);
44
static constexpr qint64 max_step = 1024;
46
qint64 left = filesize;
49
int this_step = std::min(max_step, left);
50
for(int i=0; i<this_step; ++i)
51
buf[i] = 'a' + char(qrand() % ('z'-'a'));
52
f.write(buf, this_step);
59
QCryptographicHash hash(QCryptographicHash::Sha1);
61
const auto checksum = hash.result();
65
info.info = QFileInfo(f.fileName());
66
info.checksum = checksum;