| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package internal |
| 5 |
|
| 6 |
import ( |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/stretchr/testify/assert" |
| 10 |
"github.com/stretchr/testify/require" |
| 11 |
|
| 12 |
"go.opentelemetry.io/collector/pdata/internal/json" |
| 13 |
) |
| 14 |
|
| 15 |
func TestUnmarshalJSON(t *testing.T) { |
| 16 |
iter := json.BorrowIterator(nil) |
| 17 |
defer json.ReturnIterator(iter) |
| 18 |
|
| 19 |
id := [16]byte{} |
| 20 |
unmarshalJSON(id[:], iter.ResetBytes([]byte(`""`))) |
| 21 |
require.NoError(t, iter.Error()) |
| 22 |
assert.Equal(t, [16]byte{}, id) |
| 23 |
|
| 24 |
idBytes := [16]byte{0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78} |
| 25 |
unmarshalJSON(id[:], iter.ResetBytes([]byte(`"12345678123456781234567812345678"`))) |
| 26 |
require.NoError(t, iter.Error()) |
| 27 |
assert.Equal(t, idBytes, id) |
| 28 |
|
| 29 |
unmarshalJSON(id[:], iter.ResetBytes([]byte(`"nothex"`))) |
| 30 |
require.Error(t, iter.Error()) |
| 31 |
|
| 32 |
unmarshalJSON(id[:], iter.ResetBytes([]byte(`"1"`))) |
| 33 |
require.Error(t, iter.Error()) |
| 34 |
|
| 35 |
unmarshalJSON(id[:], iter.ResetBytes([]byte(`"123"`))) |
| 36 |
require.Error(t, iter.Error()) |
| 37 |
|
| 38 |
unmarshalJSON(id[:], iter.ResetBytes([]byte(`"`))) |
| 39 |
require.Error(t, iter.Error()) |
| 40 |
} |