mirror of
https://github.com/openappsec/openappsec.git
synced 2025-09-29 11:16:30 +03:00
First release of open-appsec source code
This commit is contained in:
36
core/include/general/cptest/cptest_basic.h
Normal file
36
core/include/general/cptest/cptest_basic.h
Normal file
@@ -0,0 +1,36 @@
|
||||
// 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.
|
||||
|
||||
#ifndef __CP_TEST_BASIC_H__
|
||||
#define __CP_TEST_BASIC_H__
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "gmock/gmock.h"
|
||||
|
||||
// Before EXPECT_DEATH, call this to do all necessary preparations.
|
||||
void cptestPrepareToDie();
|
||||
|
||||
// Path to a file in the UT directory
|
||||
std::string cptestFnameInExeDir(const std::string &name);
|
||||
std::string cptestFnameInSrcDir(const std::string &name);
|
||||
|
||||
ACTION_TEMPLATE(
|
||||
SaveVoidArgPointee,
|
||||
HAS_2_TEMPLATE_PARAMS(int, k, typename, T),
|
||||
AND_1_VALUE_PARAMS(output)
|
||||
)
|
||||
{
|
||||
*output = *static_cast<T *>(::testing::get<k>(args));
|
||||
}
|
||||
|
||||
#endif // __CP_TEST_BASIC_H__
|
37
core/include/general/cptest/cptest_file.h
Normal file
37
core/include/general/cptest/cptest_file.h
Normal file
@@ -0,0 +1,37 @@
|
||||
// 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.
|
||||
|
||||
#ifndef __CPTEST_FILE_H__
|
||||
#define __CPTEST_FILE_H__
|
||||
|
||||
// Create a temporary file with some content. Delete on destruction.
|
||||
class CPTestTempfile
|
||||
{
|
||||
public:
|
||||
explicit CPTestTempfile(const std::vector<std::string> &lines);
|
||||
explicit CPTestTempfile();
|
||||
~CPTestTempfile();
|
||||
|
||||
std::string readFile() const;
|
||||
|
||||
std::string fname;
|
||||
|
||||
// Not copiable (would delete the file twice), but movable
|
||||
CPTestTempfile(const CPTestTempfile &other) = delete;
|
||||
CPTestTempfile(CPTestTempfile &&other) = default;
|
||||
CPTestTempfile & operator=(const CPTestTempfile &other) = delete;
|
||||
CPTestTempfile & operator=(CPTestTempfile &&other) = default;
|
||||
};
|
||||
|
||||
|
||||
#endif // __CPTEST_FILE_H__
|
164
core/include/general/cptest/cptest_maybe.h
Normal file
164
core/include/general/cptest/cptest_maybe.h
Normal file
@@ -0,0 +1,164 @@
|
||||
// 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.
|
||||
|
||||
#ifndef __CPTEST_MAYBE_H__
|
||||
#define __CPTEST_MAYBE_H__
|
||||
|
||||
#include "maybe_res.h"
|
||||
#include "gmock/gmock.h"
|
||||
|
||||
namespace testing {
|
||||
|
||||
namespace maybe_matcher {
|
||||
|
||||
// Matchers for Maybe<T> objects.
|
||||
// Usage examples:
|
||||
// Maybe<int> m = ...;
|
||||
// EXPECT_THAT(m, IsValue(3)); // Must hold 3
|
||||
// EXPECT_THAT(m, IsValue(_)); // Must be a value
|
||||
// EXPECT_THAT(m, IsError("HA")); // Must be an error with specific text
|
||||
// EXPECT_THAT(m, IsError(_)); // Any error (but not a value)
|
||||
|
||||
//
|
||||
// Generic classes that handle either the value or the error
|
||||
//
|
||||
|
||||
// Matcher for Maybe values.
|
||||
// Verify that Maybe<T>::ok() is as expected, and runs a matcher on the internal value or error.
|
||||
//
|
||||
// Abstract base class, inherited to match either the internal value or the error value.
|
||||
// Template parameters:
|
||||
// MaybeType - the Maybe type, possibly with "const &".
|
||||
// GetInternal - gets the internal value or error from Maybe.
|
||||
template <typename MaybeType, typename GetInternal>
|
||||
class BaseMatcher : public MatcherInterface<MaybeType>
|
||||
{
|
||||
private:
|
||||
using InternalValue = decltype(GetInternal::get(std::declval<MaybeType>()));
|
||||
|
||||
public:
|
||||
BaseMatcher(const Matcher<InternalValue> &_matcher)
|
||||
:
|
||||
label(GetInternal::expected_ok ? "Value" : "Error"),
|
||||
matcher(_matcher)
|
||||
{
|
||||
}
|
||||
|
||||
bool
|
||||
MatchAndExplain(MaybeType m, MatchResultListener *listener) const override {
|
||||
if (m.ok() != GetInternal::expected_ok) return false;
|
||||
return matcher.MatchAndExplain(GetInternal::get(m), listener);
|
||||
}
|
||||
|
||||
// LCOV_EXCL_START - Only called when a test fails, to explain why.
|
||||
void
|
||||
DescribeTo(::std::ostream *os) const override {
|
||||
*os << label << "(";
|
||||
matcher.DescribeTo(os);
|
||||
*os << ")";
|
||||
}
|
||||
|
||||
void
|
||||
DescribeNegationTo(::std::ostream *os) const override {
|
||||
*os << label << "(";
|
||||
matcher.DescribeNegationTo(os);
|
||||
*os << ")";
|
||||
}
|
||||
// LCOV_EXCL_STOP
|
||||
|
||||
private:
|
||||
std::string label;
|
||||
Matcher<InternalValue> matcher;
|
||||
};
|
||||
|
||||
// Temporary matcher for Maybe values - converted to BaseMatcher when needed.
|
||||
// Converts any matcher to a Maybe matcher, which invokes the internal matcher on the value or error.
|
||||
// Template parameters:
|
||||
// InternalMatcherType - Any matcher type that matches the value/error.
|
||||
// InternalValueGetter - gets the internal value or error from Maybe.
|
||||
template <typename InternalMatcherType, typename InternalValueGetter>
|
||||
class TempMatcher
|
||||
{
|
||||
public:
|
||||
TempMatcher(InternalMatcherType _matcher) : matcher(_matcher) {}
|
||||
|
||||
// The internal type becomes known when this object is cast to a specific
|
||||
// matcher type. Create a Maybe matcher, while casting the internal matcher to the
|
||||
// type that we now know.
|
||||
template <typename MaybeType>
|
||||
operator Matcher<MaybeType>() const
|
||||
{
|
||||
using MaybeMatcherType = BaseMatcher<MaybeType, InternalValueGetter>;
|
||||
return MakeMatcher(new MaybeMatcherType(matcher));
|
||||
}
|
||||
|
||||
private:
|
||||
InternalMatcherType matcher;
|
||||
};
|
||||
|
||||
//
|
||||
// Classes to get the internal value from a Maybe object.
|
||||
// The internal value can be either the value or the error.
|
||||
//
|
||||
|
||||
class GetValue
|
||||
{
|
||||
public:
|
||||
static const bool expected_ok = true;
|
||||
|
||||
template<typename T, typename TErr>
|
||||
static T
|
||||
get(const Maybe<T, TErr> &m)
|
||||
{
|
||||
return m.unpack();
|
||||
}
|
||||
};
|
||||
|
||||
class GetError
|
||||
{
|
||||
public:
|
||||
static const bool expected_ok = false;
|
||||
|
||||
template<typename T, typename TErr>
|
||||
static TErr
|
||||
get(const Maybe<T, TErr> &m)
|
||||
{
|
||||
return m.getErr();
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace maybe_matcher
|
||||
|
||||
//
|
||||
// Functions to return matchers - to be used by test code.
|
||||
//
|
||||
|
||||
// Convert Matcher<T> to Matcher<Maybe<T>>, which verifies that it's ok and matches the value.
|
||||
template<typename MatcherType>
|
||||
static inline ::testing::maybe_matcher::TempMatcher<MatcherType, ::testing::maybe_matcher::GetValue>
|
||||
IsValue(MatcherType matcher)
|
||||
{
|
||||
return ::testing::maybe_matcher::TempMatcher<MatcherType, ::testing::maybe_matcher::GetValue>(matcher);
|
||||
}
|
||||
|
||||
// Convert Matcher<TErr> to Matcher<Maybe<T, TErr>>, which verifies that it's not ok and matches the error.
|
||||
template<typename MatcherType>
|
||||
static inline ::testing::maybe_matcher::TempMatcher<MatcherType, ::testing::maybe_matcher::GetError>
|
||||
IsError(MatcherType matcher)
|
||||
{
|
||||
return ::testing::maybe_matcher::TempMatcher<MatcherType, ::testing::maybe_matcher::GetError>(matcher);
|
||||
}
|
||||
|
||||
} // namespace testing
|
||||
|
||||
#endif // __CPTEST_MAYBE_H__
|
27
core/include/general/cptest/cptest_singleton.h
Normal file
27
core/include/general/cptest/cptest_singleton.h
Normal file
@@ -0,0 +1,27 @@
|
||||
// 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.
|
||||
|
||||
#ifndef __CP_TEST_SINGLETON_H__
|
||||
#define __CP_TEST_SINGLETON_H__
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "gmock/gmock.h"
|
||||
#include "singleton.h"
|
||||
|
||||
// Mock objects should use Singleton::Provide<I_Face>::From<MockProvider> with the interface they mock.
|
||||
template<typename I_Face>
|
||||
class MockProvider : Singleton::Provide<I_Face>
|
||||
{};
|
||||
|
||||
#endif // __CP_TEST_SINGLETON_H__
|
||||
|
88
core/include/general/cptest/cptest_tcppacket.h
Executable file
88
core/include/general/cptest/cptest_tcppacket.h
Executable file
@@ -0,0 +1,88 @@
|
||||
// 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.
|
||||
|
||||
#ifndef __CPTEST_TCPPACKET_H__
|
||||
#define __CPTEST_TCPPACKET_H__
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include "packet.h"
|
||||
|
||||
// Generate TCP options
|
||||
class TCPOption
|
||||
{
|
||||
public:
|
||||
explicit TCPOption(const std::string &_name, const std::vector<u_char> _data);
|
||||
TCPOption(const TCPOption &from);
|
||||
~TCPOption();
|
||||
|
||||
// Accessors
|
||||
size_t size() const;
|
||||
std::vector<u_char> build() const;
|
||||
|
||||
// Well-known options - simple ones are constants, complex are are static functions
|
||||
static const TCPOption NOP;
|
||||
static const TCPOption SACK_PERMITTED;
|
||||
static TCPOption windowScaling(u_char shift_count);
|
||||
static TCPOption timeStamp(uint value, uint echo_reply);
|
||||
static TCPOption selectiveACK(const std::vector<std::pair<uint, uint>> &edges);
|
||||
|
||||
private:
|
||||
class Impl;
|
||||
std::unique_ptr<Impl> pimpl;
|
||||
};
|
||||
|
||||
class TCPPacket
|
||||
{
|
||||
public:
|
||||
// Build an empty packet
|
||||
explicit TCPPacket(CDir _cdir);
|
||||
~TCPPacket();
|
||||
|
||||
// Movable, not copiable
|
||||
TCPPacket(TCPPacket &&from);
|
||||
TCPPacket(const TCPPacket &from) = delete;
|
||||
|
||||
// Methods to set TCP properties. Return reference to this, to allow chaining
|
||||
TCPPacket & setTCPPayload(const std::vector<u_char> &payload);
|
||||
TCPPacket & setTCPPayload(const std::string &payload);
|
||||
TCPPacket & setTCPSeq(uint _tcp_seq);
|
||||
TCPPacket & setTCPAck(uint _tcp_ack);
|
||||
TCPPacket & setTCPWindow(uint16_t _tcp_window);
|
||||
TCPPacket & setTCPFlags(std::string _tcp_flags);
|
||||
TCPPacket & setTCPUrgentPtr(uint16_t _tcp_urgent_ptr);
|
||||
TCPPacket & setTCPCksum(uint _tcp_cksum_override);
|
||||
TCPPacket & setL2Header(const std::vector<u_char> &_l2_header);
|
||||
TCPPacket & addTCPOption(const TCPOption &tcp_option);
|
||||
TCPPacket & setL4HeaderSize(uint header_size);
|
||||
TCPPacket & setL4DataOffset(uint data_offset);
|
||||
|
||||
TCPPacket && move() { return std::move(*this); }
|
||||
|
||||
// Build a Packet
|
||||
std::unique_ptr<Packet> build(const ConnKey &ck) const;
|
||||
|
||||
// Get the TCP sequence
|
||||
uint getTCPSeq() const;
|
||||
|
||||
static uint16_t calcTCPv4Checksum(const std::vector<u_char> &pkt);
|
||||
static uint16_t calcTCPv6Checksum(const std::vector<u_char> &pkt);
|
||||
static uint16_t calcIPv4Checksum(const std::vector<u_char> &pkt);
|
||||
|
||||
private:
|
||||
class Impl;
|
||||
std::unique_ptr<Impl> pimpl;
|
||||
};
|
||||
|
||||
#endif // __CPTEST_TCPPACKET_H__
|
Reference in New Issue
Block a user