Home | History | Annotate | Download | only in tests
      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 (at) suug.ch>
     10  */
     11 
     12 #include "util.h"
     13 #include <netlink/attr.h>
     14 #include <netlink/msg.h>
     15 
     16 START_TEST(attr_size)
     17 {
     18 	fail_if(nla_attr_size(0) != NLA_HDRLEN,
     19 		"Length of empty attribute should match header size");
     20 	fail_if(nla_attr_size(1) != NLA_HDRLEN + 1,
     21 	        "Length of 1 bytes payload should be NLA_HDRLEN + 1");
     22 	fail_if(nla_attr_size(2) != NLA_HDRLEN + 2,
     23 	        "Length of 2 bytes payload should be NLA_HDRLEN + 2");
     24 	fail_if(nla_attr_size(3) != NLA_HDRLEN + 3,
     25 	        "Length of 3 bytes payload should be NLA_HDRLEN + 3");
     26 	fail_if(nla_attr_size(4) != NLA_HDRLEN + 4,
     27 	        "Length of 4 bytes payload should be NLA_HDRLEN + 4");
     28 
     29 	fail_if(nla_total_size(1) != NLA_HDRLEN + 4,
     30 		"Total size of 1 bytes payload should result in 8 bytes");
     31 	fail_if(nla_total_size(2) != NLA_HDRLEN + 4,
     32 		"Total size of 2 bytes payload should result in 8 bytes");
     33 	fail_if(nla_total_size(3) != NLA_HDRLEN + 4,
     34 		"Total size of 3 bytes payload should result in 8 bytes");
     35 	fail_if(nla_total_size(4) != NLA_HDRLEN + 4,
     36 		"Total size of 4 bytes payload should result in 8 bytes");
     37 
     38 	fail_if(nla_padlen(1) != 3,
     39 		"2 bytes of payload should result in 3 padding bytes");
     40 	fail_if(nla_padlen(2) != 2,
     41 		"2 bytes of payload should result in 2 padding bytes");
     42 	fail_if(nla_padlen(3) != 1,
     43 		"3 bytes of payload should result in 1 padding bytes");
     44 	fail_if(nla_padlen(4) != 0,
     45 		"4 bytes of payload should result in 0 padding bytes");
     46 	fail_if(nla_padlen(5) != 3,
     47 		"5 bytes of payload should result in 3 padding bytes");
     48 }
     49 END_TEST
     50 
     51 START_TEST(msg_construct)
     52 {
     53 	struct nl_msg *msg;
     54 	struct nlmsghdr *nlh;
     55 	struct nlattr *a;
     56 	int i, rem;
     57 
     58 	msg = nlmsg_alloc();
     59 	fail_if(!msg, "Unable to allocate netlink message");
     60 
     61 	for (i = 1; i < 256; i++) {
     62 		fail_if(nla_put_u32(msg, i, i+1) != 0,
     63 			"Unable to add attribute %d", i);
     64 	}
     65 
     66 	nlh = nlmsg_hdr(msg);
     67 	i = 1;
     68 	nlmsg_for_each_attr(a, nlh, 0, rem) {
     69 		fail_if(nla_type(a) != i, "Expected attribute %d", i);
     70 		i++;
     71 		fail_if(nla_get_u32(a) != i, "Expected attribute value %d", i);
     72 	}
     73 
     74 	nlmsg_free(msg);
     75 }
     76 END_TEST
     77 
     78 Suite *make_nl_attr_suite(void)
     79 {
     80 	Suite *suite = suite_create("Netlink attributes");
     81 
     82 	TCase *nl_attr = tcase_create("Core");
     83 	tcase_add_test(nl_attr, attr_size);
     84 	tcase_add_test(nl_attr, msg_construct);
     85 	suite_add_tcase(suite, nl_attr);
     86 
     87 	return suite;
     88 }
     89