Changeset 14289


Ignore:
Timestamp:
Jun 8, 2014, 8:01:10 PM (8 years ago)
Author:
jordan
Message:

add tr_makeMetaInfo() unit test for creating a single-file torrent

Location:
trunk/libtransmission
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/Makefile.am

    r14233 r14289  
    137137  json-test \
    138138  magnet-test \
     139  makemeta-test \
    139140  metainfo-test \
    140141  move-test \
     
    204205metainfo_test_LDFLAGS = ${apps_ldflags}
    205206
     207makemeta_test_SOURCES = makemeta-test.c $(TEST_SOURCES)
     208makemeta_test_LDADD = ${apps_ldadd}
     209makemeta_test_LDFLAGS = ${apps_ldflags}
     210
    206211move_test_SOURCES = move-test.c $(TEST_SOURCES)
    207212move_test_LDADD = ${apps_ldadd}
  • trunk/libtransmission/libtransmission-test.c

    r14288 r14289  
    1010#include <assert.h>
    1111#include <stdio.h>
     12#include <stdlib.h> /* mkstemp() */
     13
     14#include <unistd.h>
    1215
    1316#include "transmission.h"
     
    151154libtest_sandbox_create (void)
    152155{
    153   const char * path = tr_getcwd ();
     156  char * path = tr_getcwd ();
    154157  char * sandbox = tr_buildPath (path, "sandbox-XXXXXX", NULL);
     158  tr_free (path);
    155159  tr_mkdtemp (sandbox);
    156160  return sandbox;
     
    432436}
    433437
    434 void
    435 libtest_create_file_with_contents (const char * path, const char * str)
    436 {
    437   FILE * fp;
     438static void
     439build_parent_dir (const char* path)
     440{
    438441  char * dir;
    439442  const int tmperr = errno;
     
    445448  tr_free (dir);
    446449
     450  errno = tmperr;
     451}
     452
     453void
     454libtest_create_file_with_contents (const char* path, const void* payload, size_t n)
     455{
     456  FILE * fp;
     457  const int tmperr = errno;
     458
     459  build_parent_dir (path);
     460
    447461  tr_remove (path);
    448462  fp = fopen (path, "wb");
    449   fprintf (fp, "%s", str);
     463  fwrite (payload, 1, n, fp);
    450464  fclose (fp);
    451465
     
    455469}
    456470
     471void
     472libtest_create_file_with_string_contents (const char * path, const char* str)
     473{
     474  libtest_create_file_with_contents (path, str, strlen(str));
     475}
     476
     477void
     478libtest_create_tmpfile_with_contents (char* tmpl, const void* payload, size_t n)
     479{
     480  int fd;
     481  const int tmperr = errno;
     482  size_t n_left = n;
     483
     484  build_parent_dir (tmpl);
     485
     486  fd = mkstemp (tmpl);
     487  while (n_left > 0)
     488    {
     489      const ssize_t n = write (fd, payload, n_left);
     490      if (n == -1)
     491        {
     492          fprintf (stderr, "Error writing '%s': %s", tmpl, tr_strerror(errno));
     493          break;
     494        }
     495      n_left -= n;
     496    }
     497  close (fd);
     498
     499  sync ();
     500
     501  errno = tmperr;
     502}
  • trunk/libtransmission/libtransmission-test.h

    r14288 r14289  
    1414
    1515#include <stdio.h>
     16#include <string.h> /* strlen() */
    1617
    1718#include "transmission.h"
     
    8485void         libttest_blockingTorrentVerify (tr_torrent * tor);
    8586
    86 void         libtest_create_file_with_contents (const char * path, const char * str);
     87void         libtest_create_file_with_contents (const char * path, const void* contents, size_t n);
     88void         libtest_create_tmpfile_with_contents (char* tmpl, const void* payload, size_t n);
     89void         libtest_create_file_with_string_contents (const char * path, const char* str);
    8790
    8891char*        libtest_sandbox_create (void);
     
    9093
    9194
    92 
    93 
    94 
    9595#endif /* !LIBTRANSMISSION_TEST_H */
  • trunk/libtransmission/rename-test.c

    r14288 r14289  
    9797{
    9898  char * path = tr_buildPath (top, "hello-world.txt", NULL);
    99   libtest_create_file_with_contents (path, "hello, world!\n");
     99  libtest_create_file_with_string_contents (path, "hello, world!\n");
    100100  tr_free (path);
    101101}
     
    242242
    243243  path = tr_buildPath (top, "Felidae", "Felinae", "Acinonyx", "Cheetah", "Chester", NULL);
    244   libtest_create_file_with_contents (path, "It ain't easy bein' cheesy.\n");
     244  libtest_create_file_with_string_contents (path, "It ain't easy bein' cheesy.\n");
    245245  tr_free (path);
    246246
    247247  path = tr_buildPath (top, "Felidae", "Pantherinae", "Panthera", "Tiger", "Tony", NULL);
    248   libtest_create_file_with_contents (path, "They’re Grrrrreat!\n");
     248  libtest_create_file_with_string_contents (path, "They’re Grrrrreat!\n");
    249249  tr_free (path);
    250250
    251251  path = tr_buildPath (top, "Felidae", "Felinae", "Felis", "catus", "Kyphi", NULL);
    252   libtest_create_file_with_contents (path, "Inquisitive\n");
     252  libtest_create_file_with_string_contents (path, "Inquisitive\n");
    253253  tr_free (path);
    254254
    255255  path = tr_buildPath (top, "Felidae", "Felinae", "Felis", "catus", "Saffron", NULL);
    256   libtest_create_file_with_contents (path, "Tough\n");
     256  libtest_create_file_with_string_contents (path, "Tough\n");
    257257  tr_free (path);
    258258
Note: See TracChangeset for help on using the changeset viewer.