Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/Library/Hash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,24 @@ namespace usbguard
_state = rhs._state;
#endif
#if defined(USBGUARD_USE_OPENSSL)
_state = rhs._state;
#if OPENSSL_VERSION_NUMBER >= 0x10100000L

if ((_state = EVP_MD_CTX_new()) == nullptr) {
throw std::runtime_error("Dynamic memory allocation of message digest context failed.");
}

#else

if ((_state = EVP_MD_CTX_create()) == nullptr) {
throw std::runtime_error("Dynamic memory allocation of message digest context failed.");
}

#endif

if (!EVP_MD_CTX_copy_ex(_state, rhs._state)) {
throw std::runtime_error("Copying of message digest context failed.");
}

#endif
#if defined(USBGUARD_USE_LIBGCRYPT)
gcry_md_copy(&_state, rhs._state);
Expand Down
1 change: 1 addition & 0 deletions src/Tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ test_unit_SOURCES=\
Unit/test_Rule.cpp \
Unit/test_RuleParser.cpp \
Unit/test_Base64.cpp \
Unit/test_Hash.cpp \
Unit/test_UEvent.cpp \
Unit/test_UEventParser.cpp \
Unit/test_Utility.cpp \
Expand Down
47 changes: 47 additions & 0 deletions src/Tests/Unit/test_Hash.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//
// Copyright (C) 2024 Red Hat, Inc.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// Authors: Daniel Kopecek <dkopecek@redhat.com>
//
#ifdef HAVE_CATCH2_V3
#include <catch_test_macros.hpp>
#else
#include <catch.hpp>
#endif
#include <Hash.cpp>

using namespace usbguard;

TEST_CASE("Hash", "[Hash]")
{
SECTION("copy does not share state with the original") {
std::string expected;
{
Hash reference;
reference.update(std::string("foo"));
expected = reference.getBase64();
}
Hash original;
original.update(std::string("foo"));
{
Hash copy(original);
copy.update(std::string("bar"));
}
REQUIRE(original.getBase64() == expected);
}
}

/* vim: set ts=2 sw=2 et */