diff --git a/include/libnuraft/log_entry.hxx b/include/libnuraft/log_entry.hxx index f620fa44..4178c323 100644 --- a/include/libnuraft/log_entry.hxx +++ b/include/libnuraft/log_entry.hxx @@ -83,6 +83,8 @@ public: return buff_; } + void change_buf(const ptr& buff); + uint64_t get_timestamp() const { return timestamp_us_; } diff --git a/src/log_entry.cxx b/src/log_entry.cxx index ae158e8c..ed448224 100644 --- a/src/log_entry.cxx +++ b/src/log_entry.cxx @@ -18,9 +18,18 @@ log_entry::log_entry(ulong term, { if (buff_ && !has_crc32 && compute_crc) { has_crc32_ = true; - crc32_ = crc32_8( buff_->data_begin(), - buff_->size(), - 0 ); + crc32_ = crc32_8(buff_->data_begin(), + buff_->size(), + 0); } } + +void log_entry::change_buf(const ptr& buff) { + buff_ = buff; + if (buff_ && has_crc32_) { + crc32_ = crc32_8(buff_->data_begin(), + buff_->size(), + 0); + } +} } \ No newline at end of file diff --git a/tests/unit/serialization_test.cxx b/tests/unit/serialization_test.cxx index c6126cfb..11e9854a 100644 --- a/tests/unit/serialization_test.cxx +++ b/tests/unit/serialization_test.cxx @@ -21,6 +21,7 @@ limitations under the License. #include "handle_custom_notification.hxx" #include "nuraft.hxx" #include "strfmt.hxx" +#include "crc32.hxx" #include "test_common.h" @@ -196,6 +197,18 @@ int snapshot_sync_req_zero_buffer_test(bool done) { return 0; } +static int compare_log_entries(const ptr& e1, const ptr& e2) { + CHK_EQ(e1->get_term(), e2->get_term()); + CHK_EQ(e1->get_val_type(), e2->get_val_type()); + CHK_EQ(e1->get_buf().size(), e2->get_buf().size()); + for (size_t i = 0; i < e1->get_buf().size(); ++i) { + byte b1 = e1->get_buf().get_byte(); + byte b2 = e2->get_buf().get_byte(); + CHK_EQ(b1, b2); + } + return 0; +} + int log_entry_test() { ptr data = buffer::alloc(24 + rnd() % 100); for (size_t i = 0; i < data->size(); ++i) { @@ -207,17 +220,21 @@ int log_entry_test() { data, static_cast(1 + rnd() % 5) ); ptr buf2 = entry->serialize(); + CHK_Z(compare_log_entries(entry, log_entry::deserialize(*buf2))); - ptr entry1 = log_entry::deserialize(*buf2); - - CHK_EQ( entry->get_term(), entry1->get_term() ); - CHK_EQ( entry->get_val_type(), entry1->get_val_type() ); - CHK_EQ( entry->get_buf().size(), entry1->get_buf().size() ); - for (size_t i = 0; i < entry->get_buf().size(); ++i) { - byte b1 = entry->get_buf().get_byte(); - byte b2 = entry1->get_buf().get_byte(); - CHK_EQ( b1, b2 ); + // Change the data buffer in log_entry and check if everything is equal + ptr data2 = buffer::alloc(24 + rnd() % 100); + for (size_t i = 0; i < data2->size(); ++i) { + data2->put(static_cast( rnd() % 255) ); } + uint32_t new_crc = crc32_8(data2->data_begin(), + data2->size(), + 0); + entry->change_buf(data2); + buf2 = entry->serialize(); + CHK_Z(compare_log_entries(entry, log_entry::deserialize(*buf2))); + + CHK_EQ(new_crc, entry->get_crc32()); return 0; }