Add CBC_DataMatrixWriterTest tests for encoding limits.
Data Matrix barcodes have a maximum size. Test the boundary condition
and see the encoder fail once the input gets to be too big.
BUG=chromium:930394
Change-Id: I76e5e923249b6756d7c1fd29e72ed25117a3dac2
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/51490
Reviewed-by: Lei Zhang <thestig@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/fxbarcode/datamatrix/BC_DataMatrixWriter_unittest.cpp b/fxbarcode/datamatrix/BC_DataMatrixWriter_unittest.cpp
index ef5c00b..fa5d26c 100644
--- a/fxbarcode/datamatrix/BC_DataMatrixWriter_unittest.cpp
+++ b/fxbarcode/datamatrix/BC_DataMatrixWriter_unittest.cpp
@@ -21,8 +21,8 @@
TEST_F(CBC_DataMatrixWriterTest, Encode) {
CBC_DataMatrixWriter writer;
- int32_t width;
- int32_t height;
+ int32_t width = -1;
+ int32_t height = -1;
{
static constexpr int kExpectedDimension = 10;
@@ -134,3 +134,63 @@
ASSERT_TRUE(data.empty());
}
}
+
+TEST_F(CBC_DataMatrixWriterTest, EncodeLimitAlphaNumeric) {
+ CBC_DataMatrixWriter writer;
+ int32_t width = -1;
+ int32_t height = -1;
+
+ static constexpr int kMaxInputLength = 2335; // Per spec.
+ WideString input;
+ for (size_t i = 0; i < kMaxInputLength; ++i)
+ input.InsertAtBack(L'a');
+
+ {
+ static constexpr int kExpectedDimension = 144;
+ std::vector<uint8_t> data = writer.Encode(input.c_str(), &width, &height);
+ EXPECT_EQ(20736u, data.size());
+ EXPECT_EQ(kExpectedDimension, width);
+ EXPECT_EQ(kExpectedDimension, height);
+ }
+
+ // Go over the limit.
+ input.InsertAtBack(L'a');
+ {
+ width = -1;
+ height = -1;
+ std::vector<uint8_t> data = writer.Encode(input.c_str(), &width, &height);
+ EXPECT_EQ(0u, data.size());
+ EXPECT_EQ(-1, width);
+ EXPECT_EQ(-1, height);
+ }
+}
+
+TEST_F(CBC_DataMatrixWriterTest, EncodeLimitNumbers) {
+ CBC_DataMatrixWriter writer;
+ int32_t width = -1;
+ int32_t height = -1;
+
+ static constexpr int kMaxInputLength = 3116; // Per spec.
+ WideString input;
+ for (size_t i = 0; i < kMaxInputLength; ++i)
+ input.InsertAtBack(L'1');
+
+ {
+ static constexpr int kExpectedDimension = 144;
+ std::vector<uint8_t> data = writer.Encode(input.c_str(), &width, &height);
+ EXPECT_EQ(20736u, data.size());
+ EXPECT_EQ(kExpectedDimension, width);
+ EXPECT_EQ(kExpectedDimension, height);
+ }
+
+ // Go over the limit.
+ input.InsertAtBack(L'1');
+ {
+ width = -1;
+ height = -1;
+ std::vector<uint8_t> data = writer.Encode(input.c_str(), &width, &height);
+ EXPECT_EQ(0u, data.size());
+ EXPECT_EQ(-1, width);
+ EXPECT_EQ(-1, height);
+ }
+}