1 // Copyright 2018 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "mojo/public/cpp/base/read_only_buffer_mojom_traits.h"
6 #include "mojo/public/cpp/test_support/test_utils.h"
7 #include "mojo/public/mojom/base/read_only_buffer.mojom.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9
10 namespace mojo_base {
11 namespace read_only_buffer_unittest {
12
TEST(ReadOnlyBufferTest,ReadOnlyBufferEmptySpan)13 TEST(ReadOnlyBufferTest, ReadOnlyBufferEmptySpan) {
14 base::span<const uint8_t> in;
15 base::span<const uint8_t> out;
16
17 ASSERT_TRUE(
18 mojo::test::SerializeAndDeserialize<mojom::ReadOnlyBuffer>(&in, &out));
19 EXPECT_EQ(in, out);
20 }
21
TEST(ReadOnlyBufferTest,ReadOnlyBufferNonEmptySpan)22 TEST(ReadOnlyBufferTest, ReadOnlyBufferNonEmptySpan) {
23 std::vector<uint8_t> v{'1', '2', '3'};
24 base::span<const uint8_t> in(v);
25 base::span<const uint8_t> out;
26
27 // SerializeAndDeserialize doesn't work here: the traits for ReadOnlyBuffer
28 // returns a span that points into the raw bytes in the mojo::Message;however,
29 // the stack object will be freed before SerializeAndSerialize returns.
30 std::vector<uint8_t> data = mojom::ReadOnlyBuffer::Serialize(&in);
31
32 EXPECT_TRUE(mojom::ReadOnlyBuffer::Deserialize(std::move(data), &out));
33 EXPECT_EQ(in, out);
34 }
35
36 } // namespace read_only_buffer_unittest
37 } // namespace mojo_base
38