1*08855964SRyan Zezeski.\" 2*08855964SRyan Zezeski.\" This file and its contents are supplied under the terms of the 3*08855964SRyan Zezeski.\" Common Development and Distribution License ("CDDL"), version 1.0. 4*08855964SRyan Zezeski.\" You may only use this file in accordance with the terms of version 5*08855964SRyan Zezeski.\" 1.0 of the CDDL. 6*08855964SRyan Zezeski.\" 7*08855964SRyan Zezeski.\" A full copy of the text of the CDDL should have accompanied this 8*08855964SRyan Zezeski.\" source. A copy of the CDDL is also available via the Internet at 9*08855964SRyan Zezeski.\" http://www.illumos.org/license/CDDL. 10*08855964SRyan Zezeski.\" 11*08855964SRyan Zezeski.\" 12*08855964SRyan Zezeski.\" Copyright 2023 Oxide Computer Company 13*08855964SRyan Zezeski.\" 14*08855964SRyan Zezeski.Dd February 17, 2023 15*08855964SRyan Zezeski.Dt KTEST 9 16*08855964SRyan Zezeski.Os 17*08855964SRyan Zezeski.Sh NAME 18*08855964SRyan Zezeski.Nm ktest 19*08855964SRyan Zezeski.Nd kernel test facility 20*08855964SRyan Zezeski.Sh DESCRIPTION 21*08855964SRyan ZezeskiThe ktest facility provides the means for writing and executing kernel 22*08855964SRyan Zezeskitest modules. 23*08855964SRyan ZezeskiA kernel test module is a kernel module that contains test functions 24*08855964SRyan Zezeskidesigned to execute and verify some aspect of the kernel. 25*08855964SRyan ZezeskiYou can test any aspect of the kernel for which you can create the 26*08855964SRyan Zezeskirequisite context, whether that be its local arguments or shared 27*08855964SRyan Zezeskistate. 28*08855964SRyan Zezeski.Pp 29*08855964SRyan ZezeskiThe key design idea behind ktest is to put the test in the same 30*08855964SRyan Zezeskicontext as the code it's testing: kernel context. 31*08855964SRyan ZezeskiBy writing tests as kernel functions it allows one to more thoroughly test 32*08855964SRyan Zezeskiparticular aspects of the kernel. 33*08855964SRyan ZezeskiThe ktest facility complements user-space testing by allowing targeted 34*08855964SRyan Zezeskitesting of the kernel that is not as easily exercised via user-space. 35*08855964SRyan Zezeski.Pp 36*08855964SRyan ZezeskiThe ktest facility provides a kernel API for creating tests as well as 37*08855964SRyan Zezeskia character device for interacting with the facility from user-space. 38*08855964SRyan ZezeskiThe latter of which should be done by way of the 39*08855964SRyan Zezeski.Xr ktest 8 40*08855964SRyan Zezeskicommand. 41*08855964SRyan Zezeski.Pp 42*08855964SRyan ZezeskiThis page concerns itself with the various kernel-side concepts of the 43*08855964SRyan Zezeskiktest facility and acts as a guide to the various 9F APIs. 44*08855964SRyan Zezeski.Ss The Test Triple 45*08855964SRyan ZezeskiA test is uniquely identified by its test triple. 46*08855964SRyan ZezeskiThe triple is a three-level namespace comprising of the module name, 47*08855964SRyan Zezeskisuite name, and test name. 48*08855964SRyan ZezeskiEach part of the namespace is separated by the ':' character. 49*08855964SRyan Zezeski.Bd -literal -offset 4m 50*08855964SRyan Zezeski<module>:<suite>:<test> 51*08855964SRyan Zezeski.Ed 52*08855964SRyan Zezeski.Bl -tag -width 6m 53*08855964SRyan Zezeski.It module 54*08855964SRyan ZezeskiThe top of the namespace. 55*08855964SRyan ZezeskiIf the test module is primarily focused on testing one kernel module, 56*08855964SRyan Zezeskithen its module name is typically named after the module-under-test. 57*08855964SRyan ZezeskiFor example, a test module designed to test all aspects of the 58*08855964SRyan Zezeski.Sy mac 59*08855964SRyan Zezeskikernel module might use "mac" as the module name. 60*08855964SRyan ZezeskiThis is merely a convention; at the end of the day the module name is 61*08855964SRyan Zezeskisimply a string meant to represent the general theme of its underlying 62*08855964SRyan Zezeskitests. 63*08855964SRyan Zezeski.It suite 64*08855964SRyan ZezeskiEach module consists of one or more suites. 65*08855964SRyan ZezeskiThe suite name provides a second level of organization for grouping 66*08855964SRyan Zezeskirelated tests. 67*08855964SRyan ZezeskiFor example, your "mac" test module may have several tests for 68*08855964SRyan Zezeskichecksum-related routines. 69*08855964SRyan ZezeskiIn that case it might help to organize all those tests under the 70*08855964SRyan Zezeski"checksum" suite. 71*08855964SRyan Zezeski.It test 72*08855964SRyan ZezeskiThe name of the test. 73*08855964SRyan ZezeskiThis can be any name you find descriptive of the test. 74*08855964SRyan ZezeskiFor tests that focus on a single function you could use the name of 75*08855964SRyan Zezeskithe function-under-test with the "_test" suffix attached. 76*08855964SRyan ZezeskiFor tests that exercise many functions do your best to make the name 77*08855964SRyan Zezeskidescriptive. 78*08855964SRyan ZezeskiThe test functions are typically suffixed with "_test" to visually 79*08855964SRyan Zezeskiseparate them from test-helper functions; and the test name is often 80*08855964SRyan Zezeskithe same as the test function. 81*08855964SRyan Zezeski.El 82*08855964SRyan Zezeski.Pp 83*08855964SRyan ZezeskiFor a name to be considered valid it must meet the following criteria. 84*08855964SRyan Zezeski.Bl -enum 85*08855964SRyan Zezeski.It 86*08855964SRyan ZezeskiIt must be 63 characters or less (the 64th byte must hold the 87*08855964SRyan Zezeski.Sy NUL 88*08855964SRyan Zezeskicharacter). 89*08855964SRyan Zezeski.It 90*08855964SRyan ZezeskiIt must contain only the following ASCII 91*08855964SRyan Zezeskicharacters: 'A-Z', 'a-z', '0-9', '.', '_'. 92*08855964SRyan Zezeski.El 93*08855964SRyan Zezeski.Ss The Context Handle 94*08855964SRyan ZezeskiThe context handle provides the means through which the test 95*08855964SRyan Zezeskicommunicates with the ktest facility. 96*08855964SRyan ZezeskiAll test functions must conform to the following prototype. 97*08855964SRyan Zezeski.Bd -literal -offset 4m 98*08855964SRyan Zezeskitypedef void (*ktest_fn_t)(ktest_ctx_hdl_t *ctx); 99*08855964SRyan Zezeski.Ed 100*08855964SRyan Zezeski.Pp 101*08855964SRyan ZezeskiThe test interacts with 102*08855964SRyan Zezeski.Fa ctx 103*08855964SRyan Zezeskithrough various ktest routines documented in their requisite sections. 104*08855964SRyan Zezeski.Ss Setting Test Results 105*08855964SRyan ZezeskiThe ultimate goal of a test function is to return a result. 106*08855964SRyan ZezeskiSee 107*08855964SRyan Zezeski.Xr ktest_result_pass 9f 108*08855964SRyan Zezeskifor the complete guide to setting test results. 109*08855964SRyan Zezeski.Ss Test Input 110*08855964SRyan ZezeskiSome tests require an input stream to test against. 111*08855964SRyan ZezeskiThe input stream is provided at runtime by the user. 112*08855964SRyan ZezeskiThe user may run the same test against one or more different input 113*08855964SRyan Zezeskistreams. 114*08855964SRyan Zezeski.Pp 115*08855964SRyan ZezeskiThis is useful when a test applies to many different scenarios with 116*08855964SRyan Zezeskithe same general logic. 117*08855964SRyan ZezeskiFor example, a test that verifies a checksum routine applies to any 118*08855964SRyan Zezeskibyte stream; or a test that verifies TCP header parsing applies to any 119*08855964SRyan Zezeskibyte stream as well. 120*08855964SRyan Zezeski.Pp 121*08855964SRyan ZezeskiWriting a test against an input stream provides several benefits. 122*08855964SRyan Zezeski.Bl -enum 123*08855964SRyan Zezeski.It 124*08855964SRyan ZezeskiIt allows one test to verify innumerable scenarios, reducing the need 125*08855964SRyan Zezeskito replicate test logic. 126*08855964SRyan Zezeski.It 127*08855964SRyan ZezeskiIt avoids the need to hard-code test input in the test module itself. 128*08855964SRyan Zezeski.It 129*08855964SRyan ZezeskiWith the ability to change the input stream at runtime it allows 130*08855964SRyan Zezeskitesting new scenarios on the spot, avoiding the need for writing a new 131*08855964SRyan Zezeskitest. 132*08855964SRyan Zezeski.El 133*08855964SRyan Zezeski.Pp 134*08855964SRyan ZezeskiA test that requires input must specify the 135*08855964SRyan Zezeski.Sy KTEST_FLAG_INPUT 136*08855964SRyan Zezeskiflag when calling 137*08855964SRyan Zezeski.Xr ktest_add_test 9f . 138*08855964SRyan ZezeskiTo retrieve the input stream the test uses the 139*08855964SRyan Zezeski.Xr ktest_get_input 9f 140*08855964SRyan Zezeskifunction. 141*08855964SRyan Zezeski.Ss Testing Private Functions 142*08855964SRyan ZezeskiA test facility that can't test private ( 143*08855964SRyan Zezeski.Ft static 144*08855964SRyan Zezeski) functions is going to be pretty limited. 145*08855964SRyan ZezeskiSome of the most useful functions to test are those that act as 146*08855964SRyan Zezeski"helpers" to other, larger functions. 147*08855964SRyan ZezeskiThese functions are often declared 148*08855964SRyan Zezeski.Ft static 149*08855964SRyan Zezeskiand not directly accessible to the test module which is built as its 150*08855964SRyan Zezeskiown compilation unit. 151*08855964SRyan Zezeski.Pp 152*08855964SRyan ZezeskiOne solution would be to alter ktest to also allow declaring test 153*08855964SRyan Zezeskifunctions inside the regular kernel modules themselves, but it 154*08855964SRyan Zezeskicurrently does not offer that capability. 155*08855964SRyan ZezeskiInstead, ktest provides the 156*08855964SRyan Zezeski.Xr ktest_get_fn 9f 157*08855964SRyan Zezeskifunction for obtaining a handle to private functions. 158*08855964SRyan ZezeskiSee that page for more detail. 159*08855964SRyan Zezeski.Ss Registering Tests 160*08855964SRyan ZezeskiOnly registered tests are accessible to the user. 161*08855964SRyan ZezeskiSee 162*08855964SRyan Zezeski.Xr ktest_create_module 9f 163*08855964SRyan Zezeskifor more information. 164*08855964SRyan Zezeski.Ss Test Module Conventions 165*08855964SRyan ZezeskiThere are several conventions to follow when creating a new test 166*08855964SRyan Zezeskimodule. 167*08855964SRyan Zezeski.Bl -enum 168*08855964SRyan Zezeski.It 169*08855964SRyan ZezeskiThe test module should be a miscellaneous-type module using the 170*08855964SRyan Zezeski.Xr modlmisc 9S 171*08855964SRyan Zezeskilinkage structure. 172*08855964SRyan Zezeski.It 173*08855964SRyan ZezeskiTest functions are prefixed with their test module name. 174*08855964SRyan Zezeski.It 175*08855964SRyan ZezeskiTest functions are suffixed with "_test" to distinguish them from test 176*08855964SRyan Zezeskihelper functions. 177*08855964SRyan Zezeski.It 178*08855964SRyan ZezeskiTest registration should happen in 179*08855964SRyan Zezeski.Xr _init 9E . 180*08855964SRyan Zezeski.It 181*08855964SRyan ZezeskiTest deregistration should happen in 182*08855964SRyan Zezeski.Xr _fini 9E . 183*08855964SRyan Zezeski.El 184*08855964SRyan Zezeski.Sh SEE ALSO 185*08855964SRyan Zezeski.Xr KT_ASSERT 9F , 186*08855964SRyan Zezeski.Xr KT_ERROR 9F , 187*08855964SRyan Zezeski.Xr KT_FAIL 9F , 188*08855964SRyan Zezeski.Xr KT_PASS 9F , 189*08855964SRyan Zezeski.Xr KT_SKIP 9F , 190*08855964SRyan Zezeski.Xr ktest_add_suite 9F , 191*08855964SRyan Zezeski.Xr ktest_add_test 9F , 192*08855964SRyan Zezeski.Xr ktest_create_module 9F , 193*08855964SRyan Zezeski.Xr ktest_get_fn 9F , 194*08855964SRyan Zezeski.Xr ktest_get_input 9F , 195*08855964SRyan Zezeski.Xr ktest_hold_mod 9F , 196*08855964SRyan Zezeski.Xr ktest_msg_clear 9F , 197*08855964SRyan Zezeski.Xr ktest_msg_prepend 9F , 198*08855964SRyan Zezeski.Xr ktest_register_module 9F , 199*08855964SRyan Zezeski.Xr ktest_release_mod 9F , 200*08855964SRyan Zezeski.Xr ktest_unregister_module 9F , 201*08855964SRyan Zezeski.Xr modlmisc 9S 202