aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/com/squareup/okhttp/internal/spdy/MockSpdyPeer.java
blob: 088061b1eaf4ed50074cc8507336182405db6e28 (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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.squareup.okhttp.internal.spdy;

import com.squareup.okhttp.internal.Util;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;

import static java.util.concurrent.Executors.defaultThreadFactory;

/** Replays prerecorded outgoing frames and records incoming frames. */
public final class MockSpdyPeer implements Closeable {
  private int frameCount = 0;
  private final ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
  private final SpdyWriter spdyWriter = new SpdyWriter(bytesOut);
  private final List<OutFrame> outFrames = new ArrayList<OutFrame>();
  private final BlockingQueue<InFrame> inFrames = new LinkedBlockingQueue<InFrame>();
  private int port;
  private final Executor executor = Executors.newCachedThreadPool(defaultThreadFactory());
  private ServerSocket serverSocket;
  private Socket socket;

  public void acceptFrame() {
    frameCount++;
  }

  public SpdyWriter sendFrame() {
    outFrames.add(new OutFrame(frameCount++, bytesOut.size(), Integer.MAX_VALUE));
    return spdyWriter;
  }

  /**
   * Sends a frame, truncated to {@code truncateToLength} bytes. This is only
   * useful for testing error handling as the truncated frame will be
   * malformed.
   */
  public SpdyWriter sendTruncatedFrame(int truncateToLength) {
    outFrames.add(new OutFrame(frameCount++, bytesOut.size(), truncateToLength));
    return spdyWriter;
  }

  public int getPort() {
    return port;
  }

  public InFrame takeFrame() throws InterruptedException {
    return inFrames.take();
  }

  public void play() throws IOException {
    if (serverSocket != null) throw new IllegalStateException();
    serverSocket = new ServerSocket(0);
    serverSocket.setReuseAddress(true);
    this.port = serverSocket.getLocalPort();
    executor.execute(new Runnable() {
      @Override public void run() {
        try {
          readAndWriteFrames();
        } catch (IOException e) {
          throw new RuntimeException(e);
        }
      }
    });
  }

  private void readAndWriteFrames() throws IOException {
    if (socket != null) throw new IllegalStateException();
    socket = serverSocket.accept();
    OutputStream out = socket.getOutputStream();
    InputStream in = socket.getInputStream();
    SpdyReader reader = new SpdyReader(in);

    Iterator<OutFrame> outFramesIterator = outFrames.iterator();
    byte[] outBytes = bytesOut.toByteArray();
    OutFrame nextOutFrame = null;

    for (int i = 0; i < frameCount; i++) {
      if (nextOutFrame == null && outFramesIterator.hasNext()) {
        nextOutFrame = outFramesIterator.next();
      }

      if (nextOutFrame != null && nextOutFrame.sequence == i) {
        int start = nextOutFrame.start;
        int truncateToLength = nextOutFrame.truncateToLength;
        int end;
        if (outFramesIterator.hasNext()) {
          nextOutFrame = outFramesIterator.next();
          end = nextOutFrame.start;
        } else {
          end = outBytes.length;
        }

        // write a frame
        int length = Math.min(end - start, truncateToLength);
        out.write(outBytes, start, length);
      } else {
        // read a frame
        InFrame inFrame = new InFrame(i, reader);
        reader.nextFrame(inFrame);
        inFrames.add(inFrame);
      }
    }
    Util.closeQuietly(socket);
  }

  public Socket openSocket() throws IOException {
    return new Socket("localhost", port);
  }

  @Override public void close() throws IOException {
    Socket socket = this.socket;
    if (socket != null) {
      socket.close();
      this.socket = null;
    }
    ServerSocket serverSocket = this.serverSocket;
    if (serverSocket != null) {
      serverSocket.close();
      this.serverSocket = null;
    }
  }

  private static class OutFrame {
    private final int sequence;
    private final int start;
    private final int truncateToLength;

    private OutFrame(int sequence, int start, int truncateToLength) {
      this.sequence = sequence;
      this.start = start;
      this.truncateToLength = truncateToLength;
    }
  }

  public static class InFrame implements SpdyReader.Handler {
    public final int sequence;
    public final SpdyReader reader;
    public int type = -1;
    public int flags;
    public int streamId;
    public int associatedStreamId;
    public int priority;
    public int slot;
    public int statusCode;
    public int deltaWindowSize;
    public List<String> nameValueBlock;
    public byte[] data;
    public Settings settings;

    public InFrame(int sequence, SpdyReader reader) {
      this.sequence = sequence;
      this.reader = reader;
    }

    @Override public void settings(int flags, Settings settings) {
      if (this.type != -1) throw new IllegalStateException();
      this.type = SpdyConnection.TYPE_SETTINGS;
      this.flags = flags;
      this.settings = settings;
    }

    @Override public void synStream(int flags, int streamId, int associatedStreamId, int priority,
        int slot, List<String> nameValueBlock) {
      if (this.type != -1) throw new IllegalStateException();
      this.type = SpdyConnection.TYPE_SYN_STREAM;
      this.flags = flags;
      this.streamId = streamId;
      this.associatedStreamId = associatedStreamId;
      this.priority = priority;
      this.slot = slot;
      this.nameValueBlock = nameValueBlock;
    }

    @Override public void synReply(int flags, int streamId, List<String> nameValueBlock) {
      if (this.type != -1) throw new IllegalStateException();
      this.type = SpdyConnection.TYPE_SYN_REPLY;
      this.streamId = streamId;
      this.flags = flags;
      this.nameValueBlock = nameValueBlock;
    }

    @Override public void headers(int flags, int streamId, List<String> nameValueBlock) {
      if (this.type != -1) throw new IllegalStateException();
      this.type = SpdyConnection.TYPE_HEADERS;
      this.streamId = streamId;
      this.flags = flags;
      this.nameValueBlock = nameValueBlock;
    }

    @Override public void data(int flags, int streamId, InputStream in, int length)
        throws IOException {
      if (this.type != -1) throw new IllegalStateException();
      this.type = SpdyConnection.TYPE_DATA;
      this.flags = flags;
      this.streamId = streamId;
      this.data = new byte[length];
      Util.readFully(in, this.data);
    }

    @Override public void rstStream(int flags, int streamId, int statusCode) {
      if (this.type != -1) throw new IllegalStateException();
      this.type = SpdyConnection.TYPE_RST_STREAM;
      this.flags = flags;
      this.streamId = streamId;
      this.statusCode = statusCode;
    }

    @Override public void ping(int flags, int streamId) {
      if (this.type != -1) throw new IllegalStateException();
      this.type = SpdyConnection.TYPE_PING;
      this.flags = flags;
      this.streamId = streamId;
    }

    @Override public void noop() {
      if (this.type != -1) throw new IllegalStateException();
      this.type = SpdyConnection.TYPE_NOOP;
    }

    @Override public void goAway(int flags, int lastGoodStreamId, int statusCode) {
      if (this.type != -1) throw new IllegalStateException();
      this.type = SpdyConnection.TYPE_GOAWAY;
      this.flags = flags;
      this.streamId = lastGoodStreamId;
      this.statusCode = statusCode;
    }

    @Override public void windowUpdate(int flags, int streamId, int deltaWindowSize) {
      if (this.type != -1) throw new IllegalStateException();
      this.type = SpdyConnection.TYPE_WINDOW_UPDATE;
      this.flags = flags;
      this.streamId = streamId;
      this.deltaWindowSize = deltaWindowSize;
    }
  }
}