tracestate_test.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. // Copyright 2018, 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. package tracestate
  15. import (
  16. "fmt"
  17. "testing"
  18. )
  19. func checkFront(t *testing.T, tracestate *Tracestate, wantKey, testname string) {
  20. gotKey := tracestate.entries[0].Key
  21. if gotKey != wantKey {
  22. t.Errorf("test:%s: first entry in the list: got %q want %q", testname, gotKey, wantKey)
  23. }
  24. }
  25. func checkBack(t *testing.T, tracestate *Tracestate, wantKey, testname string) {
  26. gotKey := tracestate.entries[len(tracestate.entries)-1].Key
  27. if gotKey != wantKey {
  28. t.Errorf("test:%s: last entry in the list: got %q want %q", testname, gotKey, wantKey)
  29. }
  30. }
  31. func checkSize(t *testing.T, tracestate *Tracestate, wantSize int, testname string) {
  32. if gotSize := len(tracestate.entries); gotSize != wantSize {
  33. t.Errorf("test:%s: size of the list: got %q want %q", testname, gotSize, wantSize)
  34. }
  35. }
  36. func (ts *Tracestate) get(key string) (string, bool) {
  37. if ts == nil {
  38. return "", false
  39. }
  40. for _, entry := range ts.entries {
  41. if entry.Key == key {
  42. return entry.Value, true
  43. }
  44. }
  45. return "", false
  46. }
  47. func checkKeyValue(t *testing.T, tracestate *Tracestate, key, wantValue, testname string) {
  48. wantOk := true
  49. if wantValue == "" {
  50. wantOk = false
  51. }
  52. gotValue, gotOk := tracestate.get(key)
  53. if wantOk != gotOk || gotValue != wantValue {
  54. t.Errorf("test:%s: get value for key=%s failed: got %q want %q", testname, key, gotValue, wantValue)
  55. }
  56. }
  57. func checkError(t *testing.T, tracestate *Tracestate, err error, testname, msg string) {
  58. if err != nil {
  59. t.Errorf("test:%s: %s: tracestate=%v, error= %v", testname, msg, tracestate, err)
  60. }
  61. }
  62. func wantError(t *testing.T, tracestate *Tracestate, err error, testname, msg string) {
  63. if err == nil {
  64. t.Errorf("test:%s: %s: tracestate=%v, error=%v", testname, msg, tracestate, err)
  65. }
  66. }
  67. func TestCreateWithNullParent(t *testing.T) {
  68. key1, value1 := "hello", "world"
  69. testname := "TestCreateWithNullParent"
  70. entry := Entry{key1, value1}
  71. tracestate, err := New(nil, entry)
  72. checkError(t, tracestate, err, testname, "create failed from null parent")
  73. checkKeyValue(t, tracestate, key1, value1, testname)
  74. }
  75. func TestCreateFromParentWithSingleKey(t *testing.T) {
  76. key1, value1, key2, value2 := "hello", "world", "foo", "bar"
  77. testname := "TestCreateFromParentWithSingleKey"
  78. entry1 := Entry{key1, value1}
  79. entry2 := Entry{key2, value2}
  80. parent, _ := New(nil, entry1)
  81. tracestate, err := New(parent, entry2)
  82. checkError(t, tracestate, err, testname, "create failed from parent with single key")
  83. checkKeyValue(t, tracestate, key2, value2, testname)
  84. checkFront(t, tracestate, key2, testname)
  85. checkBack(t, tracestate, key1, testname)
  86. }
  87. func TestCreateFromParentWithDoubleKeys(t *testing.T) {
  88. key1, value1, key2, value2, key3, value3 := "hello", "world", "foo", "bar", "bar", "baz"
  89. testname := "TestCreateFromParentWithDoubleKeys"
  90. entry1 := Entry{key1, value1}
  91. entry2 := Entry{key2, value2}
  92. entry3 := Entry{key3, value3}
  93. parent, _ := New(nil, entry2, entry1)
  94. tracestate, err := New(parent, entry3)
  95. checkError(t, tracestate, err, testname, "create failed from parent with double keys")
  96. checkKeyValue(t, tracestate, key3, value3, testname)
  97. checkFront(t, tracestate, key3, testname)
  98. checkBack(t, tracestate, key1, testname)
  99. }
  100. func TestCreateFromParentWithExistingKey(t *testing.T) {
  101. key1, value1, key2, value2, key3, value3 := "hello", "world", "foo", "bar", "hello", "baz"
  102. testname := "TestCreateFromParentWithExistingKey"
  103. entry1 := Entry{key1, value1}
  104. entry2 := Entry{key2, value2}
  105. entry3 := Entry{key3, value3}
  106. parent, _ := New(nil, entry2, entry1)
  107. tracestate, err := New(parent, entry3)
  108. checkError(t, tracestate, err, testname, "create failed with an existing key")
  109. checkKeyValue(t, tracestate, key3, value3, testname)
  110. checkFront(t, tracestate, key3, testname)
  111. checkBack(t, tracestate, key2, testname)
  112. checkSize(t, tracestate, 2, testname)
  113. }
  114. func TestImplicitImmutableTracestate(t *testing.T) {
  115. key1, value1, key2, value2, key3, value3 := "hello", "world", "hello", "bar", "foo", "baz"
  116. testname := "TestImplicitImmutableTracestate"
  117. entry1 := Entry{key1, value1}
  118. entry2 := Entry{key2, value2}
  119. parent, _ := New(nil, entry1)
  120. tracestate, err := New(parent, entry2)
  121. checkError(t, tracestate, err, testname, "create failed")
  122. checkKeyValue(t, tracestate, key2, value2, testname)
  123. checkKeyValue(t, parent, key2, value1, testname)
  124. // Get and update entries.
  125. entries := tracestate.Entries()
  126. entry := Entry{key3, value3}
  127. entries = append(entries, entry)
  128. // Check Tracestate does not have key3.
  129. checkKeyValue(t, tracestate, key3, "", testname)
  130. // Check that we added the key3 in the entries
  131. tracestate, err = New(nil, entries...)
  132. checkError(t, tracestate, err, testname, "create failed")
  133. checkKeyValue(t, tracestate, key3, value3, testname)
  134. }
  135. func TestKeyWithValidChar(t *testing.T) {
  136. testname := "TestKeyWithValidChar"
  137. arrayRune := []rune("")
  138. for c := 'a'; c <= 'z'; c++ {
  139. arrayRune = append(arrayRune, c)
  140. }
  141. for c := '0'; c <= '9'; c++ {
  142. arrayRune = append(arrayRune, c)
  143. }
  144. arrayRune = append(arrayRune, '_')
  145. arrayRune = append(arrayRune, '-')
  146. arrayRune = append(arrayRune, '*')
  147. arrayRune = append(arrayRune, '/')
  148. key := string(arrayRune)
  149. entry := Entry{key, "world"}
  150. tracestate, err := New(nil, entry)
  151. checkError(t, tracestate, err, testname, "create failed when the key contains all valid characters")
  152. }
  153. func TestKeyWithInvalidChar(t *testing.T) {
  154. testname := "TestKeyWithInvalidChar"
  155. keys := []string{"1ab", "1ab2", "Abc", " abc", "a=b"}
  156. for _, key := range keys {
  157. entry := Entry{key, "world"}
  158. tracestate, err := New(nil, entry)
  159. wantError(t, tracestate, err, testname, fmt.Sprintf(
  160. "create did not err with invalid key=%q", key))
  161. }
  162. }
  163. func TestNilKey(t *testing.T) {
  164. testname := "TestNilKey"
  165. entry := Entry{"", "world"}
  166. tracestate, err := New(nil, entry)
  167. wantError(t, tracestate, err, testname, "create did not err when the key is nil (\"\")")
  168. }
  169. func TestValueWithInvalidChar(t *testing.T) {
  170. testname := "TestValueWithInvalidChar"
  171. keys := []string{"A=B", "A,B", "AB "}
  172. for _, value := range keys {
  173. entry := Entry{"hello", value}
  174. tracestate, err := New(nil, entry)
  175. wantError(t, tracestate, err, testname,
  176. fmt.Sprintf("create did not err when the value is invalid (%q)", value))
  177. }
  178. }
  179. func TestNilValue(t *testing.T) {
  180. testname := "TestNilValue"
  181. tracestate, err := New(nil, Entry{"hello", ""})
  182. wantError(t, tracestate, err, testname, "create did not err when the value is nil (\"\")")
  183. }
  184. func TestInvalidKeyLen(t *testing.T) {
  185. testname := "TestInvalidKeyLen"
  186. arrayRune := []rune("")
  187. for i := 0; i <= keyMaxSize+1; i++ {
  188. arrayRune = append(arrayRune, 'a')
  189. }
  190. key := string(arrayRune)
  191. tracestate, err := New(nil, Entry{key, "world"})
  192. wantError(t, tracestate, err, testname,
  193. fmt.Sprintf("create did not err when the length (%d) of the key is larger than max (%d)",
  194. len(key), keyMaxSize))
  195. }
  196. func TestInvalidValueLen(t *testing.T) {
  197. testname := "TestInvalidValueLen"
  198. arrayRune := []rune("")
  199. for i := 0; i <= valueMaxSize+1; i++ {
  200. arrayRune = append(arrayRune, 'a')
  201. }
  202. value := string(arrayRune)
  203. tracestate, err := New(nil, Entry{"hello", value})
  204. wantError(t, tracestate, err, testname,
  205. fmt.Sprintf("create did not err when the length (%d) of the value is larger than max (%d)",
  206. len(value), valueMaxSize))
  207. }
  208. func TestCreateFromArrayWithOverLimitKVPairs(t *testing.T) {
  209. testname := "TestCreateFromArrayWithOverLimitKVPairs"
  210. entries := []Entry{}
  211. for i := 0; i <= maxKeyValuePairs; i++ {
  212. key := fmt.Sprintf("a%db", i)
  213. entry := Entry{key, "world"}
  214. entries = append(entries, entry)
  215. }
  216. tracestate, err := New(nil, entries...)
  217. wantError(t, tracestate, err, testname,
  218. fmt.Sprintf("create did not err when the number (%d) of key-value pairs is larger than max (%d)",
  219. len(entries), maxKeyValuePairs))
  220. }
  221. func TestCreateFromEmptyArray(t *testing.T) {
  222. testname := "TestCreateFromEmptyArray"
  223. tracestate, err := New(nil, nil...)
  224. checkError(t, tracestate, err, testname,
  225. "failed to create nil tracestate")
  226. }
  227. func TestCreateFromParentWithOverLimitKVPairs(t *testing.T) {
  228. testname := "TestCreateFromParentWithOverLimitKVPairs"
  229. entries := []Entry{}
  230. for i := 0; i < maxKeyValuePairs; i++ {
  231. key := fmt.Sprintf("a%db", i)
  232. entry := Entry{key, "world"}
  233. entries = append(entries, entry)
  234. }
  235. parent, err := New(nil, entries...)
  236. checkError(t, parent, err, testname, fmt.Sprintf("create failed to add %d key-value pair", maxKeyValuePairs))
  237. // Add one more to go over the limit
  238. key := fmt.Sprintf("a%d", maxKeyValuePairs)
  239. tracestate, err := New(parent, Entry{key, "world"})
  240. wantError(t, tracestate, err, testname,
  241. fmt.Sprintf("create did not err when attempted to exceed the key-value pair limit of %d", maxKeyValuePairs))
  242. }
  243. func TestCreateFromArrayWithDuplicateKeys(t *testing.T) {
  244. key1, value1, key2, value2, key3, value3 := "hello", "world", "foo", "bar", "hello", "baz"
  245. testname := "TestCreateFromArrayWithDuplicateKeys"
  246. entry1 := Entry{key1, value1}
  247. entry2 := Entry{key2, value2}
  248. entry3 := Entry{key3, value3}
  249. tracestate, err := New(nil, entry1, entry2, entry3)
  250. wantError(t, tracestate, err, testname,
  251. "create did not err when entries contained duplicate keys")
  252. }
  253. func TestEntriesWithNil(t *testing.T) {
  254. ts, err := New(nil)
  255. if err != nil {
  256. t.Fatal(err)
  257. }
  258. if got, want := len(ts.Entries()), 0; got != want {
  259. t.Errorf("zero value should have no entries, got %v; want %v", got, want)
  260. }
  261. }