You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

deflate_test.c 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * Copyright (C) 2014 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. /** @file
  25. *
  26. * DEFLATE tests
  27. *
  28. */
  29. /* Forcibly enable assertions */
  30. #undef NDEBUG
  31. #include <stdint.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include <ipxe/deflate.h>
  35. #include <ipxe/test.h>
  36. /** A DEFLATE test */
  37. struct deflate_test {
  38. /** Compression format */
  39. enum deflate_format format;
  40. /** Compressed data */
  41. const void *compressed;
  42. /** Length of compressed data */
  43. size_t compressed_len;
  44. /** Expected uncompressed data */
  45. const void *expected;
  46. /** Length of expected uncompressed data */
  47. size_t expected_len;
  48. };
  49. /** A DEFLATE fragment list */
  50. struct deflate_test_fragments {
  51. /** Fragment lengths */
  52. size_t len[8];
  53. };
  54. /** Define inline data */
  55. #define DATA(...) { __VA_ARGS__ }
  56. /** Define a DEFLATE test */
  57. #define DEFLATE( name, FORMAT, COMPRESSED, EXPECTED ) \
  58. static const uint8_t name ## _compressed[] = COMPRESSED; \
  59. static const uint8_t name ## _expected[] = EXPECTED; \
  60. static struct deflate_test name = { \
  61. .format = FORMAT, \
  62. .compressed = name ## _compressed, \
  63. .compressed_len = sizeof ( name ## _compressed ), \
  64. .expected = name ## _expected, \
  65. .expected_len = sizeof ( name ## _expected ), \
  66. };
  67. /* Empty file, no compression */
  68. DEFLATE ( empty_literal, DEFLATE_RAW,
  69. DATA ( 0x01, 0x00, 0x00, 0xff, 0xff ), DATA() );
  70. /* "iPXE" string, no compression */
  71. DEFLATE ( literal, DEFLATE_RAW,
  72. DATA ( 0x01, 0x04, 0x00, 0xfb, 0xff, 0x69, 0x50, 0x58, 0x45 ),
  73. DATA ( 0x69, 0x50, 0x58, 0x45 ) );
  74. /* "iPXE" string, no compression, split into two literals */
  75. DEFLATE ( split_literal, DEFLATE_RAW,
  76. DATA ( 0x00, 0x02, 0x00, 0xfd, 0xff, 0x69, 0x50, 0x01, 0x02, 0x00,
  77. 0xfd, 0xff, 0x58, 0x45 ),
  78. DATA ( 0x69, 0x50, 0x58, 0x45 ) );
  79. /* Empty file */
  80. DEFLATE ( empty, DEFLATE_RAW, DATA ( 0x03, 0x00 ), DATA() );
  81. /* "Hello world" */
  82. DEFLATE ( hello_world, DEFLATE_RAW,
  83. DATA ( 0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0x57, 0x28, 0xcf, 0x2f, 0xca,
  84. 0x49, 0x01, 0x00 ),
  85. DATA ( 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c,
  86. 0x64 ) );
  87. /* "Hello hello world" */
  88. DEFLATE ( hello_hello_world, DEFLATE_RAW,
  89. DATA ( 0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0x57, 0xc8, 0x00, 0x93, 0xe5,
  90. 0xf9, 0x45, 0x39, 0x29, 0x00 ),
  91. DATA ( 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x68, 0x65, 0x6c, 0x6c,
  92. 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64 ) );
  93. /* "This specification defines a lossless compressed data format" */
  94. DEFLATE ( rfc_sentence, DEFLATE_RAW,
  95. DATA ( 0x0d, 0xc6, 0xdb, 0x09, 0x00, 0x21, 0x0c, 0x04, 0xc0, 0x56,
  96. 0xb6, 0x28, 0x1b, 0x08, 0x79, 0x70, 0x01, 0x35, 0xe2, 0xa6,
  97. 0x7f, 0xce, 0xf9, 0x9a, 0xf1, 0x25, 0xc1, 0xe3, 0x9a, 0x91,
  98. 0x2a, 0x9d, 0xb5, 0x61, 0x1e, 0xb9, 0x9d, 0x10, 0xcc, 0x22,
  99. 0xa7, 0x93, 0xd0, 0x5a, 0xe7, 0xbe, 0xb8, 0xc1, 0xa4, 0x05,
  100. 0x51, 0x77, 0x49, 0xff ),
  101. DATA ( 0x54, 0x68, 0x69, 0x73, 0x20, 0x73, 0x70, 0x65, 0x63, 0x69,
  102. 0x66, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x64,
  103. 0x65, 0x66, 0x69, 0x6e, 0x65, 0x73, 0x20, 0x61, 0x20, 0x6c,
  104. 0x6f, 0x73, 0x73, 0x6c, 0x65, 0x73, 0x73, 0x20, 0x63, 0x6f,
  105. 0x6d, 0x70, 0x72, 0x65, 0x73, 0x73, 0x65, 0x64, 0x20, 0x64,
  106. 0x61, 0x74, 0x61, 0x20, 0x66, 0x6f, 0x72, 0x6d, 0x61, 0x74 ) );
  107. /* "ZLIB Compressed Data Format Specification" */
  108. DEFLATE ( zlib, DEFLATE_ZLIB,
  109. DATA ( 0x78, 0x01, 0x8b, 0xf2, 0xf1, 0x74, 0x52, 0x70, 0xce, 0xcf,
  110. 0x2d, 0x28, 0x4a, 0x2d, 0x2e, 0x4e, 0x4d, 0x51, 0x70, 0x49,
  111. 0x2c, 0x49, 0x54, 0x70, 0xcb, 0x2f, 0xca, 0x4d, 0x2c, 0x51,
  112. 0x08, 0x2e, 0x48, 0x4d, 0xce, 0x4c, 0xcb, 0x4c, 0x4e, 0x2c,
  113. 0xc9, 0xcc, 0xcf, 0x03, 0x00, 0x2c, 0x0e, 0x0e, 0xeb ),
  114. DATA ( 0x5a, 0x4c, 0x49, 0x42, 0x20, 0x43, 0x6f, 0x6d, 0x70, 0x72,
  115. 0x65, 0x73, 0x73, 0x65, 0x64, 0x20, 0x44, 0x61, 0x74, 0x61,
  116. 0x20, 0x46, 0x6f, 0x72, 0x6d, 0x61, 0x74, 0x20, 0x53, 0x70,
  117. 0x65, 0x63, 0x69, 0x66, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6f,
  118. 0x6e ) );
  119. /* "ZLIB Compressed Data Format Specification" fragment list */
  120. static struct deflate_test_fragments zlib_fragments[] = {
  121. { { -1UL, } },
  122. { { 0, 1, 5, -1UL, } },
  123. { { 0, 0, 1, 0, 0, 1, -1UL } },
  124. { { 10, 8, 4, 7, 11, -1UL } },
  125. { { 45, -1UL } },
  126. { { 48, -1UL } },
  127. };
  128. /**
  129. * Report DEFLATE test result
  130. *
  131. * @v deflate Decompressor
  132. * @v test Deflate test
  133. * @v frags Fragment list, or NULL
  134. * @v file Test code file
  135. * @v line Test code line
  136. */
  137. static void deflate_okx ( struct deflate *deflate,
  138. struct deflate_test *test,
  139. struct deflate_test_fragments *frags,
  140. const char *file, unsigned int line ) {
  141. uint8_t data[ test->expected_len ];
  142. struct deflate_chunk in;
  143. struct deflate_chunk out;
  144. size_t frag_len = -1UL;
  145. size_t offset = 0;
  146. size_t remaining = test->compressed_len;
  147. unsigned int i;
  148. /* Initialise decompressor */
  149. deflate_init ( deflate, test->format );
  150. /* Initialise output chunk */
  151. deflate_chunk_init ( &out, virt_to_user ( data ), 0, sizeof ( data ) );
  152. /* Process input (in fragments, if applicable) */
  153. for ( i = 0 ; i < ( sizeof ( frags->len ) /
  154. sizeof ( frags->len[0] ) ) ; i++ ) {
  155. /* Initialise input chunk */
  156. if ( frags )
  157. frag_len = frags->len[i];
  158. if ( frag_len > remaining )
  159. frag_len = remaining;
  160. deflate_chunk_init ( &in, virt_to_user ( test->compressed ),
  161. offset, ( offset + frag_len ) );
  162. /* Decompress this fragment */
  163. okx ( deflate_inflate ( deflate, &in, &out ) == 0, file, line );
  164. okx ( in.len == ( offset + frag_len ), file, line );
  165. okx ( in.offset == in.len, file, line );
  166. /* Move to next fragment */
  167. offset = in.offset;
  168. remaining -= frag_len;
  169. if ( ! remaining )
  170. break;
  171. /* Check that decompression has not terminated early */
  172. okx ( ! deflate_finished ( deflate ), file, line );
  173. }
  174. /* Check decompression has terminated as expected */
  175. okx ( deflate_finished ( deflate ), file, line );
  176. okx ( offset == test->compressed_len, file, line );
  177. okx ( out.offset == test->expected_len, file, line );
  178. okx ( memcmp ( data, test->expected, test->expected_len ) == 0,
  179. file, line );
  180. }
  181. #define deflate_ok( deflate, test, frags ) \
  182. deflate_okx ( deflate, test, frags, __FILE__, __LINE__ )
  183. /**
  184. * Perform DEFLATE self-test
  185. *
  186. */
  187. static void deflate_test_exec ( void ) {
  188. struct deflate *deflate;
  189. unsigned int i;
  190. /* Allocate shared structure */
  191. deflate = malloc ( sizeof ( *deflate ) );
  192. ok ( deflate != NULL );
  193. /* Perform self-tests */
  194. if ( deflate ) {
  195. /* Test as a single pass */
  196. deflate_ok ( deflate, &empty_literal, NULL );
  197. deflate_ok ( deflate, &literal, NULL );
  198. deflate_ok ( deflate, &split_literal, NULL );
  199. deflate_ok ( deflate, &empty, NULL );
  200. deflate_ok ( deflate, &hello_world, NULL );
  201. deflate_ok ( deflate, &hello_hello_world, NULL );
  202. deflate_ok ( deflate, &rfc_sentence, NULL );
  203. deflate_ok ( deflate, &zlib, NULL );
  204. /* Test fragmentation */
  205. for ( i = 0 ; i < ( sizeof ( zlib_fragments ) /
  206. sizeof ( zlib_fragments[0] ) ) ; i++ ) {
  207. deflate_ok ( deflate, &zlib, &zlib_fragments[i] );
  208. }
  209. }
  210. /* Free shared structure */
  211. free ( deflate );
  212. }
  213. /** DEFLATE self-test */
  214. struct self_test deflate_test __self_test = {
  215. .name = "deflate",
  216. .exec = deflate_test_exec,
  217. };