66#include < map>
77#include < string>
88#include < utility>
9+ #include < vector>
910#include " util.h"
1011
1112#include " opentelemetry/context/context.h"
@@ -64,6 +65,28 @@ TEST(B3PropagationTest, TraceFlagsBufferGeneration)
6465 EXPECT_EQ (MapB3Context::TraceFlagsFromHex (" 1" ), trace::TraceFlags (trace::TraceFlags::kIsSampled ));
6566}
6667
68+ TEST (B3PropagationTest, PropagateEmptyContext)
69+ {
70+ TextMapCarrierTest carrier;
71+ context::Context ctx;
72+ format.Inject (carrier, ctx);
73+ EXPECT_TRUE (carrier.headers_ .empty ());
74+ formatMultiHeader.Inject (carrier, ctx);
75+ EXPECT_TRUE (carrier.headers_ .empty ());
76+ }
77+
78+ TEST (B3PropagationTest, ExtractMissingHeaders)
79+ {
80+ TextMapCarrierTest carrier;
81+ context::Context ctx;
82+ auto extracted = format.Extract (carrier, ctx);
83+ EXPECT_FALSE (nostd::holds_alternative<nostd::shared_ptr<trace::Span>>(
84+ extracted.GetValue (trace::kSpanKey )));
85+ extracted = formatMultiHeader.Extract (carrier, ctx);
86+ EXPECT_FALSE (nostd::holds_alternative<nostd::shared_ptr<trace::Span>>(
87+ extracted.GetValue (trace::kSpanKey )));
88+ }
89+
6790TEST (B3PropagationTest, PropagateInvalidContext)
6891{
6992 // Do not propagate invalid trace context.
@@ -73,6 +96,50 @@ TEST(B3PropagationTest, PropagateInvalidContext)
7396 nostd::shared_ptr<trace::Span>(new trace::DefaultSpan (trace::SpanContext::GetInvalid ()))};
7497 format.Inject (carrier, ctx);
7598 EXPECT_TRUE (carrier.headers_ .count (" b3" ) == 0 );
99+ formatMultiHeader.Inject (carrier, ctx);
100+ EXPECT_TRUE (carrier.headers_ .empty ());
101+ }
102+
103+ TEST (B3PropagationTest, ExtractInvalidMultiHeaderContext)
104+ {
105+ const std::vector<std::pair<std::string, std::string>> identities = {
106+ {" " , " e457b5a2e4d86bd1" },
107+ {" 80f198ee56343ba864fe8b2a57d3eff7" , " " },
108+ {" 00000000000000000000000000000000" , " e457b5a2e4d86bd1" },
109+ {" 80f198ee56343ba864fe8b2a57d3eff7" , " 0000000000000000" },
110+ {" 0000000zzz0000000000000000000000" , " e457b5a2e4d86bd1" },
111+ {" 80f198ee56343ba864fe8b2a57d3eff7" , " 0000000zzz000000" },
112+ };
113+ trace::SpanContext span_context{MapB3Context::TraceIdFromHex (" 80f198ee56343ba864fe8b2a57d3eff7" ),
114+ MapB3Context::SpanIdFromHex (" e457b5a2e4d86bd1" ),
115+ trace::TraceFlags{1 }, false };
116+ nostd::shared_ptr<trace::Span> existing_span{new trace::DefaultSpan{span_context}};
117+
118+ for (const auto &identity : identities)
119+ {
120+ SCOPED_TRACE (identity.first + " :" + identity.second );
121+ TextMapCarrierTest carrier;
122+ carrier.headers_ [" X-B3-Sampled" ] = " 1" ;
123+ if (!identity.first .empty ())
124+ {
125+ carrier.headers_ [" X-B3-TraceId" ] = identity.first ;
126+ }
127+ if (!identity.second .empty ())
128+ {
129+ carrier.headers_ [" X-B3-SpanId" ] = identity.second ;
130+ }
131+
132+ context::Context empty_context;
133+ auto extracted = formatMultiHeader.Extract (carrier, empty_context);
134+ EXPECT_FALSE (nostd::holds_alternative<nostd::shared_ptr<trace::Span>>(
135+ extracted.GetValue (trace::kSpanKey )));
136+
137+ context::Context existing_context{trace::kSpanKey , existing_span};
138+ extracted = formatMultiHeader.Extract (carrier, existing_context);
139+ auto span_value = extracted.GetValue (trace::kSpanKey );
140+ ASSERT_TRUE (nostd::holds_alternative<nostd::shared_ptr<trace::Span>>(span_value));
141+ EXPECT_EQ (nostd::get<nostd::shared_ptr<trace::Span>>(span_value).get (), existing_span.get ());
142+ }
76143}
77144
78145TEST (B3PropagationTest, ExtractInvalidContext)
0 commit comments