You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

155 rivejä
4.4 KiB

  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. package tag
  16. import (
  17. "context"
  18. "reflect"
  19. "sort"
  20. "testing"
  21. )
  22. func TestEncodeDecode(t *testing.T) {
  23. k1, _ := NewKey("k1")
  24. k2, _ := NewKey("k2")
  25. k3, _ := NewKey("k3 is very weird <>.,?/'\";:`~!@#$%^&*()_-+={[}]|\\")
  26. k4, _ := NewKey("k4")
  27. type keyValue struct {
  28. k Key
  29. v string
  30. }
  31. testCases := []struct {
  32. label string
  33. pairs []keyValue
  34. }{
  35. {
  36. "0",
  37. []keyValue{},
  38. },
  39. {
  40. "1",
  41. []keyValue{
  42. {k1, "v1"},
  43. },
  44. },
  45. {
  46. "2",
  47. []keyValue{
  48. {k1, "v1"},
  49. {k2, "v2"},
  50. },
  51. },
  52. {
  53. "3",
  54. []keyValue{
  55. {k1, "v1"},
  56. {k2, "v2"},
  57. {k3, "v3"},
  58. },
  59. },
  60. {
  61. "4",
  62. []keyValue{
  63. {k1, "v1"},
  64. {k2, "v2"},
  65. {k3, "v3"},
  66. {k4, "v4 is very weird <>.,?/'\";:`~!@#$%^&*()_-+={[}]|\\"},
  67. },
  68. },
  69. }
  70. for _, tc := range testCases {
  71. mods := make([]Mutator, len(tc.pairs))
  72. for i, pair := range tc.pairs {
  73. mods[i] = Upsert(pair.k, pair.v)
  74. }
  75. ctx, err := New(context.Background(), mods...)
  76. if err != nil {
  77. t.Errorf("%v: New = %v", tc.label, err)
  78. }
  79. encoded := Encode(FromContext(ctx))
  80. decoded, err := Decode(encoded)
  81. if err != nil {
  82. t.Errorf("%v: decoding encoded tag map failed: %v", tc.label, err)
  83. }
  84. got := make([]keyValue, 0)
  85. for k, v := range decoded.m {
  86. got = append(got, keyValue{k, string(v)})
  87. }
  88. want := tc.pairs
  89. sort.Slice(got, func(i, j int) bool { return got[i].k.name < got[j].k.name })
  90. sort.Slice(want, func(i, j int) bool { return got[i].k.name < got[j].k.name })
  91. if !reflect.DeepEqual(got, tc.pairs) {
  92. t.Errorf("%v: decoded tag map = %#v; want %#v", tc.label, got, want)
  93. }
  94. }
  95. }
  96. func TestDecode(t *testing.T) {
  97. k1, _ := NewKey("k1")
  98. ctx, _ := New(context.Background(), Insert(k1, "v1"))
  99. tests := []struct {
  100. name string
  101. bytes []byte
  102. want *Map
  103. wantErr bool
  104. }{
  105. {
  106. name: "valid",
  107. bytes: []byte{0, 0, 2, 107, 49, 2, 118, 49},
  108. want: FromContext(ctx),
  109. wantErr: false,
  110. },
  111. {
  112. name: "non-ascii key",
  113. bytes: []byte{0, 0, 2, 107, 49, 2, 118, 49, 0, 2, 107, 25, 2, 118, 49},
  114. want: nil,
  115. wantErr: true,
  116. },
  117. {
  118. name: "non-ascii value",
  119. bytes: []byte{0, 0, 2, 107, 49, 2, 118, 49, 0, 2, 107, 50, 2, 118, 25},
  120. want: nil,
  121. wantErr: true,
  122. },
  123. {
  124. name: "long value",
  125. 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},
  126. want: nil,
  127. wantErr: true,
  128. },
  129. }
  130. for _, tt := range tests {
  131. t.Run(tt.name, func(t *testing.T) {
  132. got, err := Decode(tt.bytes)
  133. if (err != nil) != tt.wantErr {
  134. t.Errorf("Decode() error = %v, wantErr %v", err, tt.wantErr)
  135. return
  136. }
  137. if !reflect.DeepEqual(got, tt.want) {
  138. t.Errorf("Decode() = %v, want %v", got, tt.want)
  139. }
  140. })
  141. }
  142. }