cert_test.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. },
  27. want: &tls.Config{
  28. InsecureSkipVerify: true,
  29. },
  30. wantErr: false,
  31. },
  32. {
  33. name: "no cert/key",
  34. args: args{
  35. Opts: TlsConfigurationOptions{
  36. SkipCertVerify: true,
  37. CertFile: "not_exist.crt",
  38. KeyFile: "not_exist.key",
  39. CaFile: "",
  40. },
  41. },
  42. want: nil,
  43. wantErr: true,
  44. },
  45. {
  46. name: "no cert/key",
  47. args: args{
  48. Opts: TlsConfigurationOptions{
  49. SkipCertVerify: true,
  50. CertFile: "",
  51. KeyFile: "",
  52. CaFile: "not_exist.crt",
  53. },
  54. },
  55. want: nil,
  56. wantErr: true,
  57. },
  58. }
  59. for _, tt := range tests {
  60. t.Run(tt.name, func(t *testing.T) {
  61. got, err := GenerateTLSForClient(tt.args.Opts)
  62. if (err != nil) != tt.wantErr {
  63. t.Errorf("GenerateTLSForClient() error = %v, wantErr %v", err, tt.wantErr)
  64. return
  65. }
  66. if !reflect.DeepEqual(got, tt.want) {
  67. t.Errorf("GenerateTLSForClient() got = %v, want %v", got, tt.want)
  68. }
  69. })
  70. }
  71. }