1 // SPDX-License-Identifier: GPL-2.0
3 * KUnit test of proc sysctl.
6 #include <kunit/test.h>
7 #include <linux/sysctl.h>
9 #define KUNIT_PROC_READ 0
10 #define KUNIT_PROC_WRITE 1
13 static int i_one_hundred
= 100;
16 * Test that proc_dointvec will not try to use a NULL .data field even when the
19 static void sysctl_test_api_dointvec_null_tbl_data(struct kunit
*test
)
21 struct ctl_table null_data_table
= {
24 * Here we are testing that proc_dointvec behaves correctly when
25 * we give it a NULL .data field. Normally this would point to a
26 * piece of memory where the value would be stored.
29 .maxlen
= sizeof(int),
31 .proc_handler
= proc_dointvec
,
33 .extra2
= &i_one_hundred
,
36 * proc_dointvec expects a buffer in user space, so we allocate one. We
37 * also need to cast it to __user so sparse doesn't get mad.
39 void __user
*buffer
= (void __user
*)kunit_kzalloc(test
, sizeof(int),
45 * We don't care what the starting length is since proc_dointvec should
46 * not try to read because .data is NULL.
49 KUNIT_EXPECT_EQ(test
, 0, proc_dointvec(&null_data_table
,
50 KUNIT_PROC_READ
, buffer
, &len
,
52 KUNIT_EXPECT_EQ(test
, (size_t)0, len
);
58 KUNIT_EXPECT_EQ(test
, 0, proc_dointvec(&null_data_table
,
59 KUNIT_PROC_WRITE
, buffer
, &len
,
61 KUNIT_EXPECT_EQ(test
, (size_t)0, len
);
65 * Similar to the previous test, we create a struct ctrl_table that has a .data
66 * field that proc_dointvec cannot do anything with; however, this time it is
67 * because we tell proc_dointvec that the size is 0.
69 static void sysctl_test_api_dointvec_table_maxlen_unset(struct kunit
*test
)
72 struct ctl_table data_maxlen_unset_table
= {
76 * So .data is no longer NULL, but we tell proc_dointvec its
77 * length is 0, so it still shouldn't try to use it.
81 .proc_handler
= proc_dointvec
,
83 .extra2
= &i_one_hundred
,
85 void __user
*buffer
= (void __user
*)kunit_kzalloc(test
, sizeof(int),
91 * As before, we don't care what buffer length is because proc_dointvec
92 * cannot do anything because its internal .data buffer has zero length.
95 KUNIT_EXPECT_EQ(test
, 0, proc_dointvec(&data_maxlen_unset_table
,
96 KUNIT_PROC_READ
, buffer
, &len
,
98 KUNIT_EXPECT_EQ(test
, (size_t)0, len
);
101 * See previous comment.
104 KUNIT_EXPECT_EQ(test
, 0, proc_dointvec(&data_maxlen_unset_table
,
105 KUNIT_PROC_WRITE
, buffer
, &len
,
107 KUNIT_EXPECT_EQ(test
, (size_t)0, len
);
111 * Here we provide a valid struct ctl_table, but we try to read and write from
112 * it using a buffer of zero length, so it should still fail in a similar way as
115 static void sysctl_test_api_dointvec_table_len_is_zero(struct kunit
*test
)
119 struct ctl_table table
= {
122 .maxlen
= sizeof(int),
124 .proc_handler
= proc_dointvec
,
126 .extra2
= &i_one_hundred
,
128 void __user
*buffer
= (void __user
*)kunit_kzalloc(test
, sizeof(int),
131 * However, now our read/write buffer has zero length.
136 KUNIT_EXPECT_EQ(test
, 0, proc_dointvec(&table
, KUNIT_PROC_READ
, buffer
,
138 KUNIT_EXPECT_EQ(test
, (size_t)0, len
);
140 KUNIT_EXPECT_EQ(test
, 0, proc_dointvec(&table
, KUNIT_PROC_WRITE
, buffer
,
142 KUNIT_EXPECT_EQ(test
, (size_t)0, len
);
146 * Test that proc_dointvec refuses to read when the file position is non-zero.
148 static void sysctl_test_api_dointvec_table_read_but_position_set(
153 struct ctl_table table
= {
156 .maxlen
= sizeof(int),
158 .proc_handler
= proc_dointvec
,
160 .extra2
= &i_one_hundred
,
162 void __user
*buffer
= (void __user
*)kunit_kzalloc(test
, sizeof(int),
165 * We don't care about our buffer length because we start off with a
166 * non-zero file position.
170 * proc_dointvec should refuse to read into the buffer since the file
175 KUNIT_EXPECT_EQ(test
, 0, proc_dointvec(&table
, KUNIT_PROC_READ
, buffer
,
177 KUNIT_EXPECT_EQ(test
, (size_t)0, len
);
181 * Test that we can read a two digit number in a sufficiently size buffer.
184 static void sysctl_test_dointvec_read_happy_single_positive(struct kunit
*test
)
188 struct ctl_table table
= {
191 .maxlen
= sizeof(int),
193 .proc_handler
= proc_dointvec
,
195 .extra2
= &i_one_hundred
,
199 char *buffer
= kunit_kzalloc(test
, len
, GFP_USER
);
200 char __user
*user_buffer
= (char __user
*)buffer
;
201 /* Store 13 in the data field. */
202 *((int *)table
.data
) = 13;
204 KUNIT_EXPECT_EQ(test
, 0, proc_dointvec(&table
, KUNIT_PROC_READ
,
205 user_buffer
, &len
, &pos
));
206 KUNIT_ASSERT_EQ(test
, (size_t)3, len
);
208 /* And we read 13 back out. */
209 KUNIT_EXPECT_STREQ(test
, "13\n", buffer
);
213 * Same as previous test, just now with negative numbers.
215 static void sysctl_test_dointvec_read_happy_single_negative(struct kunit
*test
)
219 struct ctl_table table
= {
222 .maxlen
= sizeof(int),
224 .proc_handler
= proc_dointvec
,
226 .extra2
= &i_one_hundred
,
230 char *buffer
= kunit_kzalloc(test
, len
, GFP_USER
);
231 char __user
*user_buffer
= (char __user
*)buffer
;
232 *((int *)table
.data
) = -16;
234 KUNIT_EXPECT_EQ(test
, 0, proc_dointvec(&table
, KUNIT_PROC_READ
,
235 user_buffer
, &len
, &pos
));
236 KUNIT_ASSERT_EQ(test
, (size_t)4, len
);
238 KUNIT_EXPECT_STREQ(test
, "-16\n", (char *)buffer
);
242 * Test that a simple positive write works.
244 static void sysctl_test_dointvec_write_happy_single_positive(struct kunit
*test
)
248 struct ctl_table table
= {
251 .maxlen
= sizeof(int),
253 .proc_handler
= proc_dointvec
,
255 .extra2
= &i_one_hundred
,
258 size_t len
= sizeof(input
) - 1;
260 char *buffer
= kunit_kzalloc(test
, len
, GFP_USER
);
261 char __user
*user_buffer
= (char __user
*)buffer
;
263 memcpy(buffer
, input
, len
);
265 KUNIT_EXPECT_EQ(test
, 0, proc_dointvec(&table
, KUNIT_PROC_WRITE
,
266 user_buffer
, &len
, &pos
));
267 KUNIT_EXPECT_EQ(test
, sizeof(input
) - 1, len
);
268 KUNIT_EXPECT_EQ(test
, sizeof(input
) - 1, (size_t)pos
);
269 KUNIT_EXPECT_EQ(test
, 9, *((int *)table
.data
));
273 * Same as previous test, but now with negative numbers.
275 static void sysctl_test_dointvec_write_happy_single_negative(struct kunit
*test
)
278 struct ctl_table table
= {
281 .maxlen
= sizeof(int),
283 .proc_handler
= proc_dointvec
,
285 .extra2
= &i_one_hundred
,
288 size_t len
= sizeof(input
) - 1;
290 char *buffer
= kunit_kzalloc(test
, len
, GFP_USER
);
291 char __user
*user_buffer
= (char __user
*)buffer
;
293 memcpy(buffer
, input
, len
);
295 KUNIT_EXPECT_EQ(test
, 0, proc_dointvec(&table
, KUNIT_PROC_WRITE
,
296 user_buffer
, &len
, &pos
));
297 KUNIT_EXPECT_EQ(test
, sizeof(input
) - 1, len
);
298 KUNIT_EXPECT_EQ(test
, sizeof(input
) - 1, (size_t)pos
);
299 KUNIT_EXPECT_EQ(test
, -9, *((int *)table
.data
));
303 * Test that writing a value smaller than the minimum possible value is not
306 static void sysctl_test_api_dointvec_write_single_less_int_min(
310 struct ctl_table table
= {
313 .maxlen
= sizeof(int),
315 .proc_handler
= proc_dointvec
,
317 .extra2
= &i_one_hundred
,
319 size_t max_len
= 32, len
= max_len
;
321 char *buffer
= kunit_kzalloc(test
, max_len
, GFP_USER
);
322 char __user
*user_buffer
= (char __user
*)buffer
;
323 unsigned long abs_of_less_than_min
= (unsigned long)INT_MAX
324 - (INT_MAX
+ INT_MIN
) + 1;
327 * We use this rigmarole to create a string that contains a value one
328 * less than the minimum accepted value.
330 KUNIT_ASSERT_LT(test
,
331 (size_t)snprintf(buffer
, max_len
, "-%lu",
332 abs_of_less_than_min
),
335 KUNIT_EXPECT_EQ(test
, -EINVAL
, proc_dointvec(&table
, KUNIT_PROC_WRITE
,
336 user_buffer
, &len
, &pos
));
337 KUNIT_EXPECT_EQ(test
, max_len
, len
);
338 KUNIT_EXPECT_EQ(test
, 0, *((int *)table
.data
));
342 * Test that writing the maximum possible value works.
344 static void sysctl_test_api_dointvec_write_single_greater_int_max(
348 struct ctl_table table
= {
351 .maxlen
= sizeof(int),
353 .proc_handler
= proc_dointvec
,
355 .extra2
= &i_one_hundred
,
357 size_t max_len
= 32, len
= max_len
;
359 char *buffer
= kunit_kzalloc(test
, max_len
, GFP_USER
);
360 char __user
*user_buffer
= (char __user
*)buffer
;
361 unsigned long greater_than_max
= (unsigned long)INT_MAX
+ 1;
363 KUNIT_ASSERT_GT(test
, greater_than_max
, (unsigned long)INT_MAX
);
364 KUNIT_ASSERT_LT(test
, (size_t)snprintf(buffer
, max_len
, "%lu",
367 KUNIT_EXPECT_EQ(test
, -EINVAL
, proc_dointvec(&table
, KUNIT_PROC_WRITE
,
368 user_buffer
, &len
, &pos
));
369 KUNIT_ASSERT_EQ(test
, max_len
, len
);
370 KUNIT_EXPECT_EQ(test
, 0, *((int *)table
.data
));
373 static struct kunit_case sysctl_test_cases
[] = {
374 KUNIT_CASE(sysctl_test_api_dointvec_null_tbl_data
),
375 KUNIT_CASE(sysctl_test_api_dointvec_table_maxlen_unset
),
376 KUNIT_CASE(sysctl_test_api_dointvec_table_len_is_zero
),
377 KUNIT_CASE(sysctl_test_api_dointvec_table_read_but_position_set
),
378 KUNIT_CASE(sysctl_test_dointvec_read_happy_single_positive
),
379 KUNIT_CASE(sysctl_test_dointvec_read_happy_single_negative
),
380 KUNIT_CASE(sysctl_test_dointvec_write_happy_single_positive
),
381 KUNIT_CASE(sysctl_test_dointvec_write_happy_single_negative
),
382 KUNIT_CASE(sysctl_test_api_dointvec_write_single_less_int_min
),
383 KUNIT_CASE(sysctl_test_api_dointvec_write_single_greater_int_max
),
387 static struct kunit_suite sysctl_test_suite
= {
388 .name
= "sysctl_test",
389 .test_cases
= sysctl_test_cases
,
392 kunit_test_suite(sysctl_test_suite
);