Merge branch 'pad'
Chaos Ci / test-and-benchmark (push) Successful in 2m39s

This commit is contained in:
hashlag
2026-06-19 01:09:50 +03:00
6 changed files with 381 additions and 0 deletions
+34
View File
@@ -0,0 +1,34 @@
#ifndef CHAOS_PADDING_PADDER_HPP
#define CHAOS_PADDING_PADDER_HPP
namespace Chaos::Padding
{
template<typename T>
class Padder
{
public:
template<typename OutputIt>
void Pad(OutputIt begin, OutputIt end) const
{
Impl().Pad(begin, end);
}
protected:
Padder() = default;
private:
const T & Impl() const
{
return static_cast<const T &>(*this);
}
T & Impl()
{
return static_cast<T &>(*this);
}
};
} // namespace Chaos::Padding
#endif // CHAOS_PADDING_PADDER_HPP
+33
View File
@@ -0,0 +1,33 @@
#ifndef CHAOS_PADDING_PADDERISO7816_HPP
#define CHAOS_PADDING_PADDERISO7816_HPP
#include <cstdint>
#include "Padding/Padder.hpp"
namespace Chaos::Padding
{
class PadderIso7816 : public Padder<PadderIso7816>
{
public:
template<typename OutputIt>
static void Pad(OutputIt begin, OutputIt end)
{
OutputIt it = begin;
if (it != end)
{
*it++ = static_cast<uint8_t>(0x80);
for (; it != end; ++it)
{
*it = 0;
}
}
}
};
} // namespace Chaos::Padding
#endif // CHAOS_PADDING_PADDERISO7816_HPP
+38
View File
@@ -0,0 +1,38 @@
#ifndef CHAOS_PADDING_PADDERPKCS7_HPP
#define CHAOS_PADDING_PADDERPKCS7_HPP
#include <cstdint>
#include <iterator>
#include <limits>
#include "Padding/Padder.hpp"
#include "Service/ChaosException.hpp"
namespace Chaos::Padding
{
class PadderPkcs7 : public Padder<PadderPkcs7>
{
public:
template<typename OutputIt>
static void Pad(OutputIt begin, OutputIt end)
{
auto dist = std::distance(begin, end);
if (dist >= 0 && dist <= std::numeric_limits<uint8_t>::max())
{
for (OutputIt it = begin; it != end; ++it)
{
*it = static_cast<uint8_t>(dist);
}
}
else
{
throw Service::ChaosException("PadderPkcs7::Pad(): invalid range");
}
}
};
} // namespace Chaos::Padding
#endif // CHAOS_PADDING_PADDERPKCS7_HPP
+2
View File
@@ -18,6 +18,8 @@ set(ChaosTests_SOURCE Hash/Md4HasherTests.cpp
Cipher/Arc4GenTests.cpp
Cipher/Arc4CryptTests.cpp
Cipher/DesCryptTests.cpp
Padding/PadderPkcs7Tests.cpp
Padding/PadderIso7816Tests.cpp
Service/SeArrayTests.cpp
Service/ChaosExceptionTests.cpp)
+131
View File
@@ -0,0 +1,131 @@
#include <gtest/gtest.h>
#include <array>
#include <cstdint>
#include <vector>
#include "Padding/PadderIso7816.hpp"
using namespace Chaos::Padding;
TEST(PadIso7816Tests, PadTest)
{
{
std::array<uint8_t, 1> fact = {};
std::array<uint8_t, 1> expected = { 0x80 };
PadderIso7816::Pad(fact.begin(), fact.end());
ASSERT_EQ(expected, fact);
}
{
std::array<uint8_t, 7> fact = {};
std::array<uint8_t, 7> expected =
{
0x80, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00
};
PadderIso7816::Pad(fact.begin(), fact.end());
ASSERT_EQ(expected, fact);
}
{
std::array<uint8_t, 10> fact = {};
std::array<uint8_t, 10> expected =
{
0x80, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00
};
PadderIso7816::Pad(fact.begin(), fact.end());
ASSERT_EQ(expected, fact);
}
for (int i = 0; i < 256; ++i)
{
std::vector<uint8_t> fact(i, 0xff);
PadderIso7816::Pad(fact.begin(), fact.end());
for (int j = 0; j < i; ++j)
{
ASSERT_EQ(j == 0 ? 0x80 : 0x00, fact[j]);
}
}
}
TEST(PadIso7816Tests, PadOutIteratorUsageTest)
{
{
std::array<uint8_t, 28> fact;
fact.fill(0xff);
std::array<uint8_t, 28> expected =
{
0xff, 0xff, 0xff,
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xff, 0xff, 0xff
};
PadderIso7816::Pad(fact.begin() + 3, fact.end() - 3);
ASSERT_EQ(expected, fact);
}
{
std::array<uint8_t, 39> fact;
fact.fill(0xff);
std::array<uint8_t, 39> expected =
{
0xff, 0xff, 0xff,
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xff, 0xff, 0xff
};
PadderIso7816::Pad(fact.begin() + 3, fact.end() - 3);
ASSERT_EQ(expected, fact);
}
{
std::array<uint8_t, 10> fact =
{
0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
0xbb, 0xbb, 0xbb, 0xbb, 0xbb
};
std::array<uint8_t, 10> expected =
{
0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
0xbb, 0xbb, 0xbb, 0xbb, 0xbb
};
PadderIso7816::Pad(fact.begin() + 5, fact.begin() + 5);
ASSERT_EQ(expected, fact);
}
}
template<typename Impl, typename OutputIt>
void PadThroughBase(const Padder<Impl> & padder, OutputIt begin, OutputIt end)
{
padder.Pad(begin, end);
}
TEST(PadIso7816Tests, PadThroughBaseTest)
{
{
std::array<uint8_t, 5> fact;
fact.fill(0xff);
std::array<uint8_t, 5> expected =
{
0x80, 0x00, 0x00, 0x00, 0x00
};
const PadderIso7816 padder;
PadThroughBase(padder, fact.begin(), fact.end());
ASSERT_EQ(expected, fact);
}
}
+143
View File
@@ -0,0 +1,143 @@
#include <gtest/gtest.h>
#include <array>
#include <cstdint>
#include <vector>
#include "Padding/PadderPkcs7.hpp"
#include "Service/ChaosException.hpp"
using namespace Chaos::Padding;
TEST(PadPkcs7Tests, PadTest)
{
{
std::array<uint8_t, 1> fact = {};
std::array<uint8_t, 1> expected = { 0x01 };
PadderPkcs7::Pad(fact.begin(), fact.end());
ASSERT_EQ(expected, fact);
}
{
std::array<uint8_t, 7> fact = {};
std::array<uint8_t, 7> expected =
{
0x07, 0x07, 0x07, 0x07, 0x07,
0x07, 0x07
};
PadderPkcs7::Pad(fact.begin(), fact.end());
ASSERT_EQ(expected, fact);
}
{
std::array<uint8_t, 10> fact = {};
std::array<uint8_t, 10> expected =
{
0x0a, 0x0a, 0x0a, 0x0a, 0x0a,
0x0a, 0x0a, 0x0a, 0x0a, 0x0a
};
PadderPkcs7::Pad(fact.begin(), fact.end());
ASSERT_EQ(expected, fact);
}
for (int i = 0; i < 256; ++i)
{
std::vector<uint8_t> fact(i, 0x00);
PadderPkcs7::Pad(fact.begin(), fact.end());
ASSERT_EQ(std::vector<uint8_t>(i, i), fact);
}
}
TEST(PadPkcs7Tests, PadInvalidRangeTest)
{
{
std::array<uint8_t, 256> out = {};
ASSERT_THROW(PadderPkcs7::Pad(out.begin(), out.end()), Chaos::Service::ChaosException);
}
{
std::array<uint8_t, 500> out = {};
ASSERT_THROW(PadderPkcs7::Pad(out.begin(), out.end()), Chaos::Service::ChaosException);
}
{
std::array<uint8_t, 50> out = {};
ASSERT_THROW(PadderPkcs7::Pad(out.end(), out.begin()), Chaos::Service::ChaosException);
}
}
TEST(PadPkcs7Tests, PadOutIteratorUsageTest)
{
{
std::array<uint8_t, 28> fact = {};
std::array<uint8_t, 28> expected =
{
0x00, 0x00, 0x00,
0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16,
0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16,
0x00, 0x00, 0x00
};
PadderPkcs7::Pad(fact.begin() + 3, fact.end() - 3);
ASSERT_EQ(expected, fact);
}
{
std::array<uint8_t, 39> fact = {};
std::array<uint8_t, 39> expected =
{
0x00, 0x00, 0x00,
0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21,
0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21,
0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21,
0x00, 0x00, 0x00
};
PadderPkcs7::Pad(fact.begin() + 3, fact.end() - 3);
ASSERT_EQ(expected, fact);
}
{
std::array<uint8_t, 10> fact =
{
0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
0xbb, 0xbb, 0xbb, 0xbb, 0xbb
};
std::array<uint8_t, 10> expected =
{
0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
0xbb, 0xbb, 0xbb, 0xbb, 0xbb
};
PadderPkcs7::Pad(fact.begin() + 5, fact.begin() + 5);
ASSERT_EQ(expected, fact);
}
}
template<typename Impl, typename OutputIt>
void PadThroughBase(const Padder<Impl> & padder, OutputIt begin, OutputIt end)
{
padder.Pad(begin, end);
}
TEST(PadPkcs7Tests, PadThroughBaseTest)
{
{
std::array<uint8_t, 5> fact = {};
std::array<uint8_t, 5> expected =
{
0x05, 0x05, 0x05, 0x05, 0x05
};
const PadderPkcs7 padder;
PadThroughBase(padder, fact.begin(), fact.end());
ASSERT_EQ(expected, fact);
}
}