I will be creating a Managed-C++ wrapper around some C functions to allow its use in other .NET solutions. I'm looking at providing a very minimalist wrapper, something like:
Signature in C header:
void DOSTH(const char*, short, long*);
Exposed managed interface:
public void doSomething(String^ input, short param, [Out] long^ %result);
To do so my solution will have the C headers and will reference the .dll that contains the compiled C API that I am building against.
As a Visual Studio newbie I'm unsure how I would unit test this. Is it possible to mock out the .dll to provide a mock implementation? Is there a library that would make this kind of task easy? Is there a particular solution structure I should aim for to make this easier?
Any guidance in this area would be great. Google searches have left me wanting for more info on unit testing a managed wrapper.
In some cases (tools limitations and/or dependency complexity comes to my mind), mocking dependency using external frameworks is out of question. Then, there's totally legitimate technique of writing mocks manually (I think that was the way to do stuff before mocking frameworks grew in popularity).
And that's basically what you want to do - fake out dependency, which in your case happens to be C library. Frameworks can't help - you might want to try manual approach.
Create some simple, faked implementation (pretty much like a stub, eg. only returning fixed values regardless of input params - naturally, might be more sophisticated than that), compile it, let it expose exactly the same headers/functions and reference it in your test project. That's the essential idea behind faking (stubbing/mocking) - one object pretending to be another.
As simple as it sounds, I haven't actually tried that - take it with a grain of salt and more as a suggestion which way you could go. Limitation of this approach (apart from whether it actually is technically possible) is very poor/none configuration options (since the extra faked DLL would act like a hardcoded stub - configuration files could help, but that feels like... too much work?).
Do you only need to be able to stub/mock out your wrapper so that your tests don't rely on the native dll?
Then you can declare an abstract base class for your wrapper, write one implementation that calls the native dll and another one for testing purposes that returns canned values. Or you can use a framework like Moq or Rhino.Mocks to mock your wrapper.
Related
I have recently tried to make a few basic projects in C (using CMake), but one aspect I find very difficult is getting all the different things that I've been making to link together nicely. For example, I started off by making a data-structure library that has some of the basic data structures, along with functions to traverse them, etc., and a testing library that handles unit testing. In most new projects I make, I find that I need to include these two libraries, but I can't find an easy way to do it. I tried doing this using git submodule, and while that did work for the most part, whenever I updated any of the dependencies, updating the dependant seemed to be a nightmare. I've also had a look into the cmake package system; find_package, and related functions; but I can't seem to get that to work (at least when I want to install it in a custom directory, that is).
I was wondering if there is some sort of "standard" way that C programmers go about dealing with this, and what that may be. Is submodules the way to go? If so, is there a way I could do it cleanly, making sure that everything is always the right version?
Thanks in advance.
For the sake of experimentation, I am looking for a way to modify some of Ruby's base code, specifically the parser. I was wondering if this was possible to do at all, let alone using a Gem.
I have narrowed the code I need to change to static int yylex() within parser.c. I was going to try to use an alias, but that seems to require that I change parser.h, which cannot be done within a Gem, as I understand.
Can this be done from a Gem?
No.
The only base C code that gems have access to is that exposed by the Ruby headers.
The parsing/lexing code is not exposed there.
If you want to define custom syntax, I would try (in order):
Loosen your requirements a bit and define a DSL. Ruby has insanely powerful metaprogramming features that can take anything you might do statically in a script and instead do it dynamically during runtime
Write your custom parser in Ruby and emit valid Ruby which you then eval. Ugly, and probably a little slow, but will allow you to do anything you want.
Modify the mruby parser instead. mruby is designed for embedded applications where you want to be able to highly customize the capabilities of the VM. I doubt that they had the parser in mind, but still it might be more feasible than messing around with MRI.
Assume you have a function read_key and normally it does some stuff. You someone should be able to replace it with his function read_key_myfunction and it does other stuff.
The general approach would of course be to build an array and register function pointers or using simple switch statements (or both).
But my target is a bit broader: People should be able to write their C-stuff and NOT interfere with my code and it should still register. Of course, I tell them which interface to implement.
What they now basically do is program a library for my software which I dynamically load based on a configuration option. Think of it like OpenSSLs engines: Anyone can write their own engine, compile it as a dll/so and distribute it. They don't need to modify (or know) OpenSSLs code, as long as they stick to the defined interface.
I just want the same (it will in the end be a wrapper for OpenSSL engine functions) for my program.
A colleague suggested I should use the same function in every file and load the libraries dynamically. This sounds like a good solution to me, but I am not quite satisfied since I don't see OpenSSL using any non-engine-specific function in their engine-code.
If some things are unclear here is my specific example:
I am extending a program called sscep which implements a protocol for automatic certificate renewal. A lot of cryptography should take place in HSMs in the future (and right now it should take place within the Windows Key Management (which is accessed by the capi-engine from OpenSSL)).
While OpenSSL already serves a generic interface, there is some stuff I need to do beforehand and it depends on the engine used. I also want to open the possibility for everyone else to extend it quickly without having to dig into my code (like I had from the person before me).
If anyone has any idea, it would be greatly appreciated to see some kind of guideline. Thanks in advance.
What you are describing is commonly called a plugin architecture/plugin framework. You need to combine cross-platform dlopen/LoadLibrary functionality with some logic for registering and performing lookup of exported functions. You should be able to find examples on how to do this on the internet.
I am trying to auto-generate Unit Tests for my C code using API sanity autotest.
But, the problem is that it is somewhat complex to use, and some tutorials / howto / other resources on how to use it would be really helpful.
Have you had any luck with API sanity autotest?
Do you think there's a better tool that can be used to auto-generate unit tests for C code?
It is a better tool (among free solutions for Unix) to fully automatically generate smoke tests if your library contains more than hundred functions. The unique feature is an ability to automatically generate reasonable input arguments for each function.
The most popular use case of this framework is a quick search for memory problems (segfaults) in the library. Historically, this framework was used to create LSB certification test suites for too big libraries like Qt3 and Qt4 that cannot be created manually in reasonable time.
Use the following command to generate, build and execute tests:
api-sanity-checker -l name -d descriptor.xml -gen -build -run
XML descriptor is a simple XML file that specifies version number, paths to headers and shared objects:
<version>
0.3.4
</version>
<headers>
/usr/local/libssh/0.3.4/include/
</headers>
<libs>
/usr/local/libssh/0.3.4/lib/
</libs>
You can improve generated tests using specialized types for input parameters.
See example of generated tests for freetype2 2.4.8.
It's a recipe for disaster in the first place. If you auto-generate unit tests, you're going to get a bunch of tests that don't mean a lot. If you have a library that is not covered in automated tests then, by definition, that library is legacy code. Consider following the conventional wisdom for legacy code...
For each change:
Pin behavior with tests
Refactor to the open-closed principle (harder to do with C but not impossible)
Drive changes for new code with tests
Also consider picking up a copy of Working Effectively with Legacy Code.
EDIT:
As a result of our discussion, it has become clear that you only want to enforce some basic standards, such has how null pointer values are handled, with your generated tests. I would argue that you don't need generated tests. Instead you need a tool that inspects a library and exercises its functions dynamically, ensuring that it meets some coding standards you have defined. I'd recommend that you write this tool, yourself, so that it can take advantage of your knowledge of the rules you want enforced and the libraries that are being tested.
I am looking at approaches to Unit Test embedded systems code written in C.
At the same time, I am also looking for a good UT framework that I can use. The framework should have a reasonably small number of dependencies.
Any great Open-source products that have good UTs?
EDIT:
Folks, thanks for the answers --I really appreciate them, but this does not directly answer my question:
Do you know of any Open-source code that uses C Unit Tests? I am looking to see how it's done, so that I can learn something from the code...
Modularize your code in such a way that there is a thin layer in the bottom which knows about the details of the underlying hardware. That layer can then be mocked on a normal PC. That way you can use normal unit test libraries for desktop development to test all code except that thin bottom layer.
My experience on developing embedded software for Linux is using C++ and not plain C. We used cppunit for unit tests.
Edit: there is a list of project using Check as unit test framework on the check project page. Some links are outdated, but you can browse the source of the unit test of pigment and SCEW ; one has to download the source for others.
minunit has no dependencies at all and can be used as a base to build your own framework.
As far as an approach is concerned, I start from the lowest level functions (the leaves). Very often, when I'm testing the higher level function they invoke the lower level functions. This is not a problem as the lower functions have already been tested.
I also modularize the code as "object", or table of objects. Stub functions allow testing in isolation of the hardware or of other components.
I get rid of the static with a define that remove them, or I include the source file containing the functions to be tested into the unit tests source file.
#if defined(UNIT_TESTING)
#define STATIC
#else
#define STATIC static
#endif
No rocket science here but this does the trick.
One last thing, test the behavior, not the implementation so that the unit tests won't break when the implementation changes.