2017-11-06 18:36:11 +00:00
|
|
|
#pragma once
|
|
|
|
#include "libp2p/yamux/yamux.h"
|
|
|
|
#include "libp2p/identify/identify.h"
|
|
|
|
#include "mock_stream.h"
|
2017-11-06 22:31:30 +00:00
|
|
|
#include "libp2p/utils/logger.h"
|
2017-11-06 18:36:11 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
* Helpers
|
|
|
|
*/
|
|
|
|
|
|
|
|
/***
|
|
|
|
* Sends back the yamux protocol to fake negotiation
|
|
|
|
*/
|
|
|
|
int mock_yamux_read_protocol(void* context, struct StreamMessage** msg, int network_timeout) {
|
|
|
|
*msg = libp2p_stream_message_new();
|
|
|
|
struct StreamMessage* message = *msg;
|
2017-11-06 21:38:55 +00:00
|
|
|
const char* id = "/yamux/1.0.0\n";
|
|
|
|
message->data_size = strlen(id);
|
|
|
|
message->data = malloc(message->data_size);
|
|
|
|
memcpy(message->data, id, message->data_size);
|
2017-11-06 18:36:11 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
* Sends back the yamux protocol to fake negotiation
|
|
|
|
*/
|
|
|
|
int mock_identify_read_protocol(void* context, struct StreamMessage** msg, int network_timeout) {
|
|
|
|
*msg = libp2p_stream_message_new();
|
|
|
|
struct StreamMessage* message = *msg;
|
2017-11-06 21:38:55 +00:00
|
|
|
const char* id = "/ipfs/id/1.0.0\n";
|
|
|
|
message->data_size = strlen(id);
|
|
|
|
message->data = malloc(message->data_size);
|
|
|
|
memcpy(message->data, id, message->data_size);
|
2017-11-06 18:36:11 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
* Tests
|
|
|
|
*/
|
|
|
|
|
|
|
|
/***
|
|
|
|
* Verify that we can initiate a yamux session
|
|
|
|
*/
|
|
|
|
int test_yamux_stream_new() {
|
|
|
|
int retVal = 0;
|
|
|
|
// setup
|
|
|
|
struct Stream* mock_stream = mock_stream_new();
|
|
|
|
mock_stream->read = mock_yamux_read_protocol;
|
|
|
|
struct Stream* yamux_stream = libp2p_yamux_stream_new(mock_stream);
|
|
|
|
if (yamux_stream == NULL)
|
|
|
|
goto exit;
|
|
|
|
// tear down
|
|
|
|
retVal = 1;
|
|
|
|
exit:
|
|
|
|
if (yamux_stream != NULL)
|
2017-11-08 15:51:43 +00:00
|
|
|
yamux_stream->close(yamux_stream);
|
2017-11-06 18:36:11 +00:00
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
* Attempt to add a protocol to the Yamux protocol
|
|
|
|
*/
|
|
|
|
int test_yamux_identify() {
|
|
|
|
int retVal = 0;
|
|
|
|
// setup
|
|
|
|
struct Stream* mock_stream = mock_stream_new();
|
|
|
|
mock_stream->read = mock_yamux_read_protocol;
|
|
|
|
struct Stream* yamux_stream = libp2p_yamux_stream_new(mock_stream);
|
|
|
|
if (yamux_stream == NULL)
|
|
|
|
goto exit;
|
2017-11-06 22:31:30 +00:00
|
|
|
// Now add in another protocol
|
2017-11-06 18:36:11 +00:00
|
|
|
mock_stream->read = mock_identify_read_protocol;
|
2017-11-08 15:51:43 +00:00
|
|
|
if (!libp2p_yamux_stream_add(yamux_stream->stream_context, libp2p_identify_stream_new(yamux_stream))) {
|
2017-11-06 18:36:11 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
// tear down
|
|
|
|
retVal = 1;
|
|
|
|
exit:
|
|
|
|
if (yamux_stream != NULL)
|
2017-11-08 15:51:43 +00:00
|
|
|
yamux_stream->close(yamux_stream);
|
2017-11-06 18:36:11 +00:00
|
|
|
return retVal;
|
|
|
|
}
|
2017-11-06 22:31:30 +00:00
|
|
|
|
|
|
|
int test_yamux_incoming_protocol_request() {
|
|
|
|
int retVal = 0;
|
2017-11-08 15:51:43 +00:00
|
|
|
|
2017-11-06 22:31:30 +00:00
|
|
|
// setup
|
2017-11-08 15:51:43 +00:00
|
|
|
// build the protocol handler that can handle yamux and identify protocol
|
2017-11-06 22:31:30 +00:00
|
|
|
struct Libp2pVector* protocol_handlers = libp2p_utils_vector_new(1);
|
|
|
|
struct Libp2pProtocolHandler* handler = libp2p_identify_build_protocol_handler(protocol_handlers);
|
|
|
|
libp2p_utils_vector_add(protocol_handlers, handler);
|
2017-11-08 15:51:43 +00:00
|
|
|
handler = libp2p_yamux_build_protocol_handler(protocol_handlers);
|
|
|
|
libp2p_utils_vector_add(protocol_handlers, handler);
|
|
|
|
struct Stream* mock_stream = mock_stream_new();
|
|
|
|
struct SessionContext* session_context = ((struct ConnectionContext*)mock_stream->stream_context)->session_context;
|
|
|
|
mock_stream->read = mock_yamux_read_protocol;
|
|
|
|
struct StreamMessage* result_message = NULL;
|
|
|
|
if (!session_context->default_stream->read(session_context->default_stream->stream_context, &result_message, 10)) {
|
|
|
|
libp2p_logger_error("test_yamux", "Unable to read Yamux protocol from mock stream.\n");
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
if (libp2p_protocol_marshal(result_message, session_context->default_stream, protocol_handlers) < 0) {
|
|
|
|
libp2p_logger_error("test_yamux", "Upgrade to Yamux protocol unsuccessful.\n");
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
// now we should have upgraded to the yamux protocol
|
|
|
|
libp2p_stream_message_free(result_message);
|
|
|
|
result_message = NULL;
|
|
|
|
if (session_context->default_stream->parent_stream == NULL) {
|
|
|
|
libp2p_logger_error("test_yamux", "Upgrade to Yamux protocol appeared susccessful, but was not.\n");
|
|
|
|
goto exit;
|
|
|
|
}
|
2017-11-06 22:31:30 +00:00
|
|
|
// Someone is requesting the identity protocol
|
|
|
|
mock_stream->read = mock_identify_read_protocol;
|
2017-11-08 15:51:43 +00:00
|
|
|
if (!session_context->default_stream->read(session_context->default_stream->stream_context, &result_message, 10)) {
|
2017-11-06 22:31:30 +00:00
|
|
|
libp2p_logger_error("test_yamux", "Unable to read identify protocol.\n");
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
// handle the marshaling of the protocol
|
2017-11-08 15:51:43 +00:00
|
|
|
libp2p_protocol_marshal(result_message, session_context->default_stream, protocol_handlers);
|
|
|
|
libp2p_stream_message_free(result_message);
|
|
|
|
result_message = NULL;
|
2017-11-06 22:31:30 +00:00
|
|
|
// now verify the results
|
2017-11-08 15:51:43 +00:00
|
|
|
struct YamuxContext* yamux_context = (struct YamuxContext*)session_context->default_stream->stream_context;
|
2017-11-06 22:31:30 +00:00
|
|
|
if (yamux_context->channels->total != 1) {
|
|
|
|
libp2p_logger_error("test_yamux", "Identify protocol was not found.\n");
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
// tear down
|
|
|
|
retVal = 1;
|
|
|
|
exit:
|
2017-11-08 15:51:43 +00:00
|
|
|
if (session_context->default_stream != NULL)
|
|
|
|
session_context->default_stream->close(session_context->default_stream);
|
|
|
|
libp2p_protocol_handlers_shutdown(protocol_handlers);
|
2017-11-06 22:31:30 +00:00
|
|
|
return retVal;
|
|
|
|
}
|