From 5628fd71197c0d476a89cf3a00810aaf9fdfc086 Mon Sep 17 00:00:00 2001 From: Tom Sepez Date: Thu, 27 Apr 2017 14:58:53 -0700 Subject: Add barcode test skeleton Individual tests need some more fleshing out. Fix spelling of "Destroy" while we're at it. Bug: pdfium:699 Change-Id: I05f1da8654bfdf92cb264adae16e1b3209587a31 Reviewed-on: https://pdfium-review.googlesource.com/4550 Commit-Queue: Tom Sepez Reviewed-by: Lei Zhang --- BUILD.gn | 2 + fpdfsdk/fpdfview.cpp | 2 +- fxbarcode/BC_Library.cpp | 3 +- fxbarcode/BC_Library.h | 2 +- testing/libfuzzer/BUILD.gn | 6 ++ testing/libfuzzer/pdf_cfx_barcode_fuzzer.cc | 38 +++++++++ testing/mock_ifx_renderdevicedriver.h | 59 +++++++++++++ xfa/fwl/cfx_barcode_unittest.cpp | 123 ++++++++++++++++++++++++++++ 8 files changed, 232 insertions(+), 3 deletions(-) create mode 100644 testing/libfuzzer/pdf_cfx_barcode_fuzzer.cc create mode 100644 testing/mock_ifx_renderdevicedriver.h create mode 100644 xfa/fwl/cfx_barcode_unittest.cpp diff --git a/BUILD.gn b/BUILD.gn index 938c01b8f3..8cdc9037c0 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -227,6 +227,7 @@ static_library("test_support") { sources = [ "testing/fx_string_testhelpers.cpp", "testing/fx_string_testhelpers.h", + "testing/mock_ifx_renderdevicedriver.h", "testing/test_support.cpp", "testing/test_support.h", "testing/utils/path_service.cpp", @@ -1896,6 +1897,7 @@ test("pdfium_unittests") { "xfa/fde/css/cfde_cssstylesheet_unittest.cpp", "xfa/fde/css/cfde_cssvaluelistparser_unittest.cpp", "xfa/fgas/layout/cfx_rtfbreak_unittest.cpp", + "xfa/fwl/cfx_barcode_unittest.cpp", "xfa/fxfa/app/cxfa_textparser_unittest.cpp", "xfa/fxfa/app/xfa_ffbarcode_unittest.cpp", "xfa/fxfa/cxfa_ffapp_unittest.cpp", diff --git a/fpdfsdk/fpdfview.cpp b/fpdfsdk/fpdfview.cpp index 6f32473985..4fea392228 100644 --- a/fpdfsdk/fpdfview.cpp +++ b/fpdfsdk/fpdfview.cpp @@ -419,7 +419,7 @@ DLLEXPORT void STDCALL FPDF_DestroyLibrary() { return; #ifdef PDF_ENABLE_XFA - BC_Library_Destory(); + BC_Library_Destroy(); FXJSE_Finalize(); #endif // PDF_ENABLE_XFA diff --git a/fxbarcode/BC_Library.cpp b/fxbarcode/BC_Library.cpp index c52e92a191..5d658ef3ec 100644 --- a/fxbarcode/BC_Library.cpp +++ b/fxbarcode/BC_Library.cpp @@ -25,7 +25,8 @@ void BC_Library_Init() { CBC_ErrorCorrection::Initialize(); CBC_PDF417HighLevelEncoder::Initialize(); } -void BC_Library_Destory() { + +void BC_Library_Destroy() { CBC_QRCoderErrorCorrectionLevel::Finalize(); CBC_QRCoderMode::Finalize(); CBC_QRCoderVersion::Finalize(); diff --git a/fxbarcode/BC_Library.h b/fxbarcode/BC_Library.h index 7be4646c68..4e98743d91 100644 --- a/fxbarcode/BC_Library.h +++ b/fxbarcode/BC_Library.h @@ -33,6 +33,6 @@ enum BC_TYPE { }; void BC_Library_Init(); -void BC_Library_Destory(); +void BC_Library_Destroy(); #endif // FXBARCODE_BC_LIBRARY_H_ diff --git a/testing/libfuzzer/BUILD.gn b/testing/libfuzzer/BUILD.gn index d9dc0ee58f..e15ac05cbb 100644 --- a/testing/libfuzzer/BUILD.gn +++ b/testing/libfuzzer/BUILD.gn @@ -39,6 +39,12 @@ template("pdfium_fuzzer") { } if (pdf_enable_xfa) { + pdfium_fuzzer("pdf_cfx_barcode_fuzzer") { + sources = [ + "pdf_cfx_barcode_fuzzer.cc", + ] + } + pdfium_fuzzer("pdf_cfx_saxreader_fuzzer") { sources = [ "pdf_cfx_saxreader_fuzzer.cc", diff --git a/testing/libfuzzer/pdf_cfx_barcode_fuzzer.cc b/testing/libfuzzer/pdf_cfx_barcode_fuzzer.cc new file mode 100644 index 0000000000..24cf0ab516 --- /dev/null +++ b/testing/libfuzzer/pdf_cfx_barcode_fuzzer.cc @@ -0,0 +1,38 @@ +// Copyright 2017 The 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 + +#include "core/fxcrt/fx_string.h" +#include "xfa/fwl/cfx_barcode.h" + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { + if (size < 2 * sizeof(wchar_t)) + return 0; + + BC_TYPE type = static_cast(data[0] % (BC_DATAMATRIX + 1)); + + // Only used one byte, but align with wchar_t for string below. + data += sizeof(wchar_t); + size -= sizeof(wchar_t); + + CFX_Barcode barcode; + if (!barcode.Create(type)) + return 0; + + // TODO(tsepez): Setup more options from |data|. + barcode.SetModuleHeight(300); + barcode.SetModuleWidth(420); + barcode.SetHeight(298); + barcode.SetWidth(418); + + CFX_WideStringC content(reinterpret_cast(data), + size / sizeof(wchar_t)); + + if (!barcode.Encode(content, false)) + return 0; + + // TODO(tsepez): Output to device. + return 0; +} diff --git a/testing/mock_ifx_renderdevicedriver.h b/testing/mock_ifx_renderdevicedriver.h new file mode 100644 index 0000000000..c9061d88b8 --- /dev/null +++ b/testing/mock_ifx_renderdevicedriver.h @@ -0,0 +1,59 @@ +// 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. + +#ifndef TESTING_MOCK_IFX_RENDERDEVICEDRIVER_H_ +#define TESTING_MOCK_IFX_RENDERDEVICEDRIVER_H_ + +#include + +#include "core/fxge/ifx_renderdevicedriver.h" +#include "testing/gmock/include/gmock/gmock.h" + +class MockIFXRenderDeviceDriver : public IFX_RenderDeviceDriver { + public: + MOCK_CONST_METHOD1(GetDeviceCaps, int(int caps_id)); + MOCK_METHOD0(SaveState, void()); + MOCK_METHOD1(RestoreState, void(bool bKeepSaved)); + MOCK_METHOD3(SetClip_PathFill, + bool(const CFX_PathData* pPathData, + const CFX_Matrix* pObject2Device, + int fill_mode)); + MOCK_METHOD7(DrawPath, + bool(const CFX_PathData* pPathData, + const CFX_Matrix* pObject2Device, + const CFX_GraphStateData* pGraphState, + uint32_t fill_color, + uint32_t stroke_color, + int fill_mode, + int blend_type)); + MOCK_METHOD1(GetClipBox, bool(FX_RECT* pRect)); + MOCK_METHOD6(SetDIBits, + bool(const CFX_RetainPtr& pBitmap, + uint32_t color, + const FX_RECT* pSrcRect, + int dest_left, + int dest_top, + int blend_type)); + + MOCK_METHOD7(StartDIBits, + bool(const CFX_RetainPtr& pBitmap, + int bitmap_alpha, + uint32_t color, + const CFX_Matrix* pMatrix, + uint32_t flags, + std::unique_ptr* handle, + int blend_type)); + MOCK_METHOD9(StretchDIBits, + bool(const CFX_RetainPtr& pBitmap, + uint32_t color, + int dest_left, + int dest_top, + int dest_width, + int dest_height, + const FX_RECT* pClipRect, + uint32_t flags, + int blend_type)); +}; + +#endif // TESTING_MOCK_IFX_RENDERDEVICEDRIVER_H_ diff --git a/xfa/fwl/cfx_barcode_unittest.cpp b/xfa/fwl/cfx_barcode_unittest.cpp new file mode 100644 index 0000000000..07d74eef81 --- /dev/null +++ b/xfa/fwl/cfx_barcode_unittest.cpp @@ -0,0 +1,123 @@ +// 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 "xfa/fwl/cfx_barcode.h" + +#include +#include + +#include "core/fxcrt/fx_coordinates.h" +#include "core/fxcrt/fx_string.h" +#include "core/fxge/cfx_renderdevice.h" +#include "testing/gtest/include/gtest/gtest.h" +#include "testing/mock_ifx_renderdevicedriver.h" +#include "testing/test_support.h" +#include "third_party/base/ptr_util.h" + +using testing::_; +using testing::AtLeast; + +class BarcodeTest : public testing::Test { + public: + void SetUp() override { + BC_Library_Init(); + barcode_ = pdfium::MakeUnique(); + device_ = pdfium::MakeUnique(); + driver_ = pdfium::MakeUnique(); + } + + void TearDown() override { + driver_.reset(); + device_.reset(); + barcode_.reset(); + BC_Library_Destroy(); + } + + CFX_Barcode* barcode() const { return barcode_.get(); } + CFX_RenderDevice* device() const { return device_.get(); } + MockIFXRenderDeviceDriver* driver() const { return driver_.get(); } + + bool Create(BC_TYPE type) { + if (!barcode_->Create(type)) + return false; + + barcode_->SetModuleHeight(300); + barcode_->SetModuleWidth(420); + barcode_->SetHeight(298); + barcode_->SetWidth(418); + return true; + } + + void HandoffDriverToDevice() { device_->SetDeviceDriver(std::move(driver_)); } + bool RenderDevice() { + return barcode_->RenderDevice(device_.get(), &matrix_); + } + + protected: + CFX_Matrix matrix_; + std::unique_ptr barcode_; + std::unique_ptr device_; + std::unique_ptr driver_; +}; + +TEST_F(BarcodeTest, Code39) { + EXPECT_TRUE(Create(BC_CODE39)); + EXPECT_TRUE(barcode()->Encode(L"clams", false)); + + EXPECT_CALL(*driver(), GetDeviceCaps(_)).Times(AtLeast(1)); + EXPECT_CALL(*driver(), GetClipBox(_)).Times(AtLeast(1)); + EXPECT_CALL(*driver(), DrawPath(_, _, _, _, _, _, _)).Times(AtLeast(1)); + HandoffDriverToDevice(); + RenderDevice(); +} + +TEST_F(BarcodeTest, CodaBar) { + EXPECT_TRUE(Create(BC_CODABAR)); + EXPECT_TRUE(barcode()->Encode(L"clams", false)); +} + +TEST_F(BarcodeTest, Code128) { + EXPECT_TRUE(Create(BC_CODE128)); + EXPECT_TRUE(barcode()->Encode(L"clams", false)); +} + +TEST_F(BarcodeTest, Code128_B) { + EXPECT_TRUE(Create(BC_CODE128_B)); + EXPECT_TRUE(barcode()->Encode(L"clams", false)); +} + +TEST_F(BarcodeTest, Code128_C) { + EXPECT_TRUE(Create(BC_CODE128_C)); + EXPECT_TRUE(barcode()->Encode(L"clams", false)); +} + +TEST_F(BarcodeTest, Ean8) { + EXPECT_TRUE(Create(BC_EAN8)); + EXPECT_TRUE(barcode()->Encode(L"clams", false)); +} + +TEST_F(BarcodeTest, UPCA) { + EXPECT_TRUE(Create(BC_UPCA)); + EXPECT_TRUE(barcode()->Encode(L"clams", false)); +} + +TEST_F(BarcodeTest, Ean13) { + EXPECT_TRUE(Create(BC_EAN13)); + EXPECT_TRUE(barcode()->Encode(L"clams", false)); +} + +TEST_F(BarcodeTest, Pdf417) { + EXPECT_TRUE(Create(BC_PDF417)); + EXPECT_TRUE(barcode()->Encode(L"clams", false)); +} + +TEST_F(BarcodeTest, DataMatrix) { + EXPECT_TRUE(Create(BC_DATAMATRIX)); + EXPECT_TRUE(barcode()->Encode(L"clams", false)); +} + +TEST_F(BarcodeTest, QrCode) { + EXPECT_TRUE(Create(BC_QR_CODE)); + EXPECT_TRUE(barcode()->Encode(L"clams", false)); +} -- cgit v1.2.3