check-attr.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * tests/check-attr.c nla_attr unit tests
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation version 2.1
  7. * of the License.
  8. *
  9. * Copyright (c) 2013 Thomas Graf <tgraf@suug.ch>
  10. */
  11. #include "util.h"
  12. #include <netlink/attr.h>
  13. #include <netlink/msg.h>
  14. START_TEST(attr_size)
  15. {
  16. fail_if(nla_attr_size(0) != NLA_HDRLEN,
  17. "Length of empty attribute should match header size");
  18. fail_if(nla_attr_size(1) != NLA_HDRLEN + 1,
  19. "Length of 1 bytes payload should be NLA_HDRLEN + 1");
  20. fail_if(nla_attr_size(2) != NLA_HDRLEN + 2,
  21. "Length of 2 bytes payload should be NLA_HDRLEN + 2");
  22. fail_if(nla_attr_size(3) != NLA_HDRLEN + 3,
  23. "Length of 3 bytes payload should be NLA_HDRLEN + 3");
  24. fail_if(nla_attr_size(4) != NLA_HDRLEN + 4,
  25. "Length of 4 bytes payload should be NLA_HDRLEN + 4");
  26. fail_if(nla_total_size(1) != NLA_HDRLEN + 4,
  27. "Total size of 1 bytes payload should result in 8 bytes");
  28. fail_if(nla_total_size(2) != NLA_HDRLEN + 4,
  29. "Total size of 2 bytes payload should result in 8 bytes");
  30. fail_if(nla_total_size(3) != NLA_HDRLEN + 4,
  31. "Total size of 3 bytes payload should result in 8 bytes");
  32. fail_if(nla_total_size(4) != NLA_HDRLEN + 4,
  33. "Total size of 4 bytes payload should result in 8 bytes");
  34. fail_if(nla_padlen(1) != 3,
  35. "2 bytes of payload should result in 3 padding bytes");
  36. fail_if(nla_padlen(2) != 2,
  37. "2 bytes of payload should result in 2 padding bytes");
  38. fail_if(nla_padlen(3) != 1,
  39. "3 bytes of payload should result in 1 padding bytes");
  40. fail_if(nla_padlen(4) != 0,
  41. "4 bytes of payload should result in 0 padding bytes");
  42. fail_if(nla_padlen(5) != 3,
  43. "5 bytes of payload should result in 3 padding bytes");
  44. }
  45. END_TEST
  46. START_TEST(msg_construct)
  47. {
  48. struct nl_msg *msg;
  49. struct nlmsghdr *nlh;
  50. struct nlattr *a;
  51. int i, rem;
  52. msg = nlmsg_alloc();
  53. fail_if(!msg, "Unable to allocate netlink message");
  54. for (i = 1; i < 256; i++) {
  55. fail_if(nla_put_u32(msg, i, i+1) != 0,
  56. "Unable to add attribute %d", i);
  57. }
  58. nlh = nlmsg_hdr(msg);
  59. i = 1;
  60. nlmsg_for_each_attr(a, nlh, 0, rem) {
  61. fail_if(nla_type(a) != i, "Expected attribute %d", i);
  62. i++;
  63. fail_if(nla_get_u32(a) != i, "Expected attribute value %d", i);
  64. }
  65. nlmsg_free(msg);
  66. }
  67. END_TEST
  68. Suite *make_nl_attr_suite(void)
  69. {
  70. Suite *suite = suite_create("Netlink attributes");
  71. TCase *nl_attr = tcase_create("Core");
  72. tcase_add_test(nl_attr, attr_size);
  73. tcase_add_test(nl_attr, msg_construct);
  74. suite_add_tcase(suite, nl_attr);
  75. return suite;
  76. }