| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package request |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"encoding/binary" |
| 9 |
"testing" |
| 10 |
|
| 11 |
"github.com/stretchr/testify/assert" |
| 12 |
"github.com/stretchr/testify/require" |
| 13 |
|
| 14 |
"go.opentelemetry.io/collector/pdata/plog" |
| 15 |
"go.opentelemetry.io/collector/pdata/pmetric" |
| 16 |
"go.opentelemetry.io/collector/pdata/pprofile" |
| 17 |
"go.opentelemetry.io/collector/pdata/ptrace" |
| 18 |
) |
| 19 |
|
| 20 |
func TestIsRequestPayloadV1(t *testing.T) { |
| 21 |
// too short |
| 22 |
assert.False(t, isRequestPayloadV1([]byte{protoTag1TypeByte, 0x00})) |
| 23 |
|
| 24 |
buf := make([]byte, 5) |
| 25 |
|
| 26 |
// wrong type: field 1, wire type 2 (length-delimited) |
| 27 |
buf[0] = 0x0A |
| 28 |
assert.False(t, isRequestPayloadV1([]byte{protoTag1TypeByte, 0x00})) |
| 29 |
|
| 30 |
// wrong version |
| 31 |
buf[0] = protoTag1TypeByte |
| 32 |
binary.LittleEndian.PutUint32(buf[1:], 2) |
| 33 |
assert.False(t, isRequestPayloadV1(buf)) |
| 34 |
|
| 35 |
binary.LittleEndian.PutUint32(buf[1:], requestFormatVersion) |
| 36 |
assert.True(t, isRequestPayloadV1(buf)) |
| 37 |
|
| 38 |
buf, err := MarshalMetrics(context.Background(), pmetric.NewMetrics()) |
| 39 |
require.NoError(t, err) |
| 40 |
assert.True(t, isRequestPayloadV1(buf)) |
| 41 |
|
| 42 |
buf, err = MarshalTraces(context.Background(), ptrace.NewTraces()) |
| 43 |
require.NoError(t, err) |
| 44 |
assert.True(t, isRequestPayloadV1(buf)) |
| 45 |
|
| 46 |
buf, err = MarshalLogs(context.Background(), plog.NewLogs()) |
| 47 |
require.NoError(t, err) |
| 48 |
assert.True(t, isRequestPayloadV1(buf)) |
| 49 |
|
| 50 |
buf, err = MarshalProfiles(context.Background(), pprofile.NewProfiles()) |
| 51 |
require.NoError(t, err) |
| 52 |
assert.True(t, isRequestPayloadV1(buf)) |
| 53 |
} |