|
| 1 | +/** |
| 2 | + @file Test for CryptoHash |
| 3 | +
|
| 4 | + @section license License |
| 5 | +
|
| 6 | + Licensed to the Apache Software Foundation (ASF) under one |
| 7 | + or more contributor license agreements. See the NOTICE file |
| 8 | + distributed with this work for additional information |
| 9 | + regarding copyright ownership. The ASF licenses this file |
| 10 | + to you under the Apache License, Version 2.0 (the |
| 11 | + "License"); you may not use this file except in compliance |
| 12 | + with the License. You may obtain a copy of the License at |
| 13 | +
|
| 14 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 15 | +
|
| 16 | + Unless required by applicable law or agreed to in writing, software |
| 17 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 18 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 19 | + See the License for the specific language governing permissions and |
| 20 | + limitations under the License. |
| 21 | +*/ |
| 22 | + |
| 23 | +#include <array> |
| 24 | +#include <string_view> |
| 25 | + |
| 26 | +#include "tscore/ink_assert.h" |
| 27 | +#include "tscore/ink_defs.h" |
| 28 | +#include "tscore/CryptoHash.h" |
| 29 | +#include "catch.hpp" |
| 30 | + |
| 31 | +TEST_CASE("CrypoHash", "[libts][CrypoHash]") |
| 32 | +{ |
| 33 | + CryptoHash hash; |
| 34 | + ats::CryptoContext ctx; |
| 35 | + std::string_view test = "asdfsfsdfljhasdfkjasdkfuy239874kasjdf"; |
| 36 | + std::string_view sha256 = "2602CBA2CC0331EB7C455E9F36030B32CE9BB432A90759075F5A702772BE123B"; |
| 37 | + std::string_view md5 = "480AEF8C24AA94B80DC6214ECEC8CD1A"; |
| 38 | + |
| 39 | + // Hash the test data |
| 40 | + ctx.update(test.data(), test.size()); |
| 41 | + ctx.finalize(hash); |
| 42 | + |
| 43 | + // Write the output to a string |
| 44 | + char buffer[(CRYPTO_HASH_SIZE * 2) + 1]; |
| 45 | + hash.toHexStr(buffer); |
| 46 | + |
| 47 | + // Compair to a known hash value |
| 48 | + if (CryptoContext::Setting == CryptoContext::SHA256) { |
| 49 | + REQUIRE(strlen(buffer) == sha256.size()); |
| 50 | + REQUIRE(memcmp(sha256.data(), buffer, sha256.size()) == 0); |
| 51 | + } else { |
| 52 | + REQUIRE(strlen(buffer) == md5.size()); |
| 53 | + REQUIRE(memcmp(md5.data(), buffer, md5.size()) == 0); |
| 54 | + } |
| 55 | +} |
0 commit comments