summaryrefslogtreecommitdiffstats
path: root/media/mtransport/test/dummysocket.h
blob: a5d138940f270624863478fd157af76a978de5b7 (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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

// Original authors: ekr@rtfm.com; ryan@tokbox.com

#ifndef MTRANSPORT_DUMMY_SOCKET_H_
#define MTRANSPORT_DUMMY_SOCKET_H_

#include "nr_socket_prsock.h"

extern "C" {
#include "transport_addr.h"
}

#include "databuffer.h"
#include "mozilla/UniquePtr.h"

#define GTEST_HAS_RTTI 0
#include "gtest/gtest.h"
#include "gtest_utils.h"

namespace mozilla {

static UniquePtr<DataBuffer> merge(UniquePtr<DataBuffer> a, UniquePtr<DataBuffer> b) {
  if (a && a->len() && b && b->len()) {
    UniquePtr<DataBuffer> merged(new DataBuffer());
    merged->Allocate(a->len() + b->len());

    memcpy(merged->data(), a->data(), a->len());
    memcpy(merged->data() + a->len(), b->data(), b->len());

    return merged;
  }

  if (a && a->len()) {
    return a;
  }

  if (b && b->len()) {
    return b;
  }

  return nullptr;
}

class DummySocket : public NrSocketBase {
 public:
  DummySocket()
      : writable_(UINT_MAX),
        write_buffer_(nullptr),
        readable_(UINT_MAX),
        read_buffer_(nullptr),
        cb_(nullptr),
        cb_arg_(nullptr),
        self_(nullptr) {}

  // the nr_socket APIs
  virtual int create(nr_transport_addr *addr) {
    return 0;
  }

  virtual int sendto(const void *msg, size_t len,
                     int flags, nr_transport_addr *to) {
    MOZ_CRASH();
    return 0;
  }

  virtual int recvfrom(void * buf, size_t maxlen,
                       size_t *len, int flags,
                       nr_transport_addr *from) {
    MOZ_CRASH();
    return 0;
  }

  virtual int getaddr(nr_transport_addr *addrp) {
    MOZ_CRASH();
    return 0;
  }

  virtual void close() {
  }

  virtual int connect(nr_transport_addr *addr) {
    nr_transport_addr_copy(&connect_addr_, addr);
    return 0;
  }

  virtual int listen(int backlog) {
    return 0;
  }

  virtual int accept(nr_transport_addr *addrp, nr_socket **sockp) {
    return 0;
  }


  virtual int write(const void *msg, size_t len, size_t *written) {
    size_t to_write = std::min(len, writable_);

    if (to_write) {
      UniquePtr<DataBuffer> msgbuf(new DataBuffer(static_cast<const uint8_t *>(msg), to_write));
      write_buffer_ = merge(Move(write_buffer_), Move(msgbuf));
    }

    *written = to_write;

    return 0;
  }

  virtual int read(void* buf, size_t maxlen, size_t *len) {
    if (!read_buffer_.get()) {
      return R_WOULDBLOCK;
    }

    size_t to_read = std::min(read_buffer_->len(),
                              std::min(maxlen, readable_));

    memcpy(buf, read_buffer_->data(), to_read);
    *len = to_read;

    if (to_read < read_buffer_->len()) {
      read_buffer_.reset(new DataBuffer(read_buffer_->data() + to_read,
                                    read_buffer_->len() - to_read));
    } else {
      read_buffer_.reset();
    }

    return 0;
  }

  // Implementations of the async_event APIs.
  // These are no-ops because we handle scheduling manually
  // for test purposes.
  virtual int async_wait(int how, NR_async_cb cb, void *cb_arg,
                         char *function, int line) {
    EXPECT_EQ(nullptr, cb_);
    cb_ = cb;
    cb_arg_ = cb_arg;

    return 0;
  }

  virtual int cancel(int how) {
    cb_ = nullptr;
    cb_arg_ = nullptr;

    return 0;
  }


  // Read/Manipulate the current state.
  void CheckWriteBuffer(const uint8_t *data, size_t len) {
    if (!len) {
      EXPECT_EQ(nullptr, write_buffer_.get());
    } else {
      EXPECT_NE(nullptr, write_buffer_.get());
      ASSERT_EQ(len, write_buffer_->len());
      ASSERT_EQ(0, memcmp(data, write_buffer_->data(), len));
    }
  }

  void ClearWriteBuffer() {
    write_buffer_.reset();
  }

  void SetWritable(size_t val) {
    writable_ = val;
  }

  void FireWritableCb() {
    NR_async_cb cb = cb_;
    void *cb_arg = cb_arg_;

    cb_ = nullptr;
    cb_arg_ = nullptr;

    cb(this, NR_ASYNC_WAIT_WRITE, cb_arg);
  }

  void SetReadBuffer(const uint8_t *data, size_t len) {
    EXPECT_EQ(nullptr, write_buffer_.get());
    read_buffer_.reset(new DataBuffer(data, len));
  }

  void ClearReadBuffer() {
    read_buffer_.reset();
  }

  void SetReadable(size_t val) {
    readable_ = val;
  }

  nr_socket *get_nr_socket() {
    if (!self_) {
      int r = nr_socket_create_int(this, vtbl(), &self_);
      AddRef();
      if (r)
        return nullptr;
    }

    return self_;
  }

  nr_transport_addr *get_connect_addr() {
    return &connect_addr_;
  }

  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(DummySocket);

 private:
  ~DummySocket() {}

  DISALLOW_COPY_ASSIGN(DummySocket);

  size_t writable_;  // Amount we allow someone to write.
  UniquePtr<DataBuffer> write_buffer_;
  size_t readable_;   // Amount we allow someone to read.
  UniquePtr<DataBuffer> read_buffer_;

  NR_async_cb cb_;
  void *cb_arg_;
  nr_socket *self_;

  nr_transport_addr connect_addr_;
};

} //namespace mozilla

#endif