test_http_range.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 "fatal.h"
11#include "HttpHeader.h"
12#include "HttpHeaderRange.h"
13#include "HttpHeaderTools.h"
14
15// TODO: refactor as cppunit test
16
17static void
18testRangeParser(char const *rangestring)
19{
20 String aString (rangestring);
21 HttpHdrRange *range = HttpHdrRange::ParseCreate (&aString);
22
23 if (!range)
24 exit(EXIT_FAILURE);
25
26 HttpHdrRange copy(*range);
27
28 assert (copy.specs.size() == range->specs.size());
29
30 HttpHdrRange::iterator pos = range->begin();
31
32 assert (*pos);
33
34 delete range;
35}
36
37static HttpHdrRange *
38rangeFromString(char const *rangestring)
39{
40 String aString (rangestring);
41 HttpHdrRange *range = HttpHdrRange::ParseCreate (&aString);
42
43 if (!range)
44 exit(EXIT_FAILURE);
45
46 return range;
47}
48
49static void
51{
52 HttpHdrRange *range=rangeFromString("bytes=0-3, 1-, -2");
53 assert (range->specs.size() == 3);
54 size_t counter = 0;
55 HttpHdrRange::iterator i = range->begin();
56
57 while (i != range->end()) {
58 ++counter;
59 ++i;
60 }
61
62 assert (counter == 3);
63 i = range->begin();
64 assert (i - range->begin() == 0);
65 ++i;
66 assert (i - range->begin() == 1);
67 assert (i - range->end() == -2);
68}
69
70static void
72{
73 HttpHdrRange *range=rangeFromString("bytes=0-3, 1-, -2");
74 assert (range->specs.size() == 3);
75
76 /* 0-3 needs a content length of 4 */
77 /* This passes in the extant code - but should it? */
78
79 if (!range->canonize(3))
80 exit(EXIT_FAILURE);
81
82 assert (range->specs.size() == 3);
83
84 delete range;
85
86 range=rangeFromString("bytes=0-3, 1-, -2");
87
88 assert (range->specs.size() == 3);
89
90 /* 0-3 needs a content length of 4 */
91 if (!range->canonize(4))
92 exit(EXIT_FAILURE);
93
94 delete range;
95
96 range=rangeFromString("bytes=3-6");
97
98 assert (range->specs.size() == 1);
99
100 /* 3-6 needs a content length of 4 or more */
101 if (range->canonize(3))
102 exit(EXIT_FAILURE);
103
104 delete range;
105
106 range=rangeFromString("bytes=3-6");
107
108 assert (range->specs.size() == 1);
109
110 /* 3-6 needs a content length of 4 or more */
111 if (!range->canonize(4))
112 exit(EXIT_FAILURE);
113
114 delete range;
115
116 range=rangeFromString("bytes=1-1,2-3");
117
118 assert (range->specs.size()== 2);
119
120 if (!range->canonize(4))
121 exit(EXIT_FAILURE);
122
123 assert (range->specs.size() == 2);
124
125 delete range;
126}
127
128int
129main(int, char **)
130{
131 try {
132 Mem::Init();
133 /* enable for debugging to console */
134 // Debug::debugOptions = xstrdup("ALL,1 64,9");
135 // Debug::BanCacheLogUse();
136 testRangeParser("bytes=0-3");
137 testRangeParser("bytes=-3");
138 testRangeParser("bytes=1-");
139 testRangeParser("bytes=0-3, 1-, -2");
142 } catch (const std::exception &e) {
143 printf("Error: dying from an unhandled exception: %s\n", e.what());
144 return EXIT_FAILURE;
145 } catch (...) {
146 printf("Error: dying from an unhandled exception.\n");
147 return EXIT_FAILURE;
148 }
149 return EXIT_SUCCESS;
150}
151
#define assert(EX)
Definition: assert.h:17
int canonize(int64_t)
std::vector< HttpHdrRangeSpec * >::iterator iterator
iterator begin()
iterator end()
std::vector< HttpHdrRangeSpec * > specs
static HttpHdrRange * ParseCreate(const String *range_spec)
void Init()
Definition: old_api.cc:425
int main(int, char **)
static void testRangeIter()
static void testRangeCanonization()
static HttpHdrRange * rangeFromString(char const *rangestring)
static void testRangeParser(char const *rangestring)

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors