1 /*
2     SPDX-FileCopyrightText: 2010 Joris Guisson <joris.guisson@gmail.com>
3 
4     SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #ifndef DUMMYTORRENTCREATOR_H
8 #define DUMMYTORRENTCREATOR_H
9 
10 #include <QMap>
11 #include <QStringList>
12 #include <QTemporaryDir>
13 #include <util/constants.h>
14 
15 /**
16     Utility class for tests, to create dummy torrents (and their files)
17     It will create the torrent and the data in a temporary dir with the following layout:
18 
19     tmpdir
20      |-> torrent // This is the torrent
21      |-> data
22           |-> filename.ext // for a single file torrent
23           or
24           |-> toplevel_dir // for a multifile torrent
25 
26     The created files will be filled with random data
27 */
28 class DummyTorrentCreator
29 {
30 public:
31     DummyTorrentCreator();
32     virtual ~DummyTorrentCreator();
33 
34     /// Set the tracker URL's (by default http://localhost:5000/announce is used)
setTrackers(const QStringList & urls)35     void setTrackers(const QStringList &urls)
36     {
37         trackers = urls;
38     }
39 
40     /**
41         Create a single file torrent
42         @param size The size of the torrent
43         @param filename The name of the file
44     */
45     bool createSingleFileTorrent(bt::Uint64 size, const QString &filename);
46 
47     /**
48         Create a multi file torrent
49         @param files Map of files in the torrent, and their respective sizes
50         @param name The name of the torrent (will be used for the toplevel directory name)
51     */
52     bool createMultiFileTorrent(const QMap<QString, bt::Uint64> &files, const QString &name);
53 
54     /// Get the full path of the torrent file
55     QString torrentPath() const;
56 
57     /// Get the full path of the single data file or toplevel directory (in case of multifile torrents)
58     QString dataPath() const;
59 
60     /// Get the temp path used
tempPath()61     QString tempPath() const
62     {
63         return tmpdir.path();
64     }
65 
66 private:
67     bool createRandomFile(const QString &path, bt::Uint64 size);
68 
69 private:
70     QTemporaryDir tmpdir;
71     QString dpath;
72 
73     QStringList trackers;
74     bt::Uint32 chunk_size;
75 };
76 
77 #endif // DUMMYTORRENTCREATOR_H
78