summaryrefslogtreecommitdiffstats
path: root/netwerk/test/gtest/TestServerTimingHeader.cpp
blob: 183726a4406265268235aa527c010d58cd5e7c12 (plain)
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
237
238
#include "gtest/gtest.h"

#include "mozilla/Unused.h"
#include "mozilla/net/nsServerTiming.h"
#include <string>
#include <vector>

using namespace mozilla;
using namespace mozilla::net;

void testServerTimingHeader(
    const char* headerValue,
    std::vector<std::vector<std::string>> expectedResults) {
  nsAutoCString header(headerValue);
  ServerTimingParser parser(header);
  parser.Parse();

  nsTArray<nsCOMPtr<nsIServerTiming>> results =
      parser.TakeServerTimingHeaders();

  ASSERT_EQ(results.Length(), expectedResults.size());

  unsigned i = 0;
  for (const auto& header : results) {
    std::vector<std::string> expectedResult(expectedResults[i++]);
    nsCString name;
    mozilla::Unused << header->GetName(name);
    ASSERT_TRUE(name.Equals(expectedResult[0].c_str()));

    double duration;
    mozilla::Unused << header->GetDuration(&duration);
    ASSERT_EQ(duration, atof(expectedResult[1].c_str()));

    nsCString description;
    mozilla::Unused << header->GetDescription(description);
    ASSERT_TRUE(description.Equals(expectedResult[2].c_str()));
  }
}

