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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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}
Note: See TracChangeset for help on using the changeset viewer.