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

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

(libT) change the API signature for tr_torrentVerify() s.t. client code can be notified when the verify is finished

File size: 4.1 KB
Line 
1#include <assert.h>
2#include <errno.h>
3#include <stdio.h> /* remove() */
4#include <string.h> /* strcmp() */
5#include <stdio.h>
6
7#include <sys/types.h> /* stat() */
8#include <sys/stat.h> /* stat() */
9#include <unistd.h> /* stat(), sync() */
10
11#include <event2/buffer.h>
12
13#include "transmission.h"
14#include "cache.h"
15#include "resume.h"
16#include "trevent.h"
17#include "torrent.h" /* tr_isTorrent() */
18#include "utils.h" /* tr_mkdirp() */
19#include "variant.h"
20
21#include "libtransmission-test.h"
22
23/***
24****
25***/
26
27static void
28zeroes_completeness_func (tr_torrent       * torrent UNUSED,
29                          tr_completeness    completeness,
30                          bool               wasRunning UNUSED,
31                          void             * user_data)
32{
33  *(tr_completeness*)user_data = completeness;
34}
35
36#define check_file_location(tor, i, expected_path) \
37  do { \
38    char * path = tr_torrentFindFile (tor, i); \
39    char * expected = expected_path; \
40    check_streq (expected, path); \
41    tr_free (expected); \
42    tr_free (path); \
43  } while (0)
44
45struct test_incomplete_dir_is_subdir_of_download_dir_data
46{
47  tr_torrent * tor;
48  tr_block_index_t block;
49  tr_piece_index_t pieceIndex;
50  uint32_t offset;
51  struct evbuffer * buf;
52  bool done;
53};
54
55static void
56test_incomplete_dir_is_subdir_of_download_dir_threadfunc (void * vdata)
57{
58  struct test_incomplete_dir_is_subdir_of_download_dir_data * data = vdata;
59  tr_cacheWriteBlock (session->cache, data->tor, 0, data->offset, data->tor->blockSize, data->buf);
60  tr_torrentGotBlock (data->tor, data->block);
61  data->done = true;
62}
63 
64
65static int
66test_incomplete_dir_is_subdir_of_download_dir (void)
67{
68  size_t i;
69  char * incomplete_dir;
70  tr_torrent * tor;
71  tr_completeness completeness;
72  const tr_completeness completeness_unset = -1;
73  const time_t deadline = time(NULL) + 5;
74
75  /* init the session */
76  libtransmission_test_session_init ();
77  incomplete_dir = tr_buildPath (downloadDir, "incomplete", NULL);
78  tr_sessionSetIncompleteDir (session, incomplete_dir);
79  tr_sessionSetIncompleteDirEnabled (session, true);
80
81  /* init an incomplete torrent.
82     the test zero_torrent will be missing its first piece */
83  tor = libtransmission_test_zero_torrent_init ();
84  libtransmission_test_zero_torrent_populate (tor, false);
85  check (tr_torrentStat(tor)->leftUntilDone == tor->info.pieceSize);
86  check_file_location (tor, 0, tr_strdup_printf("%s/%s.part", incomplete_dir, tor->info.files[0].name));
87  check_file_location (tor, 1, tr_buildPath(incomplete_dir, tor->info.files[1].name, NULL));
88  check_int_eq (tor->info.pieceSize, tr_torrentStat(tor)->leftUntilDone);
89
90  completeness = completeness_unset;
91  tr_torrentSetCompletenessCallback (tor, zeroes_completeness_func, &completeness);
92
93  /* now finish writing it */
94  {
95    tr_block_index_t first, last;
96    char * zero_block = tr_new0 (char, tor->blockSize);
97    struct test_incomplete_dir_is_subdir_of_download_dir_data data;
98
99    data.tor = tor;
100    data.pieceIndex = 0;
101    data.buf = evbuffer_new ();
102
103    tr_torGetPieceBlockRange (tor, data.pieceIndex, &first, &last);
104    for (i=first; i<=last; ++i)
105      {
106        evbuffer_add (data.buf, zero_block, tor->blockSize);
107        data.block = i;
108        data.done = false;
109        data.offset = data.block * tor->blockSize;
110        tr_runInEventThread (session, test_incomplete_dir_is_subdir_of_download_dir_threadfunc, &data);
111        do { tr_wait_msec(50); } while (!data.done);
112      }
113
114    evbuffer_free (data.buf);
115    tr_free (zero_block);
116  }
117
118  libttest_blockingTorrentVerify (tor);
119  check_int_eq (0, tr_torrentStat(tor)->leftUntilDone);
120
121  while ((completeness==completeness_unset) && (time(NULL)<=deadline))
122    tr_wait_msec (50);
123
124  check_int_eq (TR_SEED, completeness);
125  for (i=0; i<tor->info.fileCount; ++i)
126    check_file_location (tor, i, tr_buildPath (downloadDir, tor->info.files[i].name, NULL));
127
128  /* cleanup */
129  tr_torrentRemove (tor, true, remove);
130  libtransmission_test_session_close ();
131  tr_free (incomplete_dir);
132  return 0;
133}
134
135
136/***
137****
138***/
139
140int
141main (void)
142{
143  const testFunc tests[] = { test_incomplete_dir_is_subdir_of_download_dir };
144
145  return runTests (tests, NUM_TESTS (tests));
146}
147
148
Note: See TracBrowser for help on using the repository browser.