1 // Copyright (c) 2012 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/strings/string_util.h"
6 #include "media/base/decoder_buffer.h"
7 #include "testing/gtest/include/gtest/gtest.h"
11 TEST(DecoderBufferTest
, Constructors
) {
12 scoped_refptr
<DecoderBuffer
> buffer(new DecoderBuffer(0));
13 EXPECT_TRUE(buffer
->data());
14 EXPECT_EQ(0, buffer
->data_size());
15 EXPECT_FALSE(buffer
->end_of_stream());
16 EXPECT_FALSE(buffer
->is_key_frame());
18 const int kTestSize
= 10;
19 scoped_refptr
<DecoderBuffer
> buffer3(new DecoderBuffer(kTestSize
));
20 ASSERT_TRUE(buffer3
.get());
21 EXPECT_EQ(kTestSize
, buffer3
->data_size());
24 TEST(DecoderBufferTest
, CreateEOSBuffer
) {
25 scoped_refptr
<DecoderBuffer
> buffer(DecoderBuffer::CreateEOSBuffer());
26 EXPECT_TRUE(buffer
->end_of_stream());
29 TEST(DecoderBufferTest
, CopyFrom
) {
30 const uint8 kData
[] = "hello";
31 const int kDataSize
= arraysize(kData
);
33 scoped_refptr
<DecoderBuffer
> buffer2(DecoderBuffer::CopyFrom(
34 reinterpret_cast<const uint8
*>(&kData
), kDataSize
));
35 ASSERT_TRUE(buffer2
.get());
36 EXPECT_NE(kData
, buffer2
->data());
37 EXPECT_EQ(buffer2
->data_size(), kDataSize
);
38 EXPECT_EQ(0, memcmp(buffer2
->data(), kData
, kDataSize
));
39 EXPECT_FALSE(buffer2
->end_of_stream());
40 EXPECT_FALSE(buffer2
->is_key_frame());
42 scoped_refptr
<DecoderBuffer
> buffer3(DecoderBuffer::CopyFrom(
43 reinterpret_cast<const uint8
*>(&kData
), kDataSize
,
44 reinterpret_cast<const uint8
*>(&kData
), kDataSize
));
45 ASSERT_TRUE(buffer3
.get());
46 EXPECT_NE(kData
, buffer3
->data());
47 EXPECT_EQ(buffer3
->data_size(), kDataSize
);
48 EXPECT_EQ(0, memcmp(buffer3
->data(), kData
, kDataSize
));
49 EXPECT_NE(kData
, buffer3
->side_data());
50 EXPECT_EQ(buffer3
->side_data_size(), kDataSize
);
51 EXPECT_EQ(0, memcmp(buffer3
->side_data(), kData
, kDataSize
));
52 EXPECT_FALSE(buffer3
->end_of_stream());
53 EXPECT_FALSE(buffer3
->is_key_frame());
56 #if !defined(OS_ANDROID)
57 TEST(DecoderBufferTest
, PaddingAlignment
) {
58 const uint8 kData
[] = "hello";
59 const int kDataSize
= arraysize(kData
);
60 scoped_refptr
<DecoderBuffer
> buffer2(DecoderBuffer::CopyFrom(
61 reinterpret_cast<const uint8
*>(&kData
), kDataSize
));
62 ASSERT_TRUE(buffer2
.get());
64 // Padding data should always be zeroed.
65 for(int i
= 0; i
< DecoderBuffer::kPaddingSize
; i
++)
66 EXPECT_EQ((buffer2
->data() + kDataSize
)[i
], 0);
68 // If the data is padded correctly we should be able to read and write past
69 // the end of the data by DecoderBuffer::kPaddingSize bytes without crashing
70 // or Valgrind/ASAN throwing errors.
71 const uint8 kFillChar
= 0xFF;
73 buffer2
->writable_data() + kDataSize
, kFillChar
,
74 DecoderBuffer::kPaddingSize
);
75 for(int i
= 0; i
< DecoderBuffer::kPaddingSize
; i
++)
76 EXPECT_EQ((buffer2
->data() + kDataSize
)[i
], kFillChar
);
78 EXPECT_EQ(0u, reinterpret_cast<uintptr_t>(
79 buffer2
->data()) & (DecoderBuffer::kAlignmentSize
- 1));
81 EXPECT_FALSE(buffer2
->is_key_frame());
85 TEST(DecoderBufferTest
, ReadingWriting
) {
86 const char kData
[] = "hello";
87 const int kDataSize
= arraysize(kData
);
89 scoped_refptr
<DecoderBuffer
> buffer(new DecoderBuffer(kDataSize
));
90 ASSERT_TRUE(buffer
.get());
92 uint8
* data
= buffer
->writable_data();
94 ASSERT_EQ(kDataSize
, buffer
->data_size());
95 memcpy(data
, kData
, kDataSize
);
96 const uint8
* read_only_data
= buffer
->data();
97 ASSERT_EQ(data
, read_only_data
);
98 ASSERT_EQ(0, memcmp(read_only_data
, kData
, kDataSize
));
99 EXPECT_FALSE(buffer
->end_of_stream());
102 TEST(DecoderBufferTest
, DecryptConfig
) {
103 scoped_refptr
<DecoderBuffer
> buffer(new DecoderBuffer(0));
104 EXPECT_FALSE(buffer
->decrypt_config());
106 const char kKeyId
[] = "key id";
107 const char kIv
[] = "0123456789abcdef";
108 std::vector
<SubsampleEntry
> subsamples
;
109 subsamples
.push_back(SubsampleEntry(10, 5));
110 subsamples
.push_back(SubsampleEntry(15, 7));
112 DecryptConfig
decrypt_config(kKeyId
, kIv
, subsamples
);
114 buffer
->set_decrypt_config(
115 make_scoped_ptr(new DecryptConfig(kKeyId
, kIv
, subsamples
)));
117 EXPECT_TRUE(buffer
->decrypt_config());
118 EXPECT_TRUE(buffer
->decrypt_config()->Matches(decrypt_config
));
121 TEST(DecoderBufferTest
, IsKeyFrame
) {
122 scoped_refptr
<DecoderBuffer
> buffer(new DecoderBuffer(0));
123 EXPECT_FALSE(buffer
->is_key_frame());
125 buffer
->set_is_key_frame(false);
126 EXPECT_FALSE(buffer
->is_key_frame());
128 buffer
->set_is_key_frame(true);
129 EXPECT_TRUE(buffer
->is_key_frame());