Commit 473fac26 authored by Michael Davis's avatar Michael Davis
Browse files

[checksum] Implements checksum serialization/deserialization

parent fdee4898
......@@ -77,7 +77,7 @@ add_library (ctacatalogue SHARED
set_property(TARGET ctacatalogue PROPERTY SOVERSION "${CTA_SOVERSION}")
set_property(TARGET ctacatalogue PROPERTY VERSION "${CTA_LIBVERSION}")
set_property (TARGET ctacatalogue APPEND PROPERTY INSTALL_RPATH ${ORACLE-INSTANTCLIENT_RPATH})
set_property(TARGET ctacatalogue APPEND PROPERTY INSTALL_RPATH ${ORACLE-INSTANTCLIENT_RPATH})
install (TARGETS ctacatalogue DESTINATION usr/${CMAKE_INSTALL_LIBDIR})
......@@ -202,8 +202,7 @@ add_executable(cta-catalogue-schema-create
MysqlCatalogueSchema.cpp)
target_link_libraries (cta-catalogue-schema-create
ctacatalogue
${PROTOBUF3_LIBRARIES})
ctacatalogue)
set_property (TARGET cta-catalogue-schema-create APPEND PROPERTY INSTALL_RPATH ${ORACLE-INSTANTCLIENT_RPATH})
......@@ -216,8 +215,7 @@ add_executable(cta-catalogue-schema-drop
DropSchemaCmdMain.cpp)
target_link_libraries (cta-catalogue-schema-drop
ctacatalogue
${PROTOBUF3_LIBRARIES})
ctacatalogue)
set_property (TARGET cta-catalogue-schema-drop APPEND PROPERTY INSTALL_RPATH ${ORACLE-INSTANTCLIENT_RPATH})
......@@ -230,8 +228,7 @@ add_executable(cta-database-poll
PollDatabaseCmdMain.cpp)
target_link_libraries (cta-database-poll
ctacatalogue
${PROTOBUF3_LIBRARIES})
ctacatalogue)
set_property (TARGET cta-database-poll APPEND PROPERTY INSTALL_RPATH ${ORACLE-INSTANTCLIENT_RPATH})
......@@ -244,8 +241,7 @@ add_executable(cta-catalogue-admin-user-create
CreateAdminUserCmdMain.cpp)
target_link_libraries (cta-catalogue-admin-user-create
ctacatalogue
${PROTOBUF3_LIBRARIES})
ctacatalogue)
set_property (TARGET cta-catalogue-admin-user-create APPEND PROPERTY INSTALL_RPATH ${ORACLE-INSTANTCLIENT_RPATH})
......
......@@ -160,6 +160,7 @@ set_property(TARGET ctacommon PROPERTY VERSION "${CTA_LIBVERSION}")
install (TARGETS ctacommon DESTINATION usr/${CMAKE_INSTALL_LIBDIR})
target_link_libraries (ctacommon
XrdSsiPbEosCta
pthread
uuid
z
......@@ -209,4 +210,4 @@ set_property(TARGET ctacommonunittests PROPERTY VERSION "${CTA_LIBVERSION}")
install(TARGETS ctacommonunittests DESTINATION usr/${CMAKE_INSTALL_LIBDIR})
add_executable(mutexLtrace threading/MutexLtrace.cpp)
target_link_libraries(mutexLtrace ctacommon ${PROTOBUF3_LIBRARIES})
target_link_libraries(mutexLtrace ctacommon)
......@@ -23,11 +23,39 @@ namespace cta {
namespace checksum {
void ChecksumBlob::insert(ChecksumType type, const std::string &value) {
throw exception::Exception("not implemented");
// Validate the length of the checksum
size_t expectedLength;
switch(type) {
case NONE: expectedLength = 0; break;
case ADLER32:
case CRC32:
case CRC32C: expectedLength = 4; break;
case MD5: expectedLength = 16; break;
case SHA1: expectedLength = 20; break;
}
if(value.length() != expectedLength) throw exception::ChecksumValueMismatch(
"Checksum type=" + ChecksumTypeName.at(type) +
" length expected=" + std::to_string(expectedLength) +
" actual=" + std::to_string(value.length()));
m_cs[type] = value;
}
void ChecksumBlob::insert(ChecksumType type, uint32_t value) {
throw exception::Exception("not implemented");
// This method is only valid for 32-bit checksums
std::string cs;
switch(type) {
case ADLER32:
case CRC32:
case CRC32C:
for(int i = 0; i < 4; ++i) {
cs.push_back(static_cast<char>(value & 0x0F));
value >>= 1;
}
m_cs[type] = cs;
break;
default:
throw exception::ChecksumTypeMismatch(ChecksumTypeName.at(type) + " is not a 32-bit checksum");
}
}
void ChecksumBlob::validate(const ChecksumBlob &blob) const {
......@@ -44,25 +72,12 @@ void ChecksumBlob::validate(const ChecksumBlob &blob) const {
}
std::string ChecksumBlob::serialize() const {
common::ChecksumBlob p_csb;
ChecksumBlobToProtobuf(*this, p_csb);
throw exception::Exception("not implemented");
#if 0
common::ChecksumBlob csb;
for(auto &cs : m_cs) {
auto p_cs = csb.add_cs();
switch(cs.getType()) {
case Checksum::CHECKSUMTYPE_NONE:
default:
p_cs->set_type(common::ChecksumBlob::Checksum::NONE);
}
p_cs->set_value(cs.getByteArray());
}
std::string s;
csb.SerializeToString(&s);
return s;
#endif
std::string bytearray;
p_csb.SerializeToString(&bytearray);
return bytearray;
}
size_t ChecksumBlob::length() const {
......@@ -71,7 +86,7 @@ size_t ChecksumBlob::length() const {
}
void ChecksumBlob::deserialize(const std::string &bytearray) {
throw exception::Exception("not implemented");
//ProtobufToChecksumBlob(const common::ChecksumBlob &p_csb, checksum::ChecksumBlob &csb)
}
std::ostream &operator<<(std::ostream &os, const ChecksumBlob &csb) {
......
......@@ -41,6 +41,18 @@ enum ChecksumType {
SHA1 //!< SHA-1 160-bit hash
};
/*!
* String representations of the checksum types
*/
const std::map<ChecksumType, std::string> ChecksumTypeName = {
{ NONE, "NONE" },
{ ADLER32, "ADLER32" },
{ CRC32, "CRC32" },
{ CRC32C, "CRC32C" },
{ MD5, "MD5" },
{ SHA1, "SHA1" }
};
/*!
* A class to store one or more checksums
*/
......
......@@ -31,23 +31,29 @@ protected:
};
TEST_F(cta_ChecksumBlobTest, default_constructor) {
using namespace cta;
using namespace checksum;
using namespace cta::checksum;
const ChecksumBlob checksumBlob;
ASSERT_EQ(checksumBlob.length(), 0);
}
TEST_F(cta_ChecksumBlobTest, checksum_types) {
using namespace cta::checksum;
ChecksumBlob checksumBlob;
checksumBlob.insert(NONE, "");
checksumBlob.insert(ADLER32, "12\0004");
}
TEST_F(cta_ChecksumBlobTest, adler32) {
using namespace cta;
using namespace checksum;
using namespace cta::checksum;
ChecksumBlob checksumBlob;
checksumBlob.insert(checksum::ADLER32, 0x0A141E28);
checksumBlob.insert(ADLER32, 0x0A141E28);
ASSERT_EQ(checksumBlob.length(), 1);
std::string bytearray = checksumBlob.at(checksum::ADLER32);
std::string bytearray = checksumBlob.at(ADLER32);
ASSERT_EQ(4, bytearray.length());
ASSERT_EQ(static_cast<uint8_t>(10), bytearray[0]);
......@@ -56,4 +62,23 @@ TEST_F(cta_ChecksumBlobTest, adler32) {
ASSERT_EQ(static_cast<uint8_t>(40), bytearray[3]);
}
TEST_F(cta_ChecksumBlobTest, invalid_checksums) {
using namespace cta::checksum;
ChecksumBlob checksumBlob;
checksumBlob.insert(NONE, "none");
checksumBlob.insert(MD5, 0x0A141E28);
}
TEST_F(cta_ChecksumBlobTest, serialize_deserialize) {
using namespace cta::checksum;
ChecksumBlob checksumBlob1;
auto bytearray = checksumBlob1.serialize();
ChecksumBlob checksumBlob2;
checksumBlob2.deserialize(bytearray);
ASSERT_EQ(checksumBlob1, checksumBlob2);
}
} // namespace unitTests
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment