void TCPSOCKET_BIND_ADDRESS_INVALID()
{
    SKIP_IF_TCP_UNSUPPORTED();
    TCPSocket *sock = new TCPSocket;
    if (!sock) {
        TEST_FAIL();
        return;
    }
    TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock->open(NetworkInterface::get_default_instance()));
    nsapi_error_t bind_result = NSAPI_ERROR_OK;
    if (get_ip_version() == NSAPI_IPv4) {
        bind_result = sock->bind("190.2.3.4", 1024);
    } else if (get_ip_version() == NSAPI_IPv6) {
        bind_result = sock->bind("fe80::ff01", 1024);
    } else {
        TEST_FAIL_MESSAGE("This stack is neither IPv4 nor IPv6");
    }
    if (bind_result == NSAPI_ERROR_UNSUPPORTED) {
        TEST_IGNORE_MESSAGE("bind() not supported");
    } else {
        TEST_ASSERT_EQUAL(NSAPI_ERROR_PARAMETER, bind_result);
    }

    delete sock;
}
void TCPSOCKET_BIND_ADDRESS()
{
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE
    int count = fetch_stats();
    for (int j = 0; j < count; j++) {
        TEST_ASSERT_EQUAL(SOCK_CLOSED,  tcp_stats[j].state);
    }
#endif

    TCPSocket *sock = new TCPSocket;
    if (!sock) {
        TEST_FAIL();
    }
    TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock->open(get_interface()));
    SocketAddress sockAddr = SocketAddress(get_interface()->get_ip_address(), 80);
    TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock->bind(sockAddr));

    delete sock;

#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE
    count = fetch_stats();
    for (int j = 0; j < count; j++) {
        TEST_ASSERT_EQUAL(SOCK_CLOSED, tcp_stats[j].state);
    }
#endif
}
Esempio n. 3
0
bool Init()
{
    cout << "connecting to TCP " << gHost << ":" << gPort << " unum " << unum << "\n";

    try
        {
            Addr local(INADDR_ANY,INADDR_ANY);
            gSocket.bind(local);
        }

    catch (BindErr error)
        {
            cerr << "failed to bind socket with '"
                 << error.what() << "'" << endl;

            gSocket.close();
            return false;
        }

    try
        {
            Addr server(gPort,gHost);
            gSocket.connect(server);
        }

    catch (ConnectErr error)
        {
            cerr << "connection failed with: '"
                 << error.what() << "'" << endl;
            gSocket.close();
            return false;
        }

    return true;
}
void TCPSOCKET_BIND_WRONG_TYPE()
{
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
    int count = fetch_stats();
    for (int j = 0; j < count; j++) {
        TEST_ASSERT_EQUAL(SOCK_CLOSED,  tcp_stats[j].state);
    }
#endif

    TCPSocket *sock = new TCPSocket;
    if (!sock) {
        TEST_FAIL();
        return;
    }
    TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock->open(NetworkInterface::get_default_instance()));
    char addr_bytes[16] = {0xfe, 0x80, 0xff, 0x1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    SocketAddress sockAddr;
    if (get_ip_version() == NSAPI_IPv4) {
        sockAddr = SocketAddress(addr_bytes, NSAPI_IPv4, 80);
    } else if (get_ip_version() == NSAPI_IPv6) {
        sockAddr = SocketAddress(addr_bytes, NSAPI_IPv6, 80);
    } else {
        TEST_FAIL_MESSAGE("This stack is neither IPv4 nor IPv6");
    }
    nsapi_error_t bind_result = sock->bind(sockAddr);
    if (bind_result == NSAPI_ERROR_UNSUPPORTED) {
        TEST_IGNORE_MESSAGE("bind() not supported");
    } else {
        TEST_ASSERT_EQUAL(NSAPI_ERROR_PARAMETER, bind_result);
    }

    delete sock;

#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
    count = fetch_stats();
    for (int j = 0; j < count; j++) {
        TEST_ASSERT_EQUAL(SOCK_CLOSED, tcp_stats[j].state);
    }
#endif
}