Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1 | #include "gzip_helper.hpp" |
| 2 | #include "web_kvm.hpp" |
| 3 | |
Ed Tanous | 9140a67 | 2017-04-24 17:01:32 -0700 | [diff] [blame] | 4 | #include <iostream> |
| 5 | #include <sstream> |
| 6 | #include <vector> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 7 | |
Ed Tanous | 9140a67 | 2017-04-24 17:01:32 -0700 | [diff] [blame] | 8 | #include "crow.h" |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 9 | |
Ed Tanous | 9140a67 | 2017-04-24 17:01:32 -0700 | [diff] [blame] | 10 | #include <gmock/gmock.h> |
| 11 | #include <gtest/gtest.h> |
| 12 | |
| 13 | using namespace crow; |
| 14 | using namespace testing; |
| 15 | |
| 16 | // Tests static files are loaded correctly |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 17 | TEST(Kvm, BasicRfb) |
| 18 | { |
| 19 | return; // TODO(ed) Make hte code below work again |
| 20 | SimpleApp app; |
Ed Tanous | 9140a67 | 2017-04-24 17:01:32 -0700 | [diff] [blame] | 21 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 22 | 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(); |
| 27 | asio::io_service is; |
Ed Tanous | 9140a67 | 2017-04-24 17:01:32 -0700 | [diff] [blame] | 28 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 29 | { |
| 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 | } |
| 43 | catch (std::exception e) |
| 44 | { |
| 45 | // do nothing. We expect this to fail while the server is |
| 46 | // starting up |
| 47 | } |
| 48 | } |
Ed Tanous | 9140a67 | 2017-04-24 17:01:32 -0700 | [diff] [blame] | 49 | } |
Ed Tanous | 9140a67 | 2017-04-24 17:01:32 -0700 | [diff] [blame] | 50 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 51 | // 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 Tanous | 9140a67 | 2017-04-24 17:01:32 -0700 | [diff] [blame] | 62 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 63 | 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 Tanous | 9140a67 | 2017-04-24 17:01:32 -0700 | [diff] [blame] | 67 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 68 | // 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 Tanous | 9140a67 | 2017-04-24 17:01:32 -0700 | [diff] [blame] | 73 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 74 | // Check that Response is OK. |
| 75 | std::istream response_stream(&response); |
| 76 | std::string http_response; |
| 77 | std::getline(response_stream, http_response); |
Ed Tanous | 9140a67 | 2017-04-24 17:01:32 -0700 | [diff] [blame] | 78 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 79 | EXPECT_EQ(http_response, "HTTP/1.1 101 Switching Protocols\r"); |
Ed Tanous | 9140a67 | 2017-04-24 17:01:32 -0700 | [diff] [blame] | 80 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 81 | // Read the Response headers, which are terminated by a blank line. |
| 82 | boost::asio::read_until(socket, response, "\r\n\r\n"); |
Ed Tanous | 9140a67 | 2017-04-24 17:01:32 -0700 | [diff] [blame] | 83 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 84 | // 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 Tanous | 9140a67 | 2017-04-24 17:01:32 -0700 | [diff] [blame] | 91 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 92 | 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 Tanous | 9140a67 | 2017-04-24 17:01:32 -0700 | [diff] [blame] | 102 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 103 | // |
| 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 Tanous | 9140a67 | 2017-04-24 17:01:32 -0700 | [diff] [blame] | 112 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 113 | app.stop(); |
Ed Tanous | 9140a67 | 2017-04-24 17:01:32 -0700 | [diff] [blame] | 114 | } |