xref: /aosp_15_r20/external/federated-compute/fcp/tracing/test/tracing_context_utils_test.cc (revision 14675a029014e728ec732f129a32e299b2da0601)
1 // Copyright 2021 Google LLC
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 "fcp/tracing/tracing_context_utils.h"
16 
17 #include <iostream>
18 #include <optional>
19 #include <ostream>
20 
21 #include "gmock/gmock.h"
22 #include "gtest/gtest.h"
23 #include "fcp/tracing/test/test_api_message.pb.h"
24 
25 namespace fcp {
26 namespace {
27 
28 using fcp::tracing::test::ApiMessageWithContext;
29 using fcp::tracing::test::ApiMessageWithContextBytes;
30 using fcp::tracing::test::ApiMessageWithContextInt;
31 using fcp::tracing::test::ApiMessageWithoutContext;
32 using fcp::tracing::test::TestTracingContext;
33 
TEST(Tracing,SetAndRetrieveContextOnMessage)34 TEST(Tracing, SetAndRetrieveContextOnMessage) {
35   TestTracingContext original_context;
36   original_context.set_first(222);
37   original_context.set_second(333);
38 
39   ApiMessageWithContext message;
40   fcp::tracing_internal::SetTracingContextOnMessage(original_context, message);
41 
42   TestTracingContext context =
43       fcp::tracing_internal::GetContextFromMessage<TestTracingContext>(message);
44 
45   EXPECT_EQ(context.first(), 222);
46   EXPECT_EQ(context.second(), 333);
47 }
48 
TEST(Tracing,SetAndRetrieveContextBytesOnMessage)49 TEST(Tracing, SetAndRetrieveContextBytesOnMessage) {
50   TestTracingContext original_context;
51   original_context.set_first(222);
52   original_context.set_second(333);
53 
54   ApiMessageWithContextBytes message;
55   fcp::tracing_internal::SetTracingContextOnMessage(original_context, message);
56 
57   TestTracingContext context =
58       fcp::tracing_internal::GetContextFromMessage<TestTracingContext>(message);
59 
60   EXPECT_EQ(context.first(), 222);
61   EXPECT_EQ(context.second(), 333);
62 }
63 
TEST(Tracing,MessageWithoutContext)64 TEST(Tracing, MessageWithoutContext) {
65   TestTracingContext original_context;
66   original_context.set_first(222);
67   ApiMessageWithoutContext message;
68   // Setting the context on a message without it will be a no-op.
69   fcp::tracing_internal::SetTracingContextOnMessage(original_context, message);
70 
71   TestTracingContext context =
72       fcp::tracing_internal::GetContextFromMessage<TestTracingContext>(message);
73 
74   EXPECT_EQ(context.first(), 0);
75   EXPECT_EQ(context.second(), 0);
76 }
77 
TEST(Tracing,SetTracingContextOnMessageWithIntContextCheckFailure)78 TEST(Tracing, SetTracingContextOnMessageWithIntContextCheckFailure) {
79   TestTracingContext original_context;
80   ApiMessageWithContextInt message;
81   // Setting the context on a message with the wrong context type will be a
82   // no-op.
83   EXPECT_DEATH(fcp::tracing_internal::SetTracingContextOnMessage(
84                    original_context, message),
85                fcp::tracing_internal::kContextWrongTypeMessage);
86 }
87 
TEST(Tracing,GetTracingContextFromMessageWithIntContextCheckFailure)88 TEST(Tracing, GetTracingContextFromMessageWithIntContextCheckFailure) {
89   ApiMessageWithContextInt message;
90   EXPECT_DEATH(
91       TestTracingContext context =
92           fcp::tracing_internal::GetContextFromMessage<TestTracingContext>(
93               message),
94       fcp::tracing_internal::kContextWrongTypeMessage);
95 }
96 
97 }  // namespace
98 }  // namespace fcp
99