2

I want to employ an automatic unit test generation approach in C++ with the help of LLVM. The approach should automatically acquire the states of specific objects during a dynamic analysis of the application under test (AUT). After the data has been recorded I want to write the test. Here, the test should reconstruct the objects with the recorded test data as a setup before executing the method/code under test.

With object states I mean all member variable values of an object including references to other objects (for which I also need to acquire and reconstruct the whole object state). However, since ALL member values include those of private member variables, I ran into a problem. From what I have learned, there is no way to access private member variables in C++. That is, unless the object type in question is a friend with any of "my object types" or provides direct access functions to its private members.

Actually, I can solve this problem for types which have been declared in the source code of the AUT. Here, I can use LLVM to instrument the types with the necessary code during compilation. However, I cannot do this for referenced types from precompiled libraries which the AUT uses.

Hence, my question: Have you any idea how I can record and reconstruct the full states of arbritrary objects for which I do not have the source code? Could direct copying of memory help?

Since my approach is actually basic (automatic) unit test generation, I'm sure there has to be way to implement this in C++. After all, such kinds of generators have already been implemented in Java and C#.

  • Most of the unit testing frameworks provide `SetUp()` / `TearDown()` methods executed before/after each test case, to get the objects under test in a certain state. – πάντα ῥεῖ Jun 28 '14 at 12:10
  • Both Java and C# run in a VM sandbox environment which (by design) keep a whole lot of metadata about its objects (for proper functioning of the VM). In contrast C++ is a much lover-level language so it is unfair to expect it to automatically provide the same functionality as Java and C# in that regard (otherwise C++'s flexibility and performance would suffer). – YePhIcK Jun 28 '14 at 12:27
  • 1
    Note that both C# and Java provide reflection capabilities. I see that LLVM offers a "light-weight reflection", but only for source it has compiled. Doing this for precompiled code in C++ seems infeasible (although it sounds like fun to see how far you can get by a writing a run-time decompiler) – holtavolt Jun 28 '14 at 12:28

1 Answers1

0

C++ isn't designed for this because there is no object introspection and serialization in the base language. Of course you can implement this yourself, but maybe you should use a framework that can help you, such as protobuf or Qt. The main point is this will have an big impact on the code you intend to test. I recommend using another approach, maybe writing code that actually sets up the object states in your tests, it will be much less intrusive.

Tramboi
  • 151
  • 5
  • The OP stated he's working on a LLVM extension, your answer has nothing to do with this, and mentioning protobuf doesn't help for this case at all (don't confuse serialization with reflection). – πάντα ῥεῖ Jun 28 '14 at 17:40
  • a) No confusion there. The OP needs to serialize the state and deserialize it. Reflection is not sufficient. b) The author uses binary libraries, LLVM won't do anything for this external code (ecept through decompilation and recompilation, as noted by holtavolt, which is probably out of scope to write a test framework) – Tramboi Jun 28 '14 at 19:56
  • Though using protobuf for such will require appropriate mapping mechanisms. – πάντα ῥεῖ Jun 28 '14 at 19:58
  • Sure, reflection is always intrusive and painful in C++. I stand by my answer : the practical solution is probably to setup the state in raw C++. The OP should think about what his approach implies if one of his object contains a DirectX texture, a thread handle or other very opaque stuff. – Tramboi Jun 28 '14 at 20:10