...

Source file src/go.opencensus.io/tag/map_codec_test.go

Documentation: go.opencensus.io/tag

     1  // Copyright 2017, OpenCensus Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  
    16  package tag
    17  
    18  import (
    19  	"context"
    20  	"reflect"
    21  	"sort"
    22  	"testing"
    23  )
    24  
    25  func TestEncodeDecode(t *testing.T) {
    26  	k1, _ := NewKey("k1")
    27  	k2, _ := NewKey("k2")
    28  	k3, _ := NewKey("k3 is very weird <>.,?/'\";:`~!@#$%^&*()_-+={[}]|\\")
    29  	k4, _ := NewKey("k4")
    30  
    31  	type keyValue struct {
    32  		k Key
    33  		v string
    34  	}
    35  
    36  	testCases := []struct {
    37  		label string
    38  		pairs []keyValue
    39  	}{
    40  		{
    41  			"0",
    42  			[]keyValue{},
    43  		},
    44  		{
    45  			"1",
    46  			[]keyValue{
    47  				{k1, "v1"},
    48  			},
    49  		},
    50  		{
    51  			"2",
    52  			[]keyValue{
    53  				{k1, "v1"},
    54  				{k2, "v2"},
    55  			},
    56  		},
    57  		{
    58  			"3",
    59  			[]keyValue{
    60  				{k1, "v1"},
    61  				{k2, "v2"},
    62  				{k3, "v3"},
    63  			},
    64  		},
    65  		{
    66  			"4",
    67  			[]keyValue{
    68  				{k1, "v1"},
    69  				{k2, "v2"},
    70  				{k3, "v3"},
    71  				{k4, "v4 is very weird <>.,?/'\";:`~!@#$%^&*()_-+={[}]|\\"},
    72  			},
    73  		},
    74  	}
    75  
    76  	for _, tc := range testCases {
    77  		mods := make([]Mutator, len(tc.pairs))
    78  		for i, pair := range tc.pairs {
    79  			mods[i] = Upsert(pair.k, pair.v)
    80  		}
    81  		ctx, err := New(context.Background(), mods...)
    82  		if err != nil {
    83  			t.Errorf("%v: New = %v", tc.label, err)
    84  		}
    85  
    86  		encoded := Encode(FromContext(ctx))
    87  		decoded, err := Decode(encoded)
    88  		if err != nil {
    89  			t.Errorf("%v: decoding encoded tag map failed: %v", tc.label, err)
    90  		}
    91  
    92  		got := make([]keyValue, 0)
    93  		for k, v := range decoded.m {
    94  			got = append(got, keyValue{k, v.value})
    95  		}
    96  		want := tc.pairs
    97  
    98  		sort.Slice(got, func(i, j int) bool { return got[i].k.name < got[j].k.name })
    99  		sort.Slice(want, func(i, j int) bool { return got[i].k.name < got[j].k.name })
   100  
   101  		if !reflect.DeepEqual(got, tc.pairs) {
   102  			t.Errorf("%v: decoded tag map = %#v; want %#v", tc.label, got, want)
   103  		}
   104  	}
   105  }
   106  
   107  func TestDecode(t *testing.T) {
   108  	k1, _ := NewKey("k1")
   109  	ctx, _ := New(context.Background(), Insert(k1, "v1"))
   110  
   111  	tests := []struct {
   112  		name    string
   113  		bytes   []byte
   114  		want    *Map
   115  		wantErr bool
   116  	}{
   117  		{
   118  			name:    "valid",
   119  			bytes:   []byte{0, 0, 2, 107, 49, 2, 118, 49},
   120  			want:    FromContext(ctx),
   121  			wantErr: false,
   122  		},
   123  		{
   124  			name:    "non-ascii key",
   125  			bytes:   []byte{0, 0, 2, 107, 49, 2, 118, 49, 0, 2, 107, 25, 2, 118, 49},
   126  			want:    nil,
   127  			wantErr: true,
   128  		},
   129  		{
   130  			name:    "non-ascii value",
   131  			bytes:   []byte{0, 0, 2, 107, 49, 2, 118, 49, 0, 2, 107, 50, 2, 118, 25},
   132  			want:    nil,
   133  			wantErr: true,
   134  		},
   135  		{
   136  			name:    "long value",
   137  			bytes:   []byte{0, 0, 2, 107, 49, 2, 118, 49, 0, 2, 107, 50, 172, 2, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97},
   138  			want:    nil,
   139  			wantErr: true,
   140  		},
   141  	}
   142  	for _, tt := range tests {
   143  		t.Run(tt.name, func(t *testing.T) {
   144  			got, err := Decode(tt.bytes)
   145  			if (err != nil) != tt.wantErr {
   146  				t.Errorf("Decode() error = %v, wantErr %v", err, tt.wantErr)
   147  				return
   148  			}
   149  			if !reflect.DeepEqual(got, tt.want) {
   150  				t.Errorf("Decode() = %v, want %v", got, tt.want)
   151  			}
   152  		})
   153  	}
   154  }
   155  

View as plain text