]> granicus.if.org Git - transmission/commitdiff
add tr_makeMetaInfo() unit test for creating a single-file torrent
authorJordan Lee <jordan@transmissionbt.com>
Sun, 8 Jun 2014 20:01:10 +0000 (20:01 +0000)
committerJordan Lee <jordan@transmissionbt.com>
Sun, 8 Jun 2014 20:01:10 +0000 (20:01 +0000)
libtransmission/Makefile.am
libtransmission/libtransmission-test.c
libtransmission/libtransmission-test.h
libtransmission/makemeta-test.c [new file with mode: 0644]
libtransmission/rename-test.c

index 0057a5529a75d906f13cf2770aef892cbc6c33fb..f526bd01bbb424692c400f9e663ba04865bc0408 100644 (file)
@@ -136,6 +136,7 @@ TESTS = \
   history-test \
   json-test \
   magnet-test \
+  makemeta-test \
   metainfo-test \
   move-test \
   peer-msgs-test \
@@ -203,6 +204,10 @@ metainfo_test_SOURCES = metainfo-test.c $(TEST_SOURCES)
 metainfo_test_LDADD = ${apps_ldadd}
 metainfo_test_LDFLAGS = ${apps_ldflags}
 
+makemeta_test_SOURCES = makemeta-test.c $(TEST_SOURCES)
+makemeta_test_LDADD = ${apps_ldadd}
+makemeta_test_LDFLAGS = ${apps_ldflags}
+
 move_test_SOURCES = move-test.c $(TEST_SOURCES)
 move_test_LDADD = ${apps_ldadd}
 move_test_LDFLAGS = ${apps_ldflags}
index e8218342e6683ffd3cb6b1705ff98d76e28341f9..e6cef2cc3f50e334e2020dc1d28ec8860a716fc3 100644 (file)
@@ -9,6 +9,9 @@
 
 #include <assert.h>
 #include <stdio.h>
+#include <stdlib.h> /* mkstemp() */
+
+#include <unistd.h>
 
 #include "transmission.h"
 #include "platform.h" /* TR_PATH_DELIMETER */
@@ -150,8 +153,9 @@ tr_getcwd (void)
 char *
 libtest_sandbox_create (void)
 {
-  const char * path = tr_getcwd ();
+  char * path = tr_getcwd ();
   char * sandbox = tr_buildPath (path, "sandbox-XXXXXX", NULL);
+  tr_free (path);
   tr_mkdtemp (sandbox);
   return sandbox;
 }
@@ -431,10 +435,9 @@ libttest_blockingTorrentVerify (tr_torrent * tor)
     tr_wait_msec (10);
 }
 
-void
-libtest_create_file_with_contents (const char * path, const char * str)
+static void
+build_parent_dir (const char* path)
 {
-  FILE * fp;
   char * dir;
   const int tmperr = errno;
 
@@ -444,9 +447,20 @@ libtest_create_file_with_contents (const char * path, const char * str)
   assert (errno == 0);
   tr_free (dir);
 
+  errno = tmperr;
+}
+
+void
+libtest_create_file_with_contents (const char* path, const void* payload, size_t n)
+{
+  FILE * fp;
+  const int tmperr = errno;
+
+  build_parent_dir (path);
+
   tr_remove (path);
   fp = fopen (path, "wb");
-  fprintf (fp, "%s", str);
+  fwrite (payload, 1, n, fp);
   fclose (fp);
 
   sync ();
@@ -454,3 +468,35 @@ libtest_create_file_with_contents (const char * path, const char * str)
   errno = tmperr;
 }
 
