Alternative TLS implementation in Go
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

107 lignes
5.3 KiB

  1. // Copyright 2010 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package tls
  5. import (
  6. "testing"
  7. )
  8. func TestRoundUp(t *testing.T) {
  9. if roundUp(0, 16) != 0 ||
  10. roundUp(1, 16) != 16 ||
  11. roundUp(15, 16) != 16 ||
  12. roundUp(16, 16) != 16 ||
  13. roundUp(17, 16) != 32 {
  14. t.Error("roundUp broken")
  15. }
  16. }
  17. var paddingTests = []struct {
  18. in []byte
  19. good bool
  20. expectedLen int
  21. }{
  22. {[]byte{1, 2, 3, 4, 0}, true, 4},
  23. {[]byte{1, 2, 3, 4, 0, 1}, false, 0},
  24. {[]byte{1, 2, 3, 4, 99, 99}, false, 0},
  25. {[]byte{1, 2, 3, 4, 1, 1}, true, 4},
  26. {[]byte{1, 2, 3, 2, 2, 2}, true, 3},
  27. {[]byte{1, 2, 3, 3, 3, 3}, true, 2},
  28. {[]byte{1, 2, 3, 4, 3, 3}, false, 0},
  29. {[]byte{1, 4, 4, 4, 4, 4}, true, 1},
  30. {[]byte{5, 5, 5, 5, 5, 5}, true, 0},
  31. {[]byte{6, 6, 6, 6, 6, 6}, false, 0},
  32. }
  33. func TestRemovePadding(t *testing.T) {
  34. for i, test := range paddingTests {
  35. payload, good := removePadding(test.in)
  36. expectedGood := byte(255)
  37. if !test.good {
  38. expectedGood = 0
  39. }
  40. if good != expectedGood {
  41. t.Errorf("#%d: wrong validity, want:%d got:%d", i, expectedGood, good)
  42. }
  43. if good == 255 && len(payload) != test.expectedLen {
  44. t.Errorf("#%d: got %d, want %d", i, len(payload), test.expectedLen)
  45. }
  46. }
  47. }
  48. var certExampleCom = `308201403081eda003020102020101300b06092a864886f70d010105301e311c301a060355040a131354657374696e67204365727469666963617465301e170d3131313030313138353835325a170d3132303933303138353835325a301e311c301a060355040a131354657374696e67204365727469666963617465305a300b06092a864886f70d010101034b003048024100bced6e32368599eeddf18796bfd03958a154f87e5b084f96e85136a56b886733592f493f0fc68b0d6b3551781cb95e13c5de458b28d6fb60d20a9129313261410203010001a31a301830160603551d11040f300d820b6578616d706c652e636f6d300b06092a864886f70d0101050341001a0b419d2c74474c6450654e5f10b32bf426ffdf55cad1c52602e7a9151513a3424c70f5960dcd682db0c33769cc1daa3fcdd3db10809d2392ed4a1bf50ced18`
  49. var certWildcardExampleCom = `308201423081efa003020102020101300b06092a864886f70d010105301e311c301a060355040a131354657374696e67204365727469666963617465301e170d3131313030313139303034365a170d3132303933303139303034365a301e311c301a060355040a131354657374696e67204365727469666963617465305a300b06092a864886f70d010101034b003048024100bced6e32368599eeddf18796bfd03958a154f87e5b084f96e85136a56b886733592f493f0fc68b0d6b3551781cb95e13c5de458b28d6fb60d20a9129313261410203010001a31c301a30180603551d110411300f820d2a2e6578616d706c652e636f6d300b06092a864886f70d0101050341001676f0c9e7c33c1b656ed5a6476c4e2ee9ec8e62df7407accb1875272b2edd0a22096cb2c22598d11604104d604f810eb4b5987ca6bb319c7e6ce48725c54059`
  50. var certFooExampleCom = `308201443081f1a003020102020101300b06092a864886f70d010105301e311c301a060355040a131354657374696e67204365727469666963617465301e170d3131313030313139303131345a170d3132303933303139303131345a301e311c301a060355040a131354657374696e67204365727469666963617465305a300b06092a864886f70d010101034b003048024100bced6e32368599eeddf18796bfd03958a154f87e5b084f96e85136a56b886733592f493f0fc68b0d6b3551781cb95e13c5de458b28d6fb60d20a9129313261410203010001a31e301c301a0603551d1104133011820f666f6f2e6578616d706c652e636f6d300b06092a864886f70d010105034100646a2a51f2aa2477add854b462cf5207ba16d3213ffb5d3d0eed473fbf09935019192d1d5b8ca6a2407b424cf04d97c4cd9197c83ecf81f0eab9464a1109d09f`
  51. var certDoubleWildcardExampleCom = `308201443081f1a003020102020101300b06092a864886f70d010105301e311c301a060355040a131354657374696e67204365727469666963617465301e170d3131313030313139303134315a170d3132303933303139303134315a301e311c301a060355040a131354657374696e67204365727469666963617465305a300b06092a864886f70d010101034b003048024100bced6e32368599eeddf18796bfd03958a154f87e5b084f96e85136a56b886733592f493f0fc68b0d6b3551781cb95e13c5de458b28d6fb60d20a9129313261410203010001a31e301c301a0603551d1104133011820f2a2e2a2e6578616d706c652e636f6d300b06092a864886f70d0101050341001c3de267975f56ef57771c6218ef95ecc65102e57bd1defe6f7efea90d9b26cf40de5bd7ad75e46201c7f2a92aaa3e907451e9409f65e28ddb6db80d726290f6`
  52. func TestCertificateSelection(t *testing.T) {
  53. config := Config{
  54. Certificates: []Certificate{
  55. {
  56. Certificate: [][]byte{fromHex(certExampleCom)},
  57. },
  58. {
  59. Certificate: [][]byte{fromHex(certWildcardExampleCom)},
  60. },
  61. {
  62. Certificate: [][]byte{fromHex(certFooExampleCom)},
  63. },
  64. {
  65. Certificate: [][]byte{fromHex(certDoubleWildcardExampleCom)},
  66. },
  67. },
  68. }
  69. config.BuildNameToCertificate()
  70. pointerToIndex := func(c *Certificate) int {
  71. for i := range config.Certificates {
  72. if c == &config.Certificates[i] {
  73. return i
  74. }
  75. }
  76. return -1
  77. }
  78. if n := pointerToIndex(config.getCertificateForName("example.com")); n != 0 {
  79. t.Errorf("example.com returned certificate %d, not 0", n)
  80. }
  81. if n := pointerToIndex(config.getCertificateForName("bar.example.com")); n != 1 {
  82. t.Errorf("bar.example.com returned certificate %d, not 1", n)
  83. }
  84. if n := pointerToIndex(config.getCertificateForName("foo.example.com")); n != 2 {
  85. t.Errorf("foo.example.com returned certificate %d, not 2", n)
  86. }
  87. if n := pointerToIndex(config.getCertificateForName("foo.bar.example.com")); n != 3 {
  88. t.Errorf("foo.bar.example.com returned certificate %d, not 3", n)
  89. }
  90. if n := pointerToIndex(config.getCertificateForName("foo.bar.baz.example.com")); n != 0 {
  91. t.Errorf("foo.bar.baz.example.com returned certificate %d, not 0", n)
  92. }
  93. }