Browse Source

tests: introduce TEST_NLATTR_OBJECT_MINSZ

It is useful in cases where a structure grows over time and we support
decoding of only part of it.

* tests/test_nlattr.h (TEST_NLATTR_OBJECT_EX_, TEST_NLATTR_OBJECT_EX):
Add minsz_ parameter, use it instead of sizeof(obj_).
(TEST_NLATTR_OBJECT): Pass sizeof(obj_) as minsz_.
(TEST_NLATTR_OBJECT_MINSZ): New macro.
* tests/nlattr_crypto_user_alg.c (main): Add proper minsz_ argument to
TEST_NLATTR_OBJECT_EX instances.
Eugene Syromyatnikov 2 months ago
parent
commit
1e50b09fa7
2 changed files with 26 additions and 15 deletions
  1. 9
    5
      tests/nlattr_crypto_user_alg.c
  2. 17
    10
      tests/test_nlattr.h

+ 9
- 5
tests/nlattr_crypto_user_alg.c View File

@@ -85,7 +85,8 @@ main(void)
85 85
 	TEST_NLATTR_OBJECT_EX(fd, nlh0, hdrlen,
86 86
 			      init_crypto_user_alg, print_crypto_user_alg,
87 87
 			      CRYPTOCFGA_REPORT_HASH,
88
-			      pattern, rhash, print_quoted_memory,
88
+			      pattern, rhash, sizeof(rhash),
89
+			      print_quoted_memory,
89 90
 			      printf("{type=\"efgh\"");
90 91
 			      PRINT_FIELD_U(", ", rhash, blocksize);
91 92
 			      PRINT_FIELD_U(", ", rhash, digestsize);
@@ -104,7 +105,8 @@ main(void)
104 105
 	TEST_NLATTR_OBJECT_EX(fd, nlh0, hdrlen,
105 106
 			      init_crypto_user_alg, print_crypto_user_alg,
106 107
 			      CRYPTOCFGA_REPORT_BLKCIPHER,
107
-			      pattern, rblkcipher, print_quoted_memory,
108
+			      pattern, rblkcipher, sizeof(rblkcipher),
109
+			      print_quoted_memory,
108 110
 			      printf("{type=\"abcd\", geniv=\"efgh\"");
109 111
 			      PRINT_FIELD_U(", ", rblkcipher, blocksize);
110 112
 			      PRINT_FIELD_U(", ", rblkcipher, min_keysize);
@@ -124,7 +126,8 @@ main(void)
124 126
 	TEST_NLATTR_OBJECT_EX(fd, nlh0, hdrlen,
125 127
 			      init_crypto_user_alg, print_crypto_user_alg,
126 128
 			      CRYPTOCFGA_REPORT_AEAD,
127
-			      pattern, raead, print_quoted_memory,
129
+			      pattern, raead, sizeof(raead),
130
+			      print_quoted_memory,
128 131
 			      printf("{type=\"abcd\", geniv=\"efgh\"");
129 132
 			      PRINT_FIELD_U(", ", raead, blocksize);
130 133
 			      PRINT_FIELD_U(", ", raead, maxauthsize);
@@ -140,7 +143,7 @@ main(void)
140 143
 	TEST_NLATTR_OBJECT_EX(fd, nlh0, hdrlen,
141 144
 			      init_crypto_user_alg, print_crypto_user_alg,
142 145
 			      CRYPTOCFGA_REPORT_RNG,
143
-			      pattern, rrng, print_quoted_memory,
146
+			      pattern, rrng, sizeof(rrng), print_quoted_memory,
144 147
 			      printf("{type=\"abcd\"");
145 148
 			      PRINT_FIELD_U(", ", rrng, seedsize);
146 149
 			      printf("}"));
@@ -156,7 +159,8 @@ main(void)
156 159
 	TEST_NLATTR_OBJECT_EX(fd, nlh0, hdrlen,
157 160
 			      init_crypto_user_alg, print_crypto_user_alg,
158 161
 			      CRYPTOCFGA_REPORT_CIPHER,
159
-			      pattern, rcipher, print_quoted_memory,
162
+			      pattern, rcipher, sizeof(rcipher),
163
+			      print_quoted_memory,
160 164
 			      printf("{type=\"abcd\"");
161 165
 			      PRINT_FIELD_U(", ", rcipher, blocksize);
162 166
 			      PRINT_FIELD_U(", ", rcipher, min_keysize);

+ 17
- 10
tests/test_nlattr.h View File

@@ -96,11 +96,9 @@ print_nlattr(const unsigned int nla_len, const char *const nla_type, bool add_da
96 96
 #define TEST_NLATTR_OBJECT_EX_(fd_, nlh0_, hdrlen_,			\
97 97
 			       init_msg_, print_msg_,			\
98 98
 			       nla_type_, nla_type_str_,		\
99
-			       pattern_, obj_, fallback_func, ...)	\
99
+			       pattern_, obj_, minsz_, fallback_func, ...) \
100 100
 	do {								\
101
-		const unsigned int plen =				\
102
-			sizeof(obj_) - 1 > DEFAULT_STRLEN		\
103
-			? DEFAULT_STRLEN : (int) sizeof(obj_) - 1;	\
101
+		const unsigned int plen = MIN((minsz_) - 1, DEFAULT_STRLEN); \
104 102
 		/* len < sizeof(obj_) */				\
105 103
 		if (plen > 0)						\
106 104
 			TEST_NLATTR_((fd_), (nlh0_), (hdrlen_),		\
@@ -113,7 +111,7 @@ print_nlattr(const unsigned int nla_len, const char *const nla_type, bool add_da
113 111
 			(init_msg_), (print_msg_),			\
114 112
 			(nla_type_), (nla_type_str_),			\
115 113
 			sizeof(obj_),					\
116
-			(pattern_), sizeof(obj_) - 1,			\
114
+			(pattern_), (minsz_) - 1,			\
117 115
 			printf("%p",					\
118 116
 			       RTA_DATA(NLMSG_ATTR(nlh, (hdrlen_)))));	\
119 117
 		/* sizeof(obj_) */					\
@@ -128,12 +126,12 @@ print_nlattr(const unsigned int nla_len, const char *const nla_type, bool add_da
128 126
 #define TEST_NLATTR_OBJECT_EX(fd_, nlh0_, hdrlen_,			\
129 127
 			      init_msg_, print_msg_,			\
130 128
 			      nla_type_,				\
131
-			      pattern_, obj_, fallback_func, ...)	\
129
+			      pattern_, obj_, minsz_, fallback_func, ...) \
132 130
 	TEST_NLATTR_OBJECT_EX_((fd_), (nlh0_), (hdrlen_),		\
133 131
 			       (init_msg_), (print_msg_),		\
134 132
 			       (nla_type_), #nla_type_,			\
135
-			       (pattern_), (obj_), (fallback_func),	\
136
-			       __VA_ARGS__)
133
+			       (pattern_), (obj_), (minsz_),		\
134
+			       (fallback_func),	__VA_ARGS__)
137 135
 
138 136
 #define TEST_NLATTR_OBJECT(fd_, nlh0_, hdrlen_,				\
139 137
 			   init_msg_, print_msg_,			\
@@ -141,8 +139,17 @@ print_nlattr(const unsigned int nla_len, const char *const nla_type, bool add_da
141 139
 	TEST_NLATTR_OBJECT_EX_((fd_), (nlh0_), (hdrlen_),		\
142 140
 			       (init_msg_), (print_msg_),		\
143 141
 			       (nla_type_), #nla_type_,			\
144
-			       (pattern_), (obj_), print_quoted_hex,	\
145
-			       __VA_ARGS__)
142
+			       (pattern_), (obj_), sizeof(obj_),	\
143
+			       print_quoted_hex, __VA_ARGS__)
144
+
145
+#define TEST_NLATTR_OBJECT_MINSZ(fd_, nlh0_, hdrlen_,			\
146
+			   init_msg_, print_msg_,			\
147
+			   nla_type_, pattern_, obj_, minsz_, ...)	\
148
+	TEST_NLATTR_OBJECT_EX_((fd_), (nlh0_), (hdrlen_),		\
149
+			       (init_msg_), (print_msg_),		\
150
+			       (nla_type_), #nla_type_,			\
151
+			       (pattern_), (obj_), (minsz_),		\
152
+			       print_quoted_hex, __VA_ARGS__)
146 153
 
147 154
 #define TEST_NLATTR_ARRAY(fd_, nlh0_, hdrlen_,				\
148 155
 			  init_msg_, print_msg_,			\

Loading…
Cancel
Save