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
|
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "flip_protocol.h"
#include "base/scoped_ptr.h"
#include "flip_bitmasks.h" // shared with server.
#include "flip_framer.h"
#ifdef _WIN32
#include "testing/platform_test.h"
#else
#include "testing/base/public/gunit.h"
#endif
using flip::FlipDataFrame;
using flip::FlipFrame;
using flip::FlipControlFrame;
using flip::FlipSynStreamControlFrame;
using flip::FlipSynReplyControlFrame;
using flip::FlipFinStreamControlFrame;
using flip::FlipFramer;
using flip::FlipHeaderBlock;
using flip::FlagsAndLength;
using flip::kLengthMask;
using flip::kStreamIdMask;
using flip::kFlipProtocolVersion;
using flip::SYN_STREAM;
using flip::SYN_REPLY;
using flip::FIN_STREAM;
using flip::CONTROL_FLAG_FIN;
using flip::CONTROL_FLAG_NONE;
namespace {
// Test our protocol constants
TEST(FlipProtocolTest, ProtocolConstants) {
EXPECT_EQ(8u, FlipFrame::size());
EXPECT_EQ(8u, FlipDataFrame::size());
EXPECT_EQ(12u, FlipControlFrame::size());
EXPECT_EQ(14u, FlipSynStreamControlFrame::size());
EXPECT_EQ(14u, FlipSynReplyControlFrame::size());
EXPECT_EQ(16u, FlipFinStreamControlFrame::size());
EXPECT_EQ(4u, sizeof(FlagsAndLength));
EXPECT_EQ(1, SYN_STREAM);
EXPECT_EQ(2, SYN_REPLY);
EXPECT_EQ(3, FIN_STREAM);
}
// Test some of the protocol helper functions
TEST(FlipProtocolTest, FrameStructs) {
FlipFrame frame(FlipFrame::size());
frame.set_length(12345);
frame.set_flags(10);
EXPECT_EQ(12345u, frame.length());
EXPECT_EQ(10u, frame.flags());
EXPECT_EQ(false, frame.is_control_frame());
frame.set_length(0);
frame.set_flags(10);
EXPECT_EQ(0u, frame.length());
EXPECT_EQ(10u, frame.flags());
EXPECT_EQ(false, frame.is_control_frame());
}
TEST(FlipProtocolTest, DataFrameStructs) {
FlipDataFrame data_frame;
data_frame.set_stream_id(12345);
EXPECT_EQ(12345u, data_frame.stream_id());
}
TEST(FlipProtocolTest, ControlFrameStructs) {
FlipFramer framer;
FlipHeaderBlock headers;
scoped_ptr<FlipSynStreamControlFrame> syn_frame(
framer.CreateSynStream(123, 2, CONTROL_FLAG_FIN, false, &headers));
EXPECT_EQ(kFlipProtocolVersion, syn_frame->version());
EXPECT_EQ(true, syn_frame->is_control_frame());
EXPECT_EQ(SYN_STREAM, syn_frame->type());
EXPECT_EQ(123u, syn_frame->stream_id());
EXPECT_EQ(2u, syn_frame->priority());
EXPECT_EQ(2, syn_frame->header_block_len());
EXPECT_EQ(1u, syn_frame->flags());
scoped_ptr<FlipSynReplyControlFrame> syn_reply(
framer.CreateSynReply(123, CONTROL_FLAG_NONE, false, &headers));
EXPECT_EQ(kFlipProtocolVersion, syn_reply->version());
EXPECT_EQ(true, syn_reply->is_control_frame());
EXPECT_EQ(SYN_REPLY, syn_reply->type());
EXPECT_EQ(123u, syn_reply->stream_id());
EXPECT_EQ(2, syn_reply->header_block_len());
EXPECT_EQ(0, syn_reply->flags());
scoped_ptr<FlipFinStreamControlFrame> fin_frame(
framer.CreateFinStream(123, 444));
EXPECT_EQ(kFlipProtocolVersion, fin_frame->version());
EXPECT_EQ(true, fin_frame->is_control_frame());
EXPECT_EQ(FIN_STREAM, fin_frame->type());
EXPECT_EQ(123u, fin_frame->stream_id());
EXPECT_EQ(444u, fin_frame->status());
fin_frame->set_status(555);
EXPECT_EQ(555u, fin_frame->status());
EXPECT_EQ(0, fin_frame->flags());
}
TEST(FlipProtocolTest, TestDataFrame) {
FlipDataFrame frame;
// Set the stream ID to various values.
frame.set_stream_id(0);
EXPECT_EQ(0, frame.stream_id());
EXPECT_FALSE(frame.is_control_frame());
frame.set_stream_id(~0 & kStreamIdMask);
EXPECT_EQ(~0 & kStreamIdMask, frame.stream_id());
EXPECT_FALSE(frame.is_control_frame());
// Set length to various values. Make sure that when you set_length(x),
// length() == x. Also make sure the flags are unaltered.
memset(frame.data(), '1', FlipDataFrame::size());
int8 flags = frame.flags();
frame.set_length(0);
EXPECT_EQ(0, frame.length());
EXPECT_EQ(flags, frame.flags());
frame.set_length(kLengthMask);
EXPECT_EQ(kLengthMask, frame.length());
EXPECT_EQ(flags, frame.flags());
frame.set_length(5);
EXPECT_EQ(5, frame.length());
EXPECT_EQ(flags, frame.flags());
// Set flags to various values. Make sure that when you set_flags(x),
// flags() == x. Also make sure the length is unaltered.
memset(frame.data(), '1', FlipDataFrame::size());
uint32 length = frame.length();
frame.set_flags(0);
EXPECT_EQ(0, frame.flags());
EXPECT_EQ(length, frame.length());
int8 all_flags = ~0;
frame.set_flags(all_flags);
flags = frame.flags();
EXPECT_EQ(all_flags, flags);
EXPECT_EQ(length, frame.length());
frame.set_flags(5);
EXPECT_EQ(5, frame.flags());
EXPECT_EQ(length, frame.length());
}
// Make sure that overflows both die in debug mode, and do not cause problems
// in opt mode. Note: Chrome doesn't die on DCHECK failures, so the
// EXPECT_DEBUG_DEATH doesn't work.
TEST(FlipProtocolDeathTest, TestDataFrame) {
FlipDataFrame frame;
frame.set_stream_id(0);
#ifndef WIN32
EXPECT_DEBUG_DEATH(frame.set_stream_id(~0), "");
#endif
EXPECT_FALSE(frame.is_control_frame());
frame.set_flags(0);
#ifndef WIN32
EXPECT_DEBUG_DEATH(frame.set_length(~0), "");
#endif
EXPECT_EQ(0, frame.flags());
}
TEST(FlipProtocolDeathTest, TestFlipControlFrame) {
FlipControlFrame frame(FlipControlFrame::size());
memset(frame.data(), '1', FlipControlFrame::size());
// Set the stream ID to various values.
frame.set_stream_id(0);
EXPECT_EQ(0, frame.stream_id());
EXPECT_FALSE(frame.is_control_frame());
frame.set_stream_id(~0 & kStreamIdMask);
EXPECT_EQ(~0 & kStreamIdMask, frame.stream_id());
EXPECT_FALSE(frame.is_control_frame());
}
} // namespace
|