summaryrefslogtreecommitdiff
path: root/core/fxcodec
diff options
context:
space:
mode:
authorDan Sinclair <dsinclair@chromium.org>2017-03-28 10:19:22 -0400
committerChromium commit bot <commit-bot@chromium.org>2017-03-28 16:50:45 +0000
commit0eb74769c9efbcb94e57f160882ce3309684b2f9 (patch)
tree39526392244b7a9f9663be8f59af1475d142db6a /core/fxcodec
parentf0799fe84df4d86f25d342b03525f3016d674f86 (diff)
downloadpdfium-0eb74769c9efbcb94e57f160882ce3309684b2f9.tar.xz
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 <tsepez@chromium.org> Commit-Queue: dsinclair <dsinclair@chromium.org>
Diffstat (limited to 'core/fxcodec')
-rw-r--r--core/fxcodec/jbig2/JBig2_BitStream_unittest.cpp119
1 files changed, 119 insertions, 0 deletions
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 <memory>
+#include <utility>
+
+#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<uint8_t, FxFreeDeleter> data(FX_Alloc(uint8_t, 1));
+ data.get()[0] = 0xb1; // 10110001
+
+ auto in_stream = pdfium::MakeUnique<CPDF_Stream>(std::move(data), 1, nullptr);
+ auto acc = pdfium::MakeUnique<CPDF_StreamAcc>();
+ 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<uint8_t, FxFreeDeleter> data(FX_Alloc(uint8_t, 1));
+ data.get()[0] = 0xb1;
+
+ auto in_stream = pdfium::MakeUnique<CPDF_Stream>(std::move(data), 1, nullptr);
+ auto acc = pdfium::MakeUnique<CPDF_StreamAcc>();
+ 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<CPDF_Stream>(nullptr, 0, nullptr);
+ auto acc = pdfium::MakeUnique<CPDF_StreamAcc>();
+ 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<uint8_t, FxFreeDeleter> data(FX_Alloc(uint8_t, 1));
+
+ auto in_stream = pdfium::MakeUnique<CPDF_Stream>(std::move(data), 0, nullptr);
+ auto acc = pdfium::MakeUnique<CPDF_StreamAcc>();
+ 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<uint8_t, FxFreeDeleter> data(FX_Alloc(uint8_t, 1));
+ data.get()[0] = 0xb1; // 10110001
+
+ auto in_stream = pdfium::MakeUnique<CPDF_Stream>(std::move(data), 1, nullptr);
+ auto acc = pdfium::MakeUnique<CPDF_StreamAcc>();
+ 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<uint8_t, FxFreeDeleter> 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<CPDF_Stream>(std::move(data), 5, nullptr);
+ auto acc = pdfium::MakeUnique<CPDF_StreamAcc>();
+ 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);
+}