blob: f307d9866882a44c02e39118f7d9bf370918c556 [file] [log] [blame]
Finn Williams2ed809c2020-04-20 21:21:07 +01001//
Jim Flynnbbfe6032020-07-20 16:57:44 +01002// Copyright © 2020 Arm Ltd and Contributors. All rights reserved.
Finn Williams2ed809c2020-04-20 21:21:07 +01003// SPDX-License-Identifier: MIT
4//
5
Jim Flynnbbfe6032020-07-20 16:57:44 +01006#include <server/include/basePipeServer/ConnectionHandler.hpp>
Finn Williams2ed809c2020-04-20 21:21:07 +01007
8#include <SocketProfilingConnection.hpp>
9#include <Processes.hpp>
10
Sadik Armagan1625efc2021-06-10 18:24:34 +010011#include <doctest/doctest.h>
Finn Williams2ed809c2020-04-20 21:21:07 +010012
Sadik Armagan1625efc2021-06-10 18:24:34 +010013TEST_SUITE("BasePipeServerTests")
14{
Finn Williams2ed809c2020-04-20 21:21:07 +010015using namespace armnn;
Jim Flynnbbfe6032020-07-20 16:57:44 +010016using namespace arm::pipe;
Finn Williams2ed809c2020-04-20 21:21:07 +010017
Sadik Armagan1625efc2021-06-10 18:24:34 +010018TEST_CASE("BasePipeServerTest")
Finn Williams2ed809c2020-04-20 21:21:07 +010019{
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 Armagan1625efc2021-06-10 18:24:34 +010024 CHECK_NOTHROW(ConnectionHandler connectionHandler(udsNamespace, true));
Finn Williams2ed809c2020-04-20 21:21:07 +010025
Sadik Armagan1625efc2021-06-10 18:24:34 +010026 // The socket should close once we leave the scope of CHECK_NOTHROW
Finn Williams2ed809c2020-04-20 21:21:07 +010027 // and socketProfilingConnection should fail to connect
Sadik Armagan1625efc2021-06-10 18:24:34 +010028 CHECK_THROWS_AS(profiling::SocketProfilingConnection socketProfilingConnection,
Jim Flynnbbfe6032020-07-20 16:57:44 +010029 arm::pipe::SocketConnectionException);
Finn Williams2ed809c2020-04-20 21:21:07 +010030
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 Armagan1625efc2021-06-10 18:24:34 +010035 CHECK(socketProfilingConnection.IsOpen());
Finn Williams2ed809c2020-04-20 21:21:07 +010036
37 auto basePipeServer = connectionHandler.GetNewBasePipeServer(false);
38 // GetNewBasePipeServer will return null if it fails to create a socket
Sadik Armagan1625efc2021-06-10 18:24:34 +010039 CHECK(basePipeServer.get());
Finn Williams2ed809c2020-04-20 21:21:07 +010040
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 Armagan1625efc2021-06-10 18:24:34 +010051 CHECK(readBuffer);
52 CHECK(readBufferSize > 0u);
Finn Williams2ed809c2020-04-20 21:21:07 +010053
54 socketProfilingConnection.WritePacket(readBuffer,readBufferSize);
55 bufferManager.MarkRead(packetBuffer);
56
Sadik Armagan1625efc2021-06-10 18:24:34 +010057 CHECK(basePipeServer.get()->WaitForStreamMetaData());
58 CHECK(basePipeServer.get()->GetStreamMetadataPid() == armnnUtils::Processes::GetCurrentId());
59 CHECK(basePipeServer.get()->GetStreamMetadataMaxDataLen() == MAX_METADATA_PACKET_LENGTH);
Finn Williams2ed809c2020-04-20 21:21:07 +010060
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 Armagan1625efc2021-06-10 18:24:34 +010068 CHECK(readBuffer);
69 CHECK(readBufferSize > 0u);
Finn Williams2ed809c2020-04-20 21:21:07 +010070
71 socketProfilingConnection.WritePacket(readBuffer,readBufferSize);
72 bufferManager.MarkRead(packetBuffer);
73
74 auto packet1 = basePipeServer.get()->WaitForPacket(500);
75
Sadik Armagan1625efc2021-06-10 18:24:34 +010076 CHECK(!packet1.IsEmpty());
77 CHECK(packet1.GetPacketFamily() == 0);
78 CHECK(packet1.GetPacketId() == 4);
79 CHECK(packet1.GetLength() == 14);
Finn Williams2ed809c2020-04-20 21:21:07 +010080
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 Armagan1625efc2021-06-10 18:24:34 +010089 CHECK(!packet2.IsEmpty());
90 CHECK(packet2.GetPacketFamily() == 0);
91 CHECK(packet2.GetPacketId() == 4);
92 CHECK(packet2.GetLength() == 14);
Finn Williams2ed809c2020-04-20 21:21:07 +010093
94 socketProfilingConnection.Close();
95}
96
Sadik Armagan1625efc2021-06-10 18:24:34 +010097}