From e41a6c621ce904d5c7a67feca76fe6246d6c0b16 Mon Sep 17 00:00:00 2001 From: Ruchir Rastogi Date: Fri, 14 Feb 2020 10:53:03 -0800 Subject: [PATCH] AStatsEvent_overwriteTimestamp Create a function to overwrite timestamp for LogEvent tests. This function is not exposed outside of the APEX. Test: bit libstatssocket_test:* Change-Id: Id0eac485af60e952cc700f7d1231b1b971400982 --- libstats/socket/include/stats_event.h | 1 + libstats/socket/stats_event.c | 8 ++++++++ libstats/socket/tests/stats_event_test.cpp | 18 ++++++++++++++++++ 3 files changed, 27 insertions(+) diff --git a/libstats/socket/include/stats_event.h b/libstats/socket/include/stats_event.h index 6a2d9cd6e..ff842838b 100644 --- a/libstats/socket/include/stats_event.h +++ b/libstats/socket/include/stats_event.h @@ -152,6 +152,7 @@ void AStatsEvent_addBoolAnnotation(AStatsEvent* event, uint8_t annotationId, boo void AStatsEvent_addInt32Annotation(AStatsEvent* event, uint8_t annotationId, int32_t value); // Internal/test APIs. Should not be exposed outside of the APEX. +void AStatsEvent_overwriteTimestamp(AStatsEvent* event, uint64_t timestampNs); uint32_t AStatsEvent_getAtomId(AStatsEvent* event); // Size is an output parameter. uint8_t* AStatsEvent_getBuffer(AStatsEvent* event, size_t* size); diff --git a/libstats/socket/stats_event.c b/libstats/socket/stats_event.c index a26ac2d6e..b045d9341 100644 --- a/libstats/socket/stats_event.c +++ b/libstats/socket/stats_event.c @@ -115,6 +115,14 @@ void AStatsEvent_setAtomId(AStatsEvent* event, uint32_t atomId) { event->numElements++; } +// Overwrites the timestamp populated in AStatsEvent_obtain with a custom +// timestamp. Should only be called from test code. +void AStatsEvent_overwriteTimestamp(AStatsEvent* event, uint64_t timestampNs) { + memcpy(&event->buf[POS_TIMESTAMP + sizeof(uint8_t)], ×tampNs, sizeof(timestampNs)); + // Do not increment numElements because we already accounted for the timestamp + // within AStatsEvent_obtain. +} + // Side-effect: modifies event->errors if the buffer would overflow static bool overflows(AStatsEvent* event, size_t size) { if (event->size + size > MAX_EVENT_PAYLOAD) { diff --git a/libstats/socket/tests/stats_event_test.cpp b/libstats/socket/tests/stats_event_test.cpp index 48bf4b8e2..69d0a9b75 100644 --- a/libstats/socket/tests/stats_event_test.cpp +++ b/libstats/socket/tests/stats_event_test.cpp @@ -318,3 +318,21 @@ TEST(StatsEventTest, TestOverflowError) { AStatsEvent_release(event); } + +TEST(StatsEventTest, TestOverwriteTimestamp) { + uint32_t atomId = 100; + int64_t expectedTimestamp = 0x123456789; + AStatsEvent* event = AStatsEvent_obtain(); + AStatsEvent_setAtomId(event, atomId); + AStatsEvent_overwriteTimestamp(event, expectedTimestamp); + AStatsEvent_build(event); + + uint8_t* buffer = AStatsEvent_getBuffer(event, NULL); + + // Make sure that the timestamp is being overwritten. + checkMetadata(&buffer, /*numElements=*/0, /*startTime=*/expectedTimestamp, + /*endTime=*/expectedTimestamp, atomId); + + EXPECT_EQ(AStatsEvent_getErrors(event), 0); + AStatsEvent_release(event); +}