client_test.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package client
  2. import (
  3. "crypto/tls"
  4. "github.com/cloudflare/cfssl/auth"
  5. "github.com/cloudflare/cfssl/helpers"
  6. "net"
  7. "strings"
  8. "testing"
  9. )
  10. var (
  11. testProvider auth.Provider
  12. testKey = "0123456789ABCDEF0123456789ABCDEF"
  13. testAD = []byte{1, 2, 3, 4} // IP address 1.2.3.4
  14. )
  15. func TestNewServer(t *testing.T) {
  16. s := NewServer("1.1.1.1:::123456789")
  17. if s != nil {
  18. t.Fatalf("fatal error, server created with too many colons %v", s)
  19. }
  20. s2 := NewServer("1.1.1.1:[]")
  21. if s != nil {
  22. t.Fatalf("%v", s2)
  23. }
  24. _, port, _ := net.SplitHostPort("")
  25. if port != "" {
  26. t.Fatalf("%v", port)
  27. }
  28. s = NewServer("http://127.0.0.1:8888")
  29. hosts := s.Hosts()
  30. if len(hosts) != 1 || hosts[0] != "http://127.0.0.1:8888" {
  31. t.Fatalf("expected [http://127.0.0.1:8888], but have %v", hosts)
  32. }
  33. s = NewServer("http://1.1.1.1:9999")
  34. hosts = s.Hosts()
  35. if len(hosts) != 1 || hosts[0] != "http://1.1.1.1:9999" {
  36. t.Fatalf("expected [http://1.1.1.1:9999], but have %v", hosts)
  37. }
  38. s = NewServer("https://1.1.1.1:8080")
  39. hosts = s.Hosts()
  40. if len(hosts) != 1 || hosts[0] != "https://1.1.1.1:8080" {
  41. t.Fatalf("expected [https://1.1.1.1:8080], but have %v", hosts)
  42. }
  43. }
  44. func TestInvalidPort(t *testing.T) {
  45. s := NewServer("1.1.1.1:99999999999999999999999999999")
  46. if s != nil {
  47. t.Fatalf("%v", s)
  48. }
  49. }
  50. func TestAuthSign(t *testing.T) {
  51. s := NewServer(".X")
  52. testProvider, _ = auth.New(testKey, nil)
  53. testRequest := []byte(`testing 1 2 3`)
  54. as, err := s.AuthSign(testRequest, testAD, testProvider)
  55. if as != nil || err == nil {
  56. t.Fatal("expected error with auth sign function")
  57. }
  58. }
  59. func TestDefaultAuthSign(t *testing.T) {
  60. testProvider, _ = auth.New(testKey, nil)
  61. s := NewAuthServer(".X", nil, testProvider)
  62. testRequest := []byte(`testing 1 2 3`)
  63. as, err := s.Sign(testRequest)
  64. if as != nil || err == nil {
  65. t.Fatal("expected error with auth sign function")
  66. }
  67. }
  68. func TestSign(t *testing.T) {
  69. s := NewServer(".X")
  70. sign, err := s.Sign([]byte{5, 5, 5, 5})
  71. if sign != nil || err == nil {
  72. t.Fatalf("expected error with sign function")
  73. }
  74. }
  75. func TestNewMutualTLSServer(t *testing.T) {
  76. cert, _ := helpers.LoadClientCertificate("../../helpers/testdata/ca.pem", "../../helpers/testdata/ca_key.pem")
  77. s := NewServerTLS("https://nohost:8888", helpers.CreateTLSConfig(nil, cert))
  78. if s == nil {
  79. t.Fatalf("fatal error, empty server")
  80. }
  81. _, err := s.Sign([]byte{5, 5, 5, 5})
  82. if err == nil {
  83. t.Fatalf("expected error with sign function")
  84. }
  85. if !(strings.Contains(err.Error(), "Post")) && !(strings.Contains(err.Error(), "https://nohost:8888/api/v1/cfssl/sign")) && !(strings.Contains(err.Error(), "dial tcp: lookup nohost: no such host")) {
  86. t.Fatalf("no error message %v", err)
  87. }
  88. }
  89. func TestNewServerGroup(t *testing.T) {
  90. s := NewServer("cfssl1.local:8888, cfssl2.local:8888, http://cfssl3.local:8888, http://cfssl4.local:8888")
  91. ogl, ok := s.(*orderedListGroup)
  92. if !ok {
  93. t.Fatalf("expected NewServer to return an ordered group list with a list of servers, instead got a %T = %+v", ogl, ogl)
  94. }
  95. if len(ogl.remotes) != 4 {
  96. t.Fatalf("expected the remote to have four servers, but it has %d", len(ogl.remotes))
  97. }
  98. hosts := ogl.Hosts()
  99. if len(hosts) != 4 {
  100. t.Fatalf("expected 2 hosts in the group, but have %d", len(hosts))
  101. }
  102. if hosts[0] != "http://cfssl1.local:8888" {
  103. t.Fatalf("expected to see http://cfssl1.local:8888, but saw %s",
  104. hosts[0])
  105. }
  106. if hosts[1] != "http://cfssl2.local:8888" {
  107. t.Fatalf("expected to see http://cfssl2.local:8888, but saw %s",
  108. hosts[1])
  109. }
  110. if hosts[2] != "http://cfssl3.local:8888" {
  111. t.Fatalf("expected to see http://cfssl1.local:8888, but saw %s",
  112. hosts[2])
  113. }
  114. if hosts[3] != "http://cfssl4.local:8888" {
  115. t.Fatalf("expected to see http://cfssl2.local:8888, but saw %s",
  116. hosts[3])
  117. }
  118. }
  119. func TestNewTLSServerGroup(t *testing.T) {
  120. NewTLSServerGroup(t, nil)
  121. }
  122. func TestNewMutualTLSServerGroup(t *testing.T) {
  123. cert, _ := helpers.LoadClientCertificate("../../helpers/testdata/ca.pem", "../../helpers/testdata/ca_key.pem")
  124. NewTLSServerGroup(t, cert)
  125. }
  126. func NewTLSServerGroup(t *testing.T, cert *tls.Certificate) {
  127. s := NewServerTLS("https://cfssl1.local:8888, https://cfssl2.local:8888", helpers.CreateTLSConfig(nil, cert))
  128. ogl, ok := s.(*orderedListGroup)
  129. if !ok {
  130. t.Fatalf("expected NewServer to return an ordered group list with a list of servers, instead got a %T = %+v", ogl, ogl)
  131. }
  132. if len(ogl.remotes) != 2 {
  133. t.Fatalf("expected the remote to have two servers, but it has %d", len(ogl.remotes))
  134. }
  135. hosts := ogl.Hosts()
  136. if len(hosts) != 2 {
  137. t.Fatalf("expected 2 hosts in the group, but have %d", len(hosts))
  138. }
  139. if hosts[0] != "https://cfssl1.local:8888" {
  140. t.Fatalf("expected to see https://cfssl1.local:8888, but saw %s",
  141. hosts[0])
  142. }
  143. if hosts[1] != "https://cfssl2.local:8888" {
  144. t.Fatalf("expected to see https://cfssl2.local:8888, but saw %s",
  145. hosts[1])
  146. }
  147. }
  148. func TestNewOGLGroup(t *testing.T) {
  149. strategy := StrategyFromString("ordered_list")
  150. if strategy == StrategyInvalid {
  151. t.Fatal("expected StrategyOrderedList as selected strategy but have StrategyInvalid")
  152. }
  153. if strategy != StrategyOrderedList {
  154. t.Fatalf("expected StrategyOrderedList (%d) but have %d", StrategyOrderedList, strategy)
  155. }
  156. rem, err := NewGroup([]string{"ca1.local,", "ca2.local"}, nil, strategy)
  157. if err != nil {
  158. t.Fatalf("%v", err)
  159. }
  160. ogl, ok := rem.(*orderedListGroup)
  161. if !ok {
  162. t.Fatalf("expected to get an orderedListGroup but got %T", rem)
  163. }
  164. if len(ogl.remotes) != 2 {
  165. t.Fatalf("expected two remotes in the ordered group list but have %d", len(ogl.remotes))
  166. }
  167. }