Skip to content

Commit

Permalink
feat(security): add unit test for negotiation_service (XiaoMi#609)
Browse files Browse the repository at this point in the history
  • Loading branch information
levy5307 committed Dec 21, 2020
1 parent 7908b25 commit 8b04d5f
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/runtime/rpc/network.sim.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ class sim_network_provider : public connection_oriented_network
return rpc_session_ptr(new sim_client_session(*this, server_addr, parser));
}

virtual rpc_session_ptr create_server_session(::dsn::rpc_address client_addr,
rpc_session_ptr client_session)
{
message_parser_ptr parser(new_message_parser(_client_hdr_format));
return rpc_session_ptr(new sim_server_session(*this, client_addr, client_session, parser));
}

uint32_t net_delay_milliseconds() const;

private:
Expand All @@ -102,5 +109,5 @@ class sim_network_provider : public connection_oriented_network
};

//------------- inline implementations -------------
}
} // end namespace
} // namespace tools
} // namespace dsn
1 change: 1 addition & 0 deletions src/runtime/security/negotiation_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class negotiation_service : public serverlet<negotiation_service>,
negotiation_service();
void on_negotiation_request(negotiation_rpc rpc);
friend class utils::singleton<negotiation_service>;
friend class negotiation_service_test;
};

} // namespace security
Expand Down
61 changes: 61 additions & 0 deletions src/runtime/test/negotiation_service_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

#include "runtime/security/negotiation_service.h"
#include "runtime/security/negotiation_utils.h"
#include "runtime/rpc/network.sim.h"

#include <gtest/gtest.h>
#include <dsn/utility/flags.h>

namespace dsn {
namespace security {
DSN_DECLARE_bool(enable_auth);

class negotiation_service_test : public testing::Test
{
public:
negotiation_rpc create_fake_rpc()
{
std::unique_ptr<tools::sim_network_provider> sim_net(
new tools::sim_network_provider(nullptr, nullptr));
auto sim_session =
sim_net->create_server_session(rpc_address("localhost", 10086), rpc_session_ptr());
auto rpc = negotiation_rpc(make_unique<negotiation_request>(), RPC_NEGOTIATION);
rpc.dsn_request()->io_session = sim_session;
return rpc;
}

void on_negotiation_request(negotiation_rpc rpc)
{
negotiation_service::instance().on_negotiation_request(rpc);
}
};

TEST_F(negotiation_service_test, disable_auth)
{
RPC_MOCKING(negotiation_rpc)
{
FLAGS_enable_auth = false;
auto rpc = create_fake_rpc();
on_negotiation_request(rpc);

ASSERT_EQ(rpc.response().status, negotiation_status::type::SASL_AUTH_DISABLE);
}
}
} // namespace security
} // namespace dsn

0 comments on commit 8b04d5f

Please sign in to comment.