blob: fda4473d69b7ce608c4e072ae20eaac95abdb160 [file] [log] [blame]
Austin Schuh812d0d12021-11-04 20:16:48 -07001// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
Brian Silverman8fce7482020-01-05 13:18:21 -08004
5#pragma once
6
7#include <cstdio>
8#include <memory>
9#include <vector>
10
11#include "gtest/gtest.h"
Austin Schuh812d0d12021-11-04 20:16:48 -070012#include "wpi/span.h"
Brian Silverman8fce7482020-01-05 13:18:21 -080013#include "wpi/uv/Loop.h"
14#include "wpi/uv/Pipe.h"
15#include "wpi/uv/Timer.h"
16
17namespace wpi {
18
19class WebSocketTest : public ::testing::Test {
20 public:
21 static const char* pipeName;
22
23 static void SetUpTestCase();
24
25 WebSocketTest() {
26 loop = uv::Loop::Create();
27 clientPipe = uv::Pipe::Create(loop);
28 serverPipe = uv::Pipe::Create(loop);
29
30 serverPipe->Bind(pipeName);
31
32#if 0
33 auto debugTimer = uv::Timer::Create(loop);
34 debugTimer->timeout.connect([this] {
35 std::printf("Active handles:\n");
36 uv_print_active_handles(loop->GetRaw(), stdout);
37 });
38 debugTimer->Start(uv::Timer::Time{100}, uv::Timer::Time{100});
39 debugTimer->Unreference();
40#endif
41
42 auto failTimer = uv::Timer::Create(loop);
43 failTimer->timeout.connect([this] {
44 loop->Stop();
45 FAIL() << "loop failed to terminate";
46 });
47 failTimer->Start(uv::Timer::Time{1000});
48 failTimer->Unreference();
49 }
50
Austin Schuh812d0d12021-11-04 20:16:48 -070051 ~WebSocketTest() override { Finish(); }
Brian Silverman8fce7482020-01-05 13:18:21 -080052
53 void Finish() {
54 loop->Walk([](uv::Handle& it) { it.Close(); });
55 }
56
57 static std::vector<uint8_t> BuildHeader(uint8_t opcode, bool fin,
58 bool masking, uint64_t len);
59 static std::vector<uint8_t> BuildMessage(uint8_t opcode, bool fin,
60 bool masking,
Austin Schuh812d0d12021-11-04 20:16:48 -070061 span<const uint8_t> data);
62 static void AdjustMasking(span<uint8_t> message);
Brian Silverman8fce7482020-01-05 13:18:21 -080063 static const uint8_t testMask[4];
64
65 std::shared_ptr<uv::Loop> loop;
66 std::shared_ptr<uv::Pipe> clientPipe;
67 std::shared_ptr<uv::Pipe> serverPipe;
68};
69
70} // namespace wpi