test_pcx.c 10.0 KB

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