Finn Williams | 2ed809c | 2020-04-20 21:21:07 +0100 | [diff] [blame] | 1 | // |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 2 | // Copyright © 2020 Arm Ltd and Contributors. All rights reserved. |
Finn Williams | 2ed809c | 2020-04-20 21:21:07 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 6 | #include <server/include/basePipeServer/ConnectionHandler.hpp> |
Finn Williams | 2ed809c | 2020-04-20 21:21:07 +0100 | [diff] [blame] | 7 | |
| 8 | #include <SocketProfilingConnection.hpp> |
| 9 | #include <Processes.hpp> |
| 10 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 11 | #include <doctest/doctest.h> |
Finn Williams | 2ed809c | 2020-04-20 21:21:07 +0100 | [diff] [blame] | 12 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 13 | TEST_SUITE("BasePipeServerTests") |
| 14 | { |
Finn Williams | 2ed809c | 2020-04-20 21:21:07 +0100 | [diff] [blame] | 15 | using namespace armnn; |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 16 | using namespace arm::pipe; |
Finn Williams | 2ed809c | 2020-04-20 21:21:07 +0100 | [diff] [blame] | 17 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 18 | TEST_CASE("BasePipeServerTest") |
Finn Williams | 2ed809c | 2020-04-20 21:21:07 +0100 | [diff] [blame] | 19 | { |
| 20 | // Setup the mock service to bind to the UDS. |
| 21 | std::string udsNamespace = "gatord_namespace"; |
| 22 | |
| 23 | // Try to initialize a listening socket through the ConnectionHandler |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 24 | CHECK_NOTHROW(ConnectionHandler connectionHandler(udsNamespace, true)); |
Finn Williams | 2ed809c | 2020-04-20 21:21:07 +0100 | [diff] [blame] | 25 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 26 | // The socket should close once we leave the scope of CHECK_NOTHROW |
Finn Williams | 2ed809c | 2020-04-20 21:21:07 +0100 | [diff] [blame] | 27 | // and socketProfilingConnection should fail to connect |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 28 | CHECK_THROWS_AS(profiling::SocketProfilingConnection socketProfilingConnection, |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 29 | arm::pipe::SocketConnectionException); |
Finn Williams | 2ed809c | 2020-04-20 21:21:07 +0100 | [diff] [blame] | 30 | |
| 31 | // Try to initialize a listening socket through the ConnectionHandler again |
| 32 | ConnectionHandler connectionHandler(udsNamespace, true); |
| 33 | // socketProfilingConnection should connect now |
| 34 | profiling::SocketProfilingConnection socketProfilingConnection; |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 35 | CHECK(socketProfilingConnection.IsOpen()); |
Finn Williams | 2ed809c | 2020-04-20 21:21:07 +0100 | [diff] [blame] | 36 | |
| 37 | auto basePipeServer = connectionHandler.GetNewBasePipeServer(false); |
| 38 | // GetNewBasePipeServer will return null if it fails to create a socket |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 39 | CHECK(basePipeServer.get()); |
Finn Williams | 2ed809c | 2020-04-20 21:21:07 +0100 | [diff] [blame] | 40 | |
| 41 | profiling::BufferManager bufferManager; |
| 42 | profiling::SendCounterPacket sendCounterPacket(bufferManager); |
| 43 | |
| 44 | // Check that we can receive a StreamMetaDataPacket |
| 45 | sendCounterPacket.SendStreamMetaDataPacket(); |
| 46 | |
| 47 | auto packetBuffer = bufferManager.GetReadableBuffer(); |
| 48 | const unsigned char* readBuffer = packetBuffer->GetReadableData(); |
| 49 | unsigned int readBufferSize = packetBuffer->GetSize(); |
| 50 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 51 | CHECK(readBuffer); |
| 52 | CHECK(readBufferSize > 0u); |
Finn Williams | 2ed809c | 2020-04-20 21:21:07 +0100 | [diff] [blame] | 53 | |
| 54 | socketProfilingConnection.WritePacket(readBuffer,readBufferSize); |
| 55 | bufferManager.MarkRead(packetBuffer); |
| 56 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 57 | CHECK(basePipeServer.get()->WaitForStreamMetaData()); |
| 58 | CHECK(basePipeServer.get()->GetStreamMetadataPid() == armnnUtils::Processes::GetCurrentId()); |
| 59 | CHECK(basePipeServer.get()->GetStreamMetadataMaxDataLen() == MAX_METADATA_PACKET_LENGTH); |
Finn Williams | 2ed809c | 2020-04-20 21:21:07 +0100 | [diff] [blame] | 60 | |
| 61 | // Now try a simple PeriodicCounterSelectionPacket |
| 62 | sendCounterPacket.SendPeriodicCounterSelectionPacket(50, {1,2,3,4,5}); |
| 63 | |
| 64 | packetBuffer = bufferManager.GetReadableBuffer(); |
| 65 | readBuffer = packetBuffer->GetReadableData(); |
| 66 | readBufferSize = packetBuffer->GetSize(); |
| 67 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 68 | CHECK(readBuffer); |
| 69 | CHECK(readBufferSize > 0u); |
Finn Williams | 2ed809c | 2020-04-20 21:21:07 +0100 | [diff] [blame] | 70 | |
| 71 | socketProfilingConnection.WritePacket(readBuffer,readBufferSize); |
| 72 | bufferManager.MarkRead(packetBuffer); |
| 73 | |
| 74 | auto packet1 = basePipeServer.get()->WaitForPacket(500); |
| 75 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 76 | CHECK(!packet1.IsEmpty()); |
| 77 | CHECK(packet1.GetPacketFamily() == 0); |
| 78 | CHECK(packet1.GetPacketId() == 4); |
| 79 | CHECK(packet1.GetLength() == 14); |
Finn Williams | 2ed809c | 2020-04-20 21:21:07 +0100 | [diff] [blame] | 80 | |
| 81 | // Try and send the packet back to the client |
| 82 | basePipeServer.get()->SendPacket(packet1.GetPacketFamily(), |
| 83 | packet1.GetPacketId(), |
| 84 | packet1.GetData(), |
| 85 | packet1.GetLength()); |
| 86 | |
| 87 | auto packet2 = socketProfilingConnection.ReadPacket(500); |
| 88 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 89 | CHECK(!packet2.IsEmpty()); |
| 90 | CHECK(packet2.GetPacketFamily() == 0); |
| 91 | CHECK(packet2.GetPacketId() == 4); |
| 92 | CHECK(packet2.GetLength() == 14); |
Finn Williams | 2ed809c | 2020-04-20 21:21:07 +0100 | [diff] [blame] | 93 | |
| 94 | socketProfilingConnection.Close(); |
| 95 | } |
| 96 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 97 | } |