testHttpRange.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
3  *
4  * Squid software is distributed under GPLv2+ license and includes
5  * contributions from numerous individuals and organizations.
6  * Please see the COPYING and CONTRIBUTORS files for details.
7  */
8 
9 #include "squid.h"
10 #include "compat/cppunit.h"
11 #include "HttpHeaderRange.h"
12 #include "unitTestMain.h"
13 
14 class TestHttpRange : public CPPUNIT_NS::TestFixture
15 {
21 
22 protected:
23  void testRangeParser();
24  void testRangeParsing(char const *rangestring);
25  void testRangeIter();
26  void testRangeCanonization();
27 };
29 
30 void
32 {
33  testRangeParsing("bytes=0-3");
34  testRangeParsing("bytes=-3");
35  testRangeParsing("bytes=1-");
36  testRangeParsing("bytes=0-3, 1-, -2");
37 }
38 
39 void
40 TestHttpRange::testRangeParsing(char const *rangestring)
41 {
42  String aString (rangestring);
43  HttpHdrRange *range = HttpHdrRange::ParseCreate (&aString);
44 
45  if (!range)
46  exit(EXIT_FAILURE);
47 
48  HttpHdrRange copy(*range);
49 
50  CPPUNIT_ASSERT_EQUAL(range->specs.size(), copy.specs.size());
51 
52  HttpHdrRange::iterator pos = range->begin();
53 
54  CPPUNIT_ASSERT(*pos);
55 
56  delete range;
57 }
58 
59 static HttpHdrRange *
60 rangeFromString(char const *rangestring)
61 {
62  String aString (rangestring);
63  HttpHdrRange *range = HttpHdrRange::ParseCreate (&aString);
64 
65  if (!range)
66  exit(EXIT_FAILURE);
67 
68  return range;
69 }
70 
71 void
73 {
74  HttpHdrRange *range=rangeFromString("bytes=0-3, 1-, -2");
75  CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(3), range->specs.size());
76  size_t counter = 0;
77  HttpHdrRange::iterator i = range->begin();
78 
79  while (i != range->end()) {
80  ++counter;
81  ++i;
82  }
83 
84  CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(3), counter);
85  i = range->begin();
86  CPPUNIT_ASSERT_EQUAL(static_cast<ptrdiff_t>(0), i - range->begin());
87  ++i;
88  CPPUNIT_ASSERT_EQUAL(static_cast<ptrdiff_t>(1), i - range->begin());
89  CPPUNIT_ASSERT_EQUAL(static_cast<ptrdiff_t>(-2), i - range->end());
90 }
91 
92 void
94 {
95  HttpHdrRange *range=rangeFromString("bytes=0-3, 1-, -2");
96  CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(3), range->specs.size());
97 
98  /* 0-3 needs a content length of 4 */
99  /* This passes in the extant code - but should it? */
100 
101  if (!range->canonize(3))
102  exit(EXIT_FAILURE);
103 
104  CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(3), range->specs.size());
105 
106  delete range;
107 
108  range=rangeFromString("bytes=0-3, 1-, -2");
109 
110  CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(3), range->specs.size());
111 
112  /* 0-3 needs a content length of 4 */
113  if (!range->canonize(4))
114  exit(EXIT_FAILURE);
115 
116  delete range;
117 
118  range=rangeFromString("bytes=3-6");
119 
120  CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), range->specs.size());
121 
122  /* 3-6 needs a content length of 4 or more */
123  if (range->canonize(3))
124  exit(EXIT_FAILURE);
125 
126  delete range;
127 
128  range=rangeFromString("bytes=3-6");
129 
130  CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), range->specs.size());
131 
132  /* 3-6 needs a content length of 4 or more */
133  if (!range->canonize(4))
134  exit(EXIT_FAILURE);
135 
136  delete range;
137 
138  range=rangeFromString("bytes=1-1,2-3");
139 
140  CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), range->specs.size());
141 
142  if (!range->canonize(4))
143  exit(EXIT_FAILURE);
144 
145  CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), range->specs.size());
146 
147  delete range;
148 }
149 
150 int
151 main(int argc, char *argv[])
152 {
153  return TestProgram().run(argc, argv);
154 }
155 
CPPUNIT_TEST(testRangeParser)
static HttpHdrRange * rangeFromString(char const *rangestring)
CPPUNIT_TEST_SUITE(TestHttpRange)
implements test program's main() function while enabling customization
Definition: unitTestMain.h:25
void testRangeCanonization()
void testRangeIter()
std::vector< HttpHdrRangeSpec * >::iterator iterator
int run(int argc, char *argv[])
Definition: unitTestMain.h:44
CPPUNIT_TEST_SUITE_REGISTRATION(TestHttpRange)
iterator end()
static HttpHdrRange * ParseCreate(const String *range_spec)
std::vector< HttpHdrRangeSpec * > specs
int canonize(int64_t)
void testRangeParsing(char const *rangestring)
void testRangeParser()
iterator begin()
int main(int argc, char *argv[])

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors