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
  263
  264
  265
  266
  267
  268
  269
  270
  271
  272
  273
  274
  275
  276
  277
  278
  279
  280
  281
  282
  283
  284
  285
  286
  287
  288
  289
  290
  291
  292
  293
  294
  295
  296
  297
  298
  299
  300
  301
  302
  303
  304
  305
  306
  307
  308
  309
  310
  311
  312
  313
  314
  315
  316
  317
  318
  319
  320
  321
  322
  323
  324
  325
  326
  327
  328
  329
  330
  331
  332
  333
  334
  335
  336
  337
  338
  339
  340
  341
  342
  343
  344
  345
  346
  347
  348
  349
  350
  351
  352
  353
  354
  355
  356
  357
  358
  359

mojo / core / ipcz_driver / ring_buffer_test.cc [blame]

// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "mojo/core/ipcz_driver/ring_buffer.h"

#include <cstddef>
#include <cstdint>
#include <optional>
#include <string>
#include <string_view>
#include <utility>
#include <vector>

#include "base/containers/span.h"
#include "base/memory/unsafe_shared_memory_region.h"
#include "base/ranges/algorithm.h"
#include "mojo/core/ipcz_driver/shared_buffer.h"
#include "mojo/core/ipcz_driver/shared_buffer_mapping.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace mojo::core::ipcz_driver {
namespace {

using RingBufferTest = testing::Test;

std::string_view AsString(base::span<const uint8_t> bytes) {
  return std::string_view(reinterpret_cast<const char*>(bytes.data()),
                          bytes.size());
}

base::span<const uint8_t> AsBytes(std::string_view s) {
  return base::as_byte_span(s).first(s.length());
}

// Wraps a RingBuffer with more convient string-based I/O for tests to use.
class TestRingBuffer {
 public:
  explicit TestRingBuffer(size_t n) : buffer_(MapMemory(n)) {}
  explicit TestRingBuffer(SharedBufferMapping& mapping)
      : buffer_(base::WrapRefCounted(&mapping)) {}

  RingBuffer& buffer() { return buffer_; }

  size_t Write(std::string_view s) { return buffer_.Write(AsBytes(s)); }

  bool WriteAll(std::string_view s) { return buffer_.WriteAll(AsBytes(s)); }

  std::string Read(size_t n) {
    std::vector<uint8_t> data(n);
    auto bytes = base::span(data);
    const size_t size = buffer_.Read(bytes);
    return std::string(AsString(bytes.first(size)));
  }

  std::optional<std::string> ReadAll(size_t n) {
    std::vector<uint8_t> data(n);
    if (!buffer_.ReadAll(base::span(data))) {
      return std::nullopt;
    }
    return std::string(data.begin(), data.end());
  }

  std::string Peek(size_t n) {
    std::vector<uint8_t> data(n);
    auto bytes = base::span(data);
    const size_t size = buffer_.Peek(bytes);
    return std::string(AsString(bytes.first(size)));
  }

  std::optional<std::string> PeekAll(size_t n) {
    std::vector<uint8_t> data(n);
    if (!buffer_.PeekAll(base::span(data))) {
      return std::nullopt;
    }
    return std::string(data.begin(), data.end());
  }

 private:
  static scoped_refptr<SharedBufferMapping> MapMemory(size_t size) {
    auto buffer = SharedBuffer::MakeForRegion(
        base::UnsafeSharedMemoryRegion::Create(size));
    return SharedBufferMapping::Create(buffer->region());
  }

