-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for auth with custom schemes #62
base: master
Are you sure you want to change the base?
Changes from all commits
16e12ca
0cc6927
192c9ed
96aec59
6d2a046
55de52e
adeaa28
263a84f
d7561ad
89724e3
6616b4e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,6 @@ | |
#include <thread> | ||
|
||
#include "mgclient.h" | ||
#include "mgcommon.h" | ||
#include "mgsession.h" | ||
#include "mgsocket.h" | ||
|
||
|
@@ -508,76 +507,73 @@ TEST_F(ConnectTest, Success) { | |
ASSERT_MEMORY_OK(); | ||
} | ||
|
||
TEST_F(ConnectTest, Success_v4) { | ||
RunServer([](int sockfd) { | ||
// Perform handshake. | ||
{ | ||
char handshake[20]; | ||
ASSERT_EQ(RecvData(sockfd, handshake, 20), 0); | ||
ASSERT_EQ(std::string(handshake, 4), "\x60\x60\xB0\x17"s); | ||
ASSERT_EQ(std::string(handshake + 4, 4), "\x00\x00\x01\x04"s); | ||
ASSERT_EQ(std::string(handshake + 8, 4), "\x00\x00\x00\x01"s); | ||
ASSERT_EQ(std::string(handshake + 12, 4), "\x00\x00\x00\x00"s); | ||
ASSERT_EQ(std::string(handshake + 16, 4), "\x00\x00\x00\x00"s); | ||
auto run_v4_server_success = [](int sockfd) { | ||
// Perform handshake. | ||
{ | ||
char handshake[20]; | ||
ASSERT_EQ(RecvData(sockfd, handshake, 20), 0); | ||
ASSERT_EQ(std::string(handshake, 4), "\x60\x60\xB0\x17"s); | ||
ASSERT_EQ(std::string(handshake + 4, 4), "\x00\x00\x01\x04"s); | ||
ASSERT_EQ(std::string(handshake + 8, 4), "\x00\x00\x00\x01"s); | ||
ASSERT_EQ(std::string(handshake + 12, 4), "\x00\x00\x00\x00"s); | ||
ASSERT_EQ(std::string(handshake + 16, 4), "\x00\x00\x00\x00"s); | ||
|
||
uint32_t version = htobe32(0x0104); | ||
ASSERT_EQ(SendData(sockfd, (char *)&version, 4), 0); | ||
} | ||
uint32_t version = htobe32(1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why changed from 0x0104 to 1? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if |
||
ASSERT_EQ(SendData(sockfd, (char *)&version, 4), 0); | ||
} | ||
|
||
mg_session *session = mg_session_init(&mg_system_allocator); | ||
ASSERT_TRUE(session); | ||
session->version = 4; | ||
mg_raw_transport_init(sockfd, (mg_raw_transport **)&session->transport, | ||
&mg_system_allocator); | ||
mg_session *session = mg_session_init(&mg_system_allocator); | ||
ASSERT_TRUE(session); | ||
session->version = 1; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. version from 4 to 1? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yea these seems like hacks to make the client work 🤔 |
||
mg_raw_transport_init(sockfd, (mg_raw_transport **)&session->transport, | ||
&mg_system_allocator); | ||
|
||
// Read HELLO message. | ||
// Read INIT message. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Test is changed from sending HELLO msg to INIT msg, any specific reason? |
||
{ | ||
mg_message *message; | ||
ASSERT_EQ(mg_session_receive_message(session), 0); | ||
ASSERT_EQ(mg_session_read_bolt_message(session, &message), 0); | ||
ASSERT_EQ(message->type, MG_MESSAGE_TYPE_INIT); | ||
|
||
mg_message_init *msg_init = message->init_v; | ||
EXPECT_EQ( | ||
std::string(msg_init->client_name->data, msg_init->client_name->size), | ||
MG_USER_AGENT); | ||
{ | ||
mg_message *message; | ||
ASSERT_EQ(mg_session_receive_message(session), 0); | ||
ASSERT_EQ(mg_session_read_bolt_message(session, &message), 0); | ||
ASSERT_EQ(message->type, MG_MESSAGE_TYPE_HELLO); | ||
|
||
mg_message_hello *msg_hello = message->hello_v; | ||
{ | ||
ASSERT_EQ(mg_map_size(msg_hello->extra), 4u); | ||
|
||
const mg_value *user_agent_val = | ||
mg_map_at(msg_hello->extra, "user_agent"); | ||
ASSERT_TRUE(user_agent_val); | ||
ASSERT_EQ(mg_value_get_type(user_agent_val), MG_VALUE_TYPE_STRING); | ||
const mg_string *user_agent = mg_value_string(user_agent_val); | ||
ASSERT_EQ(std::string(user_agent->data, user_agent->size), | ||
MG_USER_AGENT); | ||
|
||
const mg_value *scheme_val = mg_map_at(msg_hello->extra, "scheme"); | ||
ASSERT_TRUE(scheme_val); | ||
ASSERT_EQ(mg_value_get_type(scheme_val), MG_VALUE_TYPE_STRING); | ||
const mg_string *scheme = mg_value_string(scheme_val); | ||
ASSERT_EQ(std::string(scheme->data, scheme->size), "basic"); | ||
|
||
const mg_value *principal_val = | ||
mg_map_at(msg_hello->extra, "principal"); | ||
ASSERT_TRUE(principal_val); | ||
ASSERT_EQ(mg_value_get_type(principal_val), MG_VALUE_TYPE_STRING); | ||
const mg_string *principal = mg_value_string(principal_val); | ||
ASSERT_EQ(std::string(principal->data, principal->size), "user"); | ||
ASSERT_EQ(mg_map_size(msg_init->auth_token), 3u); | ||
|
||
const mg_value *scheme_val = mg_map_at(msg_init->auth_token, "scheme"); | ||
ASSERT_TRUE(scheme_val); | ||
ASSERT_EQ(mg_value_get_type(scheme_val), MG_VALUE_TYPE_STRING); | ||
const mg_string *scheme = mg_value_string(scheme_val); | ||
ASSERT_EQ(std::string(scheme->data, scheme->size), "basic"); | ||
|
||
const mg_value *principal_val = | ||
mg_map_at(msg_init->auth_token, "principal"); | ||
ASSERT_TRUE(principal_val); | ||
ASSERT_EQ(mg_value_get_type(principal_val), MG_VALUE_TYPE_STRING); | ||
const mg_string *principal = mg_value_string(principal_val); | ||
ASSERT_EQ(std::string(principal->data, principal->size), "user"); | ||
|
||
const mg_value *credentials_val = | ||
mg_map_at(msg_init->auth_token, "credentials"); | ||
ASSERT_TRUE(credentials_val); | ||
ASSERT_EQ(mg_value_get_type(credentials_val), MG_VALUE_TYPE_STRING); | ||
const mg_string *credentials = mg_value_string(credentials_val); | ||
ASSERT_EQ(std::string(credentials->data, credentials->size), "pass"); | ||
} | ||
|
||
mg_message_destroy_ca(message, session->decoder_allocator); | ||
} | ||
|
||
const mg_value *credentials_val = | ||
mg_map_at(msg_hello->extra, "credentials"); | ||
ASSERT_TRUE(credentials_val); | ||
ASSERT_EQ(mg_value_get_type(credentials_val), MG_VALUE_TYPE_STRING); | ||
const mg_string *credentials = mg_value_string(credentials_val); | ||
ASSERT_EQ(std::string(credentials->data, credentials->size), "pass"); | ||
} | ||
// Send SUCCESS message. | ||
ASSERT_EQ(mg_session_send_success_message(session, &mg_empty_map), 0); | ||
|
||
mg_message_destroy_ca(message, session->decoder_allocator); | ||
} | ||
|
||
// Send SUCCESS message. | ||
ASSERT_EQ(mg_session_send_success_message(session, &mg_empty_map), 0); | ||
mg_session_destroy(session); | ||
}; | ||
|
||
mg_session_destroy(session); | ||
}); | ||
TEST_F(ConnectTest, Success_v4) { | ||
RunServer(run_v4_server_success); | ||
mg_session_params *params = mg_session_params_make(); | ||
mg_session_params_set_host(params, "127.0.0.1"); | ||
mg_session_params_set_port(params, port); | ||
|
@@ -592,70 +588,7 @@ TEST_F(ConnectTest, Success_v4) { | |
} | ||
|
||
TEST_F(ConnectTest, SuccessWithSSL) { | ||
RunServer([](int sockfd) { | ||
// Perform handshake. | ||
{ | ||
char handshake[20]; | ||
ASSERT_EQ(RecvData(sockfd, handshake, 20), 0); | ||
ASSERT_EQ(std::string(handshake, 4), "\x60\x60\xB0\x17"s); | ||
ASSERT_EQ(std::string(handshake + 4, 4), "\x00\x00\x01\x04"s); | ||
ASSERT_EQ(std::string(handshake + 8, 4), "\x00\x00\x00\x01"s); | ||
ASSERT_EQ(std::string(handshake + 12, 4), "\x00\x00\x00\x00"s); | ||
ASSERT_EQ(std::string(handshake + 16, 4), "\x00\x00\x00\x00"s); | ||
|
||
uint32_t version = htobe32(1); | ||
ASSERT_EQ(SendData(sockfd, (char *)&version, 4), 0); | ||
} | ||
|
||
mg_session *session = mg_session_init(&mg_system_allocator); | ||
ASSERT_TRUE(session); | ||
session->version = 1; | ||
mg_raw_transport_init(sockfd, (mg_raw_transport **)&session->transport, | ||
&mg_system_allocator); | ||
|
||
// Read INIT message. | ||
{ | ||
mg_message *message; | ||
ASSERT_EQ(mg_session_receive_message(session), 0); | ||
ASSERT_EQ(mg_session_read_bolt_message(session, &message), 0); | ||
ASSERT_EQ(message->type, MG_MESSAGE_TYPE_INIT); | ||
|
||
mg_message_init *msg_init = message->init_v; | ||
EXPECT_EQ( | ||
std::string(msg_init->client_name->data, msg_init->client_name->size), | ||
MG_USER_AGENT); | ||
{ | ||
ASSERT_EQ(mg_map_size(msg_init->auth_token), 3u); | ||
|
||
const mg_value *scheme_val = mg_map_at(msg_init->auth_token, "scheme"); | ||
ASSERT_TRUE(scheme_val); | ||
ASSERT_EQ(mg_value_get_type(scheme_val), MG_VALUE_TYPE_STRING); | ||
const mg_string *scheme = mg_value_string(scheme_val); | ||
ASSERT_EQ(std::string(scheme->data, scheme->size), "basic"); | ||
|
||
const mg_value *principal_val = | ||
mg_map_at(msg_init->auth_token, "principal"); | ||
ASSERT_TRUE(principal_val); | ||
ASSERT_EQ(mg_value_get_type(principal_val), MG_VALUE_TYPE_STRING); | ||
const mg_string *principal = mg_value_string(principal_val); | ||
ASSERT_EQ(std::string(principal->data, principal->size), "user"); | ||
|
||
const mg_value *credentials_val = | ||
mg_map_at(msg_init->auth_token, "credentials"); | ||
ASSERT_TRUE(credentials_val); | ||
ASSERT_EQ(mg_value_get_type(credentials_val), MG_VALUE_TYPE_STRING); | ||
const mg_string *credentials = mg_value_string(credentials_val); | ||
ASSERT_EQ(std::string(credentials->data, credentials->size), "pass"); | ||
} | ||
|
||
mg_message_destroy_ca(message, session->decoder_allocator); | ||
} | ||
|
||
// Send SUCCESS message. | ||
ASSERT_EQ(mg_session_send_success_message(session, &mg_empty_map), 0); | ||
|
||
mg_session_destroy(session); | ||
}); | ||
RunServer(run_v4_server_success); | ||
|
||
mg_secure_transport_init_called = 0; | ||
trust_callback_ok = 0; | ||
|
@@ -681,6 +614,22 @@ TEST_F(ConnectTest, SuccessWithSSL) { | |
ASSERT_MEMORY_OK(); | ||
} | ||
|
||
TEST_F(ConnectTest, CustomScheme) { | ||
RunServer(run_v4_server_success); | ||
mg_session_params *params = mg_session_params_make(); | ||
mg_session_params_set_host(params, "127.0.0.1"); | ||
mg_session_params_set_port(params, port); | ||
mg_session_params_set_scheme(params, "custom_scheme"); | ||
mg_session_params_set_username(params, "user"); | ||
mg_session_params_set_password(params, "pass"); | ||
mg_session *session; | ||
ASSERT_EQ(mg_connect_ca(params, &session, (mg_allocator *)&allocator), 0); | ||
EXPECT_EQ(mg_session_status(session), MG_SESSION_READY); | ||
mg_session_params_destroy(params); | ||
mg_session_destroy(session); | ||
ASSERT_MEMORY_OK(); | ||
} | ||
|
||
class RunTest : public ::testing::Test { | ||
protected: | ||
virtual void SetUp() override { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So the change is also that username and password won't be inserted to
extra
if empty?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you mean here?