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

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

(libT) better implementation of move-test

File size: 3.3 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 "torrent.h" /* tr_isTorrent() */
17#include "utils.h" /* tr_mkdirp() */
18#include "variant.h"
19
20#include "libtransmission-test.h"
21
22/***
23****
24***/
25
26static void
27zeroes_completeness_func (tr_torrent       * torrent UNUSED,
28                          tr_completeness    completeness,
29                          bool               wasRunning UNUSED,
30                          void             * user_data)
31{
32  *(tr_completeness*)user_data = completeness;
33}
34
35#define check_file_location(tor, i, expected_path) \
36  do { \
37    char * path = tr_torrentFindFile (tor, i); \
38    char * expected = expected_path; \
39    check_streq (expected, path); \
40    tr_free (expected); \
41    tr_free (path); \
42  } while (0)
43
44static int
45test_incomplete_dir_is_subdir_of_download_dir (void)
46{
47  char * incomplete_dir;
48  tr_torrent * tor;
49  tr_completeness completeness;
50  const tr_completeness completeness_unset = -1;
51  const time_t deadline = time(NULL) + 5;
52
53  /* init the session */
54  libtransmission_test_session_init ();
55  incomplete_dir = tr_buildPath (downloadDir, "incomplete", NULL);
56  tr_sessionSetIncompleteDir (session, incomplete_dir);
57  tr_sessionSetIncompleteDirEnabled (session, true);
58
59  /* init an incomplete torrent.
60     the test zero_torrent will be missing its first piece */
61  tor = libtransmission_test_zero_torrent_init ();
62  libtransmission_test_zero_torrent_populate (tor, false);
63  check (tr_torrentStat(tor)->leftUntilDone == tor->info.pieceSize);
64  check_file_location (tor, 0, tr_strdup_printf("%s/%s.part", incomplete_dir, tor->info.files[0].name));
65  check_file_location (tor, 1, tr_buildPath(incomplete_dir, tor->info.files[1].name, NULL));
66  check_int_eq (tor->info.pieceSize, tr_torrentStat(tor)->leftUntilDone);
67
68  /* now finish writing it */
69  {
70    uint32_t offset;
71    tr_block_index_t first, last, i;
72    struct evbuffer * buf = evbuffer_new ();
73    char * zero_block = tr_new0 (char, tor->blockSize);
74
75    completeness = completeness_unset;
76    tr_torrentSetCompletenessCallback (tor, zeroes_completeness_func, &completeness);
77
78    tr_torGetPieceBlockRange (tor, 0, &first, &last);
79    for (offset=0, i=first; i<=last; ++i, offset+=tor->blockSize)
80      {
81        evbuffer_add (buf, zero_block, tor->blockSize);
82        tr_cacheWriteBlock (session->cache, tor, 0, offset, tor->blockSize, buf);
83        tr_torrentGotBlock (tor, i);
84      }
85    sync ();
86
87    tr_torrentVerify (tor);
88    while ((completeness==completeness_unset) && (time(NULL)<=deadline))
89      tr_wait_msec (50);
90    check_int_eq (TR_SEED, completeness);
91    for (i=0; i<tor->info.fileCount; ++i)
92      check_file_location (tor, i, tr_buildPath (downloadDir, tor->info.files[i].name, NULL));
93
94    evbuffer_free (buf);
95    tr_free (zero_block);
96  }
97
98
99  /* cleanup */
100  tr_torrentRemove (tor, true, remove);
101  libtransmission_test_session_close ();
102  tr_free (incomplete_dir);
103  return 0;
104}
105
106
107/***
108****
109***/
110
111int
112main (void)
113{
114  const testFunc tests[] = { test_incomplete_dir_is_subdir_of_download_dir };
115
116  return runTests (tests, NUM_TESTS (tests));
117}
118
119
Note: See TracBrowser for help on using the repository browser.