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.7KB

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