source: trunk/libtransmission/move-test.c @ 14382

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

Fix compilation on Windows

This should not affect non-Win32 platforms in any way.
As for Win32 (both MinGW and MSVC), this should hopefully allow for
unpatched compilation. Correct functioning is not yet guaranteed though.

  • Property svn:keywords set to Date Rev Author Id
File size: 6.1 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 14382 2014-12-13 15:22:39Z mikedld $
8 */
9
10#include <assert.h>
11#include <stdio.h> /* remove() */
12#include <string.h> /* strcmp() */
13#include <stdio.h>
14
15#include <event2/buffer.h>
16
17#include "transmission.h"
18#include "cache.h"
19#include "file.h"
20#include "resume.h"
21#include "trevent.h"
22#include "torrent.h" /* tr_isTorrent() */
23#include "variant.h"
24
25#include "libtransmission-test.h"
26
27/***
28****
29***/
30
31static void
32zeroes_completeness_func (tr_torrent       * torrent UNUSED,
33                          tr_completeness    completeness,
34                          bool               wasRunning UNUSED,
35                          void             * user_data)
36{
37  *(tr_completeness*)user_data = completeness;
38}
39
40#define check_file_location(tor, i, expected_path) \
41  do { \
42    char * path = tr_torrentFindFile (tor, i); \
43    char * expected = expected_path; \
44    check_streq (expected, path); \
45    tr_free (expected); \
46    tr_free (path); \
47  } while (0)
48
49struct test_incomplete_dir_data
50{
51  tr_session * session;
52  tr_torrent * tor;
53  tr_block_index_t block;
54  tr_piece_index_t pieceIndex;
55  uint32_t offset;
56  struct evbuffer * buf;
57  bool done;
58};
59
60static void
61test_incomplete_dir_threadfunc (void * vdata)
62{
63  struct test_incomplete_dir_data * data = vdata;
64  tr_cacheWriteBlock (data->session->cache, data->tor, 0, data->offset, data->tor->blockSize, data->buf);
65  tr_torrentGotBlock (data->tor, data->block);
66  data->done = true;
67}
68 
69static int
70test_incomplete_dir_impl (const char * incomplete_dir, const char * download_dir)
71{
72  size_t i;
73  tr_session * session;
74  tr_torrent * tor;
75  tr_completeness completeness;
76  const tr_completeness completeness_unset = -1;
77  const time_t deadline = time(NULL) + 300;
78  tr_variant settings;
79
80  /* init the session */
81  tr_variantInitDict (&settings, 3);
82  tr_variantDictAddStr (&settings, TR_KEY_download_dir, download_dir);
83  tr_variantDictAddStr (&settings, TR_KEY_incomplete_dir, incomplete_dir);
84  tr_variantDictAddBool (&settings, TR_KEY_incomplete_dir_enabled, true);
85  session = libttest_session_init (&settings);
86  tr_variantFree (&settings);
87  download_dir = tr_sessionGetDownloadDir (session);
88  incomplete_dir = tr_sessionGetIncompleteDir (session);
89
90  /* init an incomplete torrent.
91     the test zero_torrent will be missing its first piece */
92  tor = libttest_zero_torrent_init (session);
93  libttest_zero_torrent_populate (tor, false);
94  check (tr_torrentStat(tor)->leftUntilDone == tor->info.pieceSize);
95  check_file_location (tor, 0, tr_strdup_printf("%s/%s.part", incomplete_dir, tor->info.files[0].name));
96  check_file_location (tor, 1, tr_buildPath(incomplete_dir, tor->info.files[1].name, NULL));
97  check_int_eq (tor->info.pieceSize, tr_torrentStat(tor)->leftUntilDone);
98
99  completeness = completeness_unset;
100  tr_torrentSetCompletenessCallback (tor, zeroes_completeness_func, &completeness);
101
102  /* now finish writing it */
103  {
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 (i=first; i<=last; ++i)
115      {
116        evbuffer_add (data.buf, zero_block, tor->blockSize);
117        data.block = i;
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_int_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 (i=0; i<tor->info.fileCount; ++i)
136    check_file_location (tor, i, tr_buildPath (download_dir, tor->info.files[i].name, NULL));
137
138  /* cleanup */
139  tr_torrentRemove (tor, true, 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  size_t i;
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_int_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_int_eq (0, tr_torrentStat(tor)->leftUntilDone);
199
200  /* confirm the filest really got moved */
201  libttest_sync ();
202  for (i=0; i<tor->info.fileCount; ++i)
203    check_file_location (tor, i, tr_buildPath (target_dir, tor->info.files[i].name, NULL));
204
205  /* cleanup */
206  tr_free (target_dir);
207  tr_torrentRemove (tor, true, 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.