2022-08-26 15:44:59 +00:00
|
|
|
// Copyright 2014 Google LLC. All rights reserved.
|
2014-02-14 23:21:05 +00:00
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file or at
|
|
|
|
// https://developers.google.com/open-source/licenses/bsd
|
2013-11-14 00:08:54 +00:00
|
|
|
|
2023-10-10 23:51:11 +00:00
|
|
|
#include <packager/media/base/buffer_writer.h>
|
2013-11-14 00:08:54 +00:00
|
|
|
|
2022-11-04 22:46:41 +00:00
|
|
|
#include <filesystem>
|
2014-09-30 23:52:58 +00:00
|
|
|
#include <limits>
|
2016-08-17 17:41:40 +00:00
|
|
|
#include <memory>
|
2014-09-30 23:52:58 +00:00
|
|
|
|
2023-10-13 19:42:47 +00:00
|
|
|
#include <absl/log/log.h>
|
2023-10-11 08:49:50 +00:00
|
|
|
|
2023-10-10 23:51:11 +00:00
|
|
|
#include <packager/file/file.h>
|
|
|
|
#include <packager/file/file_test_util.h>
|
|
|
|
#include <packager/media/base/buffer_reader.h>
|
|
|
|
#include <packager/status/status_test_util.h>
|
2013-11-14 00:08:54 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
const int kReservedBufferCapacity = 1000;
|
2014-09-30 21:52:21 +00:00
|
|
|
const uint8_t kuint8 = 10;
|
|
|
|
const uint16_t kuint16 = 1000;
|
|
|
|
const int16_t kint16 = -1000;
|
|
|
|
const uint32_t kuint32 = 1000000;
|
|
|
|
const int32_t kint32 = -1000000;
|
|
|
|
const uint64_t kuint64 = 10000000000ULL;
|
|
|
|
const int64_t kint64 = -10000000000LL;
|
|
|
|
const uint8_t kuint8Array[] = {10, 1, 100, 5, 3, 60};
|
2013-11-14 00:08:54 +00:00
|
|
|
} // namespace
|
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2013-11-14 00:08:54 +00:00
|
|
|
namespace media {
|
|
|
|
|
|
|
|
class BufferWriterTest : public testing::Test {
|
|
|
|
public:
|
2014-02-26 23:55:01 +00:00
|
|
|
BufferWriterTest() : writer_(new BufferWriter(kReservedBufferCapacity)) {}
|
|
|
|
|
|
|
|
void CreateReader() {
|
|
|
|
reader_.reset(new BufferReader(writer_->Buffer(), writer_->Size()));
|
|
|
|
}
|
2013-11-14 00:08:54 +00:00
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
bool ReadInt(uint8_t* v) { return reader_->Read1(v); }
|
|
|
|
bool ReadInt(uint16_t* v) { return reader_->Read2(v); }
|
|
|
|
bool ReadInt(int16_t* v) { return reader_->Read2s(v); }
|
|
|
|
bool ReadInt(uint32_t* v) { return reader_->Read4(v); }
|
|
|
|
bool ReadInt(int32_t* v) { return reader_->Read4s(v); }
|
|
|
|
bool ReadInt(uint64_t* v) { return reader_->Read8(v); }
|
|
|
|
bool ReadInt(int64_t* v) { return reader_->Read8s(v); }
|
2013-11-14 00:08:54 +00:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void ReadAndExpect(T expectation) {
|
|
|
|
T data_read;
|
|
|
|
ASSERT_TRUE(ReadInt(&data_read));
|
|
|
|
ASSERT_EQ(expectation, data_read);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2014-09-30 23:52:58 +00:00
|
|
|
void Verify(T val) {
|
|
|
|
T min = std::numeric_limits<T>::min();
|
|
|
|
T max = std::numeric_limits<T>::max();
|
|
|
|
|
2013-11-14 00:08:54 +00:00
|
|
|
writer_->AppendInt(min);
|
|
|
|
writer_->AppendInt(max);
|
|
|
|
writer_->AppendInt(val);
|
|
|
|
ASSERT_EQ(sizeof(min) + sizeof(max) + sizeof(val), writer_->Size());
|
2014-02-26 23:55:01 +00:00
|
|
|
|
|
|
|
CreateReader();
|
2013-11-14 00:08:54 +00:00
|
|
|
ReadAndExpect(min);
|
|
|
|
ReadAndExpect(max);
|
|
|
|
ReadAndExpect(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<BufferWriter> writer_;
|
|
|
|
std::unique_ptr<BufferReader> reader_;
|
2013-11-14 00:08:54 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(BufferWriterTest);
|
|
|
|
};
|
|
|
|
|
2014-09-30 23:52:58 +00:00
|
|
|
TEST_F(BufferWriterTest, Append1) { Verify(kuint8); }
|
|
|
|
TEST_F(BufferWriterTest, Append2) { Verify(kuint16); }
|
|
|
|
TEST_F(BufferWriterTest, Append2s) { Verify(kint16); }
|
|
|
|
TEST_F(BufferWriterTest, Append4) { Verify(kuint32); }
|
|
|
|
TEST_F(BufferWriterTest, Append4s) { Verify(kint32); }
|
|
|
|
TEST_F(BufferWriterTest, Append8) { Verify(kuint64); }
|
|
|
|
TEST_F(BufferWriterTest, Append8s) { Verify(kint64); }
|
2013-11-14 00:08:54 +00:00
|
|
|
|
|
|
|
TEST_F(BufferWriterTest, AppendNBytes) {
|
|
|
|
// Write the least significant four bytes and verify the result.
|
2014-09-30 21:52:21 +00:00
|
|
|
writer_->AppendNBytes(kuint64, sizeof(uint32_t));
|
|
|
|
ASSERT_EQ(sizeof(uint32_t), writer_->Size());
|
2014-02-26 23:55:01 +00:00
|
|
|
|
|
|
|
CreateReader();
|
2014-09-30 21:52:21 +00:00
|
|
|
ReadAndExpect(static_cast<uint32_t>(kuint64 & 0xFFFFFFFF));
|
2013-11-14 00:08:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BufferWriterTest, AppendEmptyVector) {
|
2014-09-30 21:52:21 +00:00
|
|
|
std::vector<uint8_t> v;
|
2013-11-14 00:08:54 +00:00
|
|
|
writer_->AppendVector(v);
|
2014-03-21 17:26:49 +00:00
|
|
|
ASSERT_EQ(0u, writer_->Size());
|
2013-11-14 00:08:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BufferWriterTest, AppendVector) {
|
2014-09-30 21:52:21 +00:00
|
|
|
std::vector<uint8_t> v(kuint8Array, kuint8Array + sizeof(kuint8Array));
|
2013-11-14 00:08:54 +00:00
|
|
|
writer_->AppendVector(v);
|
|
|
|
ASSERT_EQ(sizeof(kuint8Array), writer_->Size());
|
|
|
|
|
2014-02-26 23:55:01 +00:00
|
|
|
CreateReader();
|
2014-09-30 21:52:21 +00:00
|
|
|
std::vector<uint8_t> data_read;
|
2013-11-14 00:08:54 +00:00
|
|
|
ASSERT_TRUE(reader_->ReadToVector(&data_read, sizeof(kuint8Array)));
|
|
|
|
ASSERT_EQ(v, data_read);
|
|
|
|
}
|
|
|
|
|
2018-05-07 22:54:59 +00:00
|
|
|
TEST_F(BufferWriterTest, AppendString) {
|
|
|
|
const char kTestData[] = "test_data";
|
|
|
|
writer_->AppendString(kTestData);
|
|
|
|
// -1 to remove the null terminating character.
|
|
|
|
ASSERT_EQ(strlen(kTestData), writer_->Size());
|
|
|
|
|
|
|
|
CreateReader();
|
|
|
|
std::string data_read;
|
|
|
|
ASSERT_TRUE(reader_->ReadToString(&data_read, strlen(kTestData)));
|
|
|
|
ASSERT_EQ(kTestData, data_read);
|
|
|
|
}
|
|
|
|
|
2013-11-14 00:08:54 +00:00
|
|
|
TEST_F(BufferWriterTest, AppendArray) {
|
|
|
|
writer_->AppendArray(kuint8Array, sizeof(kuint8Array));
|
|
|
|
ASSERT_EQ(sizeof(kuint8Array), writer_->Size());
|
|
|
|
|
2014-02-26 23:55:01 +00:00
|
|
|
CreateReader();
|
2014-09-30 21:52:21 +00:00
|
|
|
std::vector<uint8_t> data_read;
|
2013-11-14 00:08:54 +00:00
|
|
|
ASSERT_TRUE(reader_->ReadToVector(&data_read, sizeof(kuint8Array)));
|
|
|
|
for (size_t i = 0; i < sizeof(kuint8Array); ++i)
|
|
|
|
EXPECT_EQ(kuint8Array[i], data_read[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BufferWriterTest, AppendBufferWriter) {
|
|
|
|
BufferWriter local_writer;
|
|
|
|
local_writer.AppendInt(kuint16);
|
|
|
|
local_writer.AppendInt(kint64);
|
|
|
|
local_writer.AppendInt(kuint32);
|
|
|
|
writer_->AppendBuffer(local_writer);
|
|
|
|
ASSERT_EQ(sizeof(kuint16) + sizeof(kint64) + sizeof(kuint32),
|
|
|
|
writer_->Size());
|
|
|
|
|
2014-02-26 23:55:01 +00:00
|
|
|
CreateReader();
|
2013-11-14 00:08:54 +00:00
|
|
|
ASSERT_NO_FATAL_FAILURE(ReadAndExpect(kuint16));
|
|
|
|
ASSERT_NO_FATAL_FAILURE(ReadAndExpect(kint64));
|
|
|
|
ASSERT_NO_FATAL_FAILURE(ReadAndExpect(kuint32));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BufferWriterTest, Swap) {
|
|
|
|
BufferWriter local_writer;
|
|
|
|
local_writer.AppendInt(kuint16);
|
|
|
|
local_writer.AppendInt(kint64);
|
|
|
|
writer_->AppendInt(kuint32);
|
|
|
|
writer_->Swap(&local_writer);
|
|
|
|
|
|
|
|
ASSERT_EQ(sizeof(kuint16) + sizeof(kint64), writer_->Size());
|
|
|
|
ASSERT_EQ(sizeof(kuint32), local_writer.Size());
|
|
|
|
|
2014-02-26 23:55:01 +00:00
|
|
|
CreateReader();
|
2013-11-14 00:08:54 +00:00
|
|
|
ASSERT_NO_FATAL_FAILURE(ReadAndExpect(kuint16));
|
|
|
|
ASSERT_NO_FATAL_FAILURE(ReadAndExpect(kint64));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BufferWriterTest, Clear) {
|
|
|
|
writer_->AppendInt(kuint32);
|
|
|
|
ASSERT_EQ(sizeof(kuint32), writer_->Size());
|
|
|
|
writer_->Clear();
|
2014-03-21 17:26:49 +00:00
|
|
|
ASSERT_EQ(0u, writer_->Size());
|
2013-11-14 00:08:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BufferWriterTest, WriteToFile) {
|
2022-11-04 22:46:41 +00:00
|
|
|
TempFile temp_file;
|
|
|
|
LOG(INFO) << "Created temporary file: " << temp_file.path();
|
2013-11-14 00:08:54 +00:00
|
|
|
|
|
|
|
// Append an array to buffer and then write to the temporary file.
|
2022-11-04 22:46:41 +00:00
|
|
|
File* const output_file = File::Open(temp_file.path().c_str(), "w");
|
2013-11-14 00:08:54 +00:00
|
|
|
writer_->AppendArray(kuint8Array, sizeof(kuint8Array));
|
|
|
|
ASSERT_EQ(sizeof(kuint8Array), writer_->Size());
|
|
|
|
ASSERT_OK(writer_->WriteToFile(output_file));
|
2014-03-21 17:26:49 +00:00
|
|
|
ASSERT_EQ(0u, writer_->Size());
|
2013-11-14 00:08:54 +00:00
|
|
|
ASSERT_TRUE(output_file->Close());
|
|
|
|
|
|
|
|
// Read the file and verify.
|
2022-11-04 22:46:41 +00:00
|
|
|
File* const input_file = File::Open(temp_file.path().c_str(), "r");
|
2013-11-14 00:08:54 +00:00
|
|
|
ASSERT_TRUE(input_file != NULL);
|
2014-09-30 21:52:21 +00:00
|
|
|
std::vector<uint8_t> data_read(sizeof(kuint8Array), 0);
|
2016-08-14 22:28:21 +00:00
|
|
|
EXPECT_EQ(sizeof(kuint8Array), static_cast<size_t>(input_file->Read(
|
|
|
|
&data_read[0], data_read.size())));
|
2013-11-14 00:08:54 +00:00
|
|
|
ASSERT_TRUE(input_file->Close());
|
|
|
|
|
|
|
|
for (size_t i = 0; i < sizeof(kuint8Array); ++i)
|
|
|
|
EXPECT_EQ(kuint8Array[i], data_read[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|