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.

217 lines
10 KiB

  1. From 84849be189c49ef9556bd84d5e5103317372e8a7 Mon Sep 17 00:00:00 2001
  2. From: Karel Slany <karel.slany@nic.cz>
  3. Date: Thu, 11 Aug 2016 11:39:39 +0200
  4. Subject: [PATCH] Added conversion to wire format when writing FNV64 hash
  5. values.
  6. Test on big-endian machines have been failing.
  7. ---
  8. src/libknot/cookies/alg-fnv64.c | 6 +++---
  9. tests/libknot/test_cookies-client.c | 20 ++++++++++----------
  10. tests/libknot/test_cookies-server.c | 20 ++++++++++----------
  11. 3 files changed, 23 insertions(+), 23 deletions(-)
  12. diff --git a/src/libknot/cookies/alg-fnv64.c b/src/libknot/cookies/alg-fnv64.c
  13. index 9989a4f..5e263c1 100644
  14. --- a/src/libknot/cookies/alg-fnv64.c
  15. +++ b/src/libknot/cookies/alg-fnv64.c
  16. @@ -17,10 +17,10 @@
  17. #include <assert.h>
  18. #include <stdint.h>
  19. #include <stdlib.h>
  20. -#include <string.h>
  21. #include "contrib/fnv/fnv.h"
  22. #include "contrib/sockaddr.h"
  23. +#include "contrib/wire.h"
  24. #include "libknot/attribute.h"
  25. #include "libknot/cookies/alg-fnv64.h"
  26. #include "libknot/rrtype/opt-cookie.h"
  27. @@ -80,7 +80,7 @@ static uint16_t cc_gen_fnv64(const struct knot_cc_input *input,
  28. assert(KNOT_OPT_COOKIE_CLNT == sizeof(hash_val));
  29. cc_len = sizeof(hash_val);
  30. - memcpy(cc_out, &hash_val, cc_len);
  31. + wire_write_u64(cc_out, hash_val);
  32. return cc_len;
  33. }
  34. @@ -127,7 +127,7 @@ static uint16_t sc_gen_fnv64(const struct knot_sc_input *input,
  35. assert(SRVR_FNV64_HASH_SIZE == sizeof(hash_val));
  36. hash_len = sizeof(hash_val);
  37. - memcpy(hash_out, &hash_val, hash_len);
  38. + wire_write_u64(hash_out, hash_val);
  39. return hash_len;
  40. }
  41. diff --git a/tests/libknot/test_cookies-client.c b/tests/libknot/test_cookies-client.c
  42. index 44be903..712dbba 100644
  43. --- a/tests/libknot/test_cookies-client.c
  44. +++ b/tests/libknot/test_cookies-client.c
  45. @@ -110,7 +110,7 @@ int main(int argc, char *argv[])
  46. hash_len = knot_cc_alg_fnv64.gen_func(&cc_in, hash, hash_len);
  47. ok(hash_len != 0 && hash_len == knot_cc_alg_fnv64.cc_size, "cookies: FNV64 client cookie output");
  48. {
  49. - uint8_t expected[] = { 0x74, 0x31, 0xf9, 0xa8, 0x03, 0xef, 0x15, 0xb1 };
  50. + uint8_t expected[] = { 0xb1, 0x15, 0xef, 0x03, 0xa8, 0xf9, 0x31, 0x74 };
  51. ok(sizeof(expected) == hash_len && 0 == memcmp(expected, hash, hash_len), "cookies: FNV64 client cookie content");
  52. }
  53. @@ -123,7 +123,7 @@ int main(int argc, char *argv[])
  54. hash_len = knot_cc_alg_fnv64.gen_func(&cc_in, hash, hash_len);
  55. ok(hash_len != 0 && hash_len == knot_cc_alg_fnv64.cc_size, "cookies: FNV64 client cookie output");
  56. {
  57. - uint8_t expected[] = { 0x7c, 0x62, 0x25, 0xd2, 0x43, 0xdd, 0x09, 0xe7 };
  58. + uint8_t expected[] = { 0xe7, 0x09, 0xdd, 0x43, 0xd2, 0x25, 0x62, 0x7c };
  59. ok(sizeof(expected) == hash_len && 0 == memcmp(expected, hash, hash_len), "cookies: FNV64 client cookie content");
  60. }
  61. @@ -136,7 +136,7 @@ int main(int argc, char *argv[])
  62. hash_len = knot_cc_alg_fnv64.gen_func(&cc_in, hash, hash_len);
  63. ok(hash_len != 0 && hash_len == knot_cc_alg_fnv64.cc_size, "cookies: FNV64 client cookie output");
  64. {
  65. - uint8_t expected[] = { 0x7c, 0x62, 0x25, 0xd2, 0x43, 0xdd, 0x09, 0xe7 };
  66. + uint8_t expected[] = { 0xe7, 0x09, 0xdd, 0x43, 0xd2, 0x25, 0x62, 0x7c };
  67. ok(sizeof(expected) == hash_len && 0 == memcmp(expected, hash, hash_len), "cookies: FNV64 client cookie content");
  68. }
  69. @@ -149,7 +149,7 @@ int main(int argc, char *argv[])
  70. hash_len = knot_cc_alg_fnv64.gen_func(&cc_in, hash, hash_len);
  71. ok(hash_len != 0 && hash_len == knot_cc_alg_fnv64.cc_size, "cookies: FNV64 client cookie output");
  72. {
  73. - uint8_t expected[] = { 0x05, 0xa9, 0xd1, 0x08, 0x1b, 0x98, 0xe0, 0xaa };
  74. + uint8_t expected[] = { 0xaa, 0xe0, 0x98, 0x1b, 0x08, 0xd1, 0xa9, 0x05 };
  75. ok(sizeof(expected) == hash_len && 0 == memcmp(expected, hash, hash_len), "cookies: FNV64 client cookie content");
  76. }
  77. @@ -162,7 +162,7 @@ int main(int argc, char *argv[])
  78. hash_len = knot_cc_alg_fnv64.gen_func(&cc_in, hash, hash_len);
  79. ok(hash_len != 0 && hash_len == knot_cc_alg_fnv64.cc_size, "cookies: FNV64 client cookie output");
  80. {
  81. - uint8_t expected[] = { 0x05, 0xa9, 0xd1, 0x08, 0x1b, 0x98, 0xe0, 0xaa };
  82. + uint8_t expected[] = { 0xaa, 0xe0, 0x98, 0x1b, 0x08, 0xd1, 0xa9, 0x05 };
  83. ok(sizeof(expected) == hash_len && 0 == memcmp(expected, hash, hash_len), "cookies: FNV64 client cookie content");
  84. }
  85. @@ -179,7 +179,7 @@ int main(int argc, char *argv[])
  86. }
  87. {
  88. - uint8_t cookie[] = { 0x33, 0x0c, 0xa6, 0x80, 0x94, 0x17, 0xe5, 0xaf };
  89. + uint8_t cookie[] = { 0xaf, 0xe5, 0x17, 0x94, 0x80, 0xa6, 0x0c, 0x33 };
  90. ret = knot_cc_check(cookie, sizeof(cookie), NULL, &knot_cc_alg_fnv64);
  91. ok(ret == KNOT_EINVAL, "cookies: FNV64 client cookie check no input");
  92. }
  93. @@ -190,7 +190,7 @@ int main(int argc, char *argv[])
  94. cc_in.secret_data = secret;
  95. cc_in.secret_len = sizeof(secret);
  96. {
  97. - uint8_t cookie[] = { 0x33, 0x0c, 0xa6, 0x80, 0x94, 0x17, 0xe5, 0xaf };
  98. + uint8_t cookie[] = { 0xaf, 0xe5, 0x17, 0x94, 0x80, 0xa6, 0x0c, 0x33 };
  99. ret = knot_cc_check(cookie, sizeof(cookie), &cc_in, NULL);
  100. ok(ret == KNOT_EINVAL, "cookies: FNV64 client cookie check no algorithm");
  101. }
  102. @@ -201,7 +201,7 @@ int main(int argc, char *argv[])
  103. cc_in.secret_data = secret;
  104. cc_in.secret_len = sizeof(secret);
  105. {
  106. - uint8_t cookie[] = { 0x33, 0x0c, 0xa6, 0x80, 0x94, 0x17, 0xe5, 0xaf };
  107. + uint8_t cookie[] = { 0xaf, 0xe5, 0x17, 0x94, 0x80, 0xa6, 0x0c, 0x33 };
  108. ret = knot_cc_check(cookie, sizeof(cookie), &cc_in, &knot_cc_alg_fnv64);
  109. ok(ret == KNOT_EOK, "cookies: FNV64 client good cookie check");
  110. }
  111. @@ -212,7 +212,7 @@ int main(int argc, char *argv[])
  112. cc_in.secret_data = secret;
  113. cc_in.secret_len = sizeof(secret);
  114. {
  115. - uint8_t cookie[] = { 0x33, 0x0c, 0xa6, 0x80, 0x94, 0x17, 0xe5, 0xaf };
  116. + uint8_t cookie[] = { 0xaf, 0xe5, 0x17, 0x94, 0x80, 0xa6, 0x0c, 0x33 };
  117. ret = knot_cc_check(cookie, sizeof(cookie) - 1, &cc_in, &knot_cc_alg_fnv64);
  118. ok(ret == KNOT_EINVAL, "cookies: FNV64 client cookie check invalid length");
  119. }
  120. @@ -223,7 +223,7 @@ int main(int argc, char *argv[])
  121. cc_in.secret_data = secret;
  122. cc_in.secret_len = sizeof(secret);
  123. {
  124. - uint8_t cookie[] = { 0x33, 0x0c, 0xa6, 0x80, 0x94, 0x17, 0xe5, 0xae };
  125. + uint8_t cookie[] = { 0xaf, 0xe5, 0x17, 0x94, 0x80, 0xa6, 0x0c, 0x32 };
  126. ret = knot_cc_check(cookie, sizeof(cookie), &cc_in, &knot_cc_alg_fnv64);
  127. ok(ret == KNOT_EINVAL, "cookies: FNV64 client cookie check invalid cookie");
  128. }
  129. diff --git a/tests/libknot/test_cookies-server.c b/tests/libknot/test_cookies-server.c
  130. index 32001c1..66a54ba 100644
  131. --- a/tests/libknot/test_cookies-server.c
  132. +++ b/tests/libknot/test_cookies-server.c
  133. @@ -28,10 +28,10 @@
  134. #include "libknot/rrtype/opt-cookie.h"
  135. const char *cookie_opts[] = {
  136. - "\x00\x0a" "\x00\x10" "\x00\x01\x02\x03\x04\x05\x06\x07" "\xe0\xd9\x95\x4e\xbc\xc3\x99\x18", /* 8 octets long wrong server cookie. */
  137. - "\x00\x0a" "\x00\x10" "\x00\x01\x02\x03\x04\x05\x06\x07" "\xe0\xd9\x95\x4e\xbc\xc3\x99\x19", /* 8 octets long OK server cookie. */
  138. - "\x00\x0a" "\x00\x18" "\x00\x01\x02\x03\x04\x05\x06\x07" "\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\xc0\xbd\xdb\xec\x19\x78\x88\x38", /* 8B nonce 8B hash long wrong server cookie. */
  139. - "\x00\x0a" "\x00\x18" "\x00\x01\x02\x03\x04\x05\x06\x07" "\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\xc0\xbd\xdb\xec\x19\x78\x88\x39" /* 8B nonce 8B hash long OK server cookie. */
  140. + "\x00\x0a" "\x00\x10" "\x00\x01\x02\x03\x04\x05\x06\x07" "\x19\x99\xc3\xbc\x4e\x95\xd9\xdf", /* 8 octets long wrong server cookie. */
  141. + "\x00\x0a" "\x00\x10" "\x00\x01\x02\x03\x04\x05\x06\x07" "\x19\x99\xc3\xbc\x4e\x95\xd9\xe0", /* 8 octets long OK server cookie. */
  142. + "\x00\x0a" "\x00\x18" "\x00\x01\x02\x03\x04\x05\x06\x07" "\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x39\x88\x78\x19\xec\xdb\xbd\xbf", /* 8B nonce 8B hash long wrong server cookie. */
  143. + "\x00\x0a" "\x00\x18" "\x00\x01\x02\x03\x04\x05\x06\x07" "\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x39\x88\x78\x19\xec\xdb\xbd\xc0"/* 8B nonce 8B hash long OK server cookie. */
  144. };
  145. #define ROPT(i) ((const uint8_t *)cookie_opts[(i)])
  146. @@ -193,7 +193,7 @@ int main(int argc, char *argv[])
  147. hash_len = knot_sc_alg_fnv64.hash_func(&sc_in, hash, hash_len);
  148. ok(hash_len != 0, "cookies: FNV64 server cookie output");
  149. {
  150. - uint8_t expected[] = { 0x75, 0x45, 0x7c, 0x9a, 0xe0, 0x13, 0xa8, 0xea };
  151. + uint8_t expected[] = { 0xea, 0xa8, 0x13, 0xe0, 0x9a, 0x7c, 0x45, 0x75 };
  152. ok(sizeof(expected) == hash_len && 0 == memcmp(expected, hash, hash_len), "cookies: FNV64 server cookie content");
  153. }
  154. @@ -210,7 +210,7 @@ int main(int argc, char *argv[])
  155. hash_len = knot_sc_alg_fnv64.hash_func(&sc_in, hash, hash_len);
  156. ok(hash_len != 0, "cookies: FNV64 server cookie output");
  157. {
  158. - uint8_t expected[] = { 0xc0, 0xbd, 0xdb, 0xec, 0x19, 0x78, 0x88, 0x39 };
  159. + uint8_t expected[] = { 0x39, 0x88, 0x78, 0x19, 0xec, 0xdb, 0xbd, 0xc0 };
  160. ok(sizeof(expected) == hash_len && 0 == memcmp(expected, hash, hash_len), "cookies: FNV64 server cookie content");
  161. }
  162. @@ -227,7 +227,7 @@ int main(int argc, char *argv[])
  163. hash_len = knot_sc_alg_fnv64.hash_func(&sc_in, hash, hash_len);
  164. ok(hash_len != 0, "cookies: FNV64 server cookie output");
  165. {
  166. - uint8_t expected[] = { 0xe0, 0xd9, 0x95, 0x4e, 0xbc, 0xc3, 0x99, 0x19 };
  167. + uint8_t expected[] = { 0x19, 0x99, 0xc3, 0xbc, 0x4e, 0x95, 0xd9, 0xe0 };
  168. ok(sizeof(expected) == hash_len && 0 == memcmp(expected, hash, hash_len), "cookies: FNV64 server cookie content");
  169. }
  170. @@ -244,7 +244,7 @@ int main(int argc, char *argv[])
  171. hash_len = knot_sc_alg_fnv64.hash_func(&sc_in, hash, hash_len);
  172. ok(hash_len != 0, "cookies: FNV64 server cookie output");
  173. {
  174. - uint8_t expected[] = { 0x4d, 0xde, 0xfa, 0x22, 0xb9, 0x0a, 0xcc, 0xd8 };
  175. + uint8_t expected[] = { 0xd8, 0xcc, 0x0a, 0xb9, 0x22, 0xfa, 0xde, 0x4d };
  176. ok(sizeof(expected) == hash_len && 0 == memcmp(expected, hash, hash_len), "cookies: FNV64 server cookie content");
  177. }
  178. @@ -261,7 +261,7 @@ int main(int argc, char *argv[])
  179. hash_len = knot_sc_alg_fnv64.hash_func(&sc_in, hash, hash_len);
  180. ok(hash_len != 0, "cookies: FNV64 server cookie output");
  181. {
  182. - uint8_t expected[] = { 0xa0, 0x35, 0xe3, 0xe0, 0x78, 0x7a, 0x91, 0xaf };
  183. + uint8_t expected[] = { 0xaf, 0x91, 0x7a, 0x78, 0xe0, 0xe3, 0x35, 0xa0 };
  184. ok(sizeof(expected) == hash_len && 0 == memcmp(expected, hash, hash_len), "cookies: FNV64 server cookie content");
  185. }
  186. @@ -278,7 +278,7 @@ int main(int argc, char *argv[])
  187. hash_len = knot_sc_alg_fnv64.hash_func(&sc_in, hash, hash_len);
  188. ok(hash_len != 0, "cookies: FNV64 server cookie output");
  189. {
  190. - uint8_t expected[] = { 0x8e, 0xa3, 0xf8, 0x97, 0x84, 0x0a, 0x3d, 0x8b };
  191. + uint8_t expected[] = { 0x8b, 0x3d, 0x0a, 0x84, 0x97, 0xf8, 0xa3, 0x8e };
  192. ok(sizeof(expected) == hash_len && 0 == memcmp(expected, hash, hash_len), "cookies: FNV64 server cookie content");
  193. }
  194. --
  195. 2.7.4