github.com/instana/go-sensor@v1.62.2-0.20240520081010-4919868049e1/w3ctrace/version_test.go (about)

     1  // (c) Copyright IBM Corp. 2021
     2  // (c) Copyright Instana Inc. 2020
     3  
     4  package w3ctrace_test
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/instana/go-sensor/w3ctrace"
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  func TestParseVersion(t *testing.T) {
    15  	examples := map[string]struct {
    16  		Header   string
    17  		Expected w3ctrace.Version
    18  	}{
    19  		"v0": {
    20  			Header:   "00",
    21  			Expected: w3ctrace.Version_0,
    22  		},
    23  		"vff": {
    24  			Header:   "ff",
    25  			Expected: w3ctrace.Version_Invalid,
    26  		},
    27  	}
    28  
    29  	for name, example := range examples {
    30  		t.Run(name, func(t *testing.T) {
    31  			st, err := w3ctrace.ParseVersion(example.Header)
    32  			require.NoError(t, err)
    33  			assert.Equal(t, example.Expected, st)
    34  		})
    35  	}
    36  }
    37  
    38  func TestParseVersion_Malformed(t *testing.T) {
    39  	examples := map[string]struct {
    40  		Header string
    41  	}{
    42  		"empty": {
    43  			Header: "",
    44  		},
    45  		"too short": {
    46  			Header: "f",
    47  		},
    48  		"too long": {
    49  			Header: "abc",
    50  		},
    51  		"non hex": {
    52  			Header: "xy",
    53  		},
    54  	}
    55  
    56  	for name, example := range examples {
    57  		t.Run(name, func(t *testing.T) {
    58  			_, err := w3ctrace.ParseVersion(example.Header)
    59  			assert.Equal(t, w3ctrace.ErrContextCorrupted, err)
    60  		})
    61  	}
    62  }
    63  
    64  func TestVersion_String(t *testing.T) {
    65  	examples := map[string]struct {
    66  		Version  w3ctrace.Version
    67  		Expected string
    68  	}{
    69  		"v0": {
    70  			Version:  w3ctrace.Version_0,
    71  			Expected: "00",
    72  		},
    73  		"vff": {
    74  			Version:  w3ctrace.Version_Invalid,
    75  			Expected: "ff",
    76  		},
    77  	}
    78  
    79  	for name, example := range examples {
    80  		t.Run(name, func(t *testing.T) {
    81  			assert.Equal(t, example.Expected, example.Version.String())
    82  		})
    83  	}
    84  }