TEST(TestServerTimingHeader, HeaderParsing)
{
  // Test cases below are copied from
  // https://cs.chromium.org/chromium/src/third_party/WebKit/Source/platform/network/HTTPParsersTest.cpp

  testServerTimingHeader("", {});
  testServerTimingHeader("metric", {{"metric", "0", ""}});
  testServerTimingHeader("metric;dur", {{"metric", "0", ""}});
  testServerTimingHeader("metric;dur=123.4", {{"metric", "123.4", ""}});
  testServerTimingHeader("metric;dur=\"123.4\"", {{"metric", "123.4", ""}});

  testServerTimingHeader("metric;desc", {{"metric", "0", ""}});
  testServerTimingHeader("metric;desc=description",
                         {{"metric", "0", "description"}});
  testServerTimingHeader("metric;desc=\"description\"",
                         {{"metric", "0", "description"}});

  testServerTimingHeader("metric;dur;desc", {{"metric", "0", ""}});
  testServerTimingHeader("metric;dur=123.4;desc", {{"metric", "123.4", ""}});
  testServerTimingHeader("metric;dur;desc=description",
                         {{"metric", "0", "description"}});
  testServerTimingHeader("metric;dur=123.4;desc=description",
                         {{"metric", "123.4", "description"}});
  testServerTimingHeader("metric;desc;dur", {{"metric", "0", ""}});
  testServerTimingHeader("metric;desc;dur=123.4", {{"metric", "123.4", ""}});
  testServerTimingHeader("metric;desc=description;dur",
                         {{"metric", "0", "description"}});
  testServerTimingHeader("metric;desc=description;dur=123.4",
                         {{"metric", "123.4", "description"}});

  // special chars in name
  testServerTimingHeader("aB3!#$%&'*+-.^_`|~",
                         {{"aB3!#$%&'*+-.^_`|~", "0", ""}});

  // delimiter chars in quoted description
  testServerTimingHeader("metric;desc=\"descr;,=iption\";dur=123.4",
                         {{"metric", "123.4", "descr;,=iption"}});

  // whitespace
  testServerTimingHeader("metric ; ", {{"metric", "0", ""}});
  testServerTimingHeader("metric , ", {{"metric", "0", ""}});
  testServerTimingHeader("metric ; dur = 123.4 ; desc = description",
                         {{"metric", "123.4", "description"}});
  testServerTimingHeader("metric ; desc = description ; dur = 123.4",
                         {{"metric", "123.4", "description"}});

  // multiple entries
  testServerTimingHeader(
      "metric1;dur=12.3;desc=description1,metric2;dur=45.6;"
      "desc=description2,metric3;dur=78.9;desc=description3",
      {{"metric1", "12.3", "description1"},
       {"metric2", "45.6", "description2"},
       {"metric3", "78.9", "description3"}});
  testServerTimingHeader("metric1,metric2 ,metric3, metric4 , metric5",
                         {{"metric1", "0", ""},
                          {"metric2", "0", ""},
                          {"metric3", "0", ""},
                          {"metric4", "0", ""},
                          {"metric5", "0", ""}});

  // quoted-strings
  // metric;desc=\ --> ''
  testServerTimingHeader("metric;desc=\\", {{"metric", "0", ""}});
  // metric;desc=" --> ''
  testServerTimingHeader("metric;desc=\"", {{"metric", "0", ""}});
  // metric;desc=\\ --> ''
  testServerTimingHeader("metric;desc=\\\\", {{"metric", "0", ""}});
  // metric;desc=\" --> ''
  testServerTimingHeader("metric;desc=\\\"", {{"metric", "0", ""}});
  // metric;desc="\ --> ''
  testServerTimingHeader("metric;desc=\"\\", {{"metric", "0", ""}});
  // metric;desc="" --> ''
  testServerTimingHeader("metric;desc=\"\"", {{"metric", "0", ""}});
  // metric;desc=\\\ --> ''
  testServerTimingHeader(R"(metric;desc=\\\)", {{"metric", "0", ""}});
  // metric;desc=\\" --> ''
  testServerTimingHeader(R"(metric;desc=\\")", {{"metric", "0", ""}});
  // metric;desc=\"\ --> ''
  testServerTimingHeader(R"(metric;desc=\"\)", {{"metric", "0", ""}});
  // metric;desc=\"" --> ''
  testServerTimingHeader(R"(metric;desc=\"")", {{"metric", "0", ""}});
  // metric;desc="\\ --> ''
  testServerTimingHeader(R"(metric;desc="\\)", {{"metric", "0", ""}});
  // metric;desc="\" --> ''
  testServerTimingHeader(R"(metric;desc="\")", {{"metric", "0", ""}});
  // metric;desc=""\ --> ''
  testServerTimingHeader(R"(metric;desc=""\)", {{"metric", "0", ""}});
  // metric;desc=""" --> ''
  testServerTimingHeader(R"(metric;desc=""")", {{"metric", "0", ""}});
  // metric;desc=\\\\ --> ''
  testServerTimingHeader(R"(metric;desc=\\\\)", {{"metric", "0", ""}});
  // metric;desc=\\\" --> ''
  testServerTimingHeader(R"(metric;desc=\\\")", {{"metric", "0", ""}});
  // metric;desc=\\"\ --> ''
  testServerTimingHeader(R"(metric;desc=\\"\)", {{"metric", "0", ""}});
  // metric;desc=\\"" --> ''
  testServerTimingHeader(R"(metric;desc=\\"")", {{"metric", "0", ""}});
  // metric;desc=\"\\ --> ''
  testServerTimingHeader(R"(metric;desc=\"\\)", {{"metric", "0", ""}});
  // metric;desc=\"\" --> ''
  testServerTimingHeader(R"(metric;desc=\"\")", {{"metric", "0", ""}});
  // metric;desc=\""\ --> ''
  testServerTimingHeader(R"(metric;desc=\""\)", {{"metric", "0", ""}});
  // metric;desc=\""" --> ''
  testServerTimingHeader(R"(metric;desc=\""")", {{"metric", "0", ""}});
  // metric;desc="\\\ --> ''
  testServerTimingHeader(R"(metric;desc="\\\)", {{"metric", "0", ""}});
  // metric;desc="\\" --> '\'
  testServerTimingHeader(R"(metric;desc="\\")", {{"metric", "0", "\\"}});
  // metric;desc="\"\ --> ''
  testServerTimingHeader(R"(metric;desc="\"\)", {{"metric", "0", ""}});
  // metric;desc="\"" --> '"'
  testServerTimingHeader(R"(metric;desc="\"")", {{"metric", "0", "\""}});
  // metric;desc=""\\ --> ''
  testServerTimingHeader(R"(metric;desc=""\\)", {{"metric", "0", ""}});
  // metric;desc=""\" --> ''
  testServerTimingHeader(R"(metric;desc=""\")", {{"metric", "0", ""}});
  // metric;desc="""\ --> ''
  testServerTimingHeader(R"(metric;desc="""\)", {{"metric", "0", ""}});
  // metric;desc="""" --> ''
  testServerTimingHeader(R"(metric;desc="""")", {{"metric", "0", ""}});

  // duplicate entry names
  testServerTimingHeader(
      "metric;dur=12.3;desc=description1,metric;dur=45.6;"
      "desc=description2",
      {{"metric", "12.3", "description1"}, {"metric", "45.6", "description2"}});

  // non-numeric durations
  testServerTimingHeader("metric;dur=foo", {{"metric", "0", ""}});
  testServerTimingHeader("metric;dur=\"foo\"", {{"metric", "0", ""}});

  // unrecognized param names
  testServerTimingHeader(
      "metric;foo=bar;desc=description;foo=bar;dur=123.4;foo=bar",
      {{"metric", "123.4", "description"}});

  // duplicate param names
  testServerTimingHeader("metric;dur=123.4;dur=567.8",
                         {{"metric", "123.4", ""}});
  testServerTimingHeader("metric;desc=description1;desc=description2",
                         {{"metric", "0", "description1"}});
  testServerTimingHeader("metric;dur=foo;dur=567.8", {{"metric", "", ""}});

  // unspecified param values
  testServerTimingHeader("metric;dur;dur=123.4", {{"metric", "0", ""}});
  testServerTimingHeader("metric;desc;desc=description", {{"metric", "0", ""}});

  // param name case
  testServerTimingHeader("metric;DuR=123.4;DeSc=description",
                         {{"metric", "123.4", "description"}});

  // nonsense
  testServerTimingHeader("metric=foo;dur;dur=123.4,metric2",
                         {{"metric", "0", ""}, {"metric2", "0", ""}});
  testServerTimingHeader("metric\"foo;dur;dur=123.4,metric2",
                         {{"metric", "0", ""}});

  // nonsense - return zero entries
  testServerTimingHeader(" ", {});
  testServerTimingHeader("=", {});
  testServerTimingHeader("[", {});
  testServerTimingHeader("]", {});
  testServerTimingHeader(";", {});
  testServerTimingHeader(",", {});
  testServerTimingHeader("=;", {});
  testServerTimingHeader(";=", {});
  testServerTimingHeader("=,", {});
  testServerTimingHeader(",=", {});
  testServerTimingHeader(";,", {});
  testServerTimingHeader(",;", {});
  testServerTimingHeader("=;,", {});

  // Invalid token
  testServerTimingHeader("met=ric", {{"met", "0", ""}});
  testServerTimingHeader("met ric", {{"met", "0", ""}});
  testServerTimingHeader("met[ric", {{"met", "0", ""}});
  testServerTimingHeader("met]ric", {{"met", "0", ""}});
  testServerTimingHeader("metric;desc=desc=123, metric2",
                         {{"metric", "0", "desc"}, {"metric2", "0", ""}});
  testServerTimingHeader("met ric;desc=de sc  , metric2",
                         {{"met", "0", "de"}, {"metric2", "0", ""}});

  // test cases from https://w3c.github.io/server-timing/#examples
  testServerTimingHeader(
      " miss, ,db;dur=53, app;dur=47.2 ",
      {{"miss", "0", ""}, {"db", "53", ""}, {"app", "47.2", ""}});
  testServerTimingHeader(" customView, dc;desc=atl ",
                         {{"customView", "0", ""}, {"dc", "0", "atl"}});
  testServerTimingHeader(" total;dur=123.4 ", {{"total", "123.4", ""}});

  // test cases for comma in quoted string
  testServerTimingHeader(R"(     metric ; desc="descr\"\";,=iption";dur=123.4)",
                         {{"metric", "123.4", "descr\"\";,=iption"}});
  testServerTimingHeader(
      " metric2;dur=\"123.4\";;desc=\",;\\\",;,\";;,  metric  ;  desc = \" "
      "\\\", ;\\\" \"; dur=123.4,",
      {{"metric2", "123.4", ",;\",;,"}, {"metric", "123.4", " \", ;\" "}});
}