1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/embedder_support/android/util/input_stream_reader.h"
#include "base/android/scoped_java_ref.h"
#include "base/functional/callback.h"
#include "base/memory/ref_counted.h"
#include "components/embedder_support/android/util/input_stream.h"
#include "net/base/io_buffer.h"
#include "net/http/http_byte_range.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using embedder_support::InputStream;
using embedder_support::InputStreamReader;
using testing::_;
using testing::DoAll;
using testing::Ge;
using testing::InSequence;
using testing::Lt;
using testing::Ne;
using testing::NotNull;
using testing::Return;
using testing::SetArgPointee;
using testing::Test;
class MockInputStream : public embedder_support::InputStream {
public:
MockInputStream() = default;
~MockInputStream() override = default;
MOCK_CONST_METHOD1(BytesAvailable, bool(int* bytes_available));
MOCK_METHOD2(Skip, bool(int64_t n, int64_t* bytes_skipped));
MOCK_METHOD3(Read, bool(net::IOBuffer* dest, int length, int* bytes_read));
};
class InputStreamReaderTest : public Test {
public:
InputStreamReaderTest() : input_stream_reader_(&input_stream_) {}
protected:
int SeekRange(int first_byte, int last_byte) {
net::HttpByteRange byte_range;
byte_range.set_first_byte_position(first_byte);
byte_range.set_last_byte_position(last_byte);
return input_stream_reader_.Seek(byte_range);
}
int ReadRawData(scoped_refptr<net::IOBuffer> buffer, int bytesToRead) {
return input_stream_reader_.ReadRawData(buffer.get(), bytesToRead);
}
MockInputStream input_stream_;
InputStreamReader input_stream_reader_;
};
TEST_F(InputStreamReaderTest, BytesAvailableFailurePropagationOnSeek) {
EXPECT_CALL(input_stream_, BytesAvailable(NotNull())).WillOnce(Return(false));
ASSERT_GT(0, SeekRange(0, 0));
}
TEST_F(InputStreamReaderTest, SkipFailurePropagationOnSeek) {
const int streamSize = 10;
const int bytesToSkip = 5;
EXPECT_CALL(input_stream_, BytesAvailable(NotNull()))
.WillOnce(DoAll(SetArgPointee<0>(streamSize), Return(true)));
EXPECT_CALL(input_stream_, Skip(bytesToSkip, NotNull()))
.WillOnce(Return(false));
ASSERT_GT(0, SeekRange(bytesToSkip, streamSize - 1));
}
TEST_F(InputStreamReaderTest, SeekToMiddle) {
const int streamSize = 10;
const int bytesToSkip = 5;
EXPECT_CALL(input_stream_, BytesAvailable(NotNull()))
.WillOnce(DoAll(SetArgPointee<0>(streamSize), Return(true)));
EXPECT_CALL(input_stream_, Skip(bytesToSkip, NotNull()))
.WillOnce(DoAll(SetArgPointee<1>(bytesToSkip), Return(true)));
ASSERT_EQ(bytesToSkip, SeekRange(bytesToSkip, streamSize - 1));
}
TEST_F(InputStreamReaderTest, SeekToMiddleInSteps) {
const int streamSize = 10;
const int bytesToSkip = 5;
EXPECT_CALL(input_stream_, BytesAvailable(NotNull()))
.Times(1)
.WillOnce(DoAll(SetArgPointee<0>(streamSize), Return(true)));
EXPECT_CALL(input_stream_, Skip(_, _)).Times(0);
{
InSequence s;
EXPECT_CALL(input_stream_, Skip(bytesToSkip, NotNull()))
.WillOnce(DoAll(SetArgPointee<1>(bytesToSkip - 3), Return(true)))
.RetiresOnSaturation();
EXPECT_CALL(input_stream_, Skip(3, NotNull()))
.WillOnce(DoAll(SetArgPointee<1>(1), Return(true)))
.RetiresOnSaturation();
EXPECT_CALL(input_stream_, Skip(2, NotNull()))
.WillOnce(DoAll(SetArgPointee<1>(2), Return(true)))
.RetiresOnSaturation();
}
ASSERT_EQ(bytesToSkip, SeekRange(bytesToSkip, streamSize - 1));
}
TEST_F(InputStreamReaderTest, SeekEmpty) {
EXPECT_CALL(input_stream_, BytesAvailable(NotNull()))
.WillOnce(DoAll(SetArgPointee<0>(0), Return(true)));
ASSERT_EQ(0, SeekRange(0, 0));
}
TEST_F(InputStreamReaderTest, SeekMoreThanAvailable) {
const int bytesAvailable = 256;
EXPECT_CALL(input_stream_, BytesAvailable(NotNull()))
.WillOnce(DoAll(SetArgPointee<0>(bytesAvailable), Return(true)));
ASSERT_GT(0, SeekRange(bytesAvailable, 2 * bytesAvailable));
}
TEST_F(InputStreamReaderTest, ReadFailure) {
const int bytesToRead = 128;
auto buffer = base::MakeRefCounted<net::IOBufferWithSize>(bytesToRead);
EXPECT_CALL(input_stream_, Read(buffer.get(), bytesToRead, NotNull()))
.WillOnce(Return(false));
ASSERT_GT(0, ReadRawData(buffer, bytesToRead));
}
TEST_F(InputStreamReaderTest, ReadNothing) {
const int bytesToRead = 0;
// Size of net::IOBuffer can't be 0.
auto buffer = base::MakeRefCounted<net::IOBufferWithSize>(1);
EXPECT_CALL(input_stream_, Read(buffer.get(), bytesToRead, NotNull()))
.Times(0);
ASSERT_EQ(0, ReadRawData(buffer, bytesToRead));
}
TEST_F(InputStreamReaderTest, ReadSuccess) {
const int bytesToRead = 128;
auto buffer = base::MakeRefCounted<net::IOBufferWithSize>(bytesToRead);
EXPECT_CALL(input_stream_, Read(buffer.get(), bytesToRead, NotNull()))
.WillOnce(DoAll(SetArgPointee<2>(bytesToRead), Return(true)));
ASSERT_EQ(bytesToRead, ReadRawData(buffer, bytesToRead));
}
|