Accessing Third Parameter of a Variadic ioctl Stubbed Function

Question:
How to access the 3rd parameter of a variadic ioctl stubbed function?
Answer:

For example, let's assume that your ioctl is declared as:
 

  int ioctl(int fd, int function, ...);
 

If your ioctl() has different parameter names, substitute those names instead in the description/code that follows.
 

Because ioctl() is a variadic function, VectorCAST does not provide direct access to the unnamed parameters via the parameter tree. Instead you can use user code to access the third parameter.
 

The user code will need to use the the macros in stdarg.h to manipulate the unnamed parameter. So the first step is to #include that header. You can also add a global variable to be used to store the parameter.

  • open your environment in the VectorCAST GUI
  • go to Environment | User Code | Edit
  • expand the Unit Appendix User Code section
  • double click the box in the value column next to your UUT
  • you should now be in edit mode. Enter this user code:

#include <stdarg.h>
int vcast_ioctl_capture_3rd_int;

  • click the Test compile icon (has a little red arrow)
  • the compile should succeed -- click OK
  • close the user code tab or mdi window
  • you should be prompted. Choose Save and link and click OK

Now you can add user code to the actual library stub definition to
capture the third parameter:

  • go to Environment | Configure Stubs | Edit
  • find the row for ioctl (probably under ioctl.h)
  • double-click the box under Beginning of stub for the ioctl row
  • you should now be in edit mode. Enter this user code:

va_list ap;
va_start(ap, function);
vcast_ioctl_capture_3rd_int = va_arg(ap, int);
va_end(ap);

  • click the Test compile icon (has a little red arrow)
  • the compile should succeed -- click OK
  • close the user code tab or mdi window
  • you should be prompted. Choose save and link and click OK
     

After making these changes, whenever your function under test calls ioctl, it will call the VectorCAST-generated library stub instead, and that stub will set the global variable vcast_ioctl_capture_3rd_int to the integer passed as the 3rd parameter to ioctl(). You can then use regular parameter user code for the ioctl() function to modify the pointer stored in that int.
 

  • create a test case
  • find ioctl in the Stubbed Subprograms section of the parameter tree
  • double-click on return for ioctl
  • go to the User Code tab
  • paste this code into the Input User Code section:

int* ptrBytesAvailable = (int*)vcast_ioctl_capture_3rd_int;
*ptrBytesAvailable = 1234;

  • click Test Compile Input
  • the test compile should succeed
  • click OK to close the Properties/User Code dialog
     

You can now execute the test case, and when ioctl is called, your parameter user code will put 1234 at the address pointed to by the 3rd parameter.
 



Article ID: 1466
Last updated: 2019-10-24
Revision: 2
VectorCAST -> C++ Unit Testing -> Accessing Third Parameter of a Variadic ioctl Stubbed Function
https://kb.vector.com/entry/1466/