Skip to content

Commit

Permalink
remove table tests for invalid cases
Browse files Browse the repository at this point in the history
Signed-off-by: Billy Zha <[email protected]>
  • Loading branch information
qweeah committed Mar 25, 2023
1 parent c282d71 commit e8277d7
Showing 1 changed file with 12 additions and 25 deletions.
37 changes: 12 additions & 25 deletions internal/crypto/certificate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@ package crypto

import (
"context"
"crypto/x509"
"encoding/pem"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"reflect"
"testing"
)

Expand Down Expand Up @@ -60,33 +58,22 @@ func TestLoadCertPool(t *testing.T) {
}
}

func TestLoadCertPool_err(t *testing.T) {
func TestLoadCertPool_invalidPem(t *testing.T) {
pemPath := filepath.Join(t.TempDir(), "invalid.pem")
if err := os.WriteFile(pemPath, []byte{}, 0644); err != nil {
t.Fatalf("unexpected error: %v", err)
}
type args struct {
path string
}
tests := []struct {
name string
args args
want *x509.CertPool
wantErr bool
}{
{"should fail if the path is doesn't exist", args{"/???"}, nil, true},
{"should fail if the file is not a pem", args{pemPath}, nil, true},
got, err := LoadCertPool(pemPath)
if err == nil {
t.Errorf("Expecting LoadCertPool to return error for a non-existent pem file, got: %v", got)
return
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := LoadCertPool(tt.args.path)
if (err != nil) != tt.wantErr {
t.Errorf("LoadCertPool() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("LoadCertPool() = %v, want %v", got, tt.want)
}
})
}

func TestLoadCertPool_pemNotExist(t *testing.T) {
got, err := LoadCertPool("/???")
if err == nil {
t.Errorf("Expecting LoadCertPool to return error for a non-existent pem file, got: %v", got)
return
}
}

0 comments on commit e8277d7

Please sign in to comment.