|
| 1 | +/** |
| 2 | + * ----------------------------------------------------------------------------- |
| 3 | + * Project: Fossil Logic |
| 4 | + * |
| 5 | + * This file is part of the Fossil Logic project, which aims to develop |
| 6 | + * high-performance, cross-platform applications and libraries. The code |
| 7 | + * contained herein is licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. You may obtain |
| 9 | + * a copy of the License at: |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 16 | + * License for the specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + * |
| 19 | + * Author: Michael Gene Brockus (Dreamer) |
| 20 | + * Date: 04/05/2014 |
| 21 | + * |
| 22 | + * Copyright (C) 2014-2025 Fossil Logic. All rights reserved. |
| 23 | + * ----------------------------------------------------------------------------- |
| 24 | + */ |
| 25 | +#include <fossil/pizza/framework.h> |
| 26 | +#include "fossil/cryptic/framework.h" |
| 27 | + |
| 28 | + |
| 29 | +// * * * * * * * * * * * * * * * * * * * * * * * * |
| 30 | +// * Fossil Logic Test Utilities |
| 31 | +// * * * * * * * * * * * * * * * * * * * * * * * * |
| 32 | +// Setup steps for things like test fixtures and |
| 33 | +// mock objects are set here. |
| 34 | +// * * * * * * * * * * * * * * * * * * * * * * * * |
| 35 | + |
| 36 | +FOSSIL_TEST_SUITE(cpp_cipher_fixture); |
| 37 | + |
| 38 | +FOSSIL_SETUP(cpp_cipher_fixture) { |
| 39 | + // Setup the test fixture |
| 40 | +} |
| 41 | + |
| 42 | +FOSSIL_TEARDOWN(cpp_cipher_fixture) { |
| 43 | + // Teardown the test fixture |
| 44 | +} |
| 45 | + |
| 46 | +// * * * * * * * * * * * * * * * * * * * * * * * * |
| 47 | +// * Fossil Logic Test Cases |
| 48 | +// * * * * * * * * * * * * * * * * * * * * * * * * |
| 49 | +// The test cases below are provided as samples, inspired |
| 50 | +// by the Meson build system's approach of using test cases |
| 51 | +// as samples for library usage. |
| 52 | +// * * * * * * * * * * * * * * * * * * * * * * * * |
| 53 | + |
| 54 | +FOSSIL_TEST_CASE(cpp_test_cipher_compute_basicpp_xor_encrypt_decrypt) { |
| 55 | + // Basic XOR cipher encryption and decryption using fossil::cryptic::Cipher |
| 56 | + const std::string algorithm = "xor"; |
| 57 | + const std::string mode_enc = "encrypt"; |
| 58 | + const std::string mode_dec = "decrypt"; |
| 59 | + const std::string bits = "u8"; |
| 60 | + const std::string key = "K"; |
| 61 | + const std::string plaintext = "hello"; |
| 62 | + |
| 63 | + auto ciphertext = fossil::cryptic::Cipher::compute( |
| 64 | + algorithm, mode_enc, bits, key, plaintext |
| 65 | + ); |
| 66 | + ASSUME_ITS_TRUE(ciphertext.size() == plaintext.size()); |
| 67 | + |
| 68 | + auto decrypted = fossil::cryptic::Cipher::compute( |
| 69 | + algorithm, mode_dec, bits, key, ciphertext |
| 70 | + ); |
| 71 | + ASSUME_ITS_TRUE(decrypted.size() == plaintext.size()); |
| 72 | + ASSUME_ITS_TRUE(memcmp(plaintext.data(), decrypted.data(), plaintext.size()) == 0); |
| 73 | +} |
| 74 | + |
| 75 | +FOSSIL_TEST_CASE(cpp_test_cipher_compute_null_arguments) { |
| 76 | + // Should fail with null arguments using fossil::cryptic::Cipher |
| 77 | + const std::string bits = "u8"; |
| 78 | + const std::string key = "key"; |
| 79 | + const std::string data = "data"; |
| 80 | + |
| 81 | + auto out1 = fossil::cryptic::Cipher::compute("", "encrypt", bits, key, data); |
| 82 | + ASSUME_ITS_TRUE(out1.empty()); |
| 83 | + |
| 84 | + auto out2 = fossil::cryptic::Cipher::compute("xor", "", bits, key, data); |
| 85 | + ASSUME_ITS_TRUE(out2.empty()); |
| 86 | + |
| 87 | + auto out3 = fossil::cryptic::Cipher::compute("xor", "encrypt", "", key, data); |
| 88 | + ASSUME_ITS_TRUE(out3.empty()); |
| 89 | + |
| 90 | + auto out4 = fossil::cryptic::Cipher::compute("xor", "encrypt", bits, "", data); |
| 91 | + ASSUME_ITS_TRUE(out4.empty()); |
| 92 | + |
| 93 | + auto out5 = fossil::cryptic::Cipher::compute("xor", "encrypt", bits, key, ""); |
| 94 | + ASSUME_ITS_TRUE(out5.empty()); |
| 95 | +} |
| 96 | + |
| 97 | +FOSSIL_TEST_CASE(cpp_test_cipher_compute_unsupported_algorithm) { |
| 98 | + // Should fail with unsupported algorithm using fossil::cryptic::Cipher |
| 99 | + const std::string algorithm = "unknown"; |
| 100 | + const std::string mode = "encrypt"; |
| 101 | + const std::string bits = "u8"; |
| 102 | + const std::string key = "key"; |
| 103 | + const std::string data = "data"; |
| 104 | + |
| 105 | + bool exception_thrown = false; |
| 106 | + try { |
| 107 | + auto out = fossil::cryptic::Cipher::compute(algorithm, mode, bits, key, data); |
| 108 | + } catch (const std::exception&) { |
| 109 | + exception_thrown = true; |
| 110 | + } |
| 111 | + ASSUME_ITS_TRUE(exception_thrown); |
| 112 | +} |
| 113 | + |
| 114 | +FOSSIL_TEST_CASE(cpp_test_cipher_compute_caesar_encrypt_decrypt) { |
| 115 | + // Caesar cipher encryption and decryption using fossil::cryptic::Cipher |
| 116 | + const std::string algorithm = "caesar"; |
| 117 | + const std::string mode_enc = "encrypt"; |
| 118 | + const std::string mode_dec = "decrypt"; |
| 119 | + const std::string bits = "u8"; |
| 120 | + const std::string key = "3"; |
| 121 | + const std::string plaintext = "abcXYZ"; |
| 122 | + |
| 123 | + auto ciphertext = fossil::cryptic::Cipher::compute( |
| 124 | + algorithm, mode_enc, bits, key, plaintext |
| 125 | + ); |
| 126 | + |
| 127 | + auto decrypted = fossil::cryptic::Cipher::compute( |
| 128 | + algorithm, mode_dec, bits, key, ciphertext |
| 129 | + ); |
| 130 | + ASSUME_ITS_TRUE(decrypted.size() == plaintext.size()); |
| 131 | + ASSUME_ITS_TRUE(memcmp(plaintext.data(), decrypted.data(), plaintext.size()) == 0); |
| 132 | +} |
| 133 | + |
| 134 | +// * * * * * * * * * * * * * * * * * * * * * * * * |
| 135 | +// * Fossil Logic Test Pool |
| 136 | +// * * * * * * * * * * * * * * * * * * * * * * * * |
| 137 | +FOSSIL_TEST_GROUP(cpp_cipher_tests) { |
| 138 | + FOSSIL_TEST_ADD(cpp_cipher_fixture, cpp_test_cipher_compute_basicpp_xor_encrypt_decrypt); |
| 139 | + FOSSIL_TEST_ADD(cpp_cipher_fixture, cpp_test_cipher_compute_null_arguments); |
| 140 | + FOSSIL_TEST_ADD(cpp_cipher_fixture, cpp_test_cipher_compute_unsupported_algorithm); |
| 141 | + FOSSIL_TEST_ADD(cpp_cipher_fixture, cpp_test_cipher_compute_caesar_encrypt_decrypt); |
| 142 | + |
| 143 | + FOSSIL_TEST_REGISTER(cpp_cipher_fixture); |
| 144 | +} // end of tests |
0 commit comments