Path: blob/main/external/curl/tests/libtest/cli_ws_pingpong.c
2649 views
/***************************************************************************1* _ _ ____ _2* Project ___| | | | _ \| |3* / __| | | | |_) | |4* | (__| |_| | _ <| |___5* \___|\___/|_| \_\_____|6*7* Copyright (C) Daniel Stenberg, <[email protected]>, et al.8*9* This software is licensed as described in the file COPYING, which10* you should have received as part of this distribution. The terms11* are also available at https://curl.se/docs/copyright.html.12*13* You may opt to use, copy, modify, merge, publish, distribute and/or sell14* copies of the Software, and permit persons to whom the Software is15* furnished to do so, under the terms of the COPYING file.16*17* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY18* KIND, either express or implied.19*20* SPDX-License-Identifier: curl21*22***************************************************************************/23#include "first.h"2425#include "testtrace.h"26#include "memdebug.h"2728#ifndef CURL_DISABLE_WEBSOCKETS2930static CURLcode pingpong(CURL *curl, const char *payload)31{32CURLcode res;33int i;3435res = ws_send_ping(curl, payload);36if(res)37return res;38for(i = 0; i < 10; ++i) {39curl_mfprintf(stderr, "Receive pong\n");40res = ws_recv_pong(curl, payload);41if(res == CURLE_AGAIN) {42curlx_wait_ms(100);43continue;44}45ws_close(curl);46return res;47}48ws_close(curl);49return CURLE_RECV_ERROR;50}5152#endif5354static CURLcode test_cli_ws_pingpong(const char *URL)55{56#ifndef CURL_DISABLE_WEBSOCKETS57CURL *curl;58CURLcode result = CURLE_OK;59const char *payload;6061if(!URL || !libtest_arg2) {62curl_mfprintf(stderr, "need args: URL payload\n");63return (CURLcode)2;64}65payload = libtest_arg2;6667if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {68curl_mfprintf(stderr, "curl_global_init() failed\n");69return (CURLcode)3;70}7172curl = curl_easy_init();73if(curl) {74curl_easy_setopt(curl, CURLOPT_URL, URL);7576/* use the callback style */77curl_easy_setopt(curl, CURLOPT_USERAGENT, "ws-pingpong");78curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);79curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 2L); /* websocket style */80result = curl_easy_perform(curl);81curl_mfprintf(stderr, "curl_easy_perform() returned %u\n", result);82if(result == CURLE_OK)83result = pingpong(curl, payload);8485/* always cleanup */86curl_easy_cleanup(curl);87}88curl_global_cleanup();89return result;9091#else /* !CURL_DISABLE_WEBSOCKETS */92(void)URL;93curl_mfprintf(stderr, "WebSockets not enabled in libcurl\n");94return (CURLcode)1;95#endif /* CURL_DISABLE_WEBSOCKETS */96}979899