Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 1 | // 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 Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 4 | |
| 5 | #pragma once |
| 6 | |
| 7 | #include <cstdio> |
| 8 | #include <memory> |
| 9 | #include <vector> |
| 10 | |
| 11 | #include "gtest/gtest.h" |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 12 | #include "wpi/span.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 13 | #include "wpi/uv/Loop.h" |
| 14 | #include "wpi/uv/Pipe.h" |
| 15 | #include "wpi/uv/Timer.h" |
| 16 | |
| 17 | namespace wpi { |
| 18 | |
| 19 | class 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 Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 51 | ~WebSocketTest() override { Finish(); } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 52 | |
| 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 Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 61 | span<const uint8_t> data); |
| 62 | static void AdjustMasking(span<uint8_t> message); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 63 | 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 |