1 // Copyright 2014 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.
5 #include "base/basictypes.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "chromecast/media/cma/ipc/media_memory_chunk.h"
9 #include "chromecast/media/cma/ipc/media_message.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 namespace chromecast
{
17 class ExternalMemoryBlock
18 : public MediaMemoryChunk
{
20 ExternalMemoryBlock(void* data
, size_t size
)
21 : data_(data
), size_(size
) {}
22 ~ExternalMemoryBlock() override
{}
24 // MediaMemoryChunk implementation.
25 void* data() const override
{ return data_
; }
26 size_t size() const override
{ return size_
; }
27 bool valid() const override
{ return true; }
34 scoped_ptr
<MediaMemoryChunk
> DummyAllocator(
35 void* data
, size_t size
, size_t alloc_size
) {
36 CHECK_LE(alloc_size
, size
);
37 return scoped_ptr
<MediaMemoryChunk
>(
38 new ExternalMemoryBlock(data
, alloc_size
));
43 TEST(MediaMessageTest
, WriteRead
) {
44 int buffer_size
= 1024;
45 scoped_ptr
<uint8
[]> buffer(new uint8
[buffer_size
]);
46 MediaMessage::MemoryAllocatorCB
mem_alloc_cb(
47 base::Bind(&DummyAllocator
, buffer
.get(), buffer_size
));
49 int msg_content_capacity
= 512;
53 scoped_ptr
<MediaMessage
> msg1(
54 MediaMessage::CreateMessage(type
, mem_alloc_cb
, msg_content_capacity
));
55 for (int k
= 0; k
< count
; k
++) {
57 EXPECT_TRUE(msg1
->WritePod(v1
));
59 EXPECT_TRUE(msg1
->WritePod(v2
));
61 EXPECT_EQ(msg1
->content_size(), count
* (sizeof(int) + sizeof(uint8
)));
63 // Verify the integrity of the message.
64 scoped_ptr
<MediaMessage
> msg2(
65 MediaMessage::MapMessage(scoped_ptr
<MediaMemoryChunk
>(
66 new ExternalMemoryBlock(&buffer
[0], buffer_size
))));
67 for (int k
= 0; k
< count
; k
++) {
69 int expected_v1
= 2 * k
+ 1;
70 EXPECT_TRUE(msg2
->ReadPod(&v1
));
71 EXPECT_EQ(v1
, expected_v1
);
73 uint8 expected_v2
= k
;
74 EXPECT_TRUE(msg2
->ReadPod(&v2
));
75 EXPECT_EQ(v2
, expected_v2
);
79 TEST(MediaMessageTest
, WriteOverflow
) {
80 int buffer_size
= 1024;
81 scoped_ptr
<uint8
[]> buffer(new uint8
[buffer_size
]);
82 MediaMessage::MemoryAllocatorCB
mem_alloc_cb(
83 base::Bind(&DummyAllocator
, buffer
.get(), buffer_size
));
85 int msg_content_capacity
= 8;
87 scoped_ptr
<MediaMessage
> msg1(
88 MediaMessage::CreateMessage(type
, mem_alloc_cb
, msg_content_capacity
));
91 EXPECT_TRUE(msg1
->WritePod(v1
));
92 EXPECT_TRUE(msg1
->WritePod(v1
));
94 EXPECT_FALSE(msg1
->WritePod(v1
));
95 EXPECT_FALSE(msg1
->WritePod(v2
));
98 TEST(MediaMessageTest
, ReadOverflow
) {
99 int buffer_size
= 1024;
100 scoped_ptr
<uint8
[]> buffer(new uint8
[buffer_size
]);
101 MediaMessage::MemoryAllocatorCB
mem_alloc_cb(
102 base::Bind(&DummyAllocator
, buffer
.get(), buffer_size
));
104 int msg_content_capacity
= 8;
106 scoped_ptr
<MediaMessage
> msg1(
107 MediaMessage::CreateMessage(type
, mem_alloc_cb
, msg_content_capacity
));
109 EXPECT_TRUE(msg1
->WritePod(v1
));
110 EXPECT_TRUE(msg1
->WritePod(v1
));
112 scoped_ptr
<MediaMessage
> msg2(
113 MediaMessage::MapMessage(scoped_ptr
<MediaMemoryChunk
>(
114 new ExternalMemoryBlock(&buffer
[0], buffer_size
))));
116 EXPECT_TRUE(msg2
->ReadPod(&v2
));
118 EXPECT_TRUE(msg2
->ReadPod(&v2
));
120 EXPECT_FALSE(msg2
->ReadPod(&v2
));
123 TEST(MediaMessageTest
, DummyMessage
) {
124 int buffer_size
= 1024;
125 scoped_ptr
<uint8
[]> buffer(new uint8
[buffer_size
]);
126 MediaMessage::MemoryAllocatorCB
mem_alloc_cb(
127 base::Bind(&DummyAllocator
, buffer
.get(), buffer_size
));
130 // Create first a dummy message to estimate the content size.
131 scoped_ptr
<MediaMessage
> msg1(
132 MediaMessage::CreateDummyMessage(type
));
134 EXPECT_TRUE(msg1
->WritePod(v1
));
135 EXPECT_TRUE(msg1
->WritePod(v1
));
137 // Create the real message and write the actual content.
138 scoped_ptr
<MediaMessage
> msg2(
139 MediaMessage::CreateMessage(type
, mem_alloc_cb
, msg1
->content_size()));
140 EXPECT_TRUE(msg2
->WritePod(v1
));
141 EXPECT_TRUE(msg2
->WritePod(v1
));
142 EXPECT_FALSE(msg2
->WritePod(v1
));
146 } // namespace chromecast