source: trunk/libtransmission/move-test.c

Last change on this file was 14721, checked in by jordan, 5 years ago

fix a handful of minor compiler warnings, mostly in the unit tests, eg field width shortening or implicit signed/unsigned conversions

  • Property svn:keywords set to Date Rev Author Id
File size: 6.3 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: move-test.c 14721 2016-03-29 03:04:54Z mikedld $
8 */
9
10#include <assert.h>
11#include <string.h> /* strcmp() */
12#include <stdio.h>
13
14#include <event2/buffer.h>
15
16#include "transmission.h"
17#include "cache.h"
18#include "file.h"
19#include "resume.h"
20#include "trevent.h"
21#include "torrent.h" /* tr_isTorrent() */
22#include "variant.h"
23
24#include "libtransmission-test.h"
25
26/***
27****
28***/
29
30static void
31zeroes_completeness_func (tr_torrent       * torrent UNUSED,
32                          tr_completeness    completeness,
33                          bool               wasRunning UNUSED,
34                          void             * user_data)
35{
36  *(tr_completeness*)user_data = completeness;
37}
38
39#define check_file_location(tor, i, expected_path) \
40  do { \
41    char * path = tr_torrentFindFile (tor, i); \
42    char * expected = expected_path; \
43    check_streq (expected, path); \
44    tr_free (expected); \
45    tr_free (path); \
46  } while (0)
47
48struct test_incomplete_dir_data
49{
50  tr_session * session;
51  tr_torrent * tor;
52  tr_block_index_t block;
53  tr_piece_index_t pieceIndex;
54  uint32_t offset;
55  struct evbuffer * buf;
56  bool done;
57};
58
59static void
60test_incomplete_dir_threadfunc (void * vdata)
61{
62  struct test_incomplete_dir_data * data = vdata;
63  tr_cacheWriteBlock (data->session->cache, data->tor, 0, data->offset, data->tor->blockSize, data->buf);
64  tr_torrentGotBlock (data->tor, data->block);
65  data->done = true;
66}
67
68static int
69test_incomplete_dir_impl (const char * incomplete_dir, const char * download_dir)
70{
71  tr_file_index_t file_index;
72  tr_session * session;
73  tr_torrent * tor;
74  tr_completeness completeness;
75  const tr_completeness completeness_unset = -1;
76  const time_t deadline = time(NULL) + 300;
77  tr_variant settings;
78
79  /* init the session */
80  tr_variantInitDict (&settings, 3);
81  tr_variantDictAddStr (&settings, TR_KEY_download_dir, download_dir);
82  tr_variantDictAddStr (&settings, TR_KEY_incomplete_dir, incomplete_dir);
83  tr_variantDictAddBool (&settings, TR_KEY_incomplete_dir_enabled, true);
84  session = libttest_session_init (&settings);
85  tr_variantFree (&settings);
86  download_dir = tr_sessionGetDownloadDir (session);
87  incomplete_dir = tr_sessionGetIncompleteDir (session);
88
89  /* init an incomplete torrent.
90     the test zero_torrent will be missing its first piece */
91  tor = libttest_zero_torrent_init (session);
92  libttest_zero_torrent_populate (tor, false);
93  check (tr_torrentStat(tor)->leftUntilDone == tor->info.pieceSize);
94  check_file_location (tor, 0, tr_strdup_printf("%s/%s.part", incomplete_dir, tor->info.files[0].name));
95  check_file_location (tor, 1, tr_buildPath(incomplete_dir, tor->info.files[1].name, NULL));
96  check_uint_eq (tor->info.pieceSize, tr_torrentStat(tor)->leftUntilDone);
97
98  completeness = completeness_unset;
99  tr_torrentSetCompletenessCallback (tor, zeroes_completeness_func, &completeness);
100
101  /* now finish writing it */
102  {
103    tr_block_index_t block_index;
104    tr_block_index_t first, last;
105    char * zero_block = tr_new0 (char, tor->blockSize);
106    struct test_incomplete_dir_data data;
107
108    data.session = session;
109    data.tor = tor;
110    data.pieceIndex = 0;
111    data.buf = evbuffer_new ();
112
113    tr_torGetPieceBlockRange (tor, data.pieceIndex, &first, &last);
114    for (block_index=first; block_index<=last; ++block_index)
115      {
116        evbuffer_add (data.buf, zero_block, tor->blockSize);
117        data.block = block_index;
118        data.done = false;
119        data.offset = data.block * tor->blockSize;
120        tr_runInEventThread (session, test_incomplete_dir_threadfunc, &data);
121        do { tr_wait_msec(50); } while (!data.done);
122      }
123
124    evbuffer_free (data.buf);
125    tr_free (zero_block);
126  }
127
128  libttest_blockingTorrentVerify (tor);
129  check_uint_eq (0, tr_torrentStat(tor)->leftUntilDone);
130
131  while ((completeness==completeness_unset) && (time(NULL)<=deadline))
132    tr_wait_msec (50);
133
134  check_int_eq (TR_SEED, completeness);
135  for (file_index=0; file_index<tor->info.fileCount; ++file_index)
136    check_file_location (tor, file_index, tr_buildPath (download_dir, tor->info.files[file_index].name, NULL));
137
138  /* cleanup */
139  tr_torrentRemove (tor, true, tr_sys_path_remove);
140  libttest_session_close (session);
141  return 0;
142}
143
144static int
145test_incomplete_dir (void)
146{
147  int rv;
148
149  /* test what happens when incompleteDir is a subdir of downloadDir*/
150  if ((rv = test_incomplete_dir_impl ("Downloads/Incomplete", "Downloads")))
151    return rv;
152
153  /* test what happens when downloadDir is a subdir of incompleteDir */
154  if ((rv = test_incomplete_dir_impl ("Downloads", "Downloads/Complete")))
155    return rv;
156
157  /* test what happens when downloadDir and incompleteDir are siblings */
158  if ((rv = test_incomplete_dir_impl ("Incomplete", "Downloads")))
159    return rv;
160
161  return 0;
162}
163
164/***
165****
166***/
167
168static int
169test_set_location (void)
170{
171  tr_file_index_t file_index;
172  int state;
173  char * target_dir;
174  tr_torrent * tor;
175  tr_session * session;
176  const time_t deadline = time(NULL) + 300;
177
178  /* init the session */
179  session = libttest_session_init (NULL);
180  target_dir = tr_buildPath (tr_sessionGetConfigDir (session), "target", NULL);
181  tr_sys_dir_create (target_dir, TR_SYS_DIR_CREATE_PARENTS, 0777, NULL);
182
183  /* init a torrent. */
184  tor = libttest_zero_torrent_init (session);
185  libttest_zero_torrent_populate (tor, true);
186  libttest_blockingTorrentVerify (tor);
187  check_uint_eq (0, tr_torrentStat(tor)->leftUntilDone);
188
189  /* now move it */
190  state = -1;
191  tr_torrentSetLocation (tor, target_dir, true, NULL, &state);
192  while ((state==TR_LOC_MOVING) && (time(NULL)<=deadline))
193    tr_wait_msec (50);
194  check_int_eq (TR_LOC_DONE, state);
195
196  /* confirm the torrent is still complete after being moved */
197  libttest_blockingTorrentVerify (tor);
198  check_uint_eq (0, tr_torrentStat(tor)->leftUntilDone);
199
200  /* confirm the filest really got moved */
201  libttest_sync ();
202  for (file_index=0; file_index<tor->info.fileCount; ++file_index)
203    check_file_location (tor, file_index, tr_buildPath (target_dir, tor->info.files[file_index].name, NULL));
204
205  /* cleanup */
206  tr_free (target_dir);
207  tr_torrentRemove (tor, true, tr_sys_path_remove);
208  libttest_session_close (session);
209  return 0;
210}
211
212/***
213****
214***/
215
216int
217main (void)
218{
219  const testFunc tests[] = { test_incomplete_dir,
220                             test_set_location };
221
222  return runTests (tests, NUM_TESTS (tests));
223}
224
225
Note: See TracBrowser for help on using the repository browser.