+void
+libtest_create_file_with_string_contents (const char * path, const char* str)
+{
+  libtest_create_file_with_contents (path, str, strlen(str));
+}
+
+void
+libtest_create_tmpfile_with_contents (char* tmpl, const void* payload, size_t n)
+{
+  int fd;
+  const int tmperr = errno;
+  size_t n_left = n;
+
+  build_parent_dir (tmpl);
+
+  fd = mkstemp (tmpl);
+  while (n_left > 0)
+    {
+      const ssize_t n = write (fd, payload, n_left);
+      if (n == -1)
+        {
+          fprintf (stderr, "Error writing '%s': %s", tmpl, tr_strerror(errno));
+          break;
+        }
+      n_left -= n;
+    }
+  close (fd);
+
+  sync ();
+
+  errno = tmperr;
+}
index 70c21073c3dcac3a985ee58ac07b28211d0538d1..2d45e8882c51b0b98fc57b2ea33c1d501ff9f629 100644 (file)
@@ -13,6 +13,7 @@
 #define LIBTRANSMISSION_TEST_H 1
 
 #include <stdio.h>
+#include <string.h> /* strlen() */
 
 #include "transmission.h"
 #include "utils.h" /* tr_strcmp0 () */
@@ -83,13 +84,12 @@ tr_torrent * libttest_zero_torrent_init (tr_session * session);
 
 void         libttest_blockingTorrentVerify (tr_torrent * tor);
 
-void         libtest_create_file_with_contents (const char * path, const char * str);
+void         libtest_create_file_with_contents (const char * path, const void* contents, size_t n);
+void         libtest_create_tmpfile_with_contents (char* tmpl, const void* payload, size_t n);
+void         libtest_create_file_with_string_contents (const char * path, const char* str);
 
 char*        libtest_sandbox_create (void);
 void         libtest_sandbox_destroy (const char * sandbox);
 
 
-
-
-
 #endif /* !LIBTRANSMISSION_TEST_H */
