pgv_test.cc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // NOLINT(namespace-envoy)
  2. #include <cstdlib>
  3. #include <iostream>
  4. // We don't use all the headers in the test below, but including them anyway as
  5. // a cheap way to get some C++ compiler sanity checking.
  6. #include "envoy/config/cluster/v3/cluster.pb.validate.h"
  7. #include "envoy/config/endpoint/v3/endpoint.pb.validate.h"
  8. #include "envoy/config/listener/v3/listener.pb.validate.h"
  9. #include "envoy/config/route/v3/route.pb.validate.h"
  10. #include "envoy/config/core/v3/protocol.pb.validate.h"
  11. #include "envoy/config/accesslog/v3/accesslog.pb.validate.h"
  12. #include "envoy/extensions/compression/gzip/decompressor/v3/gzip.pb.validate.h"
  13. #include "envoy/extensions/filters/http/buffer/v3/buffer.pb.validate.h"
  14. #include "envoy/extensions/filters/http/fault/v3/fault.pb.validate.h"
  15. #include "envoy/extensions/filters/http/grpc_json_transcoder/v3/transcoder.pb.validate.h"
  16. #include "envoy/extensions/filters/http/header_to_metadata/v3/header_to_metadata.pb.validate.h"
  17. #include "envoy/extensions/filters/http/health_check/v3/health_check.pb.validate.h"
  18. #include "envoy/extensions/filters/http/ip_tagging/v3/ip_tagging.pb.validate.h"
  19. #include "envoy/extensions/filters/http/lua/v3/lua.pb.validate.h"
  20. #include "envoy/extensions/filters/http/router/v3/router.pb.validate.h"
  21. #include "envoy/extensions/filters/network/http_connection_manager/v3/http_connection_manager.pb.validate.h"
  22. #include "envoy/extensions/filters/network/mongo_proxy/v3/mongo_proxy.pb.validate.h"
  23. #include "envoy/extensions/filters/network/redis_proxy/v3/redis_proxy.pb.validate.h"
  24. #include "envoy/extensions/filters/network/tcp_proxy/v3/tcp_proxy.pb.validate.h"
  25. #include "envoy/extensions/health_checkers/redis/v3/redis.pb.validate.h"
  26. #include "envoy/config/bootstrap/v3/bootstrap.pb.validate.h"
  27. #include "google/protobuf/text_format.h"
  28. template <class Proto> struct TestCase {
  29. void run() {
  30. std::string err;
  31. if (Validate(invalid_message, &err)) {
  32. std::cerr << "Unexpected successful validation of invalid message: "
  33. << invalid_message.DebugString() << std::endl;
  34. exit(EXIT_FAILURE);
  35. }
  36. if (!Validate(valid_message, &err)) {
  37. std::cerr << "Unexpected failed validation of valid message: " << valid_message.DebugString()
  38. << ", " << err << std::endl;
  39. exit(EXIT_FAILURE);
  40. }
  41. }
  42. Proto& invalid_message;
  43. Proto& valid_message;
  44. };
  45. // Basic protoc-gen-validate C++ validation header inclusion and Validate calls
  46. // from data plane API.
  47. int main(int /*argc*/, char* /*argv*/[]) {
  48. envoy::config::bootstrap::v3::Bootstrap invalid_bootstrap;
  49. invalid_bootstrap.mutable_static_resources()->add_clusters();
  50. // This is a baseline test of the validation features we care about. It's
  51. // probably not worth adding in every filter and field that we want to valid
  52. // in the API upfront, but as regressions occur, this is the place to add the
  53. // specific case.
  54. const std::string valid_bootstrap_text = R"EOF(
  55. node {}
  56. cluster_manager {}
  57. admin {
  58. access_log_path: "/dev/null"
  59. address { pipe { path: "/" } }
  60. }
  61. )EOF";
  62. envoy::config::bootstrap::v3::Bootstrap valid_bootstrap;
  63. if (!google::protobuf::TextFormat::ParseFromString(valid_bootstrap_text, &valid_bootstrap)) {
  64. std::cerr << "Unable to parse text proto: " << valid_bootstrap_text << std::endl;
  65. exit(EXIT_FAILURE);
  66. }
  67. TestCase<envoy::config::bootstrap::v3::Bootstrap>{invalid_bootstrap, valid_bootstrap}.run();
  68. exit(EXIT_SUCCESS);
  69. }