pg_proxy_ut.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #include <library/cpp/testing/unittest/registar.h>
  2. #include <library/cpp/testing/unittest/tests_data.h>
  3. #include <ydb/library/actors/core/executor_pool_basic.h>
  4. #include <ydb/library/actors/core/scheduler_basic.h>
  5. #include <ydb/library/actors/testlib/test_runtime.h>
  6. #include <ydb/core/pgproxy/pg_proxy.h>
  7. #include <ydb/core/pgproxy/pg_log.h>
  8. #include <ydb/core/pgproxy/pg_proxy_events.h>
  9. #include <ydb/library/services/services.pb.h>
  10. #include <util/network/socket.h>
  11. #include <util/string/hex.h>
  12. #include "pg_listener.h"
  13. #ifdef NDEBUG
  14. #define Ctest Cnull
  15. #else
  16. #define Ctest Cerr
  17. #endif
  18. using namespace NKikimr::NRawSocket;
  19. class TTestActorRuntime : public NActors::TTestActorRuntimeBase {
  20. public:
  21. void InitNodeImpl(TNodeDataBase* node, size_t nodeIndex) override {
  22. NActors::TTestActorRuntimeBase::InitNodeImpl(node, nodeIndex);
  23. node->LogSettings->Append(
  24. NKikimrServices::EServiceKikimr_MIN,
  25. NKikimrServices::EServiceKikimr_MAX,
  26. NKikimrServices::EServiceKikimr_Name
  27. );
  28. }
  29. };
  30. size_t Send(TSocket& s, const TString& data) {
  31. char buf[1024];
  32. HexDecode(data.data(), data.size(), buf);
  33. return s.Send(buf, data.size() / 2);
  34. }
  35. TString Receive(TSocket& s) {
  36. char buf[1024];
  37. size_t received = s.Recv(buf, sizeof(buf));
  38. return HexEncode(buf, received);
  39. }
  40. Y_UNIT_TEST_SUITE(TPGTest) {
  41. Y_UNIT_TEST(TestLogin) {
  42. TTestActorRuntime actorSystem;
  43. TPortManager portManager;
  44. TIpPort port = portManager.GetTcpPort();
  45. TAutoPtr<NActors::IEventHandle> handle;
  46. actorSystem.Initialize();
  47. actorSystem.SetLogPriority(NKikimrServices::PGWIRE, NActors::NLog::PRI_DEBUG);
  48. NActors::TActorId database = actorSystem.AllocateEdgeActor();
  49. NActors::TActorId poller = actorSystem.Register(NActors::CreatePollerActor());
  50. NActors::IActor* listener = NPG::CreatePGListener(poller, database, {
  51. .Port = port,
  52. });
  53. actorSystem.Register(listener);
  54. // waiting for port become listening
  55. {
  56. NActors::TDispatchOptions options;
  57. options.FinalEvents.push_back(NActors::TDispatchOptions::TFinalEventCondition(ui32(NActors::ENetwork::EvPollerRegister)));
  58. actorSystem.DispatchEvents(options);
  59. }
  60. TSocket s(TNetworkAddress("::", port));
  61. Send(s, "0000001300030000" "7573657200757365720000"); // user=user
  62. NPG::TEvPGEvents::TEvAuth* authRequest = actorSystem.GrabEdgeEvent<NPG::TEvPGEvents::TEvAuth>(handle);
  63. UNIT_ASSERT(authRequest);
  64. UNIT_ASSERT_VALUES_EQUAL(authRequest->InitialMessage->GetClientParams()["user"], "user");
  65. actorSystem.Send(new NActors::IEventHandle(handle->Sender, database, new NPG::TEvPGEvents::TEvAuthResponse()));
  66. TString received = Receive(s);
  67. UNIT_ASSERT_VALUES_EQUAL(received, "520000000800000000");
  68. }
  69. }