Changeset 13812
- Timestamp:
- Jan 20, 2013, 4:41:38 AM (8 years ago)
- Location:
- trunk/libtransmission
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/libtransmission/libtransmission-test.c
r13667 r13812 96 96 return 0; /* All tests passed */ 97 97 } 98 99 /*** 100 **** 101 ***/ 102 103 #include <sys/types.h> /* stat(), opendir() */ 104 #include <sys/stat.h> /* stat() */ 105 #include <dirent.h> /* opendir() */ 106 #include <unistd.h> /* getcwd() */ 107 108 #include <errno.h> 109 #include <string.h> /* strcmp() */ 110 111 #include "variant.h" 112 113 tr_session * session = NULL; 114 static char * sandbox = NULL; 115 116 static char* 117 tr_getcwd (void) 118 { 119 char * result; 120 char buf[2048]; 121 122 #ifdef WIN32 123 result = _getcwd (buf, sizeof (buf)); 124 #else 125 result = getcwd (buf, sizeof (buf)); 126 #endif 127 128 if (result == NULL) 129 { 130 fprintf (stderr, "getcwd error: \"%s\"", tr_strerror (errno)); 131 *buf = '\0'; 132 } 133 134 return tr_strdup (buf); 135 } 136 137 static void 138 rm_rf (const char * killme) 139 { 140 struct stat sb; 141 142 if (!stat (killme, &sb)) 143 { 144 DIR * odir; 145 146 if (S_ISDIR (sb.st_mode) && ((odir = opendir (killme)))) 147 { 148 struct dirent *d; 149 for (d = readdir(odir); d != NULL; d=readdir(odir)) 150 { 151 if (d->d_name && strcmp(d->d_name,".") && strcmp(d->d_name,"..")) 152 { 153 char * tmp = tr_buildPath (killme, d->d_name, NULL); 154 rm_rf (tmp); 155 tr_free (tmp); 156 } 157 } 158 closedir (odir); 159 } 160 161 if (verbose) 162 fprintf (stderr, "cleanup: removing %s\n", killme); 163 164 remove (killme); 165 } 166 } 167 168 void 169 libtransmission_test_session_init (void) 170 { 171 char * cwd; 172 char * downloadDir; 173 tr_variant dict; 174 175 /* create a sandbox for the test session */ 176 cwd = tr_getcwd (); 177 sandbox = tr_buildPath (cwd, "sandbox-XXXXXX", NULL); 178 tr_mkdtemp (sandbox); 179 downloadDir = tr_buildPath (sandbox, "Downloads", NULL); 180 tr_mkdirp (downloadDir, 0700); 181 182 /* create a test session */ 183 tr_variantInitDict (&dict, 3); 184 tr_variantDictAddStr (&dict, TR_KEY_download_dir, downloadDir); 185 tr_variantDictAddBool (&dict, TR_KEY_port_forwarding_enabled, false); 186 tr_variantDictAddBool (&dict, TR_KEY_dht_enabled, false); 187 session = tr_sessionInit ("rename-test", sandbox, true, &dict); 188 189 /* cleanup locals*/ 190 tr_variantFree (&dict); 191 tr_free (downloadDir); 192 tr_free (cwd); 193 } 194 195 void 196 libtransmission_test_session_close (void) 197 { 198 tr_sessionClose (session); 199 tr_freeMessageList (tr_getQueuedMessages ()); 200 rm_rf (sandbox); 201 tr_free (sandbox); 202 } -
trunk/libtransmission/libtransmission-test.h
r13667 r13812 66 66 } 67 67 68 extern tr_session * session; 69 void libtransmission_test_session_init (void); 70 void libtransmission_test_session_close (void); 71 72 68 73 #endif /* !LIBTRANSMISSION_TEST_H */ -
trunk/libtransmission/rename-test.c
r13807 r13812 5 5 #include <stdio.h> 6 6 7 #include <sys/types.h> /* stat(), opendir() */ 8 #include <sys/stat.h> /* stat() */ 9 #include <dirent.h> /* opendir() */ 10 #include <unistd.h> /* getcwd() */ 7 #include <unistd.h> /* sync() */ 11 8 12 9 #include "transmission.h" … … 17 14 18 15 #include "libtransmission-test.h" 19 20 static tr_session * session = NULL;21 22 static char*23 tr_getcwd (void)24 {25 char * result;26 char buf[2048];27 28 #ifdef WIN3229 result = _getcwd (buf, sizeof (buf));30 #else31 result = getcwd (buf, sizeof (buf));32 #endif33 34 if (result == NULL)35 {36 fprintf (stderr, "getcwd error: \"%s\"", tr_strerror (errno));37 *buf = '\0';38 }39 40 return tr_strdup (buf);41 }42 16 43 17 /*** … … 450 424 ***/ 451 425 452 static void453 rm_rf (const char * killme)454 {455 struct stat sb;456 457 if (!stat (killme, &sb))458 {459 DIR * odir;460 461 if (S_ISDIR (sb.st_mode) && ((odir = opendir (killme))))462 {463 struct dirent *d;464 for (d = readdir(odir); d != NULL; d=readdir(odir))465 {466 if (d->d_name && strcmp(d->d_name,".") && strcmp(d->d_name,".."))467 {468 char * tmp = tr_buildPath (killme, d->d_name, NULL);469 rm_rf (tmp);470 tr_free (tmp);471 }472 }473 closedir (odir);474 }475 476 if (verbose)477 fprintf (stderr, "cleanup: removing %s\n", killme);478 479 remove (killme);480 }481 }482 483 426 int 484 427 main (void) 485 428 { 486 429 int ret; 487 char * cwd;488 char * sandbox;489 char * downloadDir;490 tr_variant dict;491 430 const testFunc tests[] = { test_single_filename_torrent, 492 431 test_multifile_torrent }; 493 432 494 /* create a sandbox for the test session */ 495 cwd = tr_getcwd (); 496 sandbox = tr_buildPath (cwd, "sandbox-XXXXXX", NULL); 497 tr_mkdtemp (sandbox); 498 downloadDir = tr_buildPath (sandbox, "Downloads", NULL); 499 tr_mkdirp (downloadDir, 0700); 500 501 /* create a test session */ 502 tr_variantInitDict (&dict, 3); 503 tr_variantDictAddStr (&dict, TR_KEY_download_dir, downloadDir); 504 tr_variantDictAddBool (&dict, TR_KEY_port_forwarding_enabled, false); 505 tr_variantDictAddBool (&dict, TR_KEY_dht_enabled, false); 506 session = tr_sessionInit ("rename-test", sandbox, true, &dict); 507 508 /* run the tests */ 433 libtransmission_test_session_init (); 509 434 ret = runTests (tests, NUM_TESTS (tests)); 510 511 /* cleanup */ 512 tr_sessionClose (session); 513 tr_freeMessageList (tr_getQueuedMessages ()); 514 tr_variantFree (&dict); 515 rm_rf (sandbox); 516 tr_free (downloadDir); 517 tr_free (sandbox); 518 tr_free (cwd); 435 libtransmission_test_session_close (); 436 519 437 return ret; 520 438 }
Note: See TracChangeset
for help on using the changeset viewer.