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