Coder Social home page Coder Social logo

cppunitlite's Introduction

CppUnitLite - lite c++ testing framework

This is a modified version of CppUnitLite

Usage

Use CppUnitLite to add unit tests to the c++ side of a node addon.

Installing

Install cppunitlite with npm. It has no package dependencies, but requires node-gyp to be installed and working.

$ npm i --save-dev cppunitlite
npm http GET https://registry.npmjs.org/cppunitlite
npm http 304 https://registry.npmjs.org/cppunitlite

> [email protected] install Z:\code\node_modules\cppunitlite
> node-gyp rebuild

... platform dependent stuff ...
[email protected] node_modules\cppunitlite

Changes to your binding.gyp

Add a test target to your binding.gyp:

    {
      "target_name": "test",
      "type": "executable",
      "sources": [
          # test filenames
      ],
      "include_dirs": [
        ".",
        "src",
        "<!(node -e \"require('cppunitlite')\")",
        "<!(node -e \"require('nan')\")"
      ],
      "dependencies": [
        "node_modules/cppunitlite/binding.gyp:CppUnitLite",
      ],
      "conditions": [
        ['OS=="win"', {
          }, {
            'cflags_cc': [ '-fexceptions' ]
          }
        ]
      ],
      # sample unit test
    }

A test main

CppUnitLite does not provide a main() function, but it's easy to write a minimal one; for example:

#include "CppUnitLite/TestHarness.h"

int main()
{
    TestResult tr;
    TestRegistry::runAllTests(tr);

    return 0;
}

Write Unit Tests

The include directories are set up so that the CppUnitLite headers should be included with a path.

#include "CppUnitLite/TestHarness.h"

#include <string>

static inline SimpleString StringFrom(const std::string& value)
{
	return SimpleString(value.c_str());
}

TEST( Hello, world )
{
  std::string s1("Hello"), s2("Hello"), s3("world");

  CHECK_EQUAL(s1, s2);
  CHECK_EQUAL(s2, s1);

  CHECK(s1 != s3);
}

Version history

Original version from Michael Feathers http://www.objectmentor.com/resources/downloads.html http://www.objectmentor.com/resources/bin/CppUnitLite.zip

Some documentation here: http://c2.com/cgi/wiki?CppUnitLite

Modified version by Keith Bauer, published as an SVN repository http://www.onesadcookie.com/svn/CppUnitLite

Imported to git 2014-01-19 and pushed to github

cppunitlite's People

Contributors

kemitchell avatar smikes avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

cppunitlite's Issues

Wrong delete operator being used

in Failure.cpp, it has:

char *stage = new char [strlen (part1)
					+ expected.size ()
					+ strlen (part3)
					+ actual.size ()
					+ 1];
///other code here..

	delete stage;  //This is incorrect.. it should be `delete[]` and not `delete`.

It uses delete instead of delete[] on stage which is an array. This is undefined behaviour and can lead to unexpected crashes.

In SimpleString.cpp, it has:

SimpleString SimpleString::operator= (const SimpleString& other)
{
	delete buffer;  //This is incorrect.. it should be `delete[]` and not `delete`.
	buffer = new char [other.size() + 1];
	strcpy(buffer, other.buffer);
	return *this;
}

It uses delete instead of delete[] for buffer which is an array. This is undefined behaviour and can lead to unexpected crashes.

Slight bug in README.md documentation

The part where you're showing how to add cppunitlite in the include_dirs is slightly wrong, I think. It seems like the end should actually be ")'
So this:

'<!(node -e "require(\'cpppunitlite\')'

Should actually be:

'<!(node -e "require(\'cppunitlite\')")'

Linking test target executable against your node file

Mac OS X Yosemite: I am building my c++ bindings as a bundle (mh_bundle). This seems to be the default for node-gyp. Now, I want to use cppunitlite npm module to test my c++ bindings, but I am having issues linking or dlopen my main bindings file. Do you have an example of how to link your test target to your main target?

Compile warning on OSX

CXX(target) Release/obj.target/CppUnitLite/node_modules/cppunitlite/src/Failure.o
../node_modules/cppunitlite/src/Failure.cpp:30:16: warning: conversion from string literal to 'char *' is deprecated
[-Wdeprecated-writable-strings]
char *part1 = "expected ";
^
../node_modules/cppunitlite/src/Failure.cpp:31:16: warning: conversion from string literal to 'char *' is deprecated
[-Wdeprecated-writable-strings]
char *part3 = " but was: ";
^
2 warnings generated.

sprintf without bounds

From Brave: https://github.com/brave/browser-ios/blob/development/brave/node_modules/tracking-protection/node_modules/cppunitlite/src/SimpleString.cpp#L85

sprintf (buffer, "%lf", value);

Does not check for buffer overflows, If message's length > str's length, there's a buffer overflow.

This issue was reported to brave via hackerone, they marked my bug as informative and close it by saying.

The code that you highlighted is part of the npm module cppunitlite. The npm module is used in the tracking protection library in the iOS browser, but the code snippet that you highlighted is not used in the code.

You can report the issue here for a fix from the author: https://github.com/smikes/CppUnitLite/

Thanks, Dhiraj

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.