Commit 65aaa85e authored by sergeyu's avatar sergeyu Committed by Commit bot

Improve tests for Mixed-Case JIDs.

1. FakeAuthenticator now verifies that correct IDs are passed on both
ends of the connection.
2. All tests in JingleSessionTest now use mixed-case JIDs.

BUG=707833

Review-Url: https://codereview.chromium.org/2808283002
Cr-Commit-Position: refs/heads/master@{#463833}
parent bee3ee05
......@@ -89,11 +89,25 @@ void FakeChannelAuthenticator::CallDoneCallback() {
base::ResetAndReturn(&done_callback_).Run(result_, std::move(socket_));
}
FakeAuthenticator::Config::Config() {}
FakeAuthenticator::Config::Config(Action action) : action(action) {}
FakeAuthenticator::Config::Config(int round_trips, Action action, bool async)
: round_trips(round_trips), action(action), async(async) {}
FakeAuthenticator::FakeAuthenticator(Type type,
int round_trips,
Action action,
bool async)
: type_(type), round_trips_(round_trips), action_(action), async_(async) {}
FakeAuthenticator::Config config,
const std::string& local_id,
const std::string& remote_id)
: type_(type), config_(config), local_id_(local_id), remote_id_(remote_id) {
EXPECT_TRUE((!local_id_.empty() && !remote_id_.empty()) ||
config.round_trips == 0);
}
FakeAuthenticator::FakeAuthenticator(Action action)
: FakeAuthenticator(CLIENT,
FakeAuthenticator::Config(0, action, true),
std::string(),
std::string()) {}
FakeAuthenticator::~FakeAuthenticator() {}
......@@ -106,13 +120,13 @@ void FakeAuthenticator::Resume() {
}
Authenticator::State FakeAuthenticator::state() const {
EXPECT_LE(messages_, round_trips_ * 2);
EXPECT_LE(messages_, config_.round_trips * 2);
if (messages_ == pause_message_index_ && !resume_closure_.is_null())
return PROCESSING_MESSAGE;
if (messages_ >= round_trips_ * 2) {
if (action_ == REJECT) {
if (messages_ >= config_.round_trips * 2) {
if (config_.action == REJECT) {
return REJECTED;
} else {
return ACCEPTED;
......@@ -121,8 +135,8 @@ Authenticator::State FakeAuthenticator::state() const {
// Don't send the last message if this is a host that wants to
// reject a connection.
if (messages_ == round_trips_ * 2 - 1 &&
type_ == HOST && action_ == REJECT) {
if (messages_ == config_.round_trips * 2 - 1 && type_ == HOST &&
config_.action == REJECT) {
return REJECTED;
}
......@@ -152,13 +166,18 @@ void FakeAuthenticator::ProcessMessage(const buzz::XmlElement* message,
EXPECT_EQ(id, base::IntToString(messages_));
// On the client receive the key in the last message.
if (type_ == CLIENT && messages_ == round_trips_ * 2 - 1) {
if (type_ == CLIENT && messages_ == config_.round_trips * 2 - 1) {
std::string key_base64 =
message->TextNamed(buzz::QName(kChromotingXmlNamespace, "key"));
EXPECT_TRUE(!key_base64.empty());
EXPECT_TRUE(base::Base64Decode(key_base64, &auth_key_));
}
// Receive peer's id.
if (messages_ < 2) {
EXPECT_EQ(remote_id_, message->Attr(buzz::QName("", "id")));
}
++messages_;
if (messages_ == pause_message_index_) {
resume_closure_ = resume_callback;
......@@ -177,8 +196,13 @@ std::unique_ptr<buzz::XmlElement> FakeAuthenticator::GetNextMessage() {
id->AddText(base::IntToString(messages_));
result->AddElement(id);
// Send local id in the first outgoing message.
if (messages_ < 2) {
result->AddAttr(buzz::QName("", "id"), local_id_);
}
// Add authentication key in the last message sent from host to client.
if (type_ == HOST && messages_ == round_trips_ * 2 - 1) {
if (type_ == HOST && messages_ == config_.round_trips * 2 - 1) {
auth_key_ = base::RandBytesAsString(16);
buzz::XmlElement* key = new buzz::XmlElement(
buzz::QName(kChromotingXmlNamespace, "key"));
......@@ -201,19 +225,14 @@ const std::string& FakeAuthenticator::GetAuthKey() const {
std::unique_ptr<ChannelAuthenticator>
FakeAuthenticator::CreateChannelAuthenticator() const {
EXPECT_EQ(ACCEPTED, state());
return base::MakeUnique<FakeChannelAuthenticator>(action_ != REJECT_CHANNEL,
async_);
return base::MakeUnique<FakeChannelAuthenticator>(
config_.action != REJECT_CHANNEL, config_.async);
}
FakeHostAuthenticatorFactory::FakeHostAuthenticatorFactory(
int round_trips,
int messages_till_started,
FakeAuthenticator::Action action,
bool async)
: round_trips_(round_trips),
messages_till_started_(messages_till_started),
action_(action),
async_(async) {}
FakeAuthenticator::Config config)
: messages_till_started_(messages_till_started), config_(config) {}
FakeHostAuthenticatorFactory::~FakeHostAuthenticatorFactory() {}
std::unique_ptr<Authenticator>
......@@ -221,7 +240,7 @@ FakeHostAuthenticatorFactory::CreateAuthenticator(
const std::string& local_jid,
const std::string& remote_jid) {
std::unique_ptr<FakeAuthenticator> authenticator(new FakeAuthenticator(
FakeAuthenticator::HOST, round_trips_, action_, async_));
FakeAuthenticator::HOST, config_, local_jid, remote_jid));
authenticator->set_messages_till_started(messages_till_started_);
return std::move(authenticator);
}
......
......@@ -56,7 +56,24 @@ class FakeAuthenticator : public Authenticator {
REJECT_CHANNEL
};
FakeAuthenticator(Type type, int round_trips, Action action, bool async);
struct Config {
Config();
Config(Action action);
Config(int round_trips, Action action, bool async);
int round_trips = 1;
Action action = Action::ACCEPT;
bool async = true;
};
FakeAuthenticator(Type type,
Config config,
const std::string& local_id,
const std::string& remote_id);
// Special constructor for authenticators in ACCEPTED or REJECTED state that
// don't exchange any messages.
FakeAuthenticator(Action action);
~FakeAuthenticator() override;
......@@ -89,9 +106,9 @@ class FakeAuthenticator : public Authenticator {
protected:
const Type type_;
const int round_trips_;
const Action action_;
const bool async_;
const Config config_;
const std::string local_id_;
const std::string remote_id_;
// Total number of messages that have been processed.
int messages_ = 0;
......@@ -109,9 +126,8 @@ class FakeAuthenticator : public Authenticator {
class FakeHostAuthenticatorFactory : public AuthenticatorFactory {
public:
FakeHostAuthenticatorFactory(
int round_trips, int messages_till_start,
FakeAuthenticator::Action action, bool async);
FakeHostAuthenticatorFactory(int messages_till_start,
FakeAuthenticator::Config config);
~FakeHostAuthenticatorFactory() override;
// AuthenticatorFactory interface.
......@@ -120,10 +136,8 @@ class FakeHostAuthenticatorFactory : public AuthenticatorFactory {
const std::string& remote_jid) override;
private:
const int round_trips_;
const int messages_till_started_;
const FakeAuthenticator::Action action_;
const bool async_;
const FakeAuthenticator::Config config_;
DISALLOW_COPY_AND_ASSIGN(FakeHostAuthenticatorFactory);
};
......
......@@ -34,16 +34,14 @@ void FakeSession::SimulateConnection(FakeSession* peer) {
peer->event_handler_->OnSessionStateChange(AUTHENTICATING);
// Initialize transport and authenticator on the client.
authenticator_.reset(new FakeAuthenticator(FakeAuthenticator::CLIENT, 0,
FakeAuthenticator::ACCEPT, false));
authenticator_.reset(new FakeAuthenticator(FakeAuthenticator::ACCEPT));
authenticator_->set_auth_key(kTestAuthKey);
transport_->Start(authenticator_.get(),
base::Bind(&FakeSession::SendTransportInfo,
weak_factory_.GetWeakPtr()));
// Initialize transport and authenticator on the host.
peer->authenticator_.reset(new FakeAuthenticator(
FakeAuthenticator::HOST, 0, FakeAuthenticator::ACCEPT, false));
peer->authenticator_.reset(new FakeAuthenticator(FakeAuthenticator::ACCEPT));
peer->authenticator_->set_auth_key(kTestAuthKey);
peer->transport_->Start(peer->authenticator_.get(),
base::Bind(&FakeSession::SendTransportInfo, peer_));
......
......@@ -119,8 +119,8 @@ class IceTransportTest : public testing::Test {
nullptr, network_settings_, TransportRole::SERVER),
&host_event_handler_));
if (!host_authenticator_) {
host_authenticator_.reset(new FakeAuthenticator(
FakeAuthenticator::HOST, 0, FakeAuthenticator::ACCEPT, true));
host_authenticator_.reset(
new FakeAuthenticator(FakeAuthenticator::ACCEPT));
}
client_transport_.reset(new IceTransport(
......@@ -129,8 +129,8 @@ class IceTransportTest : public testing::Test {
nullptr, network_settings_, TransportRole::CLIENT),
&client_event_handler_));
if (!client_authenticator_) {
client_authenticator_.reset(new FakeAuthenticator(
FakeAuthenticator::CLIENT, 0, FakeAuthenticator::ACCEPT, true));
client_authenticator_.reset(
new FakeAuthenticator(FakeAuthenticator::ACCEPT));
}
host_event_handler_.set_error_callback(base::Bind(
......@@ -243,8 +243,8 @@ TEST_F(IceTransportTest, MuxDataStream) {
TEST_F(IceTransportTest, FailedChannelAuth) {
// Use host authenticator with one that rejects channel authentication.
host_authenticator_.reset(new FakeAuthenticator(
FakeAuthenticator::HOST, 0, FakeAuthenticator::REJECT_CHANNEL, true));
host_authenticator_.reset(
new FakeAuthenticator(FakeAuthenticator::REJECT_CHANNEL));
InitializeConnection();
......
......@@ -183,16 +183,15 @@ class WebrtcTransportTest : public testing::Test {
new WebrtcTransport(jingle_glue::JingleThreadWrapper::current(),
TransportContext::ForTests(TransportRole::SERVER),
&host_event_handler_));
host_authenticator_.reset(new FakeAuthenticator(
FakeAuthenticator::HOST, 0, FakeAuthenticator::ACCEPT, false));
host_authenticator_.reset(new FakeAuthenticator(FakeAuthenticator::ACCEPT));
host_authenticator_->set_auth_key(kAuthKey);
client_transport_.reset(
new WebrtcTransport(jingle_glue::JingleThreadWrapper::current(),
TransportContext::ForTests(TransportRole::CLIENT),
&client_event_handler_));
client_authenticator_.reset(new FakeAuthenticator(
FakeAuthenticator::CLIENT, 0, FakeAuthenticator::ACCEPT, false));
client_authenticator_.reset(
new FakeAuthenticator(FakeAuthenticator::ACCEPT));
client_authenticator_->set_auth_key(kAuthKey);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment