summaryrefslogtreecommitdiffstats
path: root/third_party/libwebrtc/webrtc/rtc_base/testutils.cc
blob: f2b6946ed3302ff06848755c34c4d5bbc1391292 (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
/*
 *  Copyright 2007 The WebRTC Project Authors. All rights reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include "rtc_base/testutils.h"

namespace webrtc {
namespace testing {

StreamSink::StreamSink() = default;

StreamSink::~StreamSink() = default;

StreamSource::StreamSource() {
  Clear();
}

StreamSource::~StreamSource() = default;

StreamState StreamSource::GetState() const {
  return state_;
}

StreamResult StreamSource::Read(void* buffer,
                                size_t buffer_len,
                                size_t* read,
                                int* error) {
  if (SS_CLOSED == state_) {
    if (error)
      *error = -1;
    return SR_ERROR;
  }
  if ((SS_OPENING == state_) || (readable_data_.size() <= read_block_)) {
    return SR_BLOCK;
  }
  size_t count = std::min(buffer_len, readable_data_.size() - read_block_);
  memcpy(buffer, &readable_data_[0], count);
  size_t new_size = readable_data_.size() - count;
  // Avoid undefined access beyond the last element of the vector.
  // This only happens when new_size is 0.
  if (count < readable_data_.size()) {
    memmove(&readable_data_[0], &readable_data_[count], new_size);
  }
  readable_data_.resize(new_size);
  if (read)
    *read = count;
  return SR_SUCCESS;
}

StreamResult StreamSource::Write(const void* data,
                                 size_t data_len,
                                 size_t* written,
                                 int* error) {
  if (SS_CLOSED == state_) {
    if (error)
      *error = -1;
    return SR_ERROR;
  }
  if (SS_OPENING == state_) {
    return SR_BLOCK;
  }
  if (SIZE_UNKNOWN != write_block_) {
    if (written_data_.size() >= write_block_) {
      return SR_BLOCK;
    }
    if (data_len > (write_block_ - written_data_.size())) {
      data_len = write_block_ - written_data_.size();
    }
  }
  if (written)
    *written = data_len;
  const char* cdata = static_cast<const char*>(data);
  written_data_.insert(written_data_.end(), cdata, cdata + data_len);
  return SR_SUCCESS;
}

void StreamSource::Close() {
  state_ = SS_CLOSED;
}

SocketTestClient::SocketTestClient() {
  Init(nullptr, AF_INET);
}

SocketTestClient::SocketTestClient(AsyncSocket* socket) {
  Init(socket, socket->GetLocalAddress().family());
}

SocketTestClient::SocketTestClient(const SocketAddress& address) {
  Init(nullptr, address.family());
  socket_->Connect(address);
}

SocketTestClient::~SocketTestClient() = default;

SocketTestServer::SocketTestServer(const SocketAddress& address)
    : socket_(
          Thread::Current()->socketserver()->CreateAsyncSocket(address.family(),
                                                               SOCK_STREAM)) {
  socket_->SignalReadEvent.connect(this, &SocketTestServer::OnReadEvent);
  socket_->Bind(address);
  socket_->Listen(5);
}

SocketTestServer::~SocketTestServer() {
  clear();
}

}  // namespace testing
}  // namespace webrtc