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,3 @@
add_library(singleton singleton.cc)
add_subdirectory(singleton_ut)

View File

@@ -0,0 +1,44 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "singleton.h"
#include "debug.h"
void
Singleton::registerSingleton(std::type_index type, void *ptr)
{
singles[type].insert(ptr);
}
void
Singleton::unregisterSingleton(std::type_index type, void *ptr)
{
singles[type].erase(ptr);
}
void *
Singleton::get(const std::type_index &type)
{
dbgAssert(singles[type].size() == 1) << "There is no single element from type '" << type.name() << "', "
"number of elements is " << singles[type].size();
return *(singles[type].begin());
}
bool
Singleton::exists(const std::type_index &type)
{
return singles[type].size() != 0;
}
std::map<std::type_index, std::set<void *>> Singleton::singles;
std::map<std::type_index, std::unique_ptr<Singleton::OwnedSingleton>> Singleton::owned_singles;

View File

@@ -0,0 +1,5 @@
add_unit_test(
singleton_ut
"singleton_ut.cc"
"singleton;rest;environment;metric;event_is;-lboost_regex"
)

View File

@@ -0,0 +1,209 @@
#include "singleton.h"
#include "cptest.h"
#include "config.h"
#include "config_component.h"
using namespace std;
using namespace testing;
class Example : public Singleton::Provide<Example>::Self
{
};
TEST(Singleton, NoObject)
{
::Environment env;
ConfigComponent conf;
cptestPrepareToDie();
EXPECT_DEATH(Singleton::Consume<Example>::from<Example>(), "There is no single element from type '.*");
}
TEST(Singleton, HasObject)
{
Example my_example;
EXPECT_EQ(&my_example, Singleton::Consume<Example>::from<Example>());
}
TEST(Singleton, WasObject)
{
cptestPrepareToDie();
{
Example my_example;
EXPECT_EQ(&my_example, Singleton::Consume<Example>::from<Example>());
}
EXPECT_DEATH(Singleton::Consume<Example>::from<Example>(), "There is no single element from type '.*");
}
TEST(Singleton, CheckForObject)
{
EXPECT_FALSE(Singleton::exists<Example>());
{
Example my_example;
EXPECT_TRUE(Singleton::exists<Example>());
}
EXPECT_FALSE(Singleton::exists<Example>());
}
TEST(Singleton, DeathOnDoubleObject)
{
cptestPrepareToDie();
Example my_example;
EXPECT_EQ(&my_example, Singleton::Consume<Example>::from<Example>());
Example another_example;
EXPECT_DEATH(Singleton::Consume<Example>::from<Example>(), "There is no single element from type '.*");
}
TEST(Singleton, ReturnToSingleObject)
{
Example my_example;
{
// Temporary object
Example another_example;
}
EXPECT_EQ(&my_example, Singleton::Consume<Example>::from<Example>());
}
class I_Interface
{
public:
virtual int doSomething() = 0;
};
class ExampleInterface : public Singleton::Provide<I_Interface>::SelfInterface
{
public:
int doSomething() override { return 5; }
};
class ExampleUser : public Singleton::Consume<I_Interface>
{
};
TEST(Singleton, HasInterfaceObject)
{
ExampleInterface myExample;
I_Interface *ptr = &myExample;
EXPECT_EQ(Singleton::Consume<I_Interface>::from<MockProvider<I_Interface>>(), ptr);
EXPECT_EQ((getInterface<ExampleInterface, I_Interface>()), ptr);
EXPECT_EQ((getInterface<ExampleUser, I_Interface>()), ptr);
}
class I_AnotherInterface
{
public:
virtual bool checkSomething() = 0;
};
class ExampleOwned : public I_AnotherInterface, public Singleton::OwnedSingleton
{
public:
bool checkSomething() override { return true; }
};
TEST(Singleton, CheckForOwnedObject)
{
EXPECT_FALSE(Singleton::existsOwned<ExampleOwned>());
Singleton::newOwned<ExampleOwned>();
EXPECT_TRUE(Singleton::existsOwned<ExampleOwned>());
Singleton::deleteOwned<ExampleOwned>();
EXPECT_FALSE(Singleton::existsOwned<ExampleOwned>());
}
class MockExampleOwned : public ExampleOwned
{
public:
MOCK_METHOD0(checkSomething, bool());
};
TEST(Singleton, MockOwnedObject)
{
auto ptr = make_unique<MockExampleOwned>();
auto real_obj = ptr.get();
EXPECT_FALSE(Singleton::existsOwned<ExampleOwned>());
Singleton::setOwned<ExampleOwned>(move(ptr));
EXPECT_TRUE(Singleton::existsOwned<ExampleOwned>());
EXPECT_CALL(*real_obj, checkSomething());
auto single_obj = Singleton::getOwned<ExampleOwned>();
single_obj->checkSomething();
Singleton::deleteOwned<ExampleOwned>();
EXPECT_FALSE(Singleton::existsOwned<ExampleOwned>());
}
// Fixture to test provide/consume methods
// Contains a couple of dummy components, one provides an interface and one consumes it.
class SingletonCompTest : public Test
{
public:
class I_Example {};
class Provider;
class Consumer;
};
class SingletonCompTest::Provider : Singleton::Provide<I_Example>
{
public:
Provider() : pimpl(make_unique<Impl>()) {}
~Provider() {}
private:
class Impl;
unique_ptr<Impl> pimpl;
};
class SingletonCompTest::Provider::Impl
:
Singleton::Provide<I_Example>::From<Provider>
{
};
class SingletonCompTest::Consumer : Singleton::Consume<I_Example>
{
public:
Consumer() : pimpl(make_unique<Impl>()) {}
~Consumer() {}
private:
class Impl;
unique_ptr<Impl> pimpl;
};
class SingletonCompTest::Consumer::Impl
{
public:
Impl() : p(Singleton::Consume<I_Example>::by<Consumer>()) {}
I_Example *p;
};
TEST_F(SingletonCompTest, provide)
{
EXPECT_FALSE(Singleton::exists<I_Example>());
{
// Instantiate Provider to register a singleton
Provider pro;
EXPECT_TRUE(Singleton::exists<I_Example>());
}
EXPECT_FALSE(Singleton::exists<I_Example>());
}
TEST_F(SingletonCompTest, consume)
{
Provider pro;
// Consume the interface. Just see that it compiles and doesn't crash.
Consumer con;
}
TEST_F(SingletonCompTest, consumeFrom)
{
Provider pro;
EXPECT_NE(nullptr, Singleton::Consume<I_Example>::from(pro));
}