File: TestHttpResponseHead.cpp

package info (click to toggle)
firefox 147.0.4-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,683,532 kB
  • sloc: cpp: 7,607,356; javascript: 6,533,348; ansic: 3,775,236; python: 1,415,508; xml: 634,561; asm: 438,949; java: 186,241; sh: 62,760; makefile: 18,079; objc: 13,092; perl: 12,808; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10; exp: 6
file content (236 lines) | stat: -rw-r--r-- 8,242 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#include "gtest/gtest.h"

#include "chrome/common/ipc_message.h"
#include "mozilla/net/PHttpChannelParams.h"
#include "nsHttp.h"
#include "nsIPrefBranch.h"
#include "nsIPrefService.h"
#include "nsURLHelper.h"

namespace mozilla {
namespace net {

void AssertRoundTrips(const nsHttpResponseHead& aHead) {
  {
    // Assert it round-trips via IPC.
    UniquePtr<IPC::Message> msg(new IPC::Message(MSG_ROUTING_NONE, 0));
    IPC::MessageWriter writer(*msg);
    IPC::ParamTraits<nsHttpResponseHead>::Write(&writer, aHead);

    nsHttpResponseHead deserializedHead;
    IPC::MessageReader reader(*msg);
    bool res = IPC::ParamTraits<mozilla::net::nsHttpResponseHead>::Read(
        &reader, &deserializedHead);
    ASSERT_TRUE(res);
    ASSERT_EQ(aHead, deserializedHead);
  }

  {
    // Assert it round-trips through copy-ctor.
    nsHttpResponseHead copied(aHead);
    ASSERT_EQ(aHead, copied);
  }

  {
    // Assert it round-trips through operator=
    nsHttpResponseHead copied;
    copied = aHead;
    // It is important that the below statement cannot be
    // ASSERT_EQ(aHead, copied) to avoid potential lock-order inversion problem.
    // See Bug 1829445 for more details
    ASSERT_EQ(copied, aHead);
  }
}

TEST(TestHttpResponseHead, Bug1636930)
{
  nsHttpResponseHead head;

  (void)head.ParseStatusLine("HTTP/1.1 200 OK"_ns);
  (void)head.ParseHeaderLine("content-type: text/plain"_ns);
  (void)head.ParseHeaderLine("etag: Just testing"_ns);
  (void)head.ParseHeaderLine("cache-control: max-age=99999"_ns);
  (void)head.ParseHeaderLine("accept-ranges: bytes"_ns);
  (void)head.ParseHeaderLine("content-length: 1408"_ns);
  (void)head.ParseHeaderLine("connection: close"_ns);
  (void)head.ParseHeaderLine("server: httpd.js"_ns);
  (void)head.ParseHeaderLine("date: Tue, 12 May 2020 09:24:23 GMT"_ns);

  AssertRoundTrips(head);
}

TEST(TestHttpResponseHead, bug1649807)
{
  nsHttpResponseHead head;

  (void)head.ParseStatusLine("HTTP/1.1 200 OK"_ns);
  (void)head.ParseHeaderLine("content-type: text/plain"_ns);
  (void)head.ParseHeaderLine("etag: Just testing"_ns);
  (void)head.ParseHeaderLine(
      "cache-control: public, max-age=31536000, immutable"_ns);
  (void)head.ParseHeaderLine("accept-ranges: bytes"_ns);
  (void)head.ParseHeaderLine("content-length: 1408"_ns);
  (void)head.ParseHeaderLine("connection: close"_ns);
  (void)head.ParseHeaderLine("server: httpd.js"_ns);
  (void)head.ParseHeaderLine("pragma: no-cache"_ns);
  (void)head.ParseHeaderLine("date: Tue, 12 May 2020 09:24:23 GMT"_ns);

  ASSERT_FALSE(head.NoCache())
  << "Cache-Control: immutable wins over Pragma: no-cache";
  AssertRoundTrips(head);
}

TEST(TestHttpResponseHead, bug1937766)
{
  nsHttpResponseHead head;

  (void)head.ParseStatusLine("HTTP/1.1 200 OK"_ns);
  (void)head.ParseHeaderLine("content-type: text/plain"_ns);
  (void)head.ParseHeaderLine("etag: Just testing"_ns);
  (void)head.ParseHeaderLine("cache-control: age=99999"_ns);
  (void)head.ParseHeaderLine("accept-ranges: bytes"_ns);
  (void)head.ParseHeaderLine("content-length: 1408"_ns);
  (void)head.ParseHeaderLine("connection: close"_ns);
  (void)head.ParseHeaderLine("server: httpd.js"_ns);
  (void)head.ParseHeaderLine("pragma: no-cache"_ns);
  (void)head.ParseHeaderLine("date: Tue, 12 May 2020 09:24:23 GMT"_ns);

  ASSERT_TRUE(head.NoCache())
  << "Pragma: no-cache wins over Cache-Control";
  AssertRoundTrips(head);
}

TEST(TestHttpResponseHead, bug1660200)
{
  nsHttpResponseHead head;

  (void)head.ParseStatusLine("HTTP/1.1 200 OK"_ns);
  (void)head.ParseHeaderLine("content-type: text/plain"_ns);
  (void)head.ParseHeaderLine("etag: Just testing"_ns);
  (void)head.ParseHeaderLine("cache-control: no-cache"_ns);
  (void)head.ParseHeaderLine("accept-ranges: bytes"_ns);
  (void)head.ParseHeaderLine("content-length: 1408"_ns);
  (void)head.ParseHeaderLine("connection: close"_ns);
  (void)head.ParseHeaderLine("server: httpd.js"_ns);
  (void)head.ParseHeaderLine("date: Tue, 12 May 2020 09:24:23 GMT"_ns);

  AssertRoundTrips(head);
}

TEST(TestHttpResponseHead, bug1687903)
{
  nsHttpResponseHead head;

  nsCOMPtr<nsIPrefBranch> prefs = do_GetService(NS_PREFSERVICE_CONTRACTID);

  nsresult expectation = NS_ERROR_PARSING_HTTP_STATUS_LINE;

  ASSERT_EQ(expectation, head.ParseStatusLine("HTTP/1.1 "_ns));
  ASSERT_EQ(expectation, head.ParseStatusLine("HTTP/1.1 BLAH"_ns));
  ASSERT_EQ(expectation, head.ParseStatusLine("HTTP/1.1 1000 BOO"_ns));
  ASSERT_EQ(expectation, head.ParseStatusLine("HTTP/1.1 0200 BOO"_ns));
  ASSERT_EQ(expectation, head.ParseStatusLine("HTTP/1.1 60200 200"_ns));
  ASSERT_EQ(expectation, head.ParseStatusLine("HTTP/1.1 131072 HIOK"_ns));
  ASSERT_EQ(expectation, head.ParseStatusLine("HTTP/1.1 -200 OK"_ns));
  ASSERT_EQ(expectation, head.ParseStatusLine("HTTP/1.1 0x9 OK"_ns));
  ASSERT_EQ(expectation, head.ParseStatusLine("HTTP/1.1 C8 OK"_ns));
}

TEST(TestHttpResponseHead, atoms)
{
  // Test that the resolving the content-type atom returns the initial static
  ASSERT_EQ(nsHttp::Content_Type, nsHttp::ResolveAtom("content-type"_ns));
  // Check that they're case insensitive
  ASSERT_EQ(nsHttp::ResolveAtom("Content-Type"_ns),
            nsHttp::ResolveAtom("content-type"_ns));
  // This string literal should be the backing of the atom when resolved first
  auto header1 = "CustomHeaderXXX1"_ns;
  auto atom1 = nsHttp::ResolveAtom(header1);
  auto header2 = "customheaderxxx1"_ns;
  auto atom2 = nsHttp::ResolveAtom(header2);
  ASSERT_EQ(atom1, atom2);
  ASSERT_EQ(atom1.get(), atom2.get());
  // Check that we get the expected pointer back.
  ASSERT_EQ(atom2.get(), header1.BeginReading());
}

TEST(ContentTypeParsing, CommentHandling1)
{
  bool dummy;
  const nsAutoCString val("text/html;charset=gbk(");
  nsCString contentType;
  nsCString contentCharset;

  net_ParseContentType(val, contentType, contentCharset, &dummy);

  ASSERT_TRUE(contentType.EqualsLiteral("text/html"));
  ASSERT_TRUE(contentCharset.EqualsLiteral("gbk("));
}

TEST(ContentTypeParsing, CommentHandling2)
{
  bool dummy;
  const nsAutoCString val("text/html;x=(;charset=gbk");
  nsCString contentType;
  nsCString contentCharset;

  net_ParseContentType(val, contentType, contentCharset, &dummy);

  ASSERT_TRUE(contentType.EqualsLiteral("text/html"));
  ASSERT_TRUE(contentCharset.EqualsLiteral("gbk"));
}

TEST(ContentTypeParsing, CommentHandling3)
{
  bool dummy;
  const nsAutoCString val("text/html;test=test;(;charset=gbk");
  nsCString contentType;
  nsCString contentCharset;

  net_ParseContentType(val, contentType, contentCharset, &dummy);

  ASSERT_TRUE(contentType.EqualsLiteral("text/html"));
  ASSERT_TRUE(contentCharset.EqualsLiteral("gbk"));
}

TEST(TestHttpResponseHead, MoveConstructor)
{
  // Construct an initial response head
  nsHttpResponseHead originalHead;
  (void)originalHead.ParseStatusLine("HTTP/1.1 200 OK"_ns);
  (void)originalHead.ParseHeaderLine("content-type: text/plain"_ns);
  (void)originalHead.ParseHeaderLine("content-length: 1408"_ns);

  // Move construct a new object from the initial one
  nsHttpResponseHead movedHead(std::move(originalHead));

  // Ensure the moved head retains the original status
  ASSERT_EQ(movedHead.Status(), 200);
  nsCString value;
  ASSERT_EQ(movedHead.GetHeader(nsHttp::Content_Type, value), NS_OK);
  ASSERT_EQ(value, "text/plain"_ns);
  ASSERT_EQ(movedHead.GetHeader(nsHttp::Content_Length, value), NS_OK);
  ASSERT_EQ(value, "1408"_ns);

  // Check original object is in an unspecified state
  ASSERT_EQ(originalHead.GetHeader(nsHttp::Content_Type, value),
            NS_ERROR_NOT_AVAILABLE);
  ASSERT_FALSE(originalHead.HasHeader(nsHttp::Content_Type));
  ASSERT_FALSE(originalHead.HasHeader(nsHttp::Content_Length));
}

TEST(TestHttpResponseHead, MultipleCacheControl)
{
  nsHttpResponseHead originalHead;
  (void)originalHead.ParseStatusLine("HTTP/1.1 200 OK"_ns);
  (void)originalHead.ParseHeaderLine("content-type: text/plain"_ns);
  (void)originalHead.ParseHeaderLine("content-length: 1408"_ns);
  (void)originalHead.ParseHeaderLine("cache-control: no-cache"_ns);
  (void)originalHead.ParseHeaderLine("cache-control: no-store"_ns);

  ASSERT_EQ(originalHead.NoStore(), true);
  ASSERT_EQ(originalHead.NoCache(), true);
}

}  // namespace net
}  // namespace mozilla