|
| 1 | +// Copyright 2019, OpenCensus Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "opencensus/trace/propagation/grpc_trace_bin.h" |
| 16 | + |
| 17 | +#include "gmock/gmock.h" |
| 18 | +#include "gtest/gtest.h" |
| 19 | +#include "opencensus/trace/span_context.h" |
| 20 | +#include "opencensus/trace/span_id.h" |
| 21 | +#include "opencensus/trace/trace_id.h" |
| 22 | + |
| 23 | +namespace opencensus { |
| 24 | +namespace trace { |
| 25 | +namespace propagation { |
| 26 | +namespace { |
| 27 | + |
| 28 | +MATCHER(IsValid, "is a valid SpanContext") { return arg.IsValid(); } |
| 29 | +MATCHER(IsInvalid, "is an invalid SpanContext") { return !arg.IsValid(); } |
| 30 | + |
| 31 | +TEST(GrpcTraceBinTest, ParseFull) { |
| 32 | + constexpr unsigned char header_data[] = { |
| 33 | + 0, // version_id |
| 34 | + 0, // trace_id field |
| 35 | + 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x70, 0x71, // lo |
| 36 | + 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, // hi |
| 37 | + 1, // span_id field |
| 38 | + 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, // span_id |
| 39 | + 2, // trace_options field |
| 40 | + 1, // is sampled |
| 41 | + }; |
| 42 | + absl::string_view header(reinterpret_cast<const char*>(header_data), |
| 43 | + sizeof(header_data)); |
| 44 | + SpanContext ctx = FromGrpcTraceBinHeader(header); |
| 45 | + EXPECT_THAT(ctx, IsValid()); |
| 46 | + EXPECT_EQ("64656667686970717273747576777879-8182838485868788-01", |
| 47 | + ctx.ToString()); |
| 48 | + EXPECT_EQ(header, ToGrpcTraceBinHeader(ctx)); |
| 49 | +} |
| 50 | + |
| 51 | +TEST(GrpcTraceBinTest, ParseNotSampled) { |
| 52 | + constexpr unsigned char header_data[] = { |
| 53 | + 0, // version_id |
| 54 | + 0, // trace_id field |
| 55 | + 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x70, 0x71, // lo |
| 56 | + 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, // hi |
| 57 | + 1, // span_id field |
| 58 | + 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, // span_id |
| 59 | + 2, // trace_options field |
| 60 | + 0, // not sampled |
| 61 | + }; |
| 62 | + absl::string_view header(reinterpret_cast<const char*>(header_data), |
| 63 | + sizeof(header_data)); |
| 64 | + SpanContext ctx = FromGrpcTraceBinHeader(header); |
| 65 | + EXPECT_THAT(ctx, IsValid()); |
| 66 | + EXPECT_FALSE(ctx.trace_options().IsSampled()) |
| 67 | + << "Expecting to not be sampled."; |
| 68 | + EXPECT_EQ("64656667686970717273747576777879-8182838485868788-00", |
| 69 | + ctx.ToString()); |
| 70 | + EXPECT_EQ(header, ToGrpcTraceBinHeader(ctx)); |
| 71 | +} |
| 72 | + |
| 73 | +TEST(GrpcTraceBinTest, ExpectedFailures) { |
| 74 | +#define INVALID(hdr) \ |
| 75 | + EXPECT_THAT(FromGrpcTraceBinHeader(absl::string_view( \ |
| 76 | + reinterpret_cast<const char*>(hdr), sizeof(hdr))), \ |
| 77 | + IsInvalid()) |
| 78 | + INVALID(""); |
| 79 | + { |
| 80 | + constexpr unsigned char header[] = { |
| 81 | + 0, // version_id |
| 82 | + 0, // trace_id field |
| 83 | + 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x70, 0x71, // lo |
| 84 | + 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, // hi |
| 85 | + 1, // span_id field |
| 86 | + 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, // span_id |
| 87 | + 2, // options missing |
| 88 | + }; |
| 89 | + INVALID(header) << "Too short."; |
| 90 | + } |
| 91 | + { |
| 92 | + constexpr unsigned char header[] = { |
| 93 | + 123, // version_id |
| 94 | + 0, // trace_id field |
| 95 | + 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x70, 0x71, // lo |
| 96 | + 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, // hi |
| 97 | + 1, // span_id field |
| 98 | + 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, // span_id |
| 99 | + 2, // trace_options field |
| 100 | + 1, // tracing enabled |
| 101 | + }; |
| 102 | + INVALID(header) << "Wrong version_id."; |
| 103 | + } |
| 104 | + { |
| 105 | + constexpr unsigned char header[] = { |
| 106 | + 0, // version_id |
| 107 | + 0, // trace_id field |
| 108 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // lo |
| 109 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // hi |
| 110 | + 1, // span_id field |
| 111 | + 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, // span_id |
| 112 | + 2, // trace_options field |
| 113 | + 1, // tracing enabled |
| 114 | + }; |
| 115 | + INVALID(header) << "Invalid trace_id."; |
| 116 | + } |
| 117 | + { |
| 118 | + constexpr unsigned char header[] = { |
| 119 | + 0, // version_id |
| 120 | + 0, // trace_id field |
| 121 | + 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x70, 0x71, // lo |
| 122 | + 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, // hi |
| 123 | + 1, // span_id field |
| 124 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // span_id |
| 125 | + 2, // trace_options field |
| 126 | + 1, // tracing enabled |
| 127 | + }; |
| 128 | + INVALID(header) << "Invalid span_id."; |
| 129 | + } |
| 130 | +#undef INVALID |
| 131 | +} |
| 132 | + |
| 133 | +} // namespace |
| 134 | +} // namespace propagation |
| 135 | +} // namespace trace |
| 136 | +} // namespace opencensus |
0 commit comments