Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

pccrc_test.c 20KB

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