Blame unitTests/test_basicio.cpp

Packit Service 21b5d1
#include <exiv2/basicio.hpp>
Packit Service 21b5d1
Packit Service 21b5d1
#include "gtestwrapper.h"
Packit Service 21b5d1
Packit Service 21b5d1
using namespace Exiv2;
Packit Service 21b5d1
Packit Service 21b5d1
TEST(MemIo, seek_out_of_bounds_00)
Packit Service 21b5d1
{
Packit Service 21b5d1
    byte buf[1024];
Packit Service 21b5d1
    memset(buf, 0, sizeof(buf));
Packit Service 21b5d1
Packit Service 21b5d1
    MemIo io(buf, sizeof(buf));
Packit Service 21b5d1
    ASSERT_FALSE(io.eof());
Packit Service 21b5d1
Packit Service 21b5d1
    // Regression test for bug reported in https://github.com/Exiv2/exiv2/pull/945
Packit Service 21b5d1
    // The problem is that MemIo::seek() does not check that the new offset is
Packit Service 21b5d1
    // in bounds.
Packit Service 21b5d1
    byte tmp[16];
Packit Service 21b5d1
    ASSERT_EQ(io.seek(0x10000000, BasicIo::beg), 1);
Packit Service 21b5d1
    ASSERT_TRUE(io.eof());
Packit Service 21b5d1
Packit Service 21b5d1
    // The seek was invalid, so the offset didn't change and this read still works.
Packit Service 21b5d1
    const long sizeTmp = static_cast<long>(sizeof(sizeTmp));
Packit Service 21b5d1
    ASSERT_EQ(io.read(tmp, sizeTmp), sizeTmp);
Packit Service 21b5d1
}
Packit Service 21b5d1
Packit Service 21b5d1
TEST(MemIo, seek_out_of_bounds_01)
Packit Service 21b5d1
{
Packit Service 21b5d1
    byte buf[1024];
Packit Service 21b5d1
    memset(buf, 0, sizeof(buf));
Packit Service 21b5d1
Packit Service 21b5d1
    MemIo io(buf, sizeof(buf));
Packit Service 21b5d1
    ASSERT_FALSE(io.eof());
Packit Service 21b5d1
Packit Service 21b5d1
    byte tmp[16];
Packit Service 21b5d1
Packit Service 21b5d1
    // Seek to the end of the file.
Packit Service 21b5d1
    ASSERT_EQ(io.seek(0, BasicIo::end), 0);
Packit Service 21b5d1
    ASSERT_EQ(io.read(tmp, sizeof(tmp)), 0);
Packit Service 21b5d1
Packit Service 21b5d1
    // Try to seek past the end of the file.
Packit Service 21b5d1
    ASSERT_EQ(io.seek(0x10000000, BasicIo::end), 1);
Packit Service 21b5d1
    ASSERT_TRUE(io.eof());
Packit Service 21b5d1
    ASSERT_EQ(io.read(tmp, sizeof(tmp)), 0);
Packit Service 21b5d1
}
Packit Service 21b5d1
Packit Service 21b5d1
TEST(MemIo, seek_out_of_bounds_02)
Packit Service 21b5d1
{
Packit Service 21b5d1
    byte buf[1024];
Packit Service 21b5d1
    memset(buf, 0, sizeof(buf));
Packit Service 21b5d1
Packit Service 21b5d1
    MemIo io(buf, sizeof(buf));
Packit Service 21b5d1
    ASSERT_FALSE(io.eof());
Packit Service 21b5d1
Packit Service 21b5d1
    byte tmp[16];
Packit Service 21b5d1
Packit Service 21b5d1
    // Try to seek past the end of the file.
Packit Service 21b5d1
    ASSERT_EQ(io.seek(0x10000000, BasicIo::cur), 1);
Packit Service 21b5d1
    ASSERT_TRUE(io.eof());
Packit Service 21b5d1
    // The seek was invalid, so the offset didn't change and this read still works.
Packit Service 21b5d1
    const long sizeTmp = static_cast<long>(sizeof(sizeTmp));
Packit Service 21b5d1
    ASSERT_EQ(io.read(tmp, sizeTmp), sizeTmp);
Packit Service 21b5d1
}
Packit Service 21b5d1
Packit Service 21b5d1
TEST(MemIo, seek_out_of_bounds_03)
Packit Service 21b5d1
{
Packit Service 21b5d1
    byte buf[1024];
Packit Service 21b5d1
    memset(buf, 0, sizeof(buf));
Packit Service 21b5d1
Packit Service 21b5d1
    MemIo io(buf, sizeof(buf));
Packit Service 21b5d1
    ASSERT_FALSE(io.eof());
Packit Service 21b5d1
Packit Service 21b5d1
    byte tmp[16];
Packit Service 21b5d1
Packit Service 21b5d1
    // Try to seek past the beginning of the file.
Packit Service 21b5d1
    ASSERT_EQ(io.seek(-0x10000000, BasicIo::cur), 1);
Packit Service 21b5d1
    ASSERT_FALSE(io.eof());
Packit Service 21b5d1
    // The seek was invalid, so the offset didn't change and this read still works.
Packit Service 21b5d1
    const long sizeTmp = static_cast<long>(sizeof(sizeTmp));
Packit Service 21b5d1
    ASSERT_EQ(io.read(tmp, sizeTmp), sizeTmp);
Packit Service 21b5d1
}