blob: c01b7e5582e6cbc7fa940b38def469ea02b9b375 [file] [log] [blame]
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001#include "crow.h"
2
Ed Tanous1abe55e2018-09-05 08:30:59 -07003#include "gzip_helper.hpp"
4#include "web_kvm.hpp"
5
Ed Tanous9140a672017-04-24 17:01:32 -07006#include <iostream>
7#include <sstream>
8#include <vector>
Ed Tanous1abe55e2018-09-05 08:30:59 -07009
Ed Tanous9140a672017-04-24 17:01:32 -070010#include <gmock/gmock.h>
11#include <gtest/gtest.h>
12
13using namespace crow;
14using namespace testing;
15
16// Tests static files are loaded correctly
Ed Tanous1abe55e2018-09-05 08:30:59 -070017TEST(Kvm, BasicRfb)
18{
Gunnar Millscaa3ce32020-07-08 14:46:53 -050019 return; // TODO(ed) Make the code below work again
Ed Tanous1abe55e2018-09-05 08:30:59 -070020 SimpleApp app;
Ed Tanous9140a672017-04-24 17:01:32 -070021
Ed Tanous1abe55e2018-09-05 08:30:59 -070022 crow::kvm::requestRoutes(app);
23 app.bindaddr("127.0.0.1").port(45451);
24 BMCWEB_ROUTE(app, "/")([]() { return boost::beast::http::status::ok; });
25 auto _ = async(std::launch::async, [&] { app.run(); });
26 auto routes = app.getRoutes();
Ed Tanous8f626352018-12-19 14:51:54 -080027 asio::io_context is;
Ed Tanous9140a672017-04-24 17:01:32 -070028
Ed Tanous1abe55e2018-09-05 08:30:59 -070029 {
30 // Retry a couple of times waiting for the server to come up
31 // TODO(ed) This is really unfortunate, and should use some form of
32 // mock
33 asio::ip::tcp::socket c(is);
34 for (int i = 0; i < 200; i++)
35 {
36 try
37 {
38 c.connect(asio::ip::tcp::endpoint(
39 asio::ip::address::from_string("127.0.0.1"), 45451));
40 c.close();
41 break;
42 }
Patrick Williamsbd078c32021-09-03 08:27:12 -050043 catch (const std::exception& e)
Ed Tanous1abe55e2018-09-05 08:30:59 -070044 {
45 // do nothing. We expect this to fail while the server is
46 // starting up
47 }
48 }
Ed Tanous9140a672017-04-24 17:01:32 -070049 }
Ed Tanous9140a672017-04-24 17:01:32 -070050
Ed Tanous1abe55e2018-09-05 08:30:59 -070051 // Get the websocket
52 std::string sendmsg = ("GET /kvmws HTTP/1.1\r\n"
53 "Host: localhost:45451\r\n"
54 "Connection: Upgrade\r\n"
55 "Upgrade: websocket\r\n"
56 "Sec-WebSocket-Version: 13\r\n"
57 "Sec-WebSocket-Key: aLeGkmLPZmdv5tTyEpJ3jQ==\r\n"
58 "Sec-WebSocket-Extensions: permessage-deflate; "
59 "client_max_window_bits\r\n"
60 "Sec-WebSocket-Protocol: binary\r\n"
61 "\r\n");
Ed Tanous9140a672017-04-24 17:01:32 -070062
Ed Tanous1abe55e2018-09-05 08:30:59 -070063 asio::ip::tcp::socket socket(is);
64 socket.connect(asio::ip::tcp::endpoint(
65 asio::ip::address::from_string("127.0.0.1"), 45451));
66 socket.send(asio::buffer(sendmsg));
Ed Tanous9140a672017-04-24 17:01:32 -070067
Ed Tanous1abe55e2018-09-05 08:30:59 -070068 // Read the Response status line. The Response streambuf will automatically
69 // grow to accommodate the entire line. The growth may be limited by passing
70 // a maximum size to the streambuf constructor.
71 boost::asio::streambuf response;
72 boost::asio::read_until(socket, response, "\r\n");
Ed Tanous9140a672017-04-24 17:01:32 -070073
Ed Tanous1abe55e2018-09-05 08:30:59 -070074 // Check that Response is OK.
75 std::istream response_stream(&response);
76 std::string http_response;
77 std::getline(response_stream, http_response);
Ed Tanous9140a672017-04-24 17:01:32 -070078
Ed Tanous1abe55e2018-09-05 08:30:59 -070079 EXPECT_EQ(http_response, "HTTP/1.1 101 Switching Protocols\r");
Ed Tanous9140a672017-04-24 17:01:32 -070080
Ed Tanous1abe55e2018-09-05 08:30:59 -070081 // Read the Response headers, which are terminated by a blank line.
82 boost::asio::read_until(socket, response, "\r\n\r\n");
Ed Tanous9140a672017-04-24 17:01:32 -070083
Ed Tanous1abe55e2018-09-05 08:30:59 -070084 // Process the Response headers.
85 std::string header;
86 std::vector<std::string> headers;
87 while (std::getline(response_stream, header) && header != "\r")
88 {
89 headers.push_back(header);
90 }
Ed Tanous9140a672017-04-24 17:01:32 -070091
Ed Tanous1abe55e2018-09-05 08:30:59 -070092 EXPECT_THAT(headers, Contains("Upgrade: websocket\r"));
93 EXPECT_THAT(headers, Contains("Connection: Upgrade\r"));
94 EXPECT_THAT(headers, Contains("Sec-WebSocket-Protocol: binary\r"));
95 // TODO(ed) This is the result that it gives today. Need to check websocket
96 // docs and make
97 // sure that this calclution is actually being done to spec
98 EXPECT_THAT(
99 headers,
100 Contains("Sec-WebSocket-Accept: /CnDM3l79rIxniLNyxMryXbtLEU=\r"));
101 std::array<char, 13> rfb_open_string;
Ed Tanous9140a672017-04-24 17:01:32 -0700102
Ed Tanous1abe55e2018-09-05 08:30:59 -0700103 //
104 // socket.receive(rfb_open_string.data(), rfb_open_string.size());
105 boost::asio::read(socket, boost::asio::buffer(rfb_open_string));
106 auto open_string =
107 std::string(std::begin(rfb_open_string), std::end(rfb_open_string));
108 // Todo(ed) find out what the two characters at the end of the websocket
109 // stream are
110 open_string = open_string.substr(2);
111 EXPECT_EQ(open_string, "RFB 003.008");
Ed Tanous9140a672017-04-24 17:01:32 -0700112
Ed Tanous1abe55e2018-09-05 08:30:59 -0700113 app.stop();
Patrick Williamsbd078c32021-09-03 08:27:12 -0500114}