diff --git a/Chaos/Padding/PadPkcs7.hpp b/Chaos/Padding/PadPkcs7.hpp new file mode 100644 index 0000000..4d8548d --- /dev/null +++ b/Chaos/Padding/PadPkcs7.hpp @@ -0,0 +1,37 @@ +#ifndef CHAOS_PADDING_PADPKCS7_HPP +#define CHAOS_PADDING_PADPKCS7_HPP + +#include +#include +#include + +#include "Service/ChaosException.hpp" + +namespace Chaos::Padding +{ + +class PadPkcs7 +{ +public: + template + static void Pad(OutputIt begin, OutputIt end) + { + auto dist = std::distance(begin, end); + + if (dist >= 0 && dist <= std::numeric_limits::max()) + { + for (OutputIt it = begin; it != end; ++it) + { + *it = static_cast(dist); + } + } + else + { + throw Service::ChaosException("PadPkcs7::Pad(): invalid range"); + } + } +}; + +} // namespace Chaos::Padding + +#endif // CHAOS_PADDING_PADPKCS7_HPP diff --git a/ChaosTests/CMakeLists.txt b/ChaosTests/CMakeLists.txt index 1aa05df..3413aae 100644 --- a/ChaosTests/CMakeLists.txt +++ b/ChaosTests/CMakeLists.txt @@ -18,6 +18,7 @@ set(ChaosTests_SOURCE Hash/Md4HasherTests.cpp Cipher/Arc4GenTests.cpp Cipher/Arc4CryptTests.cpp Cipher/DesCryptTests.cpp + Padding/PadPkcs7Tests.cpp Service/SeArrayTests.cpp Service/ChaosExceptionTests.cpp) diff --git a/ChaosTests/Padding/PadPkcs7Tests.cpp b/ChaosTests/Padding/PadPkcs7Tests.cpp new file mode 100644 index 0000000..b511460 --- /dev/null +++ b/ChaosTests/Padding/PadPkcs7Tests.cpp @@ -0,0 +1,121 @@ +#include +#include +#include +#include + +#include "Padding/PadPkcs7.hpp" +#include "Service/ChaosException.hpp" + +using namespace Chaos::Padding; + +TEST(PadPkcs7Tests, PadTest) +{ + { + std::array fact = {}; + std::array expected = { 0x01 }; + + PadPkcs7::Pad(fact.begin(), fact.end()); + ASSERT_EQ(expected, fact); + } + + { + std::array fact = {}; + std::array expected = + { + 0x07, 0x07, 0x07, 0x07, 0x07, + 0x07, 0x07 + }; + + PadPkcs7::Pad(fact.begin(), fact.end()); + ASSERT_EQ(expected, fact); + } + + { + std::array fact = {}; + std::array expected = + { + 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, + 0x0a, 0x0a, 0x0a, 0x0a, 0x0a + }; + + PadPkcs7::Pad(fact.begin(), fact.end()); + ASSERT_EQ(expected, fact); + } + + for (int i = 0; i < 256; ++i) + { + std::vector fact(i, 0x00); + + PadPkcs7::Pad(fact.begin(), fact.end()); + ASSERT_EQ(std::vector(i, i), fact); + } +} + +TEST(PadPkcs7Tests, PadInvalidRangeTest) +{ + { + std::array out = {}; + + ASSERT_THROW(PadPkcs7::Pad(out.begin(), out.end()), Chaos::Service::ChaosException); + } + + { + std::array out = {}; + + ASSERT_THROW(PadPkcs7::Pad(out.begin(), out.end()), Chaos::Service::ChaosException); + } + + { + std::array out = {}; + + ASSERT_THROW(PadPkcs7::Pad(out.end(), out.begin()), Chaos::Service::ChaosException); + } +} + +TEST(PadPkcs7Tests, PadOutIteratorUsageTest) +{ + { + std::array fact = {}; + std::array 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 + }; + + PadPkcs7::Pad(fact.begin() + 3, fact.end() - 3); + ASSERT_EQ(expected, fact); + } + + { + std::array fact = {}; + std::array 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 + }; + + PadPkcs7::Pad(fact.begin() + 3, fact.end() - 3); + ASSERT_EQ(expected, fact); + } + + { + std::array fact = + { + 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, + 0xbb, 0xbb, 0xbb, 0xbb, 0xbb + }; + std::array expected = + { + 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, + 0xbb, 0xbb, 0xbb, 0xbb, 0xbb + }; + + PadPkcs7::Pad(fact.begin() + 5, fact.begin() + 5); + ASSERT_EQ(expected, fact); + } +}