summaryrefslogtreecommitdiff
path: root/core/fxcodec/gif/cfx_lzwdecompressor_unittest.cpp
blob: cc3ce6367d063aa3b7994ed382e71a328f657e7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// 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/gif/cfx_lzwdecompressor.h"

#include "core/fxcrt/fx_memory.h"
#include "testing/gtest/include/gtest/gtest.h"

TEST(CFX_LZWDecompressor, CreateBadParams) {
  EXPECT_EQ(nullptr, CFX_LZWDecompressor::Create(0x10, 0x2));
  EXPECT_EQ(nullptr, CFX_LZWDecompressor::Create(0x4, 0x0F));
}

TEST(CFX_LZWDecompressor, DecodeBadParams) {
  uint8_t palette_exp = 0x0;
  uint8_t code_exp = 0x2;

  auto decoder = CFX_LZWDecompressor::Create(palette_exp, code_exp);
  ASSERT_NE(nullptr, decoder);

  uint8_t image_data[10];
  uint32_t image_size = FX_ArraySize(image_data);

  uint8_t output_data[10];
  uint32_t output_size = FX_ArraySize(output_data);

  EXPECT_EQ(CFX_GifDecodeStatus::Error,
            decoder->Decode(nullptr, image_size, output_data, &output_size));
  EXPECT_EQ(CFX_GifDecodeStatus::Error,
            decoder->Decode(image_data, 0, output_data, &output_size));
  EXPECT_EQ(CFX_GifDecodeStatus::Error,
            decoder->Decode(image_data, image_size, nullptr, &output_size));
  EXPECT_EQ(CFX_GifDecodeStatus::Error,
            decoder->Decode(image_data, image_size, output_data, nullptr));

  output_size = 0;
  EXPECT_EQ(CFX_GifDecodeStatus::InsufficientDestSize,
            decoder->Decode(image_data, image_size, output_data, &output_size));
}

TEST(CFX_LZWDecompressor, Decode1x1SingleColour) {
  uint8_t palette_exp = 0x0;
  uint8_t code_exp = 0x2;

  auto decoder = CFX_LZWDecompressor::Create(palette_exp, code_exp);
  ASSERT_NE(nullptr, decoder);

  uint8_t image_data[] = {0x44, 0x01};
  uint32_t image_size = FX_ArraySize(image_data);

  uint8_t output_data[1];
  uint32_t output_size = FX_ArraySize(output_data);

  EXPECT_EQ(CFX_GifDecodeStatus::Success,
            decoder->Decode(image_data, image_size, output_data, &output_size));
  uint8_t expected_data[] = {0x00};

  EXPECT_EQ(FX_ArraySize(output_data), output_size);
  EXPECT_TRUE(0 == memcmp(expected_data, output_data, sizeof(expected_data)));
}

TEST(CFX_LZWDecompressor, Decode10x10SingleColour) {
  uint8_t palette_exp = 0x0;
  uint8_t code_exp = 0x2;

  auto decoder = CFX_LZWDecompressor::Create(palette_exp, code_exp);
  ASSERT_NE(nullptr, decoder);

  uint8_t image_data[] = {0x84, 0x8F, 0xA9, 0xCB, 0xED, 0x0F, 0x63, 0x2B};
  uint32_t image_size = FX_ArraySize(image_data);

  uint8_t output_data[100];
  memset(output_data, 0, sizeof(output_data));
  uint32_t output_size = FX_ArraySize(output_data);

  EXPECT_EQ(CFX_GifDecodeStatus::Success,
            decoder->Decode(image_data, image_size, output_data, &output_size));
  uint8_t expected_data[] = {
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00};

  EXPECT_EQ(FX_ArraySize(output_data), output_size);
  EXPECT_TRUE(0 == memcmp(expected_data, output_data, sizeof(expected_data)));
}

TEST(CFX_LZWDecompressor, Decode10x10MultipleColour) {
  uint8_t palette_exp = 0x1;
  uint8_t code_exp = 0x2;

  auto decoder = CFX_LZWDecompressor::Create(palette_exp, code_exp);
  ASSERT_NE(nullptr, decoder);

  uint8_t image_data[] = {0x8C, 0x2D, 0x99, 0x87, 0x2A, 0x1C, 0xDC, 0x33,
                          0xA0, 0x02, 0x75, 0xEC, 0x95, 0xFA, 0xA8, 0xDE,
                          0x60, 0x8C, 0x04, 0x91, 0x4C, 0x01};
  uint32_t image_size = FX_ArraySize(image_data);

  uint8_t output_data[100];
  memset(output_data, 0, sizeof(output_data));
  uint32_t output_size = FX_ArraySize(output_data);

  EXPECT_EQ(CFX_GifDecodeStatus::Success,
            decoder->Decode(image_data, image_size, output_data, &output_size));
  uint8_t expected_data[] = {
      0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02, 0x02, 0x01, 0x01,
      0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02, 0x02, 0x01, 0x01, 0x01, 0x01,
      0x01, 0x02, 0x02, 0x02, 0x02, 0x02, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00,
      0x00, 0x02, 0x02, 0x02, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x02,
      0x02, 0x02, 0x02, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01,
      0x02, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x02, 0x02,
      0x02, 0x02, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02,
      0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02, 0x02, 0x01,
      0x01, 0x01, 0x01, 0x01};

  EXPECT_EQ(FX_ArraySize(output_data), output_size);
  EXPECT_TRUE(0 == memcmp(expected_data, output_data, sizeof(expected_data)));
}

TEST(CFX_LZWDecompressor, HandleColourCodeOutOfPalette) {
  uint8_t palette_exp = 0x2;  // Image uses 10 colours, so the palette exp
                              // should be 3, 2^(3+1) = 16 colours.
  uint8_t code_exp = 0x4;

  auto decoder = CFX_LZWDecompressor::Create(palette_exp, code_exp);
  ASSERT_NE(nullptr, decoder);

  uint8_t image_data[] = {0x30, 0xC9, 0x49, 0x81, 0xBD, 0x78, 0xE8, 0xCD,
                          0x89, 0xFF, 0x60, 0x20, 0x8E, 0xE4, 0x61, 0x9E,
                          0xA8, 0xA1, 0xAE, 0x2C, 0xE2, 0xBE, 0xB0, 0x20,
                          0xCF, 0x74, 0x61, 0xDF, 0x78, 0x04};
  uint32_t image_size = FX_ArraySize(image_data);

  uint8_t output_data[100];
  memset(output_data, 0, sizeof(output_data));
  uint32_t output_size = FX_ArraySize(output_data);

  EXPECT_EQ(CFX_GifDecodeStatus::Error,
            decoder->Decode(image_data, image_size, output_data, &output_size));
}