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

Last change on this file since 14289 was 14289, checked in by jordan, 8 years ago

add tr_makeMetaInfo() unit test for creating a single-file torrent

File size: 3.5 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: utils-test.c 14266 2014-04-27 23:10:01Z jordan $
8 */
9
10#include "libtransmission-test.h"
11
12#include "transmission.h"
13#include "makemeta.h"
14
15#include <stdlib.h> /* mktemp() */
16#include <string.h> /* strlen() */
17
18static int
19test_single_file_impl (const tr_tracker_info * trackers,
20                       const size_t            trackerCount,
21                       const void            * payload,
22                       const size_t            payloadSize,
23                       const char            * comment,
24                       bool                    isPrivate)
25{
26  char* sandbox;
27  char* input_file;
28  char* torrent_file;
29  tr_metainfo_builder* builder;
30  tr_ctor * ctor;
31  tr_parse_result parse_result;
32  tr_info inf;
33  char * tmpstr;
34
35  /* set up our local test sandbox */
36  sandbox = libtest_sandbox_create();
37
38  /* create a single input file */
39  input_file = tr_buildPath (sandbox, "test.XXXXXX", NULL);
40  libtest_create_tmpfile_with_contents (input_file, payload, payloadSize);
41  builder = tr_metaInfoBuilderCreate (input_file);
42  check_streq (input_file, builder->top);
43  check_int_eq (1, builder->fileCount);
44  check_streq (input_file, builder->files[0].filename);
45  check_int_eq (payloadSize, builder->files[0].size);
46  check_int_eq (payloadSize, builder->totalSize);
47  check (builder->isSingleFile);
48  check (!builder->abortFlag);
49
50  /* have tr_makeMetaInfo() build the .torrent file */
51  torrent_file = tr_strdup_printf ("%s.torrent", input_file);
52  tr_makeMetaInfo (builder, torrent_file, trackers, trackerCount, comment, isPrivate);
53  check (isPrivate == builder->isPrivate);
54  check_streq (torrent_file, builder->outputFile);
55  check_streq (comment, builder->comment);
56  check_int_eq (trackerCount, builder->trackerCount);
57  while (!builder->isDone)
58    tr_wait_msec (100);
59
60  /* now let's check our work: parse the  .torrent file */
61  ctor = tr_ctorNew (NULL);
62  tr_ctorSetMetainfoFromFile (ctor, torrent_file);
63  parse_result = tr_torrentParse (ctor, &inf);
64  check_int_eq (TR_PARSE_OK, parse_result);
65
66  /* quick check of some of the parsed metainfo */
67  check_int_eq (payloadSize, inf.totalSize);
68  tmpstr = tr_basename(input_file);
69  check_streq (tmpstr, inf.name);
70  tr_free (tmpstr);
71  check_streq (comment, inf.comment);
72  check_int_eq (1, inf.fileCount);
73  check_int_eq (isPrivate, inf.isPrivate);
74  check (!inf.isMultifile);
75  check_int_eq (trackerCount, inf.trackerCount);
76
77  /* cleanup */
78  tr_free (torrent_file);
79  tr_free (input_file);
80  tr_ctorFree (ctor);
81  tr_metainfoFree (&inf);
82  tr_metaInfoBuilderFree (builder);
83  libtest_sandbox_destroy (sandbox);
84  tr_free (sandbox);
85  return 0;
86}
87
88static int
89test_single_file (void)
90{
91  tr_tracker_info trackers[16];
92  size_t trackerCount;
93  bool isPrivate;
94  const char * comment;
95  const char * payload;
96  size_t payloadSize;
97
98  trackerCount = 0;
99  trackers[trackerCount].tier = trackerCount;
100  trackers[trackerCount].announce = (char*) "udp://tracker.openbittorrent.com:80";
101  ++trackerCount;
102  trackers[trackerCount].tier = trackerCount;
103  trackers[trackerCount].announce = (char*) "udp://tracker.publicbt.com:80";
104  ++trackerCount;
105  payload = "Hello, World!\n";
106  payloadSize = strlen(payload);
107  comment = "This is the comment";
108  isPrivate = false;
109  test_single_file_impl (trackers, trackerCount, payload, payloadSize, comment, isPrivate);
110
111  return 0;
112}
113
114int
115main (void)
116{
117  const testFunc tests[] = { test_single_file };
118
119  return runTests (tests, NUM_TESTS (tests));
120}
121
Note: See TracBrowser for help on using the repository browser.