Home | History | Annotate | Download | only in base
      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.
      4 
      5 #include "base/strings/string_util.h"
      6 #include "media/base/decoder_buffer.h"
      7 #include "testing/gtest/include/gtest/gtest.h"
      8 
      9 namespace media {
     10 
     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 
     17   const int kTestSize = 10;
     18   scoped_refptr<DecoderBuffer> buffer3(new DecoderBuffer(kTestSize));
     19   ASSERT_TRUE(buffer3.get());
     20   EXPECT_EQ(kTestSize, buffer3->data_size());
     21 }
     22 
     23 TEST(DecoderBufferTest, CreateEOSBuffer) {
     24   scoped_refptr<DecoderBuffer> buffer(DecoderBuffer::CreateEOSBuffer());
     25   EXPECT_TRUE(buffer->end_of_stream());
     26 }
     27 
     28 TEST(DecoderBufferTest, CopyFrom) {
     29   const uint8 kData[] = "hello";
     30   const int kDataSize = arraysize(kData);
     31   scoped_refptr<DecoderBuffer> buffer2(DecoderBuffer::CopyFrom(
     32       reinterpret_cast<const uint8*>(&kData), kDataSize));
     33   ASSERT_TRUE(buffer2.get());
     34   EXPECT_NE(kData, buffer2->data());
     35   EXPECT_EQ(buffer2->data_size(), kDataSize);
     36   EXPECT_EQ(0, memcmp(buffer2->data(), kData, kDataSize));
     37   EXPECT_FALSE(buffer2->end_of_stream());
     38   scoped_refptr<DecoderBuffer> buffer3(DecoderBuffer::CopyFrom(
     39       reinterpret_cast<const uint8*>(&kData), kDataSize,
     40       reinterpret_cast<const uint8*>(&kData), kDataSize));
     41   ASSERT_TRUE(buffer3.get());
     42   EXPECT_NE(kData, buffer3->data());
     43   EXPECT_EQ(buffer3->data_size(), kDataSize);
     44   EXPECT_EQ(0, memcmp(buffer3->data(), kData, kDataSize));
     45   EXPECT_NE(kData, buffer3->side_data());
     46   EXPECT_EQ(buffer3->side_data_size(), kDataSize);
     47   EXPECT_EQ(0, memcmp(buffer3->side_data(), kData, kDataSize));
     48   EXPECT_FALSE(buffer3->end_of_stream());
     49 }
     50 
     51 #if !defined(OS_ANDROID)
     52 TEST(DecoderBufferTest, PaddingAlignment) {
     53   const uint8 kData[] = "hello";
     54   const int kDataSize = arraysize(kData);
     55   scoped_refptr<DecoderBuffer> buffer2(DecoderBuffer::CopyFrom(
     56       reinterpret_cast<const uint8*>(&kData), kDataSize));
     57   ASSERT_TRUE(buffer2.get());
     58 
     59   // Padding data should always be zeroed.
     60   for(int i = 0; i < DecoderBuffer::kPaddingSize; i++)
     61     EXPECT_EQ((buffer2->data() + kDataSize)[i], 0);
     62 
     63   // If the data is padded correctly we should be able to read and write past
     64   // the end of the data by DecoderBuffer::kPaddingSize bytes without crashing
     65   // or Valgrind/ASAN throwing errors.
     66   const uint8 kFillChar = 0xFF;
     67   memset(
     68       buffer2->writable_data() + kDataSize, kFillChar,
     69       DecoderBuffer::kPaddingSize);
     70   for(int i = 0; i < DecoderBuffer::kPaddingSize; i++)
     71     EXPECT_EQ((buffer2->data() + kDataSize)[i], kFillChar);
     72 
     73   EXPECT_EQ(0u, reinterpret_cast<uintptr_t>(
     74       buffer2->data()) & (DecoderBuffer::kAlignmentSize - 1));
     75 }
     76 #endif
     77 
     78 TEST(DecoderBufferTest, ReadingWriting) {
     79   const char kData[] = "hello";
     80   const int kDataSize = arraysize(kData);
     81 
     82   scoped_refptr<DecoderBuffer> buffer(new DecoderBuffer(kDataSize));
     83   ASSERT_TRUE(buffer.get());
     84 
     85   uint8* data = buffer->writable_data();
     86   ASSERT_TRUE(data);
     87   ASSERT_EQ(kDataSize, buffer->data_size());
     88   memcpy(data, kData, kDataSize);
     89   const uint8* read_only_data = buffer->data();
     90   ASSERT_EQ(data, read_only_data);
     91   ASSERT_EQ(0, memcmp(read_only_data, kData, kDataSize));
     92   EXPECT_FALSE(buffer->end_of_stream());
     93 }
     94 
     95 TEST(DecoderBufferTest, GetDecryptConfig) {
     96   scoped_refptr<DecoderBuffer> buffer(new DecoderBuffer(0));
     97   EXPECT_FALSE(buffer->decrypt_config());
     98 }
     99 
    100 }  // namespace media
    101