remoting_ice_config_request_unittest.cc 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "remoting/protocol/remoting_ice_config_request.h"
  5. #include "base/bind.h"
  6. #include "base/test/mock_callback.h"
  7. #include "base/test/task_environment.h"
  8. #include "remoting/base/protobuf_http_status.h"
  9. #include "remoting/base/protobuf_http_test_responder.h"
  10. #include "remoting/proto/remoting/v1/network_traversal_messages.pb.h"
  11. #include "remoting/protocol/ice_config.h"
  12. #include "testing/gmock/include/gmock/gmock.h"
  13. #include "testing/gtest/include/gtest/gtest.h"
  14. namespace remoting {
  15. namespace protocol {
  16. namespace {
  17. using testing::_;
  18. using MockOnResultCallback =
  19. base::MockCallback<IceConfigRequest::OnIceConfigCallback>;
  20. } // namespace
  21. class RemotingIceConfigRequestTest : public testing::Test {
  22. protected:
  23. std::unique_ptr<MockOnResultCallback> SendRequest(
  24. base::RunLoop* run_loop_to_quit,
  25. IceConfig* out_config) {
  26. auto mock_on_result = std::make_unique<MockOnResultCallback>();
  27. EXPECT_CALL(*mock_on_result, Run(_)).WillOnce([=](const IceConfig& config) {
  28. *out_config = config;
  29. run_loop_to_quit->Quit();
  30. });
  31. request_.Send(mock_on_result->Get());
  32. return mock_on_result;
  33. }
  34. base::test::TaskEnvironment task_environment_;
  35. ProtobufHttpTestResponder test_responder_;
  36. RemotingIceConfigRequest request_{test_responder_.GetUrlLoaderFactory(),
  37. nullptr};
  38. };
  39. TEST_F(RemotingIceConfigRequestTest, SuccessfulRequest) {
  40. base::RunLoop run_loop;
  41. IceConfig received_config;
  42. auto mock_on_result = SendRequest(&run_loop, &received_config);
  43. std::string api_key;
  44. ASSERT_TRUE(
  45. test_responder_.GetMostRecentPendingRequest().request.headers.GetHeader(
  46. "x-goog-api-key", &api_key));
  47. EXPECT_FALSE(api_key.empty());
  48. // Fill out the response.
  49. apis::v1::GetIceConfigResponse response;
  50. response.mutable_lifetime_duration()->set_seconds(43200);
  51. apis::v1::IceServer* turn_server = response.add_servers();
  52. turn_server->add_urls("turns:the_server.com");
  53. turn_server->set_username("123");
  54. turn_server->set_credential("abc");
  55. apis::v1::IceServer* stun_server = response.add_servers();
  56. stun_server->add_urls("stun:stun_server.com:18344");
  57. test_responder_.AddResponseToMostRecentRequestUrl(response);
  58. run_loop.Run();
  59. ASSERT_FALSE(received_config.is_null());
  60. EXPECT_EQ(1U, received_config.turn_servers.size());
  61. EXPECT_EQ(1U, received_config.stun_servers.size());
  62. }
  63. TEST_F(RemotingIceConfigRequestTest, FailedRequest) {
  64. base::RunLoop run_loop;
  65. IceConfig received_config;
  66. auto mock_on_result = SendRequest(&run_loop, &received_config);
  67. ASSERT_LT(0, test_responder_.GetNumPending());
  68. test_responder_.AddErrorToMostRecentRequestUrl(
  69. ProtobufHttpStatus(ProtobufHttpStatus::Code::INVALID_ARGUMENT, ""));
  70. run_loop.Run();
  71. EXPECT_TRUE(received_config.is_null());
  72. }
  73. } // namespace protocol
  74. } // namespace remoting