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.

pccrc_test.c 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. /*
  2. * Copyright (C) 2015 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 (at your option) 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. * Peer Content Caching and Retrieval: Content Identification [MS-PCCRC] tests
  27. *
  28. */
  29. /* Forcibly enable assertions */
  30. #undef NDEBUG
  31. #include <stdint.h>
  32. #include <string.h>
  33. #include <assert.h>
  34. #include <ipxe/uaccess.h>
  35. #include <ipxe/pccrc.h>
  36. #include <ipxe/sha256.h>
  37. #include <ipxe/sha512.h>
  38. #include <ipxe/hmac.h>
  39. #include <ipxe/test.h>
  40. /** Define inline raw data */
  41. #define DATA(...) { __VA_ARGS__ }
  42. /**
  43. * Define an inline content range
  44. *
  45. * @v START Start offset
  46. * @v END End offset
  47. * @ret range Content range
  48. */
  49. #define RANGE( START, END ) { .start = START, .end = END }
  50. /**
  51. * Define an inline trimmed content range
  52. *
  53. * @v START Start offset
  54. * @v END End offset
  55. * @ret trim Trimmed content range
  56. */
  57. #define TRIM( START, END ) { .start = START, .end = END }
  58. /** A content information test */
  59. struct peerdist_info_test {
  60. /** Raw content information */
  61. const void *data;
  62. /** Length of raw content information */
  63. size_t len;
  64. /** Expected digest algorithm */
  65. struct digest_algorithm *expected_digest;
  66. /** Expected digest size */
  67. size_t expected_digestsize;
  68. /** Expected content range */
  69. struct peerdist_range expected_range;
  70. /** Expected trimmed content range */
  71. struct peerdist_range expected_trim;
  72. /** Expected number of segments */
  73. unsigned int expected_segments;
  74. };
  75. /**
  76. * Define a content information test
  77. *
  78. * @v name Test name
  79. * @v DATA Raw content information
  80. * @v DIGEST Expected digest algorithm
  81. * @v DIGESTSIZE Expected digest size
  82. * @v RANGE Expected content range
  83. * @v TRIM Expected trimmer content range
  84. * @v SEGMENTS Expected number of segments
  85. * @ret test Content information test
  86. *
  87. * Raw content information can be obtained from PeerDist-capable web
  88. * servers using wget's "--header" option to inject the relevant
  89. * PeerDist headers. For example:
  90. *
  91. * wget --header "Accept-Encoding: peerdist" \
  92. * --header "X-P2P-PeerDist: Version=1.0" \
  93. * http://peerdist.server.address/test.url -O - | xxd -i -c 11
  94. *
  95. * Version 1 content information can be retrieved using the headers:
  96. *
  97. * Accept-Encoding: peerdist
  98. * X-P2P-PeerDist: Version=1.0
  99. *
  100. * Version 2 content information can be retrieved (from compatible
  101. * servers) using the headers:
  102. *
  103. * Accept-Encoding: peerdist
  104. * X-P2P-PeerDist: Version=1.1
  105. * X-P2P-PeerDistEx: MinContentInformation=2.0, MaxContentInformation=2.0
  106. */
  107. #define PEERDIST_INFO_TEST( name, DATA, DIGEST, DIGESTSIZE, RANGE, \
  108. TRIM, SEGMENTS ) \
  109. static const uint8_t name ## _data[] = DATA; \
  110. static struct peerdist_info_test name = { \
  111. .data = name ## _data, \
  112. .len = sizeof ( name ## _data ), \
  113. .expected_digest = DIGEST, \
  114. .expected_digestsize = DIGESTSIZE, \
  115. .expected_range = RANGE, \
  116. .expected_trim = TRIM, \
  117. .expected_segments = SEGMENTS, \
  118. }
  119. /** A content information segment test */
  120. struct peerdist_info_segment_test {
  121. /** Segment index */
  122. unsigned int index;
  123. /** Expected content range */
  124. struct peerdist_range expected_range;
  125. /** Expected number of blocks */
  126. unsigned int expected_blocks;
  127. /** Expected block size */
  128. size_t expected_blksize;
  129. /** Expected segment hash of data */
  130. uint8_t expected_hash[PEERDIST_DIGEST_MAX_SIZE];
  131. /** Expected segment secret */
  132. uint8_t expected_secret[PEERDIST_DIGEST_MAX_SIZE];
  133. /** Expected segment identifier */
  134. uint8_t expected_id[PEERDIST_DIGEST_MAX_SIZE];
  135. };
  136. /**
  137. * Define a content information segment test
  138. *
  139. * @v name Test name
  140. * @v INDEX Segment index
  141. * @v RANGE Expected content range
  142. * @v BLOCKS Expected number of blocks
  143. * @v BLKSIZE Expected block size
  144. * @v HASH Expected segment hash of data
  145. * @v SECRET Expected segment secret
  146. * @v ID Expected segment identifier
  147. * @ret test Content information segment test
  148. */
  149. #define PEERDIST_INFO_SEGMENT_TEST( name, INDEX, RANGE, BLOCKS, \
  150. BLKSIZE, HASH, SECRET, ID ) \
  151. static struct peerdist_info_segment_test name = { \
  152. .index = INDEX, \
  153. .expected_range = RANGE, \
  154. .expected_blocks = BLOCKS, \
  155. .expected_blksize = BLKSIZE, \
  156. .expected_hash = HASH, \
  157. .expected_secret = SECRET, \
  158. .expected_id = ID, \
  159. }
  160. /** A content information block test */
  161. struct peerdist_info_block_test {
  162. /** Block index */
  163. unsigned int index;
  164. /** Expected content range */
  165. struct peerdist_range expected_range;
  166. /** Expected trimmed content range */
  167. struct peerdist_range expected_trim;
  168. /** Expected hash of data */
  169. uint8_t expected_hash[PEERDIST_DIGEST_MAX_SIZE];
  170. };
  171. /**
  172. * Define a content information block test
  173. *
  174. * @v name Test name
  175. * @v INDEX Block index
  176. * @v RANGE Expected content range
  177. * @v TRIM Expected trimmed content range
  178. * @v HASH Expected hash of data
  179. * @ret test Content information block test
  180. */
  181. #define PEERDIST_INFO_BLOCK_TEST( name, INDEX, RANGE, TRIM, HASH ) \
  182. static struct peerdist_info_block_test name = { \
  183. .index = INDEX, \
  184. .expected_range = RANGE, \
  185. .expected_trim = TRIM, \
  186. .expected_hash = HASH, \
  187. }
  188. /**
  189. * Define a server passphrase
  190. *
  191. * @v name Server passphrase name
  192. * @v DATA Raw server passphrase
  193. *
  194. * The server passphrase can be exported from a Windows BranchCache
  195. * server using the command:
  196. *
  197. * netsh branchcache exportkey exported.key somepassword
  198. *
  199. * and this encrypted exported key can be decrypted using the
  200. * oSSL_key_dx or mcrypt_key_dx utilities found in the (prototype)
  201. * Prequel project at https://fedorahosted.org/prequel/ :
  202. *
  203. * oSSL_key_dx exported.key somepassword
  204. * or
  205. * mcrypt_key_dx exported.key somepassword
  206. *
  207. * Either command will display both the server passphrase and the
  208. * "Server Secret". Note that this latter is the version 1 server
  209. * secret (i.e. the SHA-256 of the server passphrase); the
  210. * corresponding version 2 server secret can be obtained by
  211. * calculating the truncated SHA-512 of the server passphrase.
  212. *
  213. * We do not know the server passphrase during normal operation. We
  214. * use it in the self-tests only to check for typos and other errors
  215. * in the test vectors, by checking that the segment secret defined in
  216. * a content information segment test is as expected.
  217. */
  218. #define SERVER_PASSPHRASE( name, DATA ) \
  219. static uint8_t name[] = DATA
  220. /** Server passphrase used for these test vectors */
  221. SERVER_PASSPHRASE ( passphrase,
  222. DATA ( 0x2a, 0x3d, 0x73, 0xeb, 0x43, 0x5e, 0x9f, 0x2b, 0x8a, 0x34, 0x42,
  223. 0x67, 0xe7, 0x46, 0x7a, 0x3c, 0x73, 0x85, 0xc6, 0xe0, 0x55, 0xe2,
  224. 0xb4, 0xd3, 0x0d, 0xfe, 0xc7, 0xc3, 0x8b, 0x0e, 0xd7, 0x2c ) );
  225. /** IIS logo (iis-85.png) content information version 1 */
  226. PEERDIST_INFO_TEST ( iis_85_png_v1,
  227. DATA ( 0x00, 0x01, 0x0c, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  228. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  229. 0x00, 0x00, 0x00, 0x00, 0x7e, 0x85, 0x01, 0x00, 0x00, 0x00, 0x01,
  230. 0x00, 0xd8, 0xd9, 0x76, 0x35, 0x4a, 0x48, 0x72, 0xe9, 0x25, 0x76,
  231. 0x18, 0x03, 0xf4, 0x58, 0xd9, 0xda, 0xaa, 0x67, 0xf8, 0xe3, 0x1c,
  232. 0x63, 0x0f, 0xb7, 0x4e, 0x6a, 0x31, 0x2e, 0xf8, 0xa2, 0x5a, 0xba,
  233. 0x11, 0xaf, 0xc0, 0xd7, 0x94, 0x92, 0x43, 0xf9, 0x4f, 0x9c, 0x1f,
  234. 0xab, 0x35, 0xd9, 0xfd, 0x1e, 0x33, 0x1f, 0xcf, 0x78, 0x11, 0xa2,
  235. 0xe0, 0x1d, 0x35, 0x87, 0xb3, 0x8d, 0x77, 0x0a, 0x29, 0xe2, 0x02,
  236. 0x00, 0x00, 0x00, 0x73, 0xc1, 0x8a, 0xb8, 0x54, 0x91, 0x10, 0xf8,
  237. 0xe9, 0x0e, 0x71, 0xbb, 0xc3, 0xab, 0x2a, 0xa8, 0xc4, 0x4d, 0x13,
  238. 0xf4, 0x92, 0x94, 0x99, 0x25, 0x5b, 0x66, 0x0f, 0x24, 0xec, 0x77,
  239. 0x80, 0x0b, 0x97, 0x4b, 0xdd, 0x65, 0x56, 0x7f, 0xde, 0xec, 0xcd,
  240. 0xaf, 0xe4, 0x57, 0xa9, 0x50, 0x3b, 0x45, 0x48, 0xf6, 0x6e, 0xd3,
  241. 0xb1, 0x88, 0xdc, 0xfd, 0xa0, 0xac, 0x38, 0x2b, 0x09, 0x71, 0x1a,
  242. 0xcc ),
  243. &sha256_algorithm, 32, RANGE ( 0, 99710 ), TRIM ( 0, 99710 ), 1 );
  244. /** IIS logo (iis-85.png) content information version 1 segment 0 */
  245. PEERDIST_INFO_SEGMENT_TEST ( iis_85_png_v1_s0, 0,
  246. RANGE ( 0, 99710 ), 2, 65536,
  247. DATA ( 0xd8, 0xd9, 0x76, 0x35, 0x4a, 0x48, 0x72, 0xe9, 0x25, 0x76, 0x18,
  248. 0x03, 0xf4, 0x58, 0xd9, 0xda, 0xaa, 0x67, 0xf8, 0xe3, 0x1c, 0x63,
  249. 0x0f, 0xb7, 0x4e, 0x6a, 0x31, 0x2e, 0xf8, 0xa2, 0x5a, 0xba ),
  250. DATA ( 0x11, 0xaf, 0xc0, 0xd7, 0x94, 0x92, 0x43, 0xf9, 0x4f, 0x9c, 0x1f,
  251. 0xab, 0x35, 0xd9, 0xfd, 0x1e, 0x33, 0x1f, 0xcf, 0x78, 0x11, 0xa2,
  252. 0xe0, 0x1d, 0x35, 0x87, 0xb3, 0x8d, 0x77, 0x0a, 0x29, 0xe2 ),
  253. DATA ( 0x49, 0x1b, 0x21, 0x7d, 0xbe, 0xe2, 0xb5, 0xf1, 0x2c, 0xa7, 0x9b,
  254. 0x01, 0x5e, 0x06, 0xf4, 0xbb, 0xe6, 0x4f, 0x97, 0x45, 0xba, 0xd7,
  255. 0x86, 0x7a, 0xef, 0x17, 0xde, 0x59, 0x92, 0x7e, 0xdc, 0xe9 ) );
  256. /** IIS logo (iis-85.png) content information version 1 segment 0 block 0 */
  257. PEERDIST_INFO_BLOCK_TEST ( iis_85_png_v1_s0_b0, 0,
  258. RANGE ( 0, 65536 ),
  259. TRIM ( 0, 65536 ),
  260. DATA ( 0x73, 0xc1, 0x8a, 0xb8, 0x54, 0x91, 0x10, 0xf8, 0xe9, 0x0e, 0x71,
  261. 0xbb, 0xc3, 0xab, 0x2a, 0xa8, 0xc4, 0x4d, 0x13, 0xf4, 0x92, 0x94,
  262. 0x99, 0x25, 0x5b, 0x66, 0x0f, 0x24, 0xec, 0x77, 0x80, 0x0b ) );
  263. /** IIS logo (iis-85.png) content information version 1 segment 0 block 1 */
  264. PEERDIST_INFO_BLOCK_TEST ( iis_85_png_v1_s0_b1, 1,
  265. RANGE ( 65536, 99710 ),
  266. TRIM ( 65536, 99710 ),
  267. DATA ( 0x97, 0x4b, 0xdd, 0x65, 0x56, 0x7f, 0xde, 0xec, 0xcd, 0xaf, 0xe4,
  268. 0x57, 0xa9, 0x50, 0x3b, 0x45, 0x48, 0xf6, 0x6e, 0xd3, 0xb1, 0x88,
  269. 0xdc, 0xfd, 0xa0, 0xac, 0x38, 0x2b, 0x09, 0x71, 0x1a, 0xcc ) );
  270. /** IIS logo (iis-85.png) content information version 2 */
  271. PEERDIST_INFO_TEST ( iis_85_png_v2,
  272. DATA ( 0x00, 0x02, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  273. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  274. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  275. 0x00, 0x00, 0x88, 0x00, 0x00, 0x99, 0xde, 0xe0, 0xd0, 0xc3, 0x58,
  276. 0xe2, 0x68, 0x4b, 0x62, 0x33, 0x0d, 0x32, 0xb5, 0xf1, 0x97, 0x87,
  277. 0x24, 0xa0, 0xd0, 0xa5, 0x2b, 0xdc, 0x5e, 0x78, 0x1f, 0xae, 0x71,
  278. 0xff, 0x57, 0xa8, 0xbe, 0x3d, 0xd4, 0x58, 0x03, 0x7e, 0xd4, 0x04,
  279. 0x11, 0x6b, 0xb6, 0x16, 0xd9, 0xb1, 0x41, 0x16, 0x08, 0x85, 0x20,
  280. 0xc4, 0x7c, 0xdc, 0x50, 0xab, 0xce, 0xa3, 0xfa, 0xe1, 0x88, 0xa9,
  281. 0x8e, 0xa2, 0x2d, 0xf3, 0xc0, 0x00, 0x00, 0xeb, 0xa0, 0x33, 0x81,
  282. 0xd0, 0xd0, 0xcb, 0x74, 0xf4, 0xb6, 0x13, 0xd8, 0x21, 0x0f, 0x37,
  283. 0xf0, 0x02, 0xa0, 0x6f, 0x39, 0x10, 0x58, 0x60, 0x96, 0xa1, 0x30,
  284. 0xd3, 0x43, 0x98, 0xc0, 0x8e, 0x66, 0xd7, 0xbc, 0xb8, 0xb6, 0xeb,
  285. 0x77, 0x83, 0xe4, 0xf8, 0x07, 0x64, 0x7b, 0x63, 0xf1, 0x46, 0xb5,
  286. 0x2f, 0x4a, 0xc8, 0x9c, 0xcc, 0x7a, 0xbf, 0x5f, 0xa1, 0x1a, 0xca,
  287. 0xfc, 0x2a, 0xcf, 0x50, 0x28, 0x58, 0x6c ),
  288. &sha512_algorithm, 32, RANGE ( 0, 99710 ), TRIM ( 0, 99710 ), 2 );
  289. /** IIS logo (iis-85.png) content information version 2 segment 0 */
  290. PEERDIST_INFO_SEGMENT_TEST ( iis_85_png_v2_s0, 0,
  291. RANGE ( 0, 39390 ), 1, 39390,
  292. DATA ( 0xe0, 0xd0, 0xc3, 0x58, 0xe2, 0x68, 0x4b, 0x62, 0x33, 0x0d, 0x32,
  293. 0xb5, 0xf1, 0x97, 0x87, 0x24, 0xa0, 0xd0, 0xa5, 0x2b, 0xdc, 0x5e,
  294. 0x78, 0x1f, 0xae, 0x71, 0xff, 0x57, 0xa8, 0xbe, 0x3d, 0xd4 ),
  295. DATA ( 0x58, 0x03, 0x7e, 0xd4, 0x04, 0x11, 0x6b, 0xb6, 0x16, 0xd9, 0xb1,
  296. 0x41, 0x16, 0x08, 0x85, 0x20, 0xc4, 0x7c, 0xdc, 0x50, 0xab, 0xce,
  297. 0xa3, 0xfa, 0xe1, 0x88, 0xa9, 0x8e, 0xa2, 0x2d, 0xf3, 0xc0 ),
  298. DATA ( 0x33, 0x71, 0xbb, 0xea, 0xdd, 0xb6, 0x23, 0x53, 0xad, 0xce, 0xf9,
  299. 0x70, 0xa0, 0x6f, 0xdf, 0x65, 0x00, 0x1e, 0x04, 0x21, 0xf4, 0xc7,
  300. 0x10, 0x82, 0x76, 0xb0, 0xc3, 0x7a, 0x9f, 0x9e, 0xc1, 0x0f ) );
  301. /** IIS logo (iis-85.png) content information version 2 segment 0 block 0 */
  302. PEERDIST_INFO_BLOCK_TEST ( iis_85_png_v2_s0_b0, 0,
  303. RANGE ( 0, 39390 ),
  304. TRIM ( 0, 39390 ),
  305. DATA ( 0xe0, 0xd0, 0xc3, 0x58, 0xe2, 0x68, 0x4b, 0x62, 0x33, 0x0d, 0x32,
  306. 0xb5, 0xf1, 0x97, 0x87, 0x24, 0xa0, 0xd0, 0xa5, 0x2b, 0xdc, 0x5e,
  307. 0x78, 0x1f, 0xae, 0x71, 0xff, 0x57, 0xa8, 0xbe, 0x3d, 0xd4 ) );
  308. /** IIS logo (iis-85.png) content information version 2 segment 1 */
  309. PEERDIST_INFO_SEGMENT_TEST ( iis_85_png_v2_s1, 1,
  310. RANGE ( 39390, 99710 ), 1, 60320,
  311. DATA ( 0x33, 0x81, 0xd0, 0xd0, 0xcb, 0x74, 0xf4, 0xb6, 0x13, 0xd8, 0x21,
  312. 0x0f, 0x37, 0xf0, 0x02, 0xa0, 0x6f, 0x39, 0x10, 0x58, 0x60, 0x96,
  313. 0xa1, 0x30, 0xd3, 0x43, 0x98, 0xc0, 0x8e, 0x66, 0xd7, 0xbc ),
  314. DATA ( 0xb8, 0xb6, 0xeb, 0x77, 0x83, 0xe4, 0xf8, 0x07, 0x64, 0x7b, 0x63,
  315. 0xf1, 0x46, 0xb5, 0x2f, 0x4a, 0xc8, 0x9c, 0xcc, 0x7a, 0xbf, 0x5f,
  316. 0xa1, 0x1a, 0xca, 0xfc, 0x2a, 0xcf, 0x50, 0x28, 0x58, 0x6c ),
  317. DATA ( 0xd7, 0xe9, 0x24, 0x42, 0x5e, 0x8f, 0x4f, 0x88, 0xf0, 0x1d, 0xc6,
  318. 0xa9, 0xbb, 0x1b, 0xc3, 0x7b, 0xe1, 0x13, 0xec, 0x79, 0x17, 0xc7,
  319. 0x45, 0xd4, 0x96, 0x5c, 0x2b, 0x55, 0xfa, 0x16, 0x3a, 0x6e ) );
  320. /** IIS logo (iis-85.png) content information version 2 segment 1 block 0 */
  321. PEERDIST_INFO_BLOCK_TEST ( iis_85_png_v2_s1_b0, 0,
  322. RANGE ( 39390, 99710 ),
  323. TRIM ( 39390, 99710 ),
  324. DATA ( 0x33, 0x81, 0xd0, 0xd0, 0xcb, 0x74, 0xf4, 0xb6, 0x13, 0xd8, 0x21,
  325. 0x0f, 0x37, 0xf0, 0x02, 0xa0, 0x6f, 0x39, 0x10, 0x58, 0x60, 0x96,
  326. 0xa1, 0x30, 0xd3, 0x43, 0x98, 0xc0, 0x8e, 0x66, 0xd7, 0xbc ) );
  327. /**
  328. * Report content information test result
  329. *
  330. * @v test Content information test
  331. * @v info Content information to fill in
  332. * @v file Test code file
  333. * @v line Test code line
  334. */
  335. static void peerdist_info_okx ( struct peerdist_info_test *test,
  336. struct peerdist_info *info,
  337. const char *file, unsigned int line ) {
  338. /* Parse content information */
  339. okx ( peerdist_info ( virt_to_user ( test->data ), test->len,
  340. info ) == 0, file, line );
  341. /* Verify content information */
  342. okx ( info->raw.data == virt_to_user ( test->data ), file, line );
  343. okx ( info->raw.len == test->len, file, line );
  344. okx ( info->digest == test->expected_digest, file, line );
  345. okx ( info->digestsize == test->expected_digestsize, file, line );
  346. okx ( info->range.start == test->expected_range.start, file, line );
  347. okx ( info->range.end == test->expected_range.end, file, line );
  348. okx ( info->trim.start == test->expected_trim.start, file, line );
  349. okx ( info->trim.end == test->expected_trim.end, file, line );
  350. okx ( info->trim.start >= info->range.start, file, line );
  351. okx ( info->trim.end <= info->range.end, file, line );
  352. okx ( info->segments == test->expected_segments, file, line );
  353. }
  354. #define peerdist_info_ok( test, info ) \
  355. peerdist_info_okx ( test, info, __FILE__, __LINE__ )
  356. /**
  357. * Report content information segment test result
  358. *
  359. * @v test Content information segment test
  360. * @v info Content information
  361. * @v segment Segment information to fill in
  362. * @v file Test code file
  363. * @v line Test code line
  364. */
  365. static void peerdist_info_segment_okx ( struct peerdist_info_segment_test *test,
  366. const struct peerdist_info *info,
  367. struct peerdist_info_segment *segment,
  368. const char *file, unsigned int line ) {
  369. size_t digestsize = info->digestsize;
  370. /* Parse content information segment */
  371. okx ( peerdist_info_segment ( info, segment, test->index ) == 0,
  372. file, line );
  373. /* Verify content information segment */
  374. okx ( segment->info == info, file, line );
  375. okx ( segment->index == test->index, file, line );
  376. okx ( segment->range.start == test->expected_range.start, file, line );
  377. okx ( segment->range.end == test->expected_range.end, file, line );
  378. okx ( segment->blocks == test->expected_blocks, file, line );
  379. okx ( segment->blksize == test->expected_blksize, file, line );
  380. okx ( memcmp ( segment->hash, test->expected_hash,
  381. digestsize ) == 0, file, line );
  382. okx ( memcmp ( segment->secret, test->expected_secret,
  383. digestsize ) == 0, file, line );
  384. okx ( memcmp ( segment->id, test->expected_id,
  385. digestsize ) == 0, file, line );
  386. }
  387. #define peerdist_info_segment_ok( test, info, segment ) \
  388. peerdist_info_segment_okx ( test, info, segment, __FILE__, __LINE__ )
  389. /**
  390. * Report content information block test result
  391. *
  392. * @v test Content information block test
  393. * @v segment Segment information
  394. * @v block Block information to fill in
  395. * @v file Test code file
  396. * @v line Test code line
  397. */
  398. static void
  399. peerdist_info_block_okx ( struct peerdist_info_block_test *test,
  400. const struct peerdist_info_segment *segment,
  401. struct peerdist_info_block *block,
  402. const char *file, unsigned int line ) {
  403. const struct peerdist_info *info = segment->info;
  404. size_t digestsize = info->digestsize;
  405. /* Parse content information block */
  406. okx ( peerdist_info_block ( segment, block, test->index ) == 0,
  407. file, line );
  408. /* Verify content information block */
  409. okx ( block->segment == segment, file, line );
  410. okx ( block->index == test->index, file, line );
  411. okx ( block->range.start == test->expected_range.start, file, line );
  412. okx ( block->range.end == test->expected_range.end, file, line );
  413. okx ( block->trim.start == test->expected_trim.start, file, line );
  414. okx ( block->trim.end == test->expected_trim.end, file, line );
  415. okx ( memcmp ( block->hash, test->expected_hash,
  416. digestsize ) == 0, file, line );
  417. }
  418. #define peerdist_info_block_ok( test, segment, block ) \
  419. peerdist_info_block_okx ( test, segment, block, __FILE__, __LINE__ )
  420. /**
  421. * Report server passphrase test result
  422. *
  423. * @v test Content information segment test
  424. * @v info Content information
  425. * @v pass Server passphrase
  426. * @v pass_len Length of server passphrase
  427. * @v file Test code file
  428. * @v line Test code line
  429. */
  430. static void
  431. peerdist_info_passphrase_okx ( struct peerdist_info_segment_test *test,
  432. const struct peerdist_info *info,
  433. uint8_t *pass, size_t pass_len,
  434. const char *file, unsigned int line ) {
  435. struct digest_algorithm *digest = info->digest;
  436. uint8_t ctx[digest->ctxsize];
  437. uint8_t secret[digest->digestsize];
  438. uint8_t expected[digest->digestsize];
  439. size_t digestsize = info->digestsize;
  440. size_t secretsize = digestsize;
  441. /* Calculate server secret */
  442. digest_init ( digest, ctx );
  443. digest_update ( digest, ctx, pass, pass_len );
  444. digest_final ( digest, ctx, secret );
  445. /* Calculate expected segment secret */
  446. hmac_init ( digest, ctx, secret, &secretsize );
  447. assert ( secretsize == digestsize );
  448. hmac_update ( digest, ctx, test->expected_hash, digestsize );
  449. hmac_final ( digest, ctx, secret, &secretsize, expected );
  450. assert ( secretsize == digestsize );
  451. /* Verify segment secret */
  452. okx ( memcmp ( test->expected_secret, expected, digestsize ) == 0,
  453. file, line );
  454. }
  455. #define peerdist_info_passphrase_ok( test, info, pass, pass_len ) \
  456. peerdist_info_passphrase_okx ( test, info, pass, pass_len, \
  457. __FILE__, __LINE__ )
  458. /**
  459. * Perform content information self-tests
  460. *
  461. */
  462. static void peerdist_info_test_exec ( void ) {
  463. struct peerdist_info info;
  464. struct peerdist_info_segment segment;
  465. struct peerdist_info_block block;
  466. /* IIS logo (iis-85.png) content information version 1 */
  467. peerdist_info_ok ( &iis_85_png_v1, &info );
  468. peerdist_info_passphrase_ok ( &iis_85_png_v1_s0, &info,
  469. passphrase, sizeof ( passphrase ) );
  470. peerdist_info_segment_ok ( &iis_85_png_v1_s0, &info, &segment );
  471. peerdist_info_block_ok ( &iis_85_png_v1_s0_b0, &segment, &block );
  472. peerdist_info_block_ok ( &iis_85_png_v1_s0_b1, &segment, &block );
  473. /* IIS logo (iis-85.png) content information version 2 */
  474. peerdist_info_ok ( &iis_85_png_v2, &info );
  475. peerdist_info_passphrase_ok ( &iis_85_png_v2_s0, &info,
  476. passphrase, sizeof ( passphrase ) );
  477. peerdist_info_segment_ok ( &iis_85_png_v2_s0, &info, &segment );
  478. peerdist_info_block_ok ( &iis_85_png_v2_s0_b0, &segment, &block );
  479. peerdist_info_passphrase_ok ( &iis_85_png_v2_s1, &info,
  480. passphrase, sizeof ( passphrase ) );
  481. peerdist_info_segment_ok ( &iis_85_png_v2_s1, &info, &segment );
  482. peerdist_info_block_ok ( &iis_85_png_v2_s1_b0, &segment, &block );
  483. }
  484. /** Content information self-test */
  485. struct self_test peerdist_info_test __self_test = {
  486. .name = "pccrc",
  487. .exec = peerdist_info_test_exec,
  488. };