From d9e0e6e46d1f24231b8ab3def4cc197554e96fe7 Mon Sep 17 00:00:00 2001 From: Lei Zhang Date: Fri, 28 Apr 2017 16:54:10 -0700 Subject: Change BarcodeTest to render to bitmaps. BarcodeTest renders to bitmaps verifies their checksums. Add some commonly used checksumming code to testing/test_support.h, and use it in tests that have duplicate code. Change-Id: I4a440674ff1084685b5d89576d967333da458a8a Reviewed-on: https://pdfium-review.googlesource.com/4618 Reviewed-by: Tom Sepez Commit-Queue: Lei Zhang --- BUILD.gn | 1 - core/fdrm/crypto/fx_crypt_unittest.cpp | 23 +++---------- testing/embedder_test.cpp | 14 +------- testing/mock_ifx_renderdevicedriver.h | 59 ---------------------------------- testing/test_support.cpp | 18 +++++++++++ testing/test_support.h | 4 +++ xfa/fwl/cfx_barcode_unittest.cpp | 54 ++++++++++++++++++++++--------- 7 files changed, 66 insertions(+), 107 deletions(-) delete mode 100644 testing/mock_ifx_renderdevicedriver.h diff --git a/BUILD.gn b/BUILD.gn index c4cc895498..5f74122a5b 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -227,7 +227,6 @@ 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", diff --git a/core/fdrm/crypto/fx_crypt_unittest.cpp b/core/fdrm/crypto/fx_crypt_unittest.cpp index 4e3da6e186..f5fb457d3d 100644 --- a/core/fdrm/crypto/fx_crypt_unittest.cpp +++ b/core/fdrm/crypto/fx_crypt_unittest.cpp @@ -11,34 +11,21 @@ #include "core/fxcrt/fx_basic.h" #include "testing/gtest/include/gtest/gtest.h" +#include "testing/test_support.h" namespace { -std::string CRYPT_ToBase16(const uint8_t* digest) { - static char const zEncode[] = "0123456789abcdef"; - std::string ret; - ret.resize(32); - for (int i = 0, j = 0; i < 16; i++, j += 2) { - uint8_t a = digest[i]; - ret[j] = zEncode[(a >> 4) & 0xf]; - ret[j + 1] = zEncode[a & 0xf]; - } - return ret; -} - std::string CRYPT_MD5String(const char* str) { - uint8_t digest[16]; - CRYPT_MD5Generate(reinterpret_cast(str), strlen(str), digest); - return CRYPT_ToBase16(digest); + return GenerateMD5Base16(reinterpret_cast(str), strlen(str)); } } // namespace -TEST(FXCRYPT, CRYPT_ToBase16) { +TEST(FXCRYPT, CryptToBase16) { uint8_t data[] = {0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04, 0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e}; - std::string actual = CRYPT_ToBase16(data); + std::string actual = CryptToBase16(data); std::string expected = "d41d8cd98f00b204e9800998ecf8427e"; EXPECT_EQ(expected, actual); @@ -194,7 +181,7 @@ TEST(FXCRYPT, ContextWithStringData) { uint8_t digest[16]; CRYPT_MD5Finish(&ctx, digest); - std::string actual = CRYPT_ToBase16(digest); + std::string actual = CryptToBase16(digest); std::string expected = "900150983cd24fb0d6963f7d28e17f72"; EXPECT_EQ(expected, actual); } diff --git a/testing/embedder_test.cpp b/testing/embedder_test.cpp index 79074153fb..1baeb57206 100644 --- a/testing/embedder_test.cpp +++ b/testing/embedder_test.cpp @@ -42,18 +42,6 @@ FPDF_BOOL Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) { void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) {} -std::string CRYPT_ToBase16(const uint8_t* digest) { - static char const zEncode[] = "0123456789abcdef"; - std::string ret; - ret.resize(32); - for (int i = 0, j = 0; i < 16; i++, j += 2) { - uint8_t a = digest[i]; - ret[j] = zEncode[(a >> 4) & 0xf]; - ret[j + 1] = zEncode[a & 0xf]; - } - return ret; -} - } // namespace EmbedderTest::EmbedderTest() @@ -352,7 +340,7 @@ void EmbedderTest::CompareBitmap(FPDF_BITMAP bitmap, uint8_t digest[16]; CRYPT_MD5Generate(static_cast(FPDFBitmap_GetBuffer(bitmap)), expected_stride * expected_height, digest); - EXPECT_EQ(expected_md5sum, CRYPT_ToBase16(digest)); + EXPECT_EQ(expected_md5sum, CryptToBase16(digest)); } // Can't use gtest-provided main since we need to stash the path to the diff --git a/testing/mock_ifx_renderdevicedriver.h b/testing/mock_ifx_renderdevicedriver.h deleted file mode 100644 index c9061d88b8..0000000000 --- a/testing/mock_ifx_renderdevicedriver.h +++ /dev/null @@ -1,59 +0,0 @@ -// 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/testing/test_support.cpp b/testing/test_support.cpp index a2e9a6a6dd..608e4ae75b 100644 --- a/testing/test_support.cpp +++ b/testing/test_support.cpp @@ -150,6 +150,24 @@ std::unique_ptr GetFPDFWideString( return result; } +std::string CryptToBase16(const uint8_t* digest) { + static char const zEncode[] = "0123456789abcdef"; + std::string ret; + ret.resize(32); + for (int i = 0, j = 0; i < 16; i++, j += 2) { + uint8_t a = digest[i]; + ret[j] = zEncode[(a >> 4) & 0xf]; + ret[j + 1] = zEncode[a & 0xf]; + } + return ret; +} + +std::string GenerateMD5Base16(const uint8_t* data, uint32_t size) { + uint8_t digest[16]; + CRYPT_MD5Generate(data, size, digest); + return CryptToBase16(digest); +} + #ifdef PDF_ENABLE_V8 #ifdef V8_USE_EXTERNAL_STARTUP_DATA bool InitializeV8ForPDFium(const std::string& exe_path, diff --git a/testing/test_support.h b/testing/test_support.h index 4fe7c295cd..a2d3528e73 100644 --- a/testing/test_support.h +++ b/testing/test_support.h @@ -10,6 +10,7 @@ #include #include +#include "core/fdrm/crypto/fx_crypt.h" #include "public/fpdf_save.h" #include "public/fpdfview.h" #include "testing/gtest/include/gtest/gtest.h" @@ -76,6 +77,9 @@ std::wstring GetPlatformWString(const FPDF_WIDESTRING wstr); std::unique_ptr GetFPDFWideString( const std::wstring& wstr); +std::string CryptToBase16(const uint8_t* digest); +std::string GenerateMD5Base16(const uint8_t* data, uint32_t size); + #ifdef PDF_ENABLE_V8 #ifdef V8_USE_EXTERNAL_STARTUP_DATA bool InitializeV8ForPDFium(const std::string& exe_path, diff --git a/xfa/fwl/cfx_barcode_unittest.cpp b/xfa/fwl/cfx_barcode_unittest.cpp index 07d74eef81..a6b908788d 100644 --- a/xfa/fwl/cfx_barcode_unittest.cpp +++ b/xfa/fwl/cfx_barcode_unittest.cpp @@ -5,38 +5,40 @@ #include "xfa/fwl/cfx_barcode.h" #include +#include #include #include "core/fxcrt/fx_coordinates.h" #include "core/fxcrt/fx_string.h" +#include "core/fxge/cfx_fxgedevice.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(); + + auto device = pdfium::MakeUnique(); + auto bitmap = pdfium::MakeRetain(); + if (bitmap->Create(640, 480, FXDIB_Rgb32)) + bitmap_ = bitmap; + ASSERT_TRUE(bitmap_); + ASSERT_TRUE(device->Attach(bitmap_, false, nullptr, false)); + device_ = std::move(device); } void TearDown() override { - driver_.reset(); + bitmap_.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)) @@ -49,75 +51,95 @@ class BarcodeTest : public testing::Test { return true; } - void HandoffDriverToDevice() { device_->SetDeviceDriver(std::move(driver_)); } bool RenderDevice() { return barcode_->RenderDevice(device_.get(), &matrix_); } + std::string BitmapChecksum() { + return GenerateMD5Base16(bitmap_->GetBuffer(), + bitmap_->GetPitch() * bitmap_->GetHeight()); + } + protected: CFX_Matrix matrix_; std::unique_ptr barcode_; std::unique_ptr device_; - std::unique_ptr driver_; + CFX_RetainPtr bitmap_; }; 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(); + EXPECT_EQ("e0d784db2d4fb5dab7836722a1ad7001", BitmapChecksum()); } TEST_F(BarcodeTest, CodaBar) { EXPECT_TRUE(Create(BC_CODABAR)); EXPECT_TRUE(barcode()->Encode(L"clams", false)); + RenderDevice(); + EXPECT_EQ("26b50593e698a0a9714fd2a60131ef70", BitmapChecksum()); } TEST_F(BarcodeTest, Code128) { EXPECT_TRUE(Create(BC_CODE128)); EXPECT_TRUE(barcode()->Encode(L"clams", false)); + RenderDevice(); + EXPECT_EQ("0beb98f447c632e3085a6b1eae49379f", BitmapChecksum()); } TEST_F(BarcodeTest, Code128_B) { EXPECT_TRUE(Create(BC_CODE128_B)); EXPECT_TRUE(barcode()->Encode(L"clams", false)); + RenderDevice(); + EXPECT_EQ("0beb98f447c632e3085a6b1eae49379f", BitmapChecksum()); } TEST_F(BarcodeTest, Code128_C) { EXPECT_TRUE(Create(BC_CODE128_C)); EXPECT_TRUE(barcode()->Encode(L"clams", false)); + RenderDevice(); + EXPECT_EQ("5d75bfdd601494fc4f6451cce7452922", BitmapChecksum()); } TEST_F(BarcodeTest, Ean8) { EXPECT_TRUE(Create(BC_EAN8)); EXPECT_TRUE(barcode()->Encode(L"clams", false)); + RenderDevice(); + EXPECT_EQ("0a9a7bc34e6d0c82c2950fa592a0039e", BitmapChecksum()); } TEST_F(BarcodeTest, UPCA) { EXPECT_TRUE(Create(BC_UPCA)); EXPECT_TRUE(barcode()->Encode(L"clams", false)); + RenderDevice(); + EXPECT_EQ("d3f993c0fc0131ce11b863a73e27f8e6", BitmapChecksum()); } TEST_F(BarcodeTest, Ean13) { EXPECT_TRUE(Create(BC_EAN13)); EXPECT_TRUE(barcode()->Encode(L"clams", false)); + RenderDevice(); + EXPECT_EQ("461acb9e4f1a284a8e699f9f121a09d3", BitmapChecksum()); } TEST_F(BarcodeTest, Pdf417) { EXPECT_TRUE(Create(BC_PDF417)); EXPECT_TRUE(barcode()->Encode(L"clams", false)); + RenderDevice(); + EXPECT_EQ("2bdb9b39f20c5763da6a0d7c7b1f6933", BitmapChecksum()); } TEST_F(BarcodeTest, DataMatrix) { EXPECT_TRUE(Create(BC_DATAMATRIX)); EXPECT_TRUE(barcode()->Encode(L"clams", false)); + RenderDevice(); + EXPECT_EQ("5e5cd9a680b86fcd4ffd53ed36e3c980", BitmapChecksum()); } TEST_F(BarcodeTest, QrCode) { EXPECT_TRUE(Create(BC_QR_CODE)); EXPECT_TRUE(barcode()->Encode(L"clams", false)); + RenderDevice(); + EXPECT_EQ("4751c6e0f67749fabe24f787128decee", BitmapChecksum()); } -- cgit v1.2.3