diff --git a/libtransmission/makemeta-test.c b/libtransmission/makemeta-test.c
new file mode 100644 (file)
index 0000000..5011524
--- /dev/null
@@ -0,0 +1,121 @@
+/*
+ * This file Copyright (C) 2013-2014 Mnemosyne LLC
+ *
+ * It may be used under the GNU GPL versions 2 or 3
+ * or any future license endorsed by Mnemosyne LLC.
+ *
+ * $Id: utils-test.c 14266 2014-04-27 23:10:01Z jordan $
+ */
+
+#include "libtransmission-test.h"
+
+#include "transmission.h"
+#include "makemeta.h"
+
+#include <stdlib.h> /* mktemp() */
+#include <string.h> /* strlen() */
+
+static int
+test_single_file_impl (const tr_tracker_info * trackers,
+                       const size_t            trackerCount,
+                       const void            * payload,
+                       const size_t            payloadSize,
+                       const char            * comment,
+                       bool                    isPrivate)
+{
+  char* sandbox;
+  char* input_file;
+  char* torrent_file;
+  tr_metainfo_builder* builder;
+  tr_ctor * ctor;
+  tr_parse_result parse_result;
+  tr_info inf;
+  char * tmpstr;
+
+  /* set up our local test sandbox */
+  sandbox = libtest_sandbox_create();
+
+  /* create a single input file */
+  input_file = tr_buildPath (sandbox, "test.XXXXXX", NULL);
+  libtest_create_tmpfile_with_contents (input_file, payload, payloadSize);
+  builder = tr_metaInfoBuilderCreate (input_file);
+  check_streq (input_file, builder->top);
+  check_int_eq (1, builder->fileCount);
+  check_streq (input_file, builder->files[0].filename);
+  check_int_eq (payloadSize, builder->files[0].size);
+  check_int_eq (payloadSize, builder->totalSize);
+  check (builder->isSingleFile);
+  check (!builder->abortFlag);
+
+  /* have tr_makeMetaInfo() build the .torrent file */
+  torrent_file = tr_strdup_printf ("%s.torrent", input_file);
+  tr_makeMetaInfo (builder, torrent_file, trackers, trackerCount, comment, isPrivate);
+  check (isPrivate == builder->isPrivate);
+  check_streq (torrent_file, builder->outputFile);
+  check_streq (comment, builder->comment);
+  check_int_eq (trackerCount, builder->trackerCount);
+  while (!builder->isDone)
+    tr_wait_msec (100);
+
+  /* now let's check our work: parse the  .torrent file */
+  ctor = tr_ctorNew (NULL);
+  tr_ctorSetMetainfoFromFile (ctor, torrent_file);
+  parse_result = tr_torrentParse (ctor, &inf);
+  check_int_eq (TR_PARSE_OK, parse_result);
+
+  /* quick check of some of the parsed metainfo */
+  check_int_eq (payloadSize, inf.totalSize);
+  tmpstr = tr_basename(input_file);
+  check_streq (tmpstr, inf.name);
+  tr_free (tmpstr);
+  check_streq (comment, inf.comment);
+  check_int_eq (1, inf.fileCount);
+  check_int_eq (isPrivate, inf.isPrivate);
+  check (!inf.isMultifile);
+  check_int_eq (trackerCount, inf.trackerCount);
+
+  /* cleanup */
+  tr_free (torrent_file);
+  tr_free (input_file);
+  tr_ctorFree (ctor);
+  tr_metainfoFree (&inf);
+  tr_metaInfoBuilderFree (builder);
+  libtest_sandbox_destroy (sandbox);
+  tr_free (sandbox);
+  return 0;
+}
+
+static int
+test_single_file (void)
+{
+  tr_tracker_info trackers[16];
+  size_t trackerCount;
+  bool isPrivate;
+  const char * comment;
+  const char * payload;
+  size_t payloadSize;
+
+  trackerCount = 0;
+  trackers[trackerCount].tier = trackerCount;
+  trackers[trackerCount].announce = (char*) "udp://tracker.openbittorrent.com:80";
+  ++trackerCount;
+  trackers[trackerCount].tier = trackerCount;
+  trackers[trackerCount].announce = (char*) "udp://tracker.publicbt.com:80";
+  ++trackerCount;
+  payload = "Hello, World!\n";
+  payloadSize = strlen(payload);
+  comment = "This is the comment";
+  isPrivate = false;
+  test_single_file_impl (trackers, trackerCount, payload, payloadSize, comment, isPrivate);
+
+  return 0;
+}
+
+int
+main (void)
+{
+  const testFunc tests[] = { test_single_file };
+
+  return runTests (tests, NUM_TESTS (tests));
+}
+
index 14a38aa3bb8ec627ba4c626beb162662db6a6371..9022f31d26916fa324ddcd41e8ee3148c21b6943 100644 (file)
@@ -96,7 +96,7 @@ static void
 create_single_file_torrent_contents (const char * top)
 {
   char * path = tr_buildPath (top, "hello-world.txt", NULL);
-  libtest_create_file_with_contents (path, "hello, world!\n");
+  libtest_create_file_with_string_contents (path, "hello, world!\n");
   tr_free (path);
 }
 
@@ -241,19 +241,19 @@ create_multifile_torrent_contents (const char * top)
   char * path;
 
   path = tr_buildPath (top, "Felidae", "Felinae", "Acinonyx", "Cheetah", "Chester", NULL);
-  libtest_create_file_with_contents (path, "It ain't easy bein' cheesy.\n");
+  libtest_create_file_with_string_contents (path, "It ain't easy bein' cheesy.\n");
   tr_free (path);
 
   path = tr_buildPath (top, "Felidae", "Pantherinae", "Panthera", "Tiger", "Tony", NULL);
-  libtest_create_file_with_contents (path, "They’re Grrrrreat!\n");
+  libtest_create_file_with_string_contents (path, "They’re Grrrrreat!\n");
   tr_free (path);
 
   path = tr_buildPath (top, "Felidae", "Felinae", "Felis", "catus", "Kyphi", NULL);
-  libtest_create_file_with_contents (path, "Inquisitive\n");
+  libtest_create_file_with_string_contents (path, "Inquisitive\n");
   tr_free (path);
 
   path = tr_buildPath (top, "Felidae", "Felinae", "Felis", "catus", "Saffron", NULL);
-  libtest_create_file_with_contents (path, "Tough\n");
+  libtest_create_file_with_string_contents (path, "Tough\n");
   tr_free (path);
 
   sync ();