Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 1 | /*----------------------------------------------------------------------------*/ |
| 2 | /* Copyright (c) 2018 FIRST. All Rights Reserved. */ |
| 3 | /* Open Source Software - may be modified and shared by FRC teams. The code */ |
| 4 | /* must be accompanied by the FIRST BSD license file in the root directory of */ |
| 5 | /* the project. */ |
| 6 | /*----------------------------------------------------------------------------*/ |
| 7 | |
| 8 | #pragma once |
| 9 | |
| 10 | #include <cstdio> |
| 11 | #include <memory> |
| 12 | #include <vector> |
| 13 | |
| 14 | #include "gtest/gtest.h" |
| 15 | #include "wpi/ArrayRef.h" |
| 16 | #include "wpi/uv/Loop.h" |
| 17 | #include "wpi/uv/Pipe.h" |
| 18 | #include "wpi/uv/Timer.h" |
| 19 | |
| 20 | namespace wpi { |
| 21 | |
| 22 | class WebSocketTest : public ::testing::Test { |
| 23 | public: |
| 24 | static const char* pipeName; |
| 25 | |
| 26 | static void SetUpTestCase(); |
| 27 | |
| 28 | WebSocketTest() { |
| 29 | loop = uv::Loop::Create(); |
| 30 | clientPipe = uv::Pipe::Create(loop); |
| 31 | serverPipe = uv::Pipe::Create(loop); |
| 32 | |
| 33 | serverPipe->Bind(pipeName); |
| 34 | |
| 35 | #if 0 |
| 36 | auto debugTimer = uv::Timer::Create(loop); |
| 37 | debugTimer->timeout.connect([this] { |
| 38 | std::printf("Active handles:\n"); |
| 39 | uv_print_active_handles(loop->GetRaw(), stdout); |
| 40 | }); |
| 41 | debugTimer->Start(uv::Timer::Time{100}, uv::Timer::Time{100}); |
| 42 | debugTimer->Unreference(); |
| 43 | #endif |
| 44 | |
| 45 | auto failTimer = uv::Timer::Create(loop); |
| 46 | failTimer->timeout.connect([this] { |
| 47 | loop->Stop(); |
| 48 | FAIL() << "loop failed to terminate"; |
| 49 | }); |
| 50 | failTimer->Start(uv::Timer::Time{1000}); |
| 51 | failTimer->Unreference(); |
| 52 | } |
| 53 | |
| 54 | ~WebSocketTest() { Finish(); } |
| 55 | |
| 56 | void Finish() { |
| 57 | loop->Walk([](uv::Handle& it) { it.Close(); }); |
| 58 | } |
| 59 | |
| 60 | static std::vector<uint8_t> BuildHeader(uint8_t opcode, bool fin, |
| 61 | bool masking, uint64_t len); |
| 62 | static std::vector<uint8_t> BuildMessage(uint8_t opcode, bool fin, |
| 63 | bool masking, |
| 64 | ArrayRef<uint8_t> data); |
| 65 | static void AdjustMasking(MutableArrayRef<uint8_t> message); |
| 66 | static const uint8_t testMask[4]; |
| 67 | |
| 68 | std::shared_ptr<uv::Loop> loop; |
| 69 | std::shared_ptr<uv::Pipe> clientPipe; |
| 70 | std::shared_ptr<uv::Pipe> serverPipe; |
| 71 | }; |
| 72 | |
| 73 | } // namespace wpi |