source: trunk/libtransmission/libtransmission-test.c @ 13908

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

fix oops

File size: 9.3 KB
Line 
1#include <assert.h>
2#include <stdio.h>
3
4#include "transmission.h"
5#include "platform.h" /* TR_PATH_DELIMETER */
6#include "torrent.h"
7#include "libtransmission-test.h"
8
9bool verbose = false;
10
11int current_test = 0;
12
13bool
14should_print (bool pass)
15{
16  if (!pass)
17    return true;
18
19  if (verbose)
20    return true;
21
22  return false;
23#ifdef VERBOSE
24  return true;
25#else
26  return false;
27#endif
28}
29
30bool
31check_condition_impl (const char * file, int line, bool condition)
32{
33  const bool pass = condition;
34
35  if (should_print (pass))
36    fprintf (stderr, "%s %s:%d\n", pass?"PASS":"FAIL", file, line);
37
38  return pass;
39}
40
41bool
42check_streq_impl (const char * file, int line, const char * expected, const char * actual)
43{
44  const bool pass = !tr_strcmp0 (expected, actual);
45
46  if (should_print (pass)) {
47    if (pass)
48      fprintf (stderr, "PASS %s:%d\n", file, line);
49    else
50      fprintf (stderr, "FAIL %s:%d, expected \"%s\", got \"%s\"\n", file, line, expected?expected:" (null)", actual?actual:" (null)");
51  }
52
53  return pass;
54}
55
56bool
57check_int_eq_impl (const char * file, int line, int64_t expected, int64_t actual)
58{
59  const bool pass = expected == actual;
60
61  if (should_print (pass)) {
62    if (pass)
63      fprintf (stderr, "PASS %s:%d\n", file, line);
64    else
65      fprintf (stderr, "FAIL %s:%d, expected \"%"PRId64"\", got \"%"PRId64"\"\n", file, line, expected, actual);
66  }
67
68  return pass;
69}
70
71bool
72check_ptr_eq_impl (const char * file, int line, const void * expected, const void * actual)
73{
74  const bool pass = expected == actual;
75
76  if (should_print (pass)) {
77    if (pass)
78      fprintf (stderr, "PASS %s:%d\n", file, line);
79    else
80      fprintf (stderr, "FAIL %s:%d, expected \"%p\", got \"%p\"\n", file, line, expected, actual);
81  }
82
83  return pass;
84}
85
86int
87runTests (const testFunc * const tests, int numTests)
88{
89  int i;
90  int ret;
91
92  (void) current_test; /* Use test even if we don't have any tests to run */
93
94  for (i=0; i<numTests; i++)
95    if ((ret = (*tests[i])()))
96      return ret;
97
98  return 0; /* All tests passed */
99}
100
101/***
102****
103***/
104
105#include <sys/types.h> /* stat(), opendir() */
106#include <sys/stat.h> /* stat() */
107#include <dirent.h> /* opendir() */
108#include <unistd.h> /* getcwd() */
109
110#include <errno.h>
111#include <string.h> /* strcmp() */
112
113#include "variant.h"
114
115tr_session * session = NULL;
116char * sandbox = NULL;
117char * downloadDir = NULL;
118char * blocklistDir = NULL;
119
120static char*
121tr_getcwd (void)
122{
123  char * result;
124  char buf[2048];
125
126#ifdef WIN32
127  result = _getcwd (buf, sizeof (buf));
128#else
129  result = getcwd (buf, sizeof (buf));
130#endif
131
132  if (result == NULL)
133    {
134      fprintf (stderr, "getcwd error: \"%s\"", tr_strerror (errno));
135      *buf = '\0';
136    }
137
138  return tr_strdup (buf);
139}
140
141static void
142rm_rf (const char * killme)
143{
144  struct stat sb;
145
146  if (!stat (killme, &sb))
147    {
148      DIR * odir;
149
150      if (S_ISDIR (sb.st_mode) && ((odir = opendir (killme))))
151        {
152          struct dirent *d;
153          for (d = readdir(odir); d != NULL; d=readdir(odir))
154            {
155              if (d->d_name && strcmp(d->d_name,".") && strcmp(d->d_name,".."))
156                {
157                  char * tmp = tr_buildPath (killme, d->d_name, NULL);
158                  rm_rf (tmp);
159                  tr_free (tmp);
160                }
161            }
162          closedir (odir);
163        }
164
165      if (verbose)
166        fprintf (stderr, "cleanup: removing %s\n", killme);
167
168      remove (killme);
169    }
170}
171
172#define MEM_K 1024
173#define MEM_B_STR   "B"
174#define MEM_K_STR "KiB"
175#define MEM_M_STR "MiB"
176#define MEM_G_STR "GiB"
177#define MEM_T_STR "TiB"
178
179#define DISK_K 1000
180#define DISK_B_STR  "B"
181#define DISK_K_STR "kB"
182#define DISK_M_STR "MB"
183#define DISK_G_STR "GB"
184#define DISK_T_STR "TB"
185
186#define SPEED_K 1000
187#define SPEED_B_STR  "B/s"
188#define SPEED_K_STR "kB/s"
189#define SPEED_M_STR "MB/s"
190#define SPEED_G_STR "GB/s"
191#define SPEED_T_STR "TB/s"
192
193void
194libtransmission_test_session_init_formatters (void)
195{
196  tr_formatter_mem_init (MEM_K, MEM_K_STR, MEM_M_STR, MEM_G_STR, MEM_T_STR);
197  tr_formatter_size_init (DISK_K,DISK_K_STR, DISK_M_STR, DISK_G_STR, DISK_T_STR);
198  tr_formatter_speed_init (SPEED_K, SPEED_K_STR, SPEED_M_STR, SPEED_G_STR, SPEED_T_STR);
199}
200
201void
202libtransmission_test_session_init_sandbox (void)
203{
204  char * cwd;
205
206  /* create a sandbox for the test session */
207  cwd = tr_getcwd ();
208  sandbox = tr_buildPath (cwd, "sandbox-XXXXXX", NULL);
209  tr_mkdtemp (sandbox);
210  downloadDir = tr_buildPath (sandbox, "Downloads", NULL);
211  tr_mkdirp (downloadDir, 0700);
212  blocklistDir = tr_buildPath (sandbox, "blocklists", NULL);
213  tr_mkdirp (blocklistDir, 0700);
214
215  /* cleanup locals*/
216  tr_free (cwd);
217}
218
219void
220libtransmission_test_session_init_session (void)
221{
222  tr_variant dict;
223
224  /* libtransmission_test_session_init_sandbox() has to be called first */
225  assert (sandbox != NULL);
226  assert (session == NULL);
227
228  /* init the session */
229  tr_variantInitDict    (&dict, 4);
230  tr_variantDictAddStr  (&dict, TR_KEY_download_dir, downloadDir);
231  tr_variantDictAddBool (&dict, TR_KEY_port_forwarding_enabled, false);
232  tr_variantDictAddBool (&dict, TR_KEY_dht_enabled, false);
233  tr_variantDictAddInt  (&dict, TR_KEY_message_level, verbose ? TR_LOG_DEBUG : TR_LOG_ERROR);
234  session = tr_sessionInit ("libtransmission-test", sandbox, !verbose, &dict);
235
236  /* cleanup locals*/
237  tr_variantFree (&dict);
238}
239
240void
241libtransmission_test_session_init (void)
242{
243  libtransmission_test_session_init_formatters ();
244  libtransmission_test_session_init_sandbox ();
245  libtransmission_test_session_init_session ();
246}
247
248void
249libtransmission_test_session_close (void)
250{
251  tr_sessionClose (session);
252  tr_logFreeQueue (tr_logGetQueue ());
253  session = NULL;
254
255  rm_rf (sandbox);
256
257  tr_free (blocklistDir);
258  blocklistDir = NULL;
259
260  tr_free (downloadDir);
261  downloadDir = NULL;
262
263  tr_free (sandbox);
264  sandbox = NULL;
265}
266
267/***
268****
269***/
270
271tr_torrent *
272libtransmission_test_zero_torrent_init (void)
273{
274  int err;
275  int metainfo_len;
276  char * metainfo;
277  const char * metainfo_base64;
278  tr_torrent * tor;
279  tr_ctor * ctor;
280
281  /*
282     1048576 files-filled-with-zeroes/1048576
283        4096 files-filled-with-zeroes/4096
284         512 files-filled-with-zeroes/512
285   */
286  metainfo_base64 =
287    "ZDg6YW5ub3VuY2UzMTpodHRwOi8vd3d3LmV4YW1wbGUuY29tL2Fubm91bmNlMTA6Y3JlYXRlZCBi"
288    "eTI1OlRyYW5zbWlzc2lvbi8yLjYxICgxMzQwNykxMzpjcmVhdGlvbiBkYXRlaTEzNTg3MDQwNzVl"
289    "ODplbmNvZGluZzU6VVRGLTg0OmluZm9kNTpmaWxlc2xkNjpsZW5ndGhpMTA0ODU3NmU0OnBhdGhs"
290    "NzoxMDQ4NTc2ZWVkNjpsZW5ndGhpNDA5NmU0OnBhdGhsNDo0MDk2ZWVkNjpsZW5ndGhpNTEyZTQ6"
291    "cGF0aGwzOjUxMmVlZTQ6bmFtZTI0OmZpbGVzLWZpbGxlZC13aXRoLXplcm9lczEyOnBpZWNlIGxl"
292    "bmd0aGkzMjc2OGU2OnBpZWNlczY2MDpRiEMYSbRhMVL9e9umo/8KT9ZCS1GIQxhJtGExUv1726aj"
293    "/wpP1kJLUYhDGEm0YTFS/XvbpqP/Ck/WQktRiEMYSbRhMVL9e9umo/8KT9ZCS1GIQxhJtGExUv17"
294    "26aj/wpP1kJLUYhDGEm0YTFS/XvbpqP/Ck/WQktRiEMYSbRhMVL9e9umo/8KT9ZCS1GIQxhJtGEx"
295    "Uv1726aj/wpP1kJLUYhDGEm0YTFS/XvbpqP/Ck/WQktRiEMYSbRhMVL9e9umo/8KT9ZCS1GIQxhJ"
296    "tGExUv1726aj/wpP1kJLUYhDGEm0YTFS/XvbpqP/Ck/WQktRiEMYSbRhMVL9e9umo/8KT9ZCS1GI"
297    "QxhJtGExUv1726aj/wpP1kJLUYhDGEm0YTFS/XvbpqP/Ck/WQktRiEMYSbRhMVL9e9umo/8KT9ZC"
298    "S1GIQxhJtGExUv1726aj/wpP1kJLUYhDGEm0YTFS/XvbpqP/Ck/WQktRiEMYSbRhMVL9e9umo/8K"
299    "T9ZCS1GIQxhJtGExUv1726aj/wpP1kJLUYhDGEm0YTFS/XvbpqP/Ck/WQktRiEMYSbRhMVL9e9um"
300    "o/8KT9ZCS1GIQxhJtGExUv1726aj/wpP1kJLUYhDGEm0YTFS/XvbpqP/Ck/WQktRiEMYSbRhMVL9"
301    "e9umo/8KT9ZCS1GIQxhJtGExUv1726aj/wpP1kJLUYhDGEm0YTFS/XvbpqP/Ck/WQktRiEMYSbRh"
302    "MVL9e9umo/8KT9ZCS1GIQxhJtGExUv1726aj/wpP1kJLUYhDGEm0YTFS/XvbpqP/Ck/WQktRiEMY"
303    "SbRhMVL9e9umo/8KT9ZCS1GIQxhJtGExUv1726aj/wpP1kJLOlf5A+Tz30nMBVuNM2hpV3wg/103"
304    "OnByaXZhdGVpMGVlZQ==";
305
306  /* create the torrent ctor */
307  metainfo = tr_base64_decode (metainfo_base64, -1, &metainfo_len);
308  assert (metainfo != NULL);
309  assert (metainfo_len > 0);
310  assert (session != NULL);
311  ctor = tr_ctorNew (session);
312  tr_ctorSetMetainfo (ctor, (uint8_t*)metainfo, metainfo_len);
313  tr_ctorSetPaused (ctor, TR_FORCE, true);
314
315  /* create the torrent */
316  err = 0;
317  tor = tr_torrentNew (ctor, &err);
318  assert (!err);
319
320  /* cleanup */
321  tr_free (metainfo);
322  tr_ctorFree (ctor);
323  return tor;
324}
325
326#define verify_and_block_until_done(tor) \
327  do { \
328    do { tr_wait_msec (10); } while (tor->verifyState != TR_VERIFY_NONE); \
329    tr_torrentVerify (tor); \
330    do { tr_wait_msec (10); } while (tor->verifyState != TR_VERIFY_NONE); \
331  } while (0)
332
333
334void
335libtransmission_test_zero_torrent_populate (tr_torrent * tor, bool complete)
336{
337  tr_file_index_t i;
338
339  for (i=0; i<tor->info.fileCount; ++i)
340    {
341      int rv;
342      uint64_t j;
343      FILE * fp;
344      char * path;
345      char * dirname;
346      const tr_file * file = &tor->info.files[i];
347      struct stat sb;
348
349      if (!complete && (i==0))
350        path = tr_strdup_printf ("%s%c%s.part", tor->currentDir, TR_PATH_DELIMITER, file->name);
351      else
352        path = tr_strdup_printf ("%s%c%s", tor->currentDir, TR_PATH_DELIMITER, file->name);
353      dirname = tr_dirname (path);
354      tr_mkdirp (dirname, 0700);
355      fp = fopen (path, "wb+");
356      for (j=0; j<file->length; ++j)
357        fputc (((!complete) && (i==0) && (j<tor->info.pieceSize)) ? '\1' : '\0', fp);
358      fclose (fp);
359
360      tr_free (dirname);
361      tr_free (path);
362
363      path = tr_torrentFindFile (tor, i);
364      assert (path != NULL);
365      rv = stat (path, &sb);
366      assert (rv == 0);
367      tr_free (path);
368    }
369
370  sync ();
371  verify_and_block_until_done (tor);
372
373  if (complete)
374    assert (tr_torrentStat(tor)->leftUntilDone == 0);
375  else
376    assert (tr_torrentStat(tor)->leftUntilDone == tor->info.pieceSize);
377}
Note: See TracBrowser for help on using the repository browser.