hex_test.cc 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. #include <gtest/gtest.h>
  4. #include <stdint.h>
  5. #include <string>
  6. #include "opentelemetry/trace/propagation/detail/hex.h"
  7. using namespace opentelemetry;
  8. TEST(HexTest, ConvertOddLength)
  9. {
  10. const int kLength = 16;
  11. std::string trace_id_hex = "78cfcfec62ae9e9";
  12. uint8_t trace_id[kLength];
  13. trace::propagation::detail::HexToBinary(trace_id_hex, trace_id, sizeof(trace_id));
  14. const uint8_t expected_trace_id[kLength] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  15. 0x7, 0x8c, 0xfc, 0xfe, 0xc6, 0x2a, 0xe9, 0xe9};
  16. for (int i = 0; i < kLength; ++i)
  17. {
  18. EXPECT_EQ(trace_id[i], expected_trace_id[i]);
  19. }
  20. }
  21. TEST(HexTest, ConvertEvenLength)
  22. {
  23. const int kLength = 16;
  24. std::string trace_id_hex = "078cfcfec62ae9e9";
  25. uint8_t trace_id[kLength];
  26. trace::propagation::detail::HexToBinary(trace_id_hex, trace_id, sizeof(trace_id));
  27. const uint8_t expected_trace_id[kLength] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  28. 0x7, 0x8c, 0xfc, 0xfe, 0xc6, 0x2a, 0xe9, 0xe9};
  29. for (int i = 0; i < kLength; ++i)
  30. {
  31. EXPECT_EQ(trace_id[i], expected_trace_id[i]);
  32. }
  33. }