source: trunk/libtransmission/makemeta-test.c @ 14369

Last change on this file since 14369 was 14369, checked in by mikedld, 8 years ago

Add missing svn:keywords property to recently (and not so recently) added files

  • Property svn:keywords set to Date Rev Author Id
File size: 9.2 KB
Line 
1/*
2 * This file Copyright (C) 2013-2014 Mnemosyne LLC
3 *
4 * It may be used under the GNU GPL versions 2 or 3
5 * or any future license endorsed by Mnemosyne LLC.
6 *
7 * $Id: makemeta-test.c 14369 2014-12-10 18:58:12Z mikedld $
8 */
9
10#include "libtransmission-test.h"
11
12#include "transmission.h"
13#include "crypto-utils.h"
14#include "file.h"
15#include "makemeta.h"
16
17#include <unistd.h> /* sync() */
18
19#include <stdlib.h> /* mktemp() */
20#include <string.h> /* strlen() */
21
22static int
23test_single_file_impl (const tr_tracker_info * trackers,
24                       const size_t            trackerCount,
25                       const void            * payload,
26                       const size_t            payloadSize,
27                       const char            * comment,
28                       bool                    isPrivate)
29{
30  char* sandbox;
31  char* input_file;
32  char* torrent_file;
33  tr_metainfo_builder* builder;
34  tr_ctor * ctor;
35  tr_parse_result parse_result;
36  tr_info inf;
37  char * tmpstr;
38
39  /* set up our local test sandbox */
40  sandbox = libtest_sandbox_create();
41
42  /* create a single input file */
43  input_file = tr_buildPath (sandbox, "test.XXXXXX", NULL);
44  libtest_create_tmpfile_with_contents (input_file, payload, payloadSize);
45  builder = tr_metaInfoBuilderCreate (input_file);
46  check_streq (input_file, builder->top);
47  check_int_eq (1, builder->fileCount);
48  check_streq (input_file, builder->files[0].filename);
49  check_int_eq (payloadSize, builder->files[0].size);
50  check_int_eq (payloadSize, builder->totalSize);
51  check (!builder->isFolder);
52  check (!builder->abortFlag);
53
54  /* have tr_makeMetaInfo() build the .torrent file */
55  torrent_file = tr_strdup_printf ("%s.torrent", input_file);
56  tr_makeMetaInfo (builder, torrent_file, trackers, trackerCount, comment, isPrivate);
57  check (isPrivate == builder->isPrivate);
58  check_streq (torrent_file, builder->outputFile);
59  check_streq (comment, builder->comment);
60  check_int_eq (trackerCount, builder->trackerCount);
61  while (!builder->isDone)
62    tr_wait_msec (100);
63
64  /* now let's check our work: parse the  .torrent file */
65  ctor = tr_ctorNew (NULL);
66  sync ();
67  tr_ctorSetMetainfoFromFile (ctor, torrent_file);
68  parse_result = tr_torrentParse (ctor, &inf);
69  check_int_eq (TR_PARSE_OK, parse_result);
70
71  /* quick check of some of the parsed metainfo */
72  check_int_eq (payloadSize, inf.totalSize);
73  tmpstr = tr_sys_path_basename (input_file, NULL);
74  check_streq (tmpstr, inf.name);
75  tr_free (tmpstr);
76  check_streq (comment, inf.comment);
77  check_int_eq (1, inf.fileCount);
78  check_int_eq (isPrivate, inf.isPrivate);
79  check (!inf.isFolder);
80  check_int_eq (trackerCount, inf.trackerCount);
81
82  /* cleanup */
83  tr_free (torrent_file);
84  tr_free (input_file);
85  tr_ctorFree (ctor);
86  tr_metainfoFree (&inf);
87  tr_metaInfoBuilderFree (builder);
88  libtest_sandbox_destroy (sandbox);
89  tr_free (sandbox);
90  return 0;
91}
92
93static int
94test_single_file (void)
95{
96  tr_tracker_info trackers[16];
97  size_t trackerCount;
98  bool isPrivate;
99  const char * comment;
100  const char * payload;
101  size_t payloadSize;
102
103  trackerCount = 0;
104  trackers[trackerCount].tier = trackerCount;
105  trackers[trackerCount].announce = (char*) "udp://tracker.openbittorrent.com:80";
106  ++trackerCount;
107  trackers[trackerCount].tier = trackerCount;
108  trackers[trackerCount].announce = (char*) "udp://tracker.publicbt.com:80";
109  ++trackerCount;
110  payload = "Hello, World!\n";
111  payloadSize = strlen(payload);
112  comment = "This is the comment";
113  isPrivate = false;
114  test_single_file_impl (trackers, trackerCount, payload, payloadSize, comment, isPrivate);
115
116  return 0;
117}
118
119
120static int
121test_single_directory_impl (const tr_tracker_info * trackers,
122                            const size_t            trackerCount,
123                            const void           ** payloads,
124                            const size_t          * payloadSizes,
125                            const size_t            payloadCount,
126                            const char            * comment,
127                            const bool              isPrivate)
128{
129  char* sandbox;
130  char* torrent_file;
131  tr_metainfo_builder* builder;
132  tr_ctor * ctor;
133  tr_parse_result parse_result;
134  tr_info inf;
135  char * top;
136  char ** files;
137  size_t totalSize;
138  size_t i;
139  char* tmpstr;
140 
141
142  /* set up our local test sandbox */
143  sandbox = libtest_sandbox_create();
144
145  /* create the top temp directory */
146  top = tr_buildPath (sandbox, "folder.XXXXXX", NULL);
147  tr_sys_dir_create_temp (top, NULL);
148
149  /* build the payload files that go into the top temp directory */
150  files = tr_new (char*, payloadCount);
151  totalSize = 0;
152  for (i=0; i<payloadCount; i++)
153    {
154      char tmpl[16];
155      tr_snprintf (tmpl, sizeof(tmpl), "file.%04zu%s", i, "XXXXXX");
156      files[i] = tr_buildPath (top, tmpl, NULL);
157      libtest_create_tmpfile_with_contents (files[i], payloads[i], payloadSizes[i]);
158      totalSize += payloadSizes[i];
159    }
160  sync ();
161
162  /* init the builder */
163  builder = tr_metaInfoBuilderCreate (top);
164  check (!builder->abortFlag);
165  check_streq (top, builder->top);
166  check_int_eq (payloadCount, builder->fileCount);
167  check_int_eq (totalSize, builder->totalSize);
168  check (builder->isFolder);
169  for (i=0; i<builder->fileCount; i++)
170    {
171      check_streq (files[i], builder->files[i].filename);
172      check_int_eq (payloadSizes[i], builder->files[i].size);
173    }
174
175  /* call tr_makeMetaInfo() to build the .torrent file */
176  torrent_file = tr_strdup_printf ("%s.torrent", top);
177  tr_makeMetaInfo (builder, torrent_file, trackers, trackerCount, comment, isPrivate);
178  check (isPrivate == builder->isPrivate);
179  check_streq (torrent_file, builder->outputFile);
180  check_streq (comment, builder->comment);
181  check_int_eq (trackerCount, builder->trackerCount);
182  while (!builder->isDone)
183    tr_wait_msec (100);
184
185  /* now let's check our work: parse the  .torrent file */
186  ctor = tr_ctorNew (NULL);
187  sync ();
188  tr_ctorSetMetainfoFromFile (ctor, torrent_file);
189  parse_result = tr_torrentParse (ctor, &inf);
190  check_int_eq (TR_PARSE_OK, parse_result);
191
192  /* quick check of some of the parsed metainfo */
193  check_int_eq (totalSize, inf.totalSize);
194  tmpstr = tr_sys_path_basename (top, NULL);
195  check_streq (tmpstr, inf.name);
196  tr_free (tmpstr);
197  check_streq (comment, inf.comment);
198  check_int_eq (payloadCount, inf.fileCount);
199  check_int_eq (isPrivate, inf.isPrivate);
200  check_int_eq (builder->isFolder, inf.isFolder);
201  check_int_eq (trackerCount, inf.trackerCount);
202
203  /* cleanup */
204  tr_free (torrent_file);
205  tr_ctorFree (ctor);
206  tr_metainfoFree (&inf);
207  tr_metaInfoBuilderFree (builder);
208  for (i=0; i<payloadCount; i++)
209    tr_free (files[i]);
210  tr_free (files);
211  libtest_sandbox_destroy (sandbox);
212  tr_free (sandbox);
213  tr_free (top);
214
215  return 0;
216}
217 
218static int
219test_single_directory_random_payload_impl (const tr_tracker_info * trackers,
220                                           const size_t            trackerCount,
221                                           const size_t            maxFileCount,
222                                           const size_t            maxFileSize,
223                                           const char            * comment,
224                                           const bool              isPrivate)
225{
226  size_t i;
227  void ** payloads;
228  size_t * payloadSizes;
229  size_t payloadCount;
230
231  /* build random payloads */
232  payloadCount = 1 + tr_rand_int_weak (maxFileCount);
233  payloads = tr_new0 (void*, payloadCount);
234  payloadSizes = tr_new0 (size_t, payloadCount);
235  for (i=0; i<payloadCount; i++)
236    {
237      const size_t n = 1 + tr_rand_int_weak (maxFileSize);
238      payloads[i] = tr_new (char, n);
239      tr_rand_buffer (payloads[i], n);
240      payloadSizes[i] = n;
241    }
242
243  /* run the test */
244  test_single_directory_impl (trackers,
245                              trackerCount,
246                              (const void**) payloads,
247                              payloadSizes,
248                              payloadCount,
249                              comment,
250                              isPrivate);
251
252  /* cleanup */
253  for (i=0; i<payloadCount; i++)
254    tr_free (payloads[i]);
255  tr_free (payloads);
256  tr_free (payloadSizes);
257
258  return 0;
259}
260
261#define DEFAULT_MAX_FILE_COUNT 16
262#define DEFAULT_MAX_FILE_SIZE 1024
263
264static int
265test_single_directory_random_payload (void)
266{
267  tr_tracker_info trackers[16];
268  size_t trackerCount;
269  bool isPrivate;
270  const char * comment;
271  size_t i;
272
273  trackerCount = 0;
274  trackers[trackerCount].tier = trackerCount;
275  trackers[trackerCount].announce = (char*) "udp://tracker.openbittorrent.com:80";
276  ++trackerCount;
277  trackers[trackerCount].tier = trackerCount;
278  trackers[trackerCount].announce = (char*) "udp://tracker.publicbt.com:80";
279  ++trackerCount;
280  comment = "This is the comment";
281  isPrivate = false;
282
283  for (i=0; i<10; i++)
284    {
285      test_single_directory_random_payload_impl (trackers,
286                                                 trackerCount,
287                                                 DEFAULT_MAX_FILE_COUNT,
288                                                 DEFAULT_MAX_FILE_SIZE,
289                                                 comment,
290                                                 isPrivate);
291    }
292
293  return 0;
294}
295
296int
297main (void)
298{
299  const testFunc tests[] = { test_single_file,
300                             test_single_directory_random_payload };
301
302  return runTests (tests, NUM_TESTS (tests));
303}
Note: See TracBrowser for help on using the repository browser.