  RingBuffer buffer_;
};

TEST_F(RingBufferTest, EmptyReads) {
  TestRingBuffer ring(256);
  EXPECT_EQ(0u, ring.buffer().data_size());
  EXPECT_EQ(256u, ring.buffer().available_capacity());

  uint8_t bytes[1];
  EXPECT_EQ(0u, ring.buffer().Read(bytes));
  EXPECT_EQ(0u, ring.buffer().Peek(bytes));
  EXPECT_FALSE(ring.buffer().ReadAll(bytes));
  EXPECT_FALSE(ring.buffer().PeekAll(bytes));

  RingBuffer::DirectReader reader(ring.buffer());
  EXPECT_TRUE(reader.bytes().empty());
  EXPECT_FALSE(std::move(reader).Consume(1));
}

TEST_F(RingBufferTest, FullWrites) {
  uint8_t data[256] = {};
  TestRingBuffer ring(256);
  ASSERT_TRUE(ring.buffer().WriteAll(data));
  EXPECT_EQ(256u, ring.buffer().data_size());
  EXPECT_EQ(0u, ring.buffer().available_capacity());

  const uint8_t bytes[] = {42};
  EXPECT_EQ(0u, ring.buffer().Write(bytes));
  EXPECT_FALSE(ring.buffer().WriteAll(bytes));

  RingBuffer::DirectWriter writer(ring.buffer());
  EXPECT_TRUE(writer.bytes().empty());
  EXPECT_FALSE(std::move(writer).Commit(1));
}

TEST_F(RingBufferTest, DirectReader) {
  TestRingBuffer ring(8);
  ASSERT_TRUE(ring.WriteAll("abcdefgh"));

  // Since this is a fresh buffer, the data starts at the front of the buffer
  // and a DirectReader can therefore see all of it.
  {
    RingBuffer::DirectReader reader(ring.buffer());
    EXPECT_EQ(reader.bytes().size(), ring.buffer().data_size());
    EXPECT_EQ("abcdefgh", AsString(reader.bytes()));

    // Consume 3 bytes and replace them with "xyz".
    EXPECT_TRUE(std::move(reader).Consume(3));
    EXPECT_EQ(5u, ring.buffer().data_size());
    EXPECT_EQ(3u, ring.buffer().available_capacity());
    ASSERT_TRUE(ring.WriteAll("xyz"));
    EXPECT_EQ(8u, ring.buffer().data_size());
    EXPECT_EQ(0u, ring.buffer().available_capacity());
  }

  // The buffer should contain "xyzdefgh" now, with the next read starting at
  // the 'd'. The DirectReader cannot see the full contents, because they wrap
  // around to the front of the buffer.
  {
    RingBuffer::DirectReader reader(ring.buffer());
    EXPECT_EQ(5u, reader.bytes().size());
    EXPECT_EQ("defgh", AsString(reader.bytes()));

    // Consume 4 bytes and replace them with "1234".
    EXPECT_TRUE(std::move(reader).Consume(4));
    EXPECT_EQ(4u, ring.buffer().data_size());
    EXPECT_EQ(4u, ring.buffer().available_capacity());
    ASSERT_TRUE(ring.WriteAll("1234"));
    EXPECT_EQ(8u, ring.buffer().data_size());
    EXPECT_EQ(0u, ring.buffer().available_capacity());
  }

  // Still only the end of the physical buffer is visible.
  {
    RingBuffer::DirectReader reader(ring.buffer());
    EXPECT_EQ(1u, reader.bytes().size());
    EXPECT_EQ("h", AsString(reader.bytes()));
    EXPECT_TRUE(std::move(reader).Consume(1));
  }

  // Now that the end of the buffer has been consumed a new DirectReader can
  // see the remaining data, starting at the front of the buffer.
  {
    RingBuffer::DirectReader reader(ring.buffer());
    EXPECT_EQ(7u, reader.bytes().size());
    EXPECT_EQ("xyz1234", AsString(reader.bytes()));
    EXPECT_TRUE(std::move(reader).Consume(3));
  }

  ASSERT_TRUE(ring.WriteAll("!!!!"));

  // DirectReader can't consume more than it exposes, even if there is more data
  // available to read elsewhere.
  RingBuffer::DirectReader reader(ring.buffer());
  EXPECT_EQ(5u, reader.bytes().size());
  EXPECT_EQ("1234!", AsString(reader.bytes()));
  EXPECT_FALSE(std::move(reader).Consume(6));
  EXPECT_EQ("1234!!!!", *ring.ReadAll(8));
}

TEST_F(RingBufferTest, DirectWriter) {
  TestRingBuffer ring(8);

  // A DirectWriter can initially see all capacity in the buffer, because the
  // the buffer is empty and the next available byte is at the front.
  {
    RingBuffer::DirectWriter writer(ring.buffer());
    EXPECT_EQ(8u, writer.bytes().size());
    EXPECT_EQ(0u, ring.buffer().data_size());
    EXPECT_EQ(8u, ring.buffer().available_capacity());

    base::ranges::copy(AsBytes("abc"), writer.bytes().begin());
    EXPECT_TRUE(std::move(writer).Commit(3));
    EXPECT_EQ(3u, ring.buffer().data_size());
    EXPECT_EQ(5u, ring.buffer().available_capacity());
  }

  EXPECT_EQ("ab", ring.ReadAll(2));

  // Although there are now 7 bytes of available capacity, only 5 of them are
  // contiguous with the starting byte.
  {
    RingBuffer::DirectWriter writer(ring.buffer());
    EXPECT_EQ(1u, ring.buffer().data_size());
    EXPECT_EQ(7u, ring.buffer().available_capacity());
    EXPECT_EQ(5u, writer.bytes().size());

    base::ranges::copy(AsBytes("defgh"), writer.bytes().begin());
    EXPECT_TRUE(std::move(writer).Commit(5));
  }

  EXPECT_EQ("cdefgh", ring.PeekAll(6));
  EXPECT_EQ("cde", ring.ReadAll(3));

  // Now available capacity starts at the front of the buffer again.
  {
    RingBuffer::DirectWriter writer(ring.buffer());
    EXPECT_EQ(3u, ring.buffer().data_size());
    EXPECT_EQ(5u, ring.buffer().available_capacity());
    EXPECT_EQ(5u, writer.bytes().size());

    base::ranges::copy(AsBytes("12345"), writer.bytes().begin());
    EXPECT_TRUE(std::move(writer).Commit(5));
  }

  EXPECT_EQ("fgh12345", ring.PeekAll(8));
  EXPECT_FALSE(RingBuffer::DirectWriter(ring.buffer()).Commit(1));
}

TEST_F(RingBufferTest, BasicWrite) {
  TestRingBuffer ring(8);

  EXPECT_EQ(4u, ring.Write("hihi"));
  EXPECT_EQ(4u, ring.buffer().data_size());
  EXPECT_EQ(4u, ring.buffer().available_capacity());
  EXPECT_EQ("hihi", ring.Peek(8));

  EXPECT_EQ(1u, ring.buffer().Discard(1));
  EXPECT_EQ(5u, ring.Write("hehe! hehe!"));
  EXPECT_EQ(8u, ring.buffer().data_size());
  EXPECT_EQ(0u, ring.buffer().available_capacity());
  EXPECT_EQ("ihihehe!", ring.Peek(8));

  EXPECT_TRUE(ring.buffer().Discard(2));
  EXPECT_EQ(6u, ring.buffer().data_size());
  EXPECT_EQ(2u, ring.buffer().available_capacity());
  EXPECT_EQ("ihehe!", ring.Peek(6));

  EXPECT_EQ(2u, ring.Write("!!!!!!!!!"));
  EXPECT_EQ("ihehe!!!", ring.PeekAll(8));
}

TEST_F(RingBufferTest, BasicRead) {
  TestRingBuffer ring(8);
  EXPECT_TRUE(ring.WriteAll("abcdefgh"));
  EXPECT_EQ(8u, ring.buffer().data_size());
  EXPECT_EQ(0u, ring.buffer().available_capacity());

  EXPECT_EQ("abc", ring.ReadAll(3));
  EXPECT_EQ(5u, ring.buffer().data_size());
  EXPECT_EQ(3u, ring.buffer().available_capacity());

  EXPECT_EQ("d", ring.Read(1));
  EXPECT_EQ(4u, ring.buffer().data_size());
  EXPECT_EQ(4u, ring.buffer().available_capacity());

  EXPECT_TRUE(ring.WriteAll("1234"));
  EXPECT_EQ(8u, ring.buffer().data_size());
  EXPECT_EQ(0u, ring.buffer().available_capacity());

  EXPECT_EQ("efgh1234", ring.Peek(8));

  EXPECT_EQ("efgh12", ring.Read(6));
  EXPECT_EQ(2u, ring.buffer().data_size());
  EXPECT_EQ(6u, ring.buffer().available_capacity());

  EXPECT_TRUE(ring.WriteAll("xyzw"));
  EXPECT_EQ("34xyzw", ring.Read(6));
  EXPECT_EQ(0u, ring.buffer().data_size());
  EXPECT_EQ(8u, ring.buffer().available_capacity());
}

TEST_F(RingBufferTest, ExtendDataRange) {
  TestRingBuffer ring(8);
  base::ranges::copy(AsBytes("abcdefgh"),
                     ring.buffer().mapping().bytes().begin());
  EXPECT_EQ(0u, ring.buffer().data_size());
  EXPECT_EQ(8u, ring.buffer().available_capacity());

  // Too large.
  EXPECT_FALSE(ring.buffer().ExtendDataRange(9));
  EXPECT_EQ(0u, ring.buffer().data_size());
  EXPECT_EQ(8u, ring.buffer().available_capacity());

  // Include the first 5 bytes of the buffer as readable data.
  EXPECT_TRUE(ring.buffer().ExtendDataRange(5));
  EXPECT_EQ(5u, ring.buffer().data_size());
  EXPECT_EQ(3u, ring.buffer().available_capacity());

  // Now consume two bytes and add two more to the end.
  EXPECT_EQ("abcde", ring.Peek(8));
  EXPECT_EQ("ab", ring.ReadAll(2));
  EXPECT_EQ(3u, ring.buffer().data_size());
  EXPECT_EQ(5u, ring.buffer().available_capacity());
  EXPECT_TRUE(ring.buffer().ExtendDataRange(2));
  EXPECT_EQ(5u, ring.buffer().data_size());
  EXPECT_EQ(3u, ring.buffer().available_capacity());
  EXPECT_EQ("cdefg", ring.Peek(8));

  // Finally, extend further so the data wraps around to the front of the
  // buffer. All bytes should be included, with next available being 'c'.
  EXPECT_TRUE(ring.buffer().ExtendDataRange(3));
  EXPECT_EQ(8u, ring.buffer().data_size());
  EXPECT_EQ(0u, ring.buffer().available_capacity());
  EXPECT_EQ("cdefghab", ring.Peek(8));
}

TEST_F(RingBufferTest, BoundaryChecks) {
  TestRingBuffer ring(8);
  EXPECT_FALSE(ring.WriteAll("123456789"));
  EXPECT_FALSE(ring.ReadAll(1));
  EXPECT_FALSE(ring.PeekAll(1));
  EXPECT_FALSE(ring.buffer().DiscardAll(1));
  EXPECT_EQ("", ring.Read(1));
  EXPECT_EQ("", ring.Peek(1));
  EXPECT_EQ(0u, ring.buffer().Discard(1));

  EXPECT_TRUE(ring.WriteAll("12345678"));
  EXPECT_FALSE(ring.WriteAll("!"));
  EXPECT_EQ(0u, ring.Write("!"));
}

TEST_F(RingBufferTest, Serialization) {
  TestRingBuffer ring(8);
  EXPECT_TRUE(ring.WriteAll("hello!"));
  EXPECT_TRUE(ring.buffer().DiscardAll(1));
  EXPECT_EQ("ello!", ring.Peek(8));

  RingBuffer::SerializedState state;
  ring.buffer().Serialize(state);

  TestRingBuffer new_ring(ring.buffer().mapping());
  EXPECT_TRUE(new_ring.buffer().Deserialize(state));
  EXPECT_EQ("ello!", new_ring.Peek(8));

  TestRingBuffer bad_ring(ring.buffer().mapping());
  // Invalid offset.
  EXPECT_FALSE(bad_ring.buffer().Deserialize({.offset = 8, .size = 1}));
  // Invalid size.
  EXPECT_FALSE(bad_ring.buffer().Deserialize({.offset = 0, .size = 9}));
}

}  // namespace
}  // namespace mojo::core::ipcz_driver