I've written a unit test for the parse_content_range() method.
However, I haven't yet populated it with various test cases.
Sharing the patch for the unit test here. I will add more test cases
for this test later.

Kindly do review the patch. If no one complains, I'll push it in a
couple of days.

-- 
Thanking You,
Darshit Shah
From 154760a79b3981f8cb5fcf7f643ae2e2579aa887 Mon Sep 17 00:00:00 2001
From: Darshit Shah <[email protected]>
Date: Sat, 29 Aug 2015 23:08:39 +0530
Subject: [PATCH] Add unit test for parse_content_range() method

    * http.c (test_parse_range_header): New function to test the
    function for parsing the HTTP/1.1 Content-Range header.
    * test.[ch]: Same
---
 src/http.c | 38 ++++++++++++++++++++++++++++++++++++++
 src/test.c |  1 +
 src/test.h |  1 +
 3 files changed, 40 insertions(+)

diff --git a/src/http.c b/src/http.c
index e96cad7..9bba036 100644
--- a/src/http.c
+++ b/src/http.c
@@ -4892,6 +4892,44 @@ ensure_extension (struct http_stat *hs, const char *ext, int *dt)
 }
 
 #ifdef TESTING
+
+const char *
+test_parse_range_header(void)
+{
+  static const struct {
+    const char * rangehdr;
+    const wgint firstbyte;
+    const wgint lastbyte;
+    const wgint length;
+  } test_array[] = {
+      { "bytes 0-1000/100000000", 0, 1000, 100000000}
+  };
+
+  /* wgint *firstbyteptr = xmalloc(sizeof(wgint)); */
+  wgint firstbyteptr[sizeof(wgint)];
+  wgint *lastbyteptr = xmalloc(sizeof(wgint));
+  wgint *lengthptr = xmalloc(sizeof(wgint));
+  bool result;
+  for (unsigned i = 0; i < countof (test_array); i++)
+    {
+      result = parse_content_range(test_array[0].rangehdr, firstbyteptr, lastbyteptr, lengthptr);
+#if 0
+      printf("%ld %ld", test_array[i].firstbyte, *firstbyteptr);
+      printf("%ld %ld", test_array[i].lastbyte, *lastbyteptr);
+      printf("%ld %ld", test_array[i].length, *lengthptr);
+#endif
+      mu_assert("test_parse_range_header: Parsing failed", result);
+      mu_assert("test_parse_range_header: Bad parse", test_array[i].firstbyte == *firstbyteptr &&
+                                                      test_array[i].lastbyte == *lastbyteptr &&
+                                                      test_array[i].length == *lengthptr);
+    }
+
+  /* xfree(firstbyteptr); */
+  xfree(lastbyteptr);
+  xfree(lengthptr);
+  return NULL;
+}
+
 const char *
 test_parse_content_disposition(void)
 {
diff --git a/src/test.c b/src/test.c
index 5278925..cb01de3 100644
--- a/src/test.c
+++ b/src/test.c
@@ -54,6 +54,7 @@ all_tests(void)
   mu_run_test (test_has_key);
 #endif
   mu_run_test (test_parse_content_disposition);
+  mu_run_test (test_parse_range_header);
   mu_run_test (test_subdir_p);
   mu_run_test (test_dir_matches_p);
   mu_run_test (test_commands_sorted);
diff --git a/src/test.h b/src/test.h
index f74c162..4e0e1f2 100644
--- a/src/test.h
+++ b/src/test.h
@@ -48,6 +48,7 @@ const char *test_has_key (void);
 const char *test_find_key_value (void);
 const char *test_find_key_values (void);
 const char *test_parse_content_disposition(void);
+const char *test_parse_range_header(void);
 const char *test_commands_sorted(void);
 const char *test_cmd_spec_restrict_file_names(void);
 const char *test_is_robots_txt_url(void);
-- 
2.5.0

Reply via email to