First release of open-appsec source code

This commit is contained in:
roybarda
2022-10-26 19:33:19 +03:00
parent 3883109caf
commit a883352f79
1353 changed files with 276290 additions and 1 deletions

View File

@@ -0,0 +1,49 @@
#include <cmock/cmock.h>
#include "math.h"
using namespace ::testing;
class MathMocker : public CMockMocker<MathMocker>
{
public:
MOCK_METHOD2(add, int(int, int));
MOCK_METHOD2(substract, int(int, int));
MOCK_METHOD1(negate, int(int));
};
CMOCK_MOCK_FUNCTION2(MathMocker, add, int(int, int));
CMOCK_MOCK_FUNCTION2(MathMocker, substract, int(int, int));
CMOCK_MOCK_FUNCTION1(MathMocker, negate, int(int));
/**
* Functions add and substract are mocked as long as MathMocker instance exists.
* Once a mock function is destroyed, a call directs to a real function.
*/
TEST(FunctionClassMockersOldStyleTest, MocksFunctionAsLongAsMockerInstanceExists) {
{
MathMocker mock;
EXPECT_CALL(mock, add(1, 1)).WillOnce(Return(11));
ASSERT_EQ(11, add(1, 1));
EXPECT_CALL(mock, substract(1, 2)).WillOnce(Return(12));
ASSERT_EQ(12, substract(1, 2));
}
ASSERT_EQ(2, add(1, 1));
ASSERT_EQ(-1, substract(1, 2));
}
TEST(FunctionClassMockersOldStyleTest, ThrowsExceptionIfRealFunctionNotFound) {
{
MathMocker mock;
EXPECT_CALL(mock, negate(3)).WillOnce(Return(-3));
ASSERT_EQ(-3, negate(3));
}
EXPECT_THROW(negate(3), std::logic_error);
}

View File

@@ -0,0 +1,60 @@
#include <cmock/cmock.h>
#include "math.h"
using namespace ::testing;
class MathMocker : public CMockMocker<MathMocker>
{
public:
CMOCK_MOCK_METHOD(int, add, (int, int));
CMOCK_MOCK_METHOD(int, substract, (int, int));
CMOCK_MOCK_METHOD(int, negate, (int));
};
CMOCK_MOCK_FUNCTION(MathMocker, int, add, (int, int));
CMOCK_MOCK_FUNCTION(MathMocker, int, substract, (int, int));
CMOCK_MOCK_FUNCTION(MathMocker, int, negate, (int));
/**
* Functions add and substract are mocked as long as MathMocker instance exists.
* Once a mock function is destroyed, a call directs to a real function.
*/
TEST(FunctionClassMockersTest, MocksFunctionAsLongAsMockerInstanceExists) {
{
MathMocker mock;
EXPECT_CALL(mock, add(1, 1)).WillOnce(Return(11));
ASSERT_EQ(11, add(1, 1));
EXPECT_CALL(mock, substract(1, 2)).WillOnce(Return(12));
ASSERT_EQ(12, substract(1, 2));
}
ASSERT_EQ(2, add(1, 1));
ASSERT_EQ(-1, substract(1, 2));
}
TEST(FunctionClassMockersTest, ThrowsExceptionIfRealFunctionNotFound) {
{
MathMocker mock;
EXPECT_CALL(mock, negate(3)).WillOnce(Return(-3));
ASSERT_EQ(-3, negate(3));
}
EXPECT_THROW(negate(3), std::logic_error);
}
TEST(FunctionClassMockersTest, ProvidesMeansToCallRealFunction) {
{
MathMocker mock;
ASSERT_EQ(2, CMOCK_REAL_FUNCTION(MathMocker, add)(1, 1));
}
ASSERT_EQ(2, CMOCK_REAL_FUNCTION(MathMocker, add)(1, 1));
}

View File

@@ -0,0 +1,50 @@
#include <cmock/cmock.h>
#include "math.h"
using namespace ::testing;
DECLARE_FUNCTION_MOCK2(AddFunctionMock, add, int(int, int));
DECLARE_FUNCTION_MOCK1(NegateFunctionMock, negate, int(int));
IMPLEMENT_FUNCTION_MOCK2(AddFunctionMock, add, int(int, int));
IMPLEMENT_FUNCTION_MOCK1(NegateFunctionMock, negate, int(int));
/**
* Function add is mocked as long as AddFunctionMock instance exists.
* Once mock function is destroyed, a call directs to a real function.
*/
TEST(FunctionMockersTest, MocksFunctionAsLongAsMockInstanceExists) {
{
AddFunctionMock mock;
EXPECT_FUNCTION_CALL(mock, (1, 2)).WillOnce(Return(12));
ASSERT_EQ(12, add(1, 2));
}
ASSERT_EQ(3, add(1, 2));
}
/**
* real static mock class field holds pointer to a real function.
*/
TEST(FunctionMockersTest, FunctionMockExportsRealFunctionPointer) {
EXPECT_EQ(3, AddFunctionMock::real(1, 2));
}
/**
* Function negate doesn't exist, but can be mocked.
*/
TEST(FunctionMockersTest, ThrowsExceptionIfRealFunctionNotFound)
{
{
NegateFunctionMock mock;
EXPECT_FUNCTION_CALL(mock, (9)).WillOnce(Return(3));
ASSERT_EQ(3, negate(9));
}
EXPECT_THROW(negate(9), std::logic_error);
}

View File

@@ -0,0 +1,17 @@
#include <cmock/cmock.h>
#include "math.h"
DECLARE_FUNCTION_MOCK2(AddFunctionMock, add, int(int, int));
IMPLEMENT_FUNCTION_MOCK2(AddFunctionMock, add, int(int, int));
TEST(SpecBuildersTest, ExpectFunctionCallCompiles) {
AddFunctionMock mock;
EXPECT_FUNCTION_CALL(mock, (1, 2)).Times(0);
}
TEST(SpecBuildersTest, OnFunctionCallCompiles) {
AddFunctionMock mock;
ON_FUNCTION_CALL(mock, (1, 2));
}

11
external/C-Mock/test/math.c vendored Normal file
View File

@@ -0,0 +1,11 @@
#include "math.h"
int add(int a1, int a2)
{
return a1 + a2;
}
int substract(int a1, int a2)
{
return a1 - a2;
}

18
external/C-Mock/test/math.h vendored Normal file
View File

@@ -0,0 +1,18 @@
#ifndef CMOCK_TEST_MATH_H_
#define CMOCK_TEST_MATH_H_
#ifdef __cplusplus
extern "C" {
#endif
int add(int a1, int a2);
int substract(int a1, int a2);
/* This function isn't implemented, but still can be mocked. */
int negate(int n);
#ifdef __cplusplus
}
#endif
#endif /* CMOCK_TEST_MATH_H_ */