xref: /aosp_15_r20/external/grpc-grpc/test/core/util/audit_logging_utils.h (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1 //
2 // Copyright 2023 gRPC authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #ifndef GRPC_TEST_CORE_UTIL_AUDIT_LOGGING_UTILS_H
18 #define GRPC_TEST_CORE_UTIL_AUDIT_LOGGING_UTILS_H
19 
20 #include <memory>
21 #include <string>
22 #include <vector>
23 
24 #include "absl/status/statusor.h"
25 #include "absl/strings/string_view.h"
26 
27 #include <grpc/grpc_audit_logging.h>
28 #include <grpc/support/json.h>
29 #include <grpc/support/port_platform.h>
30 
31 namespace grpc_core {
32 namespace testing {
33 
34 class TestAuditLogger : public experimental::AuditLogger {
35  public:
TestAuditLogger(std::vector<std::string> * audit_logs)36   explicit TestAuditLogger(std::vector<std::string>* audit_logs)
37       : audit_logs_(audit_logs) {}
38 
39   absl::string_view name() const override;
40   void Log(const experimental::AuditContext& context) override;
41 
42  private:
43   std::vector<std::string>* audit_logs_;
44 };
45 
46 class TestAuditLoggerFactory : public experimental::AuditLoggerFactory {
47  public:
48   class Config : public AuditLoggerFactory::Config {
49     absl::string_view name() const override;
ToString()50     std::string ToString() const override { return "{}"; }
51   };
52 
TestAuditLoggerFactory(std::vector<std::string> * audit_logs)53   explicit TestAuditLoggerFactory(std::vector<std::string>* audit_logs)
54       : audit_logs_(audit_logs) {}
55 
56   absl::string_view name() const override;
57   absl::StatusOr<std::unique_ptr<AuditLoggerFactory::Config>>
58   ParseAuditLoggerConfig(const experimental::Json&) override;
59   std::unique_ptr<experimental::AuditLogger> CreateAuditLogger(
60       std::unique_ptr<AuditLoggerFactory::Config>) override;
61 
62  private:
63   std::vector<std::string>* audit_logs_;
64 };
65 
66 }  // namespace testing
67 }  // namespace grpc_core
68 
69 #endif  // GRPC_TEST_CORE_UTIL_AUDIT_LOGGING_UTILS_H
70