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