cert_test.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package cert
  2. import (
  3. "crypto/tls"
  4. "reflect"
  5. "testing"
  6. )
  7. func TestGenerateTLSForClient(t *testing.T) {
  8. type args struct {
  9. Opts TlsConfigurationOptions
  10. }
  11. tests := []struct {
  12. name string
  13. args args
  14. want *tls.Config
  15. wantErr bool
  16. }{
  17. {
  18. name: "do not set tls",
  19. args: args{
  20. Opts: TlsConfigurationOptions{
  21. SkipCertVerify: true,
  22. CertFile: "",
  23. KeyFile: "",
  24. CaFile: "",
  25. }},
  26. want: &tls.Config{
  27. InsecureSkipVerify: true,
  28. },
  29. wantErr: false,
  30. },
  31. {
  32. name: "no cert/key",
  33. args: args{
  34. Opts: TlsConfigurationOptions{
  35. SkipCertVerify: true,
  36. CertFile: "not_exist.crt",
  37. KeyFile: "not_exist.key",
  38. CaFile: "",
  39. }},
  40. want: nil,
  41. wantErr: true,
  42. },
  43. {
  44. name: "no cert/key",
  45. args: args{
  46. Opts: TlsConfigurationOptions{
  47. SkipCertVerify: true,
  48. CertFile: "",
  49. KeyFile: "",
  50. CaFile: "not_exist.crt",
  51. }},
  52. want: nil,
  53. wantErr: true,
  54. },
  55. }
  56. for _, tt := range tests {
  57. t.Run(tt.name, func(t *testing.T) {
  58. got, err := GenerateTLSForClient(tt.args.Opts)
  59. if (err != nil) != tt.wantErr {
  60. t.Errorf("GenerateTLSForClient() error = %v, wantErr %v", err, tt.wantErr)
  61. return
  62. }
  63. if !reflect.DeepEqual(got, tt.want) {
  64. t.Errorf("GenerateTLSForClient() got = %v, want %v", got, tt.want)
  65. }
  66. })
  67. }
  68. }