1 /*
2     libzint - the open source barcode library
3     Copyright (C) 2020 - 2021 Robin Stuart <rstuart114@gmail.com>
4 
5     Redistribution and use in source and binary forms, with or without
6     modification, are permitted provided that the following conditions
7     are met:
8 
9     1. Redistributions of source code must retain the above copyright
10        notice, this list of conditions and the following disclaimer.
11     2. Redistributions in binary form must reproduce the above copyright
12        notice, this list of conditions and the following disclaimer in the
13        documentation and/or other materials provided with the distribution.
14     3. Neither the name of the project nor the names of its contributors
15        may be used to endorse or promote products derived from this software
16        without specific prior written permission.
17 
18     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19     ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21     ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
22     FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23     DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24     OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25     HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26     LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27     OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28     SUCH DAMAGE.
29  */
30 /* vim: set ts=4 sw=4 et : */
31 
32 #include "testcommon.h"
33 
test_print(int index,int generate,int debug)34 static void test_print(int index, int generate, int debug) {
35 
36     struct item {
37         int symbology;
38         int border_width;
39         int output_options;
40         int whitespace_width;
41         int whitespace_height;
42         int option_1;
43         int option_2;
44         char *fgcolour;
45         char *bgcolour;
46         float scale;
47         char *data;
48         char *expected_file;
49     };
50     // s/\/\*[ 0-9]*\*\//\=printf("\/*%3d*\/", line(".") - line("'<"))
51     struct item data[] = {
52         /*  0*/ { BARCODE_GRIDMATRIX, -1, -1, -1, -1, -1, -1, "C3C3C3", "", 0.75, "Grid Matrix", "gridmatrix_fg_0.75.pcx" },
53         /*  1*/ { BARCODE_CODABLOCKF, -1, -1, -1, -1, -1, 20, "FFFFFF", "000000", 0, "1234567890123456789012345678901234567890", "codeblockf_reverse.pcx" },
54         /*  2*/ { BARCODE_QRCODE, -1, -1, -1, -1, 2, 1, "", "D2E3F4", 0, "1234567890", "qr_bg.pcx" },
55         /*  3*/ { BARCODE_ULTRA, 1, BARCODE_BOX, 1, 1, -1, -1, "FF0000", "0000FF", 0, "ULTRACODE_123456789!", "ultra_fg_bg_hvwsp1_box1.pcx" },
56     };
57     int data_size = ARRAY_SIZE(data);
58     int i, length, ret;
59     struct zint_symbol *symbol;
60 
61     const char *data_dir = "/backend/tests/data/pcx";
62     char *pcx = "out.pcx";
63     char expected_file[4096];
64     char escaped[1024];
65     int escaped_size = 1024;
66 
67     int have_identify = testUtilHaveIdentify();
68 
69     testStart("test_pcx");
70 
71     if (generate) {
72         char data_dir_path[1024];
73         assert_nonzero(testUtilDataPath(data_dir_path, sizeof(data_dir_path), data_dir, NULL), "testUtilDataPath(%s) == 0\n", data_dir);
74         if (!testUtilDirExists(data_dir_path)) {
75             ret = testUtilMkDir(data_dir_path);
76             assert_zero(ret, "testUtilMkDir(%s) ret %d != 0 (%d: %s)\n", data_dir_path, ret, errno, strerror(errno));
77         }
78     }
79 
80     for (i = 0; i < data_size; i++) {
81 
82         if (index != -1 && i != index) continue;
83 
84         symbol = ZBarcode_Create();
85         assert_nonnull(symbol, "Symbol not created\n");
86 
87         length = testUtilSetSymbol(symbol, data[i].symbology, -1 /*input_mode*/, -1 /*eci*/, data[i].option_1, data[i].option_2, -1, data[i].output_options, data[i].data, -1, debug);
88         if (data[i].border_width != -1) {
89             symbol->border_width = data[i].border_width;
90         }
91         if (data[i].whitespace_width != -1) {
92             symbol->whitespace_width = data[i].whitespace_width;
93         }
94         if (data[i].whitespace_height != -1) {
95             symbol->whitespace_height = data[i].whitespace_height;
96         }
97         if (*data[i].fgcolour) {
98             strcpy(symbol->fgcolour, data[i].fgcolour);
99         }
100         if (*data[i].bgcolour) {
101             strcpy(symbol->bgcolour, data[i].bgcolour);
102         }
103         if (data[i].scale != 0) {
104             symbol->scale = data[i].scale;
105         }
106         symbol->debug |= debug;
107 
108         ret = ZBarcode_Encode(symbol, (unsigned char *) data[i].data, length);
109         assert_zero(ret, "i:%d %s ZBarcode_Encode ret %d != 0 %s\n", i, testUtilBarcodeName(data[i].symbology), ret, symbol->errtxt);
110 
111         strcpy(symbol->outfile, pcx);
112         ret = ZBarcode_Print(symbol, 0);
113         assert_zero(ret, "i:%d %s ZBarcode_Print %s ret %d != 0\n", i, testUtilBarcodeName(data[i].symbology), symbol->outfile, ret);
114 
115         assert_nonzero(testUtilDataPath(expected_file, sizeof(expected_file), data_dir, data[i].expected_file), "i:%d testUtilDataPath == 0\n", i);
116 
117         if (generate) {
118             printf("        /*%3d*/ { %s, %d, %s, %d, %d, %d, %d, \"%s\", \"%s\", \"%s\", \"%s\"},\n",
119                     i, testUtilBarcodeName(data[i].symbology), data[i].border_width, testUtilOutputOptionsName(data[i].output_options),
120                     data[i].whitespace_width, data[i].whitespace_height,
121                     data[i].option_1, data[i].option_2, data[i].fgcolour, data[i].bgcolour,
122                     testUtilEscape(data[i].data, length, escaped, escaped_size), data[i].expected_file);
123             ret = testUtilRename(symbol->outfile, expected_file);
124             assert_zero(ret, "i:%d testUtilRename(%s, %s) ret %d != 0 (%d: %s)\n", i, symbol->outfile, expected_file, ret, errno, strerror(errno));
125             if (have_identify) {
126                 ret = testUtilVerifyIdentify(expected_file, debug);
127                 assert_zero(ret, "i:%d %s identify %s ret %d != 0\n", i, testUtilBarcodeName(data[i].symbology), expected_file, ret);
128             }
129         } else {
130             assert_nonzero(testUtilExists(symbol->outfile), "i:%d testUtilExists(%s) == 0\n", i, symbol->outfile);
131             assert_nonzero(testUtilExists(expected_file), "i:%d testUtilExists(%s) == 0\n", i, expected_file);
132 
133             ret = testUtilCmpBins(symbol->outfile, expected_file);
134             assert_zero(ret, "i:%d %s testUtilCmpBins(%s, %s) %d != 0\n", i, testUtilBarcodeName(data[i].symbology), symbol->outfile, expected_file, ret);
135             assert_zero(remove(symbol->outfile), "i:%d remove(%s) != 0\n", i, symbol->outfile);
136         }
137 
138         ZBarcode_Delete(symbol);
139     }
140 
141     testFinish();
142 }
143 
144 INTERNAL int pcx_pixel_plot(struct zint_symbol *symbol, unsigned char *pixelbuf);
145 
test_outfile(void)146 static void test_outfile(void) {
147     int ret;
148     struct zint_symbol symbol = {0};
149     unsigned char data[] = { "1" };
150 
151     testStart("test_outfile");
152 
153     symbol.symbology = BARCODE_CODE128;
154     symbol.bitmap = data;
155     symbol.bitmap_width = symbol.bitmap_height = 1;
156 
157     strcpy(symbol.outfile, "nosuch_dir/out.pcx");
158 
159     ret = pcx_pixel_plot(&symbol, data);
160     assert_equal(ret, ZINT_ERROR_FILE_ACCESS, "pcx_pixel_plot ret %d != ZINT_ERROR_FILE_ACCESS (%d) (%s)\n", ret, ZINT_ERROR_FILE_ACCESS, symbol.errtxt);
161 
162     symbol.output_options |= BARCODE_STDOUT;
163 
164     ret = pcx_pixel_plot(&symbol, data);
165     printf(" - ignore (PCX to stdout)\n"); fflush(stdout);
166     assert_zero(ret, "pcx_pixel_plot ret %d != 0 (%s)\n", ret, symbol.errtxt);
167 
168     testFinish();
169 }
170 
main(int argc,char * argv[])171 int main(int argc, char *argv[]) {
172 
173     testFunction funcs[] = { /* name, func, has_index, has_generate, has_debug */
174         { "test_print", test_print, 1, 1, 1 },
175         { "test_outfile", test_outfile, 0, 0, 0 },
176     };
177 
178     testRun(argc, argv, funcs, ARRAY_SIZE(funcs));
179 
180     testReport();
181 
182     return 0;
183 }
184