Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/sentry_envelope.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,15 @@ sentry_uuid_t
sentry__envelope_get_event_id(const sentry_envelope_t *envelope)
{
if (envelope->is_raw) {
return sentry_uuid_nil();
const char *payload = envelope->contents.raw.payload;
size_t payload_len = envelope->contents.raw.payload_len;
const char *newline = memchr(payload, '\n', payload_len);
size_t header_len = newline ? (size_t)(newline - payload) : payload_len;
sentry_value_t header = sentry__value_from_json(payload, header_len);
sentry_uuid_t event_id = sentry_uuid_from_string(sentry_value_as_string(
sentry_value_get_by_key(header, "event_id")));
sentry_value_decref(header);
return event_id;
}
return sentry_uuid_from_string(sentry_value_as_string(
sentry_value_get_by_key(envelope->contents.items.headers, "event_id")));
Expand Down
4 changes: 2 additions & 2 deletions src/sentry_envelope.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ sentry_envelope_t *sentry__envelope_from_path(const sentry_path_t *path);

/**
* This returns the UUID of the event associated with this envelope.
* If there is no event inside this envelope, or the envelope was previously
* loaded from disk, the empty nil UUID will be returned.
* If there is no event inside this envelope, the empty nil UUID will be
* returned.
*/
sentry_uuid_t sentry__envelope_get_event_id(const sentry_envelope_t *envelope);

Expand Down
59 changes: 59 additions & 0 deletions tests/unit/test_envelopes.c
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,65 @@ SENTRY_TEST(write_raw_envelope_to_file)
sentry_close();
}

SENTRY_TEST(raw_envelope_event_id)
{
sentry_envelope_t *envelope = create_test_envelope();
const char *test_file_str = SENTRY_TEST_PATH_PREFIX "sentry_test_envelope";
sentry_path_t *test_file_path = sentry__path_from_str(test_file_str);
TEST_CHECK_INT_EQUAL(
sentry_envelope_write_to_file(envelope, test_file_str), 0);

sentry_envelope_t *raw_envelope
= sentry__envelope_from_path(test_file_path);
TEST_CHECK(!!raw_envelope);

sentry_uuid_t event_id = sentry__envelope_get_event_id(raw_envelope);
char event_id_str[37];
sentry_uuid_as_string(&event_id, event_id_str);
TEST_CHECK_STRING_EQUAL(
event_id_str, "c993afb6-b4ac-48a6-b61b-2558e601d65d");

sentry__path_remove(test_file_path);
sentry__path_free(test_file_path);
sentry_envelope_free(envelope);
sentry_envelope_free(raw_envelope);

// missing event_id
const char header_no_event_id[]
= "{\"dsn\":\"https://foo@sentry.invalid/42\"}\n{}";
test_file_path = sentry__path_from_str(test_file_str);
TEST_CHECK_INT_EQUAL(
sentry__path_write_buffer(
test_file_path, header_no_event_id, sizeof(header_no_event_id) - 1),
0);
raw_envelope = sentry__envelope_from_path(test_file_path);
TEST_CHECK(!!raw_envelope);
event_id = sentry__envelope_get_event_id(raw_envelope);
TEST_CHECK(sentry_uuid_is_nil(&event_id));
sentry__path_remove(test_file_path);
sentry__path_free(test_file_path);
sentry_envelope_free(raw_envelope);

// missing newline
const char header_no_newline[]
= "{\"event_id\":\"c993afb6-b4ac-48a6-b61b-2558e601d65d\"}";
test_file_path = sentry__path_from_str(test_file_str);
TEST_CHECK_INT_EQUAL(sentry__path_write_buffer(test_file_path,
header_no_newline, sizeof(header_no_newline) - 1),
0);
raw_envelope = sentry__envelope_from_path(test_file_path);
TEST_CHECK(!!raw_envelope);
event_id = sentry__envelope_get_event_id(raw_envelope);
sentry_uuid_as_string(&event_id, event_id_str);
TEST_CHECK_STRING_EQUAL(
event_id_str, "c993afb6-b4ac-48a6-b61b-2558e601d65d");
sentry__path_remove(test_file_path);
sentry__path_free(test_file_path);
sentry_envelope_free(raw_envelope);

sentry_close();
}

SENTRY_TEST(read_envelope_from_file)
{
const char *test_file_str = SENTRY_TEST_PATH_PREFIX "sentry_test_envelope";
Expand Down
1 change: 1 addition & 0 deletions tests/unit/tests.inc
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ XX(procmaps_parser)
XX(propagation_context_init)
XX(query_consent_requirement)
XX(rate_limit_parsing)
XX(raw_envelope_event_id)
XX(read_envelope_from_file)
XX(read_write_envelope_to_file_null)
XX(read_write_envelope_to_invalid_path)
Expand Down
Loading