henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef RTC_BASE_TESTUTILS_H_ |
| 12 | #define RTC_BASE_TESTUTILS_H_ |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 13 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 14 | // Utilities for testing rtc infrastructure in unittests |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 15 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 16 | #include <algorithm> |
| 17 | #include <map> |
| 18 | #include <memory> |
| 19 | #include <vector> |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #include "rtc_base/asyncsocket.h" |
| 21 | #include "rtc_base/checks.h" |
| 22 | #include "rtc_base/gunit.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 23 | #include "rtc_base/stream.h" |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 24 | |
| 25 | namespace webrtc { |
| 26 | namespace testing { |
| 27 | |
| 28 | using namespace rtc; |
| 29 | |
| 30 | /////////////////////////////////////////////////////////////////////////////// |
| 31 | // StreamSink - Monitor asynchronously signalled events from StreamInterface |
| 32 | // or AsyncSocket (which should probably be a StreamInterface. |
| 33 | /////////////////////////////////////////////////////////////////////////////// |
| 34 | |
| 35 | // Note: Any event that is an error is treaded as SSE_ERROR instead of that |
| 36 | // event. |
| 37 | |
| 38 | enum StreamSinkEvent { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 39 | SSE_OPEN = SE_OPEN, |
| 40 | SSE_READ = SE_READ, |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 41 | SSE_WRITE = SE_WRITE, |
| 42 | SSE_CLOSE = SE_CLOSE, |
| 43 | SSE_ERROR = 16 |
| 44 | }; |
| 45 | |
| 46 | class StreamSink : public sigslot::has_slots<> { |
| 47 | public: |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 48 | StreamSink(); |
| 49 | ~StreamSink() override; |
| 50 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 51 | void Monitor(StreamInterface* stream) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 52 | stream->SignalEvent.connect(this, &StreamSink::OnEvent); |
| 53 | events_.erase(stream); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 54 | } |
| 55 | void Unmonitor(StreamInterface* stream) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 56 | stream->SignalEvent.disconnect(this); |
| 57 | // In case you forgot to unmonitor a previous object with this address |
| 58 | events_.erase(stream); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 59 | } |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 60 | bool Check(StreamInterface* stream, |
| 61 | StreamSinkEvent event, |
| 62 | bool reset = true) { |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 63 | return DoCheck(stream, event, reset); |
| 64 | } |
| 65 | int Events(StreamInterface* stream, bool reset = true) { |
| 66 | return DoEvents(stream, reset); |
| 67 | } |
| 68 | |
| 69 | void Monitor(AsyncSocket* socket) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 70 | socket->SignalConnectEvent.connect(this, &StreamSink::OnConnectEvent); |
| 71 | socket->SignalReadEvent.connect(this, &StreamSink::OnReadEvent); |
| 72 | socket->SignalWriteEvent.connect(this, &StreamSink::OnWriteEvent); |
| 73 | socket->SignalCloseEvent.connect(this, &StreamSink::OnCloseEvent); |
| 74 | // In case you forgot to unmonitor a previous object with this address |
| 75 | events_.erase(socket); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 76 | } |
| 77 | void Unmonitor(AsyncSocket* socket) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 78 | socket->SignalConnectEvent.disconnect(this); |
| 79 | socket->SignalReadEvent.disconnect(this); |
| 80 | socket->SignalWriteEvent.disconnect(this); |
| 81 | socket->SignalCloseEvent.disconnect(this); |
| 82 | events_.erase(socket); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 83 | } |
| 84 | bool Check(AsyncSocket* socket, StreamSinkEvent event, bool reset = true) { |
| 85 | return DoCheck(socket, event, reset); |
| 86 | } |
| 87 | int Events(AsyncSocket* socket, bool reset = true) { |
| 88 | return DoEvents(socket, reset); |
| 89 | } |
| 90 | |
| 91 | private: |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 92 | typedef std::map<void*, int> EventMap; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 93 | |
| 94 | void OnEvent(StreamInterface* stream, int events, int error) { |
| 95 | if (error) { |
| 96 | events = SSE_ERROR; |
| 97 | } |
| 98 | AddEvents(stream, events); |
| 99 | } |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 100 | void OnConnectEvent(AsyncSocket* socket) { AddEvents(socket, SSE_OPEN); } |
| 101 | void OnReadEvent(AsyncSocket* socket) { AddEvents(socket, SSE_READ); } |
| 102 | void OnWriteEvent(AsyncSocket* socket) { AddEvents(socket, SSE_WRITE); } |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 103 | void OnCloseEvent(AsyncSocket* socket, int error) { |
| 104 | AddEvents(socket, (0 == error) ? SSE_CLOSE : SSE_ERROR); |
| 105 | } |
| 106 | |
| 107 | void AddEvents(void* obj, int events) { |
| 108 | EventMap::iterator it = events_.find(obj); |
| 109 | if (events_.end() == it) { |
| 110 | events_.insert(EventMap::value_type(obj, events)); |
| 111 | } else { |
| 112 | it->second |= events; |
| 113 | } |
| 114 | } |
| 115 | bool DoCheck(void* obj, StreamSinkEvent event, bool reset) { |
| 116 | EventMap::iterator it = events_.find(obj); |
| 117 | if ((events_.end() == it) || (0 == (it->second & event))) { |
| 118 | return false; |
| 119 | } |
| 120 | if (reset) { |
| 121 | it->second &= ~event; |
| 122 | } |
| 123 | return true; |
| 124 | } |
| 125 | int DoEvents(void* obj, bool reset) { |
| 126 | EventMap::iterator it = events_.find(obj); |
| 127 | if (events_.end() == it) |
| 128 | return 0; |
| 129 | int events = it->second; |
| 130 | if (reset) { |
| 131 | it->second = 0; |
| 132 | } |
| 133 | return events; |
| 134 | } |
| 135 | |
| 136 | EventMap events_; |
| 137 | }; |
| 138 | |
| 139 | /////////////////////////////////////////////////////////////////////////////// |
| 140 | // StreamSource - Implements stream interface and simulates asynchronous |
| 141 | // events on the stream, without a network. Also buffers written data. |
| 142 | /////////////////////////////////////////////////////////////////////////////// |
| 143 | |
| 144 | class StreamSource : public StreamInterface { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 145 | public: |
| 146 | StreamSource(); |
| 147 | ~StreamSource() override; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 148 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 149 | void Clear() { |
| 150 | readable_data_.clear(); |
| 151 | written_data_.clear(); |
| 152 | state_ = SS_CLOSED; |
| 153 | read_block_ = 0; |
| 154 | write_block_ = SIZE_UNKNOWN; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 155 | } |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 156 | void QueueString(const char* data) { QueueData(data, strlen(data)); } |
Niels Möller | 00f934a | 2017-12-14 13:30:46 +0100 | [diff] [blame] | 157 | #if defined(__GNUC__) |
| 158 | // Note: Implicit |this| argument counts as the first argument. |
| 159 | __attribute__((__format__(__printf__, 2, 3))) |
| 160 | #endif |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 161 | void |
| 162 | QueueStringF(const char* format, ...) { |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 163 | va_list args; |
| 164 | va_start(args, format); |
| 165 | char buffer[1024]; |
Niels Möller | aba0633 | 2018-10-16 15:14:15 +0200 | [diff] [blame^] | 166 | size_t len = vsnprintf(buffer, sizeof(buffer), format, args); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 167 | RTC_CHECK(len < sizeof(buffer) - 1); |
| 168 | va_end(args); |
| 169 | QueueData(buffer, len); |
| 170 | } |
| 171 | void QueueData(const char* data, size_t len) { |
| 172 | readable_data_.insert(readable_data_.end(), data, data + len); |
| 173 | if ((SS_OPEN == state_) && (readable_data_.size() == len)) { |
| 174 | SignalEvent(this, SE_READ, 0); |
| 175 | } |
| 176 | } |
| 177 | std::string ReadData() { |
| 178 | std::string data; |
| 179 | // avoid accessing written_data_[0] if it is undefined |
| 180 | if (written_data_.size() > 0) { |
| 181 | data.insert(0, &written_data_[0], written_data_.size()); |
| 182 | } |
| 183 | written_data_.clear(); |
| 184 | return data; |
| 185 | } |
| 186 | void SetState(StreamState state) { |
| 187 | int events = 0; |
| 188 | if ((SS_OPENING == state_) && (SS_OPEN == state)) { |
| 189 | events |= SE_OPEN; |
| 190 | if (!readable_data_.empty()) { |
| 191 | events |= SE_READ; |
| 192 | } |
| 193 | } else if ((SS_CLOSED != state_) && (SS_CLOSED == state)) { |
| 194 | events |= SE_CLOSE; |
| 195 | } |
| 196 | state_ = state; |
| 197 | if (events) { |
| 198 | SignalEvent(this, events, 0); |
| 199 | } |
| 200 | } |
| 201 | // Will cause Read to block when there are pos bytes in the read queue. |
| 202 | void SetReadBlock(size_t pos) { read_block_ = pos; } |
| 203 | // Will cause Write to block when there are pos bytes in the write queue. |
| 204 | void SetWriteBlock(size_t pos) { write_block_ = pos; } |
| 205 | |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 206 | StreamState GetState() const override; |
| 207 | StreamResult Read(void* buffer, |
| 208 | size_t buffer_len, |
| 209 | size_t* read, |
| 210 | int* error) override; |
| 211 | StreamResult Write(const void* data, |
| 212 | size_t data_len, |
| 213 | size_t* written, |
| 214 | int* error) override; |
| 215 | void Close() override; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 216 | |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 217 | private: |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 218 | typedef std::vector<char> Buffer; |
| 219 | Buffer readable_data_, written_data_; |
| 220 | StreamState state_; |
| 221 | size_t read_block_, write_block_; |
| 222 | }; |
| 223 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 224 | } // namespace testing |
| 225 | } // namespace webrtc |
| 226 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 227 | #endif // RTC_BASE_TESTUTILS_H_ |