From 0eb74769c9efbcb94e57f160882ce3309684b2f9 Mon Sep 17 00:00:00 2001 From: Dan Sinclair Date: Tue, 28 Mar 2017 10:19:22 -0400 Subject: Add JBig2_BitStream ReadNBits unittests Add test for reading past end of buffer and successful read of bits. Bug: chromium:672176 Change-Id: Ibe2d818185cdb2260011d3fc8cad94ebb16984b7 Reviewed-on: https://pdfium-review.googlesource.com/3233 Reviewed-by: Tom Sepez Commit-Queue: dsinclair --- core/fxcodec/jbig2/JBig2_BitStream_unittest.cpp | 119 ++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 core/fxcodec/jbig2/JBig2_BitStream_unittest.cpp (limited to 'core/fxcodec/jbig2') diff --git a/core/fxcodec/jbig2/JBig2_BitStream_unittest.cpp b/core/fxcodec/jbig2/JBig2_BitStream_unittest.cpp new file mode 100644 index 0000000000..f9d69b9c53 --- /dev/null +++ b/core/fxcodec/jbig2/JBig2_BitStream_unittest.cpp @@ -0,0 +1,119 @@ +// Copyright 2017 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "core/fxcodec/jbig2/JBig2_BitStream.h" + +#include +#include + +#include "core/fpdfapi/parser/cpdf_stream_acc.h" +#include "testing/gtest/include/gtest/gtest.h" +#include "third_party/base/ptr_util.h" + +TEST(JBig2_BitStream, ReadNBits) { + std::unique_ptr data(FX_Alloc(uint8_t, 1)); + data.get()[0] = 0xb1; // 10110001 + + auto in_stream = pdfium::MakeUnique(std::move(data), 1, nullptr); + auto acc = pdfium::MakeUnique(); + acc->LoadAllData(in_stream.get()); + + CJBig2_BitStream stream(acc.get()); + + uint32_t val1; + EXPECT_EQ(0, stream.readNBits(1, &val1)); + EXPECT_EQ(1U, val1); + + int32_t val2; + EXPECT_EQ(0, stream.readNBits(1, &val2)); + EXPECT_EQ(0, val2); + + EXPECT_EQ(0, stream.readNBits(2, &val2)); + EXPECT_EQ(3, val2); + + EXPECT_EQ(0, stream.readNBits(4, &val2)); + EXPECT_EQ(1, val2); +} + +TEST(JBig2_BitStream, ReadNBitsLargerThenData) { + std::unique_ptr data(FX_Alloc(uint8_t, 1)); + data.get()[0] = 0xb1; + + auto in_stream = pdfium::MakeUnique(std::move(data), 1, nullptr); + auto acc = pdfium::MakeUnique(); + acc->LoadAllData(in_stream.get()); + + CJBig2_BitStream stream(acc.get()); + + uint32_t val1; + EXPECT_EQ(0, stream.readNBits(10, &val1)); + EXPECT_EQ(0xb1U, val1); +} + +TEST(JBig2_BitStream, ReadNBitsNullStream) { + auto in_stream = pdfium::MakeUnique(nullptr, 0, nullptr); + auto acc = pdfium::MakeUnique(); + acc->LoadAllData(in_stream.get()); + + CJBig2_BitStream stream(acc.get()); + + uint32_t val1; + EXPECT_EQ(-1, stream.readNBits(1, &val1)); + + int32_t val2; + EXPECT_EQ(-1, stream.readNBits(2, &val2)); +} + +TEST(JBig2_BitStream, ReadNBitsEmptyStream) { + std::unique_ptr data(FX_Alloc(uint8_t, 1)); + + auto in_stream = pdfium::MakeUnique(std::move(data), 0, nullptr); + auto acc = pdfium::MakeUnique(); + acc->LoadAllData(in_stream.get()); + + CJBig2_BitStream stream(acc.get()); + + uint32_t val1; + EXPECT_EQ(-1, stream.readNBits(1, &val1)); + + int32_t val2; + EXPECT_EQ(-1, stream.readNBits(2, &val2)); +} + +TEST(JBig2_BitStream, ReadNBitsOutOfBounds) { + std::unique_ptr data(FX_Alloc(uint8_t, 1)); + data.get()[0] = 0xb1; // 10110001 + + auto in_stream = pdfium::MakeUnique(std::move(data), 1, nullptr); + auto acc = pdfium::MakeUnique(); + acc->LoadAllData(in_stream.get()); + + CJBig2_BitStream stream(acc.get()); + + uint32_t val1; + EXPECT_EQ(0, stream.readNBits(8, &val1)); + + int32_t val2; + EXPECT_EQ(-1, stream.readNBits(2, &val2)); +} + +TEST(JBig2_BitStream, ReadNBitsWhereNIs36) { + std::unique_ptr data(FX_Alloc(uint8_t, 5)); + data.get()[0] = 0xb0; + data.get()[1] = 0x01; + data.get()[2] = 0x00; + data.get()[3] = 0x00; + data.get()[4] = 0x40; + + auto in_stream = pdfium::MakeUnique(std::move(data), 5, nullptr); + auto acc = pdfium::MakeUnique(); + acc->LoadAllData(in_stream.get()); + + CJBig2_BitStream stream(acc.get()); + + // This will shift off the top two bits and they end up lost. + uint32_t val1; + EXPECT_EQ(0, stream.readNBits(34, &val1)); + EXPECT_EQ(0xc0040001U, val1); +} -- cgit v1.2.3