Category Archives: Testing and Debugging

C++ (legacy) quality tools

Have you ever questioned why not all software teams are able to work at the same speed?

This article by Dirk-Jan Swagerman, outlines the hidden costs of “owning” software, and what business leaders can do to take control of their legacy software.

The hidden costs of software inventory

How to know, control, and improve the quality of your applications written with legacy C++ applications. Where to invest time to increase maintainability? How to reduce complexity to allow new developers to quickly get up to speed when joining the team?
Let’s think about C++ application written in C++ with use of MFC, COM interfaces and libraries that are only compatible with Visual Studio 2010 😉 I want to have an integration of various significant tools running in Jenkins.
What tools can I used to achieve this?

Unit testing

When I develop or refactor code, I do not want to unintensionaly effect other parts of the system. I want to verify this quickly. Then I use unit tests.

  • Catch2 (not compatible with Visual Studio 2010 compiler)
  • Boost.Test

Static Code Analysis

I want a tool to warn me for possible errors in the code.

  • CppCheck

Memory leak testing

I want a tool to identity memory (de)alocation errors.

  • Visual Studio CRT (under investigation)
  • Dr.Memory (under investigation)

Application testing

I want to test my application by simulation a user. I want to replay use cases and test the reponse of the application. I do not want repetative manual tests. I want a tool that can recognize the id’s, names and classes of Win32 controls (like shown by Spy++).

  • PyWinAuto (Python)

Code metrics

I want to quantify, control and observe the size and quality of my code. I want to measure and to improve maintainability and to decrease complexity.

  • Not available in Visual Studio 2010.
  • CCCC (produces HTML output)
  • SourceMonitor (no Jenkins plugin for latest version)

Continuous integration

I want everything to be done and checked automatically, preferably when I am sleeping at night…

  • Jenkins


Redirecting error messages from Command Prompt

To print the errors and standard output of an executable to a single file by using the &1 command to redirect the output for both STDERR to STDOUT and then sending the output from STDOUT to a text file:

app.exe 1> output.txt 2>&1


Enable build selection (debug or release) in Visual Studio 2010 Express

In the menu tools select settings. Here’s an expert mode you can enable.
Again in the tools menu select options. Open the item calles projects and solutions. On the general page you can enable show Advanced build configurations.


Security enhancements in CRT string functions

How to replace non-secure string functions with their secure ( +  _s) counterparts?

Sscanf_s

Read formatted data from a string. Parse part of strings and store results into variables. Consider a string like this:

char* source = "William                182";

We want to parse the name and the number into two variables, i.e. the destination of the sscanf function.

char scanned_name[200];
int scanned_value = 0;

Earlier we could do it like this:

sscanf( source, "%s %d", scanned_name, &scanned_value);

With help of the secure function we have to do it like this:

sscanf_s( source, "%s %d", scanned_name, sizeof(scanned_name), &scanned_value);

We give the length of the destination buffer as an additional argument sizeof(scanned_name) to the function. This additional argument is placed immediately after the destination buffer and before any other argument that is going to be scanned. The known size prevents the function to have a buffer overflow and prevents exploitation of your a system running the software.

Getenv_s

This is an example from MFC (CString) and wchar’s:

CString sVideoMemory = _tgetenv_s(_T("ORSVideoMemory"));

Into

wchar_t ptr[80];
size_t len;
_tgetenv_s(&len, ptr, 80, _T("ORSVideoMemory"));
CString sVideoMemory(ptr);

Strcpy_s

Todo


Test and debug C++ code with NUnit

If you like NUnit, you can also use it to test your C++ sofwtare. You can create managed C++ unit tests that call native C++ software. Instead of Nunit you can also use the test framework of Visual Studio 2008.

Installed on PC

– Visual Studio 2008 Professional
– NUnit 2.5.7

Let’s assume you have written a C++ class library that you want to test. We call this software the SUT (software or system under test).

Create a unit test project

  1. Open the solution of the SUT
  2. Add a new project: E.g. “UnitTests”
  3. Choose type: Visual C++, CLR and ‘CLR Empty project’
  4. I made a managed C++ class library for this.
  5. Add reference to unit. Right click on ‘UnitTests’ projects and select references. Add a new reference to the Nunit dll. This can be something like: c:\Program Files\NUnit 2.5.7\bin\net-2.0\framework\nunit.framework.dll
  6. Create a new class in the project. This class will be the managed wrapper for test functions that call your native code.
    public ref class UnitTestForSUT
    {
    ...
    }
  7. Import the NUnit namespace into the code of the test project:
    using namespace NUnit::Framework;
  8. Include your C++ class
    // Softwareunder test
    #pragma unmanaged
    #include ".Sut.h"
    #pragma managed
  9. Create a function that will contain the test code.  Add the [TestMethod] attribute. Nunit will use this attribute to find the functions to execute.
    [Test]
    void UnitTest1()
    {
    }
  10. Implement a test with help of the Assert::… statement[Test]

    void UnitTest1()
    {
    SUT test = new SUT();
     

    test.SetInput(4);
    test.IncrementInput(); // some processing, e.g. to increment to input value
    int output = test.GetOutput( );

    Assert::AreEqual(5, output, “Incorrect output value.”);

    delete test;
    }

Running the test

There are many ways. You can download a visual studio plugin, or start Nunit from Windows’ start menu and select the DLL of the SUT .

Debugging the test

I have configured the debug build to be tested with the console program of NUnit. To see the output of any printf() statements. And I use the GUI of NUnit in release build. I want NUnit to run when I start the test project from visual studio.

  1. In the test project settings. In the debugging tab set the following parameters:For Debug:Command: C:\Program Files\NUnit 2.5.7\bin\net-2.0\nunit.exe
    Command arguments: /nothread  $(OutDir)\$(ProjectName).dll 

    For Release:

    Command: C:\Program Files\NUnit 2.5.7\bin\net-2.0\nunit.exe
    Command Arguments: $(OutDir)\$(ProjectName).dll

    We pass the name of our TestProject to NUnit. Not the name of our SUT library. You will have to check that the working directory of your test project is the same as the output directory of the SUT.

  2. Important! Make sure you can access native code. Set ‘Debugger Type’ to Mixed. Otherwise you can nicely debug your test code, but cannot ‘step into’ the software under test.
  3. Put a break point in your test code or in the SUT, and start the test project.

An alternative way to debug is to start NUnit from the Windows’ start menu, and attach the visual studio debugger to this process.