Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 

238 rindas
6.5 KiB

  1. /*
  2. *
  3. * Copyright 2014 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package transport
  19. import (
  20. "fmt"
  21. "reflect"
  22. "testing"
  23. "time"
  24. )
  25. func TestTimeoutEncode(t *testing.T) {
  26. for _, test := range []struct {
  27. in string
  28. out string
  29. }{
  30. {"12345678ns", "12345678n"},
  31. {"123456789ns", "123457u"},
  32. {"12345678us", "12345678u"},
  33. {"123456789us", "123457m"},
  34. {"12345678ms", "12345678m"},
  35. {"123456789ms", "123457S"},
  36. {"12345678s", "12345678S"},
  37. {"123456789s", "2057614M"},
  38. {"12345678m", "12345678M"},
  39. {"123456789m", "2057614H"},
  40. } {
  41. d, err := time.ParseDuration(test.in)
  42. if err != nil {
  43. t.Fatalf("failed to parse duration string %s: %v", test.in, err)
  44. }
  45. out := encodeTimeout(d)
  46. if out != test.out {
  47. t.Fatalf("timeoutEncode(%s) = %s, want %s", test.in, out, test.out)
  48. }
  49. }
  50. }
  51. func TestTimeoutDecode(t *testing.T) {
  52. for _, test := range []struct {
  53. // input
  54. s string
  55. // output
  56. d time.Duration
  57. err error
  58. }{
  59. {"1234S", time.Second * 1234, nil},
  60. {"1234x", 0, fmt.Errorf("transport: timeout unit is not recognized: %q", "1234x")},
  61. {"1", 0, fmt.Errorf("transport: timeout string is too short: %q", "1")},
  62. {"", 0, fmt.Errorf("transport: timeout string is too short: %q", "")},
  63. } {
  64. d, err := decodeTimeout(test.s)
  65. if d != test.d || fmt.Sprint(err) != fmt.Sprint(test.err) {
  66. t.Fatalf("timeoutDecode(%q) = %d, %v, want %d, %v", test.s, int64(d), err, int64(test.d), test.err)
  67. }
  68. }
  69. }
  70. func TestContentSubtype(t *testing.T) {
  71. tests := []struct {
  72. contentType string
  73. want string
  74. wantValid bool
  75. }{
  76. {"application/grpc", "", true},
  77. {"application/grpc+", "", true},
  78. {"application/grpc+blah", "blah", true},
  79. {"application/grpc;", "", true},
  80. {"application/grpc;blah", "blah", true},
  81. {"application/grpcd", "", false},
  82. {"application/grpd", "", false},
  83. {"application/grp", "", false},
  84. }
  85. for _, tt := range tests {
  86. got, gotValid := contentSubtype(tt.contentType)
  87. if got != tt.want || gotValid != tt.wantValid {
  88. t.Errorf("contentSubtype(%q) = (%v, %v); want (%v, %v)", tt.contentType, got, gotValid, tt.want, tt.wantValid)
  89. }
  90. }
  91. }
  92. func TestEncodeGrpcMessage(t *testing.T) {
  93. for _, tt := range []struct {
  94. input string
  95. expected string
  96. }{
  97. {"", ""},
  98. {"Hello", "Hello"},
  99. {"\u0000", "%00"},
  100. {"%", "%25"},
  101. {"系统", "%E7%B3%BB%E7%BB%9F"},
  102. {string([]byte{0xff, 0xfe, 0xfd}), "%EF%BF%BD%EF%BF%BD%EF%BF%BD"},
  103. } {
  104. actual := encodeGrpcMessage(tt.input)
  105. if tt.expected != actual {
  106. t.Errorf("encodeGrpcMessage(%q) = %q, want %q", tt.input, actual, tt.expected)
  107. }
  108. }
  109. // make sure that all the visible ASCII chars except '%' are not percent encoded.
  110. for i := ' '; i <= '~' && i != '%'; i++ {
  111. output := encodeGrpcMessage(string(i))
  112. if output != string(i) {
  113. t.Errorf("encodeGrpcMessage(%v) = %v, want %v", string(i), output, string(i))
  114. }
  115. }
  116. // make sure that all the invisible ASCII chars and '%' are percent encoded.
  117. for i := rune(0); i == '%' || (i >= rune(0) && i < ' ') || (i > '~' && i <= rune(127)); i++ {
  118. output := encodeGrpcMessage(string(i))
  119. expected := fmt.Sprintf("%%%02X", i)
  120. if output != expected {
  121. t.Errorf("encodeGrpcMessage(%v) = %v, want %v", string(i), output, expected)
  122. }
  123. }
  124. }
  125. func TestDecodeGrpcMessage(t *testing.T) {
  126. for _, tt := range []struct {
  127. input string
  128. expected string
  129. }{
  130. {"", ""},
  131. {"Hello", "Hello"},
  132. {"H%61o", "Hao"},
  133. {"H%6", "H%6"},
  134. {"%G0", "%G0"},
  135. {"%E7%B3%BB%E7%BB%9F", "系统"},
  136. {"%EF%BF%BD", "�"},
  137. } {
  138. actual := decodeGrpcMessage(tt.input)
  139. if tt.expected != actual {
  140. t.Errorf("decodeGrpcMessage(%q) = %q, want %q", tt.input, actual, tt.expected)
  141. }
  142. }
  143. // make sure that all the visible ASCII chars except '%' are not percent decoded.
  144. for i := ' '; i <= '~' && i != '%'; i++ {
  145. output := decodeGrpcMessage(string(i))
  146. if output != string(i) {
  147. t.Errorf("decodeGrpcMessage(%v) = %v, want %v", string(i), output, string(i))
  148. }
  149. }
  150. // make sure that all the invisible ASCII chars and '%' are percent decoded.
  151. for i := rune(0); i == '%' || (i >= rune(0) && i < ' ') || (i > '~' && i <= rune(127)); i++ {
  152. output := decodeGrpcMessage(fmt.Sprintf("%%%02X", i))
  153. if output != string(i) {
  154. t.Errorf("decodeGrpcMessage(%v) = %v, want %v", fmt.Sprintf("%%%02X", i), output, string(i))
  155. }
  156. }
  157. }
  158. // Decode an encoded string should get the same thing back, except for invalid
  159. // utf8 chars.
  160. func TestDecodeEncodeGrpcMessage(t *testing.T) {
  161. testCases := []struct {
  162. orig string
  163. want string
  164. }{
  165. {"", ""},
  166. {"hello", "hello"},
  167. {"h%6", "h%6"},
  168. {"%G0", "%G0"},
  169. {"系统", "系统"},
  170. {"Hello, 世界", "Hello, 世界"},
  171. {string([]byte{0xff, 0xfe, 0xfd}), "���"},
  172. {string([]byte{0xff}) + "Hello" + string([]byte{0xfe}) + "世界" + string([]byte{0xfd}), "�Hello�世界�"},
  173. }
  174. for _, tC := range testCases {
  175. got := decodeGrpcMessage(encodeGrpcMessage(tC.orig))
  176. if got != tC.want {
  177. t.Errorf("decodeGrpcMessage(encodeGrpcMessage(%q)) = %q, want %q", tC.orig, got, tC.want)
  178. }
  179. }
  180. }
  181. const binaryValue = string(128)
  182. func TestEncodeMetadataHeader(t *testing.T) {
  183. for _, test := range []struct {
  184. // input
  185. kin string
  186. vin string
  187. // output
  188. vout string
  189. }{
  190. {"key", "abc", "abc"},
  191. {"KEY", "abc", "abc"},
  192. {"key-bin", "abc", "YWJj"},
  193. {"key-bin", binaryValue, "woA"},
  194. } {
  195. v := encodeMetadataHeader(test.kin, test.vin)
  196. if !reflect.DeepEqual(v, test.vout) {
  197. t.Fatalf("encodeMetadataHeader(%q, %q) = %q, want %q", test.kin, test.vin, v, test.vout)
  198. }
  199. }
  200. }
  201. func TestDecodeMetadataHeader(t *testing.T) {
  202. for _, test := range []struct {
  203. // input
  204. kin string
  205. vin string
  206. // output
  207. vout string
  208. err error
  209. }{
  210. {"a", "abc", "abc", nil},
  211. {"key-bin", "Zm9vAGJhcg==", "foo\x00bar", nil},
  212. {"key-bin", "Zm9vAGJhcg", "foo\x00bar", nil},
  213. {"key-bin", "woA=", binaryValue, nil},
  214. {"a", "abc,efg", "abc,efg", nil},
  215. } {
  216. v, err := decodeMetadataHeader(test.kin, test.vin)
  217. if !reflect.DeepEqual(v, test.vout) || !reflect.DeepEqual(err, test.err) {
  218. t.Fatalf("decodeMetadataHeader(%q, %q) = %q, %v, want %q, %v", test.kin, test.vin, v, err, test.vout, test.err)
  219. }
  220. }
  221. }