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 | #include "rtc_base/stream.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 12 | #include "rtc_base/gunit.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 13 | |
| 14 | namespace rtc { |
| 15 | |
| 16 | /////////////////////////////////////////////////////////////////////////////// |
| 17 | // TestStream |
| 18 | /////////////////////////////////////////////////////////////////////////////// |
| 19 | |
| 20 | class TestStream : public StreamInterface { |
| 21 | public: |
| 22 | TestStream() : pos_(0) { } |
| 23 | |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 24 | StreamState GetState() const override { return SS_OPEN; } |
| 25 | |
| 26 | StreamResult Read(void* buffer, |
| 27 | size_t buffer_len, |
| 28 | size_t* read, |
| 29 | int* error) override { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 30 | unsigned char* uc_buffer = static_cast<unsigned char*>(buffer); |
| 31 | for (size_t i = 0; i < buffer_len; ++i) { |
| 32 | uc_buffer[i] = static_cast<unsigned char>(pos_++); |
| 33 | } |
| 34 | if (read) |
| 35 | *read = buffer_len; |
| 36 | return SR_SUCCESS; |
| 37 | } |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 38 | |
| 39 | StreamResult Write(const void* data, |
| 40 | size_t data_len, |
| 41 | size_t* written, |
| 42 | int* error) override { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 43 | if (error) |
| 44 | *error = -1; |
| 45 | return SR_ERROR; |
| 46 | } |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 47 | |
| 48 | void Close() override {} |
| 49 | |
| 50 | bool SetPosition(size_t position) override { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 51 | pos_ = position; |
| 52 | return true; |
| 53 | } |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 54 | |
| 55 | bool GetPosition(size_t* position) const override { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 56 | if (position) *position = pos_; |
| 57 | return true; |
| 58 | } |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 59 | |
| 60 | bool GetSize(size_t* size) const override { return false; } |
| 61 | |
| 62 | bool GetAvailable(size_t* size) const override { return false; } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 63 | |
| 64 | private: |
| 65 | size_t pos_; |
| 66 | }; |
| 67 | |
| 68 | bool VerifyTestBuffer(unsigned char* buffer, size_t len, |
| 69 | unsigned char value) { |
| 70 | bool passed = true; |
| 71 | for (size_t i = 0; i < len; ++i) { |
| 72 | if (buffer[i] != value++) { |
| 73 | passed = false; |
| 74 | break; |
| 75 | } |
| 76 | } |
| 77 | // Ensure that we don't pass again without re-writing |
| 78 | memset(buffer, 0, len); |
| 79 | return passed; |
| 80 | } |
| 81 | |
| 82 | void SeekTest(StreamInterface* stream, const unsigned char value) { |
| 83 | size_t bytes; |
| 84 | unsigned char buffer[13] = { 0 }; |
| 85 | const size_t kBufSize = sizeof(buffer); |
| 86 | |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 87 | EXPECT_EQ(stream->Read(buffer, kBufSize, &bytes, nullptr), SR_SUCCESS); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 88 | EXPECT_EQ(bytes, kBufSize); |
| 89 | EXPECT_TRUE(VerifyTestBuffer(buffer, kBufSize, value)); |
| 90 | EXPECT_TRUE(stream->GetPosition(&bytes)); |
| 91 | EXPECT_EQ(13U, bytes); |
| 92 | |
| 93 | EXPECT_TRUE(stream->SetPosition(7)); |
| 94 | |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 95 | EXPECT_EQ(stream->Read(buffer, kBufSize, &bytes, nullptr), SR_SUCCESS); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 96 | EXPECT_EQ(bytes, kBufSize); |
| 97 | EXPECT_TRUE(VerifyTestBuffer(buffer, kBufSize, value + 7)); |
| 98 | EXPECT_TRUE(stream->GetPosition(&bytes)); |
| 99 | EXPECT_EQ(20U, bytes); |
| 100 | } |
| 101 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 102 | TEST(FifoBufferTest, TestAll) { |
| 103 | const size_t kSize = 16; |
| 104 | const char in[kSize * 2 + 1] = "0123456789ABCDEFGHIJKLMNOPQRSTUV"; |
| 105 | char out[kSize * 2]; |
| 106 | void* p; |
| 107 | const void* q; |
| 108 | size_t bytes; |
| 109 | FifoBuffer buf(kSize); |
| 110 | StreamInterface* stream = &buf; |
| 111 | |
| 112 | // Test assumptions about base state |
| 113 | EXPECT_EQ(SS_OPEN, stream->GetState()); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 114 | EXPECT_EQ(SR_BLOCK, stream->Read(out, kSize, &bytes, nullptr)); |
| 115 | EXPECT_TRUE(nullptr != stream->GetReadData(&bytes)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 116 | EXPECT_EQ((size_t)0, bytes); |
| 117 | stream->ConsumeReadData(0); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 118 | EXPECT_TRUE(nullptr != stream->GetWriteBuffer(&bytes)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 119 | EXPECT_EQ(kSize, bytes); |
| 120 | stream->ConsumeWriteBuffer(0); |
| 121 | |
| 122 | // Try a full write |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 123 | EXPECT_EQ(SR_SUCCESS, stream->Write(in, kSize, &bytes, nullptr)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 124 | EXPECT_EQ(kSize, bytes); |
| 125 | |
| 126 | // Try a write that should block |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 127 | EXPECT_EQ(SR_BLOCK, stream->Write(in, kSize, &bytes, nullptr)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 128 | |
| 129 | // Try a full read |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 130 | EXPECT_EQ(SR_SUCCESS, stream->Read(out, kSize, &bytes, nullptr)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 131 | EXPECT_EQ(kSize, bytes); |
| 132 | EXPECT_EQ(0, memcmp(in, out, kSize)); |
| 133 | |
| 134 | // Try a read that should block |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 135 | EXPECT_EQ(SR_BLOCK, stream->Read(out, kSize, &bytes, nullptr)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 136 | |
| 137 | // Try a too-big write |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 138 | EXPECT_EQ(SR_SUCCESS, stream->Write(in, kSize * 2, &bytes, nullptr)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 139 | EXPECT_EQ(bytes, kSize); |
| 140 | |
| 141 | // Try a too-big read |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 142 | EXPECT_EQ(SR_SUCCESS, stream->Read(out, kSize * 2, &bytes, nullptr)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 143 | EXPECT_EQ(kSize, bytes); |
| 144 | EXPECT_EQ(0, memcmp(in, out, kSize)); |
| 145 | |
| 146 | // Try some small writes and reads |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 147 | EXPECT_EQ(SR_SUCCESS, stream->Write(in, kSize / 2, &bytes, nullptr)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 148 | EXPECT_EQ(kSize / 2, bytes); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 149 | EXPECT_EQ(SR_SUCCESS, stream->Read(out, kSize / 2, &bytes, nullptr)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 150 | EXPECT_EQ(kSize / 2, bytes); |
| 151 | EXPECT_EQ(0, memcmp(in, out, kSize / 2)); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 152 | EXPECT_EQ(SR_SUCCESS, stream->Write(in, kSize / 2, &bytes, nullptr)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 153 | EXPECT_EQ(kSize / 2, bytes); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 154 | EXPECT_EQ(SR_SUCCESS, stream->Write(in, kSize / 2, &bytes, nullptr)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 155 | EXPECT_EQ(kSize / 2, bytes); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 156 | EXPECT_EQ(SR_SUCCESS, stream->Read(out, kSize / 2, &bytes, nullptr)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 157 | EXPECT_EQ(kSize / 2, bytes); |
| 158 | EXPECT_EQ(0, memcmp(in, out, kSize / 2)); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 159 | EXPECT_EQ(SR_SUCCESS, stream->Read(out, kSize / 2, &bytes, nullptr)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 160 | EXPECT_EQ(kSize / 2, bytes); |
| 161 | EXPECT_EQ(0, memcmp(in, out, kSize / 2)); |
| 162 | |
| 163 | // Try wraparound reads and writes in the following pattern |
| 164 | // WWWWWWWWWWWW.... 0123456789AB.... |
| 165 | // RRRRRRRRXXXX.... ........89AB.... |
| 166 | // WWWW....XXXXWWWW 4567....89AB0123 |
| 167 | // XXXX....RRRRXXXX 4567........0123 |
| 168 | // XXXXWWWWWWWWXXXX 4567012345670123 |
| 169 | // RRRRXXXXXXXXRRRR ....01234567.... |
| 170 | // ....RRRRRRRR.... ................ |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 171 | EXPECT_EQ(SR_SUCCESS, stream->Write(in, kSize * 3 / 4, &bytes, nullptr)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 172 | EXPECT_EQ(kSize * 3 / 4, bytes); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 173 | EXPECT_EQ(SR_SUCCESS, stream->Read(out, kSize / 2, &bytes, nullptr)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 174 | EXPECT_EQ(kSize / 2, bytes); |
| 175 | EXPECT_EQ(0, memcmp(in, out, kSize / 2)); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 176 | EXPECT_EQ(SR_SUCCESS, stream->Write(in, kSize / 2, &bytes, nullptr)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 177 | EXPECT_EQ(kSize / 2, bytes); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 178 | EXPECT_EQ(SR_SUCCESS, stream->Read(out, kSize / 4, &bytes, nullptr)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 179 | EXPECT_EQ(kSize / 4 , bytes); |
| 180 | EXPECT_EQ(0, memcmp(in + kSize / 2, out, kSize / 4)); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 181 | EXPECT_EQ(SR_SUCCESS, stream->Write(in, kSize / 2, &bytes, nullptr)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 182 | EXPECT_EQ(kSize / 2, bytes); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 183 | EXPECT_EQ(SR_SUCCESS, stream->Read(out, kSize / 2, &bytes, nullptr)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 184 | EXPECT_EQ(kSize / 2 , bytes); |
| 185 | EXPECT_EQ(0, memcmp(in, out, kSize / 2)); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 186 | EXPECT_EQ(SR_SUCCESS, stream->Read(out, kSize / 2, &bytes, nullptr)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 187 | EXPECT_EQ(kSize / 2 , bytes); |
| 188 | EXPECT_EQ(0, memcmp(in, out, kSize / 2)); |
| 189 | |
| 190 | // Use GetWriteBuffer to reset the read_position for the next tests |
| 191 | stream->GetWriteBuffer(&bytes); |
| 192 | stream->ConsumeWriteBuffer(0); |
| 193 | |
| 194 | // Try using GetReadData to do a full read |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 195 | EXPECT_EQ(SR_SUCCESS, stream->Write(in, kSize, &bytes, nullptr)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 196 | q = stream->GetReadData(&bytes); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 197 | EXPECT_TRUE(nullptr != q); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 198 | EXPECT_EQ(kSize, bytes); |
| 199 | EXPECT_EQ(0, memcmp(q, in, kSize)); |
| 200 | stream->ConsumeReadData(kSize); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 201 | EXPECT_EQ(SR_BLOCK, stream->Read(out, kSize, &bytes, nullptr)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 202 | |
| 203 | // Try using GetReadData to do some small reads |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 204 | EXPECT_EQ(SR_SUCCESS, stream->Write(in, kSize, &bytes, nullptr)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 205 | q = stream->GetReadData(&bytes); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 206 | EXPECT_TRUE(nullptr != q); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 207 | EXPECT_EQ(kSize, bytes); |
| 208 | EXPECT_EQ(0, memcmp(q, in, kSize / 2)); |
| 209 | stream->ConsumeReadData(kSize / 2); |
| 210 | q = stream->GetReadData(&bytes); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 211 | EXPECT_TRUE(nullptr != q); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 212 | EXPECT_EQ(kSize / 2, bytes); |
| 213 | EXPECT_EQ(0, memcmp(q, in + kSize / 2, kSize / 2)); |
| 214 | stream->ConsumeReadData(kSize / 2); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 215 | EXPECT_EQ(SR_BLOCK, stream->Read(out, kSize, &bytes, nullptr)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 216 | |
| 217 | // Try using GetReadData in a wraparound case |
| 218 | // WWWWWWWWWWWWWWWW 0123456789ABCDEF |
| 219 | // RRRRRRRRRRRRXXXX ............CDEF |
| 220 | // WWWWWWWW....XXXX 01234567....CDEF |
| 221 | // ............RRRR 01234567........ |
| 222 | // RRRRRRRR........ ................ |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 223 | EXPECT_EQ(SR_SUCCESS, stream->Write(in, kSize, &bytes, nullptr)); |
| 224 | EXPECT_EQ(SR_SUCCESS, stream->Read(out, kSize * 3 / 4, &bytes, nullptr)); |
| 225 | EXPECT_EQ(SR_SUCCESS, stream->Write(in, kSize / 2, &bytes, nullptr)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 226 | q = stream->GetReadData(&bytes); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 227 | EXPECT_TRUE(nullptr != q); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 228 | EXPECT_EQ(kSize / 4, bytes); |
| 229 | EXPECT_EQ(0, memcmp(q, in + kSize * 3 / 4, kSize / 4)); |
| 230 | stream->ConsumeReadData(kSize / 4); |
| 231 | q = stream->GetReadData(&bytes); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 232 | EXPECT_TRUE(nullptr != q); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 233 | EXPECT_EQ(kSize / 2, bytes); |
| 234 | EXPECT_EQ(0, memcmp(q, in, kSize / 2)); |
| 235 | stream->ConsumeReadData(kSize / 2); |
| 236 | |
| 237 | // Use GetWriteBuffer to reset the read_position for the next tests |
| 238 | stream->GetWriteBuffer(&bytes); |
| 239 | stream->ConsumeWriteBuffer(0); |
| 240 | |
| 241 | // Try using GetWriteBuffer to do a full write |
| 242 | p = stream->GetWriteBuffer(&bytes); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 243 | EXPECT_TRUE(nullptr != p); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 244 | EXPECT_EQ(kSize, bytes); |
| 245 | memcpy(p, in, kSize); |
| 246 | stream->ConsumeWriteBuffer(kSize); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 247 | EXPECT_EQ(SR_SUCCESS, stream->Read(out, kSize, &bytes, nullptr)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 248 | EXPECT_EQ(kSize, bytes); |
| 249 | EXPECT_EQ(0, memcmp(in, out, kSize)); |
| 250 | |
| 251 | // Try using GetWriteBuffer to do some small writes |
| 252 | p = stream->GetWriteBuffer(&bytes); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 253 | EXPECT_TRUE(nullptr != p); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 254 | EXPECT_EQ(kSize, bytes); |
| 255 | memcpy(p, in, kSize / 2); |
| 256 | stream->ConsumeWriteBuffer(kSize / 2); |
| 257 | p = stream->GetWriteBuffer(&bytes); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 258 | EXPECT_TRUE(nullptr != p); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 259 | EXPECT_EQ(kSize / 2, bytes); |
| 260 | memcpy(p, in + kSize / 2, kSize / 2); |
| 261 | stream->ConsumeWriteBuffer(kSize / 2); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 262 | EXPECT_EQ(SR_SUCCESS, stream->Read(out, kSize, &bytes, nullptr)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 263 | EXPECT_EQ(kSize, bytes); |
| 264 | EXPECT_EQ(0, memcmp(in, out, kSize)); |
| 265 | |
| 266 | // Try using GetWriteBuffer in a wraparound case |
| 267 | // WWWWWWWWWWWW.... 0123456789AB.... |
| 268 | // RRRRRRRRXXXX.... ........89AB.... |
| 269 | // ........XXXXWWWW ........89AB0123 |
| 270 | // WWWW....XXXXXXXX 4567....89AB0123 |
| 271 | // RRRR....RRRRRRRR ................ |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 272 | EXPECT_EQ(SR_SUCCESS, stream->Write(in, kSize * 3 / 4, &bytes, nullptr)); |
| 273 | EXPECT_EQ(SR_SUCCESS, stream->Read(out, kSize / 2, &bytes, nullptr)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 274 | p = stream->GetWriteBuffer(&bytes); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 275 | EXPECT_TRUE(nullptr != p); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 276 | EXPECT_EQ(kSize / 4, bytes); |
| 277 | memcpy(p, in, kSize / 4); |
| 278 | stream->ConsumeWriteBuffer(kSize / 4); |
| 279 | p = stream->GetWriteBuffer(&bytes); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 280 | EXPECT_TRUE(nullptr != p); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 281 | EXPECT_EQ(kSize / 2, bytes); |
| 282 | memcpy(p, in + kSize / 4, kSize / 4); |
| 283 | stream->ConsumeWriteBuffer(kSize / 4); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 284 | EXPECT_EQ(SR_SUCCESS, stream->Read(out, kSize * 3 / 4, &bytes, nullptr)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 285 | EXPECT_EQ(kSize * 3 / 4, bytes); |
| 286 | EXPECT_EQ(0, memcmp(in + kSize / 2, out, kSize / 4)); |
| 287 | EXPECT_EQ(0, memcmp(in, out + kSize / 4, kSize / 4)); |
| 288 | |
| 289 | // Check that the stream is now empty |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 290 | EXPECT_EQ(SR_BLOCK, stream->Read(out, kSize, &bytes, nullptr)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 291 | |
| 292 | // Try growing the buffer |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 293 | EXPECT_EQ(SR_SUCCESS, stream->Write(in, kSize, &bytes, nullptr)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 294 | EXPECT_EQ(kSize, bytes); |
| 295 | EXPECT_TRUE(buf.SetCapacity(kSize * 2)); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 296 | EXPECT_EQ(SR_SUCCESS, stream->Write(in + kSize, kSize, &bytes, nullptr)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 297 | EXPECT_EQ(kSize, bytes); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 298 | EXPECT_EQ(SR_SUCCESS, stream->Read(out, kSize * 2, &bytes, nullptr)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 299 | EXPECT_EQ(kSize * 2, bytes); |
| 300 | EXPECT_EQ(0, memcmp(in, out, kSize * 2)); |
| 301 | |
| 302 | // Try shrinking the buffer |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 303 | EXPECT_EQ(SR_SUCCESS, stream->Write(in, kSize, &bytes, nullptr)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 304 | EXPECT_EQ(kSize, bytes); |
| 305 | EXPECT_TRUE(buf.SetCapacity(kSize)); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 306 | EXPECT_EQ(SR_BLOCK, stream->Write(in, kSize, &bytes, nullptr)); |
| 307 | EXPECT_EQ(SR_SUCCESS, stream->Read(out, kSize, &bytes, nullptr)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 308 | EXPECT_EQ(kSize, bytes); |
| 309 | EXPECT_EQ(0, memcmp(in, out, kSize)); |
| 310 | |
| 311 | // Write to the stream, close it, read the remaining bytes |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 312 | EXPECT_EQ(SR_SUCCESS, stream->Write(in, kSize / 2, &bytes, nullptr)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 313 | stream->Close(); |
| 314 | EXPECT_EQ(SS_CLOSED, stream->GetState()); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 315 | EXPECT_EQ(SR_EOS, stream->Write(in, kSize / 2, &bytes, nullptr)); |
| 316 | EXPECT_EQ(SR_SUCCESS, stream->Read(out, kSize / 2, &bytes, nullptr)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 317 | EXPECT_EQ(0, memcmp(in, out, kSize / 2)); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 318 | EXPECT_EQ(SR_EOS, stream->Read(out, kSize / 2, &bytes, nullptr)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | TEST(FifoBufferTest, FullBufferCheck) { |
| 322 | FifoBuffer buff(10); |
| 323 | buff.ConsumeWriteBuffer(10); |
| 324 | |
| 325 | size_t free; |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 326 | EXPECT_TRUE(buff.GetWriteBuffer(&free) != nullptr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 327 | EXPECT_EQ(0U, free); |
| 328 | } |
| 329 | |
| 330 | TEST(FifoBufferTest, WriteOffsetAndReadOffset) { |
| 331 | const size_t kSize = 16; |
| 332 | const char in[kSize * 2 + 1] = "0123456789ABCDEFGHIJKLMNOPQRSTUV"; |
| 333 | char out[kSize * 2]; |
| 334 | FifoBuffer buf(kSize); |
| 335 | |
| 336 | // Write 14 bytes. |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 337 | EXPECT_EQ(SR_SUCCESS, buf.Write(in, 14, nullptr, nullptr)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 338 | |
| 339 | // Make sure data is in |buf|. |
| 340 | size_t buffered; |
| 341 | EXPECT_TRUE(buf.GetBuffered(&buffered)); |
| 342 | EXPECT_EQ(14u, buffered); |
| 343 | |
| 344 | // Read 10 bytes. |
| 345 | buf.ConsumeReadData(10); |
| 346 | |
| 347 | // There should be now 12 bytes of available space. |
| 348 | size_t remaining; |
| 349 | EXPECT_TRUE(buf.GetWriteRemaining(&remaining)); |
| 350 | EXPECT_EQ(12u, remaining); |
| 351 | |
| 352 | // Write at offset 12, this should fail. |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 353 | EXPECT_EQ(SR_BLOCK, buf.WriteOffset(in, 10, 12, nullptr)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 354 | |
| 355 | // Write 8 bytes at offset 4, this wraps around the buffer. |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 356 | EXPECT_EQ(SR_SUCCESS, buf.WriteOffset(in, 8, 4, nullptr)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 357 | |
| 358 | // Number of available space remains the same until we call |
| 359 | // ConsumeWriteBuffer(). |
| 360 | EXPECT_TRUE(buf.GetWriteRemaining(&remaining)); |
| 361 | EXPECT_EQ(12u, remaining); |
| 362 | buf.ConsumeWriteBuffer(12); |
| 363 | |
| 364 | // There's 4 bytes bypassed and 4 bytes no read so skip them and verify the |
| 365 | // 8 bytes written. |
| 366 | size_t read; |
| 367 | EXPECT_EQ(SR_SUCCESS, buf.ReadOffset(out, 8, 8, &read)); |
| 368 | EXPECT_EQ(8u, read); |
| 369 | EXPECT_EQ(0, memcmp(out, in, 8)); |
| 370 | |
| 371 | // There should still be 16 bytes available for reading. |
| 372 | EXPECT_TRUE(buf.GetBuffered(&buffered)); |
| 373 | EXPECT_EQ(16u, buffered); |
| 374 | |
| 375 | // Read at offset 16, this should fail since we don't have that much data. |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 376 | EXPECT_EQ(SR_BLOCK, buf.ReadOffset(out, 10, 16, nullptr)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 377 | } |
| 378 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 379 | } // namespace rtc |