简体   繁体   English

使用 C 语言中的任意类型的函数指针和任意类型的变量参数对远程嵌入式 mcu 上的任意函数进行单元测试

[英]unit test of arbitrary function on a remote embedded mcu using function pointers and variable arguments of arbitrary type, in C

the idea is to write ONE universal unit test function that would execute arbitrary function-under-test on the embedded target mcu side using an externally generated test input arguments (from communication for example).这个想法是编写一个通用单元测试函数,该函数将使用外部生成的测试输入参数(例如来自通信)在嵌入式目标 mcu 端执行任意被测函数。 so that the unit test function for the remote mcu is written only once and the test cases are generated on a host PC by sending the known function address and input arguments through some communication means.这样远程mcu的单元测试函数只写一次,测试用例通过某种通信方式发送已知函数地址和输入参数在主机PC上生成。

for example, if on a remote mcu there is a function of int sumoftwoints(int, int) to be tested, the host pc would pass to this unitTestFcn pointer the address of sumoftwoints (known by linker) and two random ints generated on the host pc then send to the embedded target by uart.例如,如果在远程 mcu 上有一个 int sumoftwoints(int, int) 的函数要测试,主机 pc 会将 sumoftwoints 的地址(链接器知道)和主机上生成的两个随机 int 传递给这个 unitTestFcn 指针pc 然后通过 uart 发送到嵌入式目标。 the embedded target "calls" by the function pointer received with the supplied number of argument and returns the result to send back to the host pc for result check.嵌入式目标通过接收到的函数指针“调用”提供的参数数量,并返回结果以发送回主机 pc 以进行结果检查。

any feedback on if this is plausible or there is fundamental flaw in this test scheme?关于这是否合理或该测试方案存在根本缺陷的任何反馈?

the benefit is to move the unit test from the embedded side to the host pc side, which has infinite test case expandability.好处是将单元测试从嵌入式端移到主机端,具有无限的测试用例可扩展性。

If I were to make a system like this, I'd make it consist of 3 parts:如果我要制作这样的系统,我会让它由 3 个部分组成:

  • The PC-side software where you specify your tests in the form of a function address and arguments.以函数地址和参数的形式指定测试的 PC 端软件。
  • A receiver/transmitter pair that implements some kind of communication between PC and MCU, eg over serial, Wifi or whatever's available.在 PC 和 MCU 之间实现某种通信的接收器/发射器对,例如通过串行、Wifi 或任何可用的通信。
  • A test executor that runs on the MCU, that executes the function, and returns the result, if any.在 MCU 上运行的测试执行器,它执行函数并返回结果(如果有)。

Tbe PC-side software could read some artifacts produced by your build process to simplify things, eg so you may use actual function names instead of having to specify the actual address in your tests. PC 端软件可以读取您的构建过程产生的一些工件以简化事情,例如,您可以使用实际的函数名称,而不必在测试中指定实际地址。

The communication protocol should be text based, relying on the receiver to convert values as appropriate for the receiving platform.通信协议应该是基于文本的,依靠接收器来转换适合接收平台的值。

The executor would have to implement the ABI specific to the architecture of the MCU.执行器必须实现特定于 MCU 架构的 ABI。 This is where you'll reach your first major hurdle.这是你将达到你的第一个主要障碍的地方。 First of all, ABIs vary a lot.首先,ABI 变化很大。 From Arm where some arguments are passed in one or more registers, some as pointers to the actual argument, and some on stack, to x86 where it's all stack based.从一些参数在一个或多个寄存器中传递的 Arm,一些作为指向实际参数的指针,一些在堆栈上,到 x86,它都是基于堆栈的。

Your next major hurdle is going to be structs.您的下一个主要障碍将是结构。 If you're going to allow functions with struct parameters or return values to be called, you have to know how your compiler chose to pack the fields for each specific struct.如果您要允许调用带有结构参数或返回值的函数,您必须知道您的编译器如何选择为每个特定结构打包字段。

All in all, creating something like this would be a non-trivial undertaking, and probably ending up being not that useful, unless you put a lot of work into it, as in making it a commercial product.总而言之,创建这样的东西将是一项艰巨的任务,并且可能最终没有那么有用,除非您投入大量工作,例如将其变成商业产品。

Another alternative is that you leave some scratch space on the device, then crosscompile your tests on the PC, then send them over to the MCU one by one or in groups, to be installed in the scratch space and be executed.另一种选择是在设备上留一些暂存空间,然后在PC上交叉编译你的测试,然后将它们一个一个或一组发送到MCU,安装在暂存空间中并执行。

In any case, this kind of functionality should never be included in a shipping product, due to obvious security concerns.在任何情况下,由于明显的安全问题,此类功能绝不应包含在运输产品中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM