File: types_test.go

package info (click to toggle)
golang-github-smallstep-crypto 0.70.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 3,848 kB
  • sloc: sh: 66; makefile: 50
file content (113 lines) | stat: -rw-r--r-- 2,761 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package sshutil

import (
	"reflect"
	"testing"
)

func TestCertTypeFromString(t *testing.T) {
	type args struct {
		s string
	}
	tests := []struct {
		name    string
		args    args
		want    CertType
		wantErr bool
	}{
		{"user", args{"user"}, UserCert, false},
		{"USER", args{"USER"}, UserCert, false},
		{"host", args{"host"}, HostCert, false},
		{"Host", args{"Host"}, HostCert, false},
		{" user ", args{" user "}, 0, true},
		{"invalid", args{"invalid"}, 0, true},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got, err := CertTypeFromString(tt.args.s)
			if (err != nil) != tt.wantErr {
				t.Errorf("CertTypeFromString() error = %v, wantErr %v", err, tt.wantErr)
				return
			}
			if got != tt.want {
				t.Errorf("CertTypeFromString() = %v, want %v", got, tt.want)
			}
		})
	}
}

func TestCertType_String(t *testing.T) {
	tests := []struct {
		name string
		c    CertType
		want string
	}{
		{"user", UserCert, "user"},
		{"host", HostCert, "host"},
		{"empty", 100, ""},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := tt.c.String(); got != tt.want {
				t.Errorf("CertType.String() = %v, want %v", got, tt.want)
			}
		})
	}
}

func TestCertType_MarshalJSON(t *testing.T) {
	tests := []struct {
		name    string
		c       CertType
		want    []byte
		wantErr bool
	}{
		{"user", UserCert, []byte(`"user"`), false},
		{"host", HostCert, []byte(`"host"`), false},
		{"error", 100, nil, true},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got, err := tt.c.MarshalJSON()
			if (err != nil) != tt.wantErr {
				t.Errorf("CertType.MarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
				return
			}
			if !reflect.DeepEqual(got, tt.want) {
				t.Errorf("CertType.MarshalJSON() = %v, want %v", got, tt.want)
			}
		})
	}
}

func TestCertType_UnmarshalJSON(t *testing.T) {
	type args struct {
		data []byte
	}
	tests := []struct {
		name    string
		args    args
		want    CertType
		wantErr bool
	}{
		{"user", args{[]byte(`"user"`)}, UserCert, false},
		{"USER", args{[]byte(`"USER"`)}, UserCert, false},
		{"host", args{[]byte(`"host"`)}, HostCert, false},
		{"HosT", args{[]byte(`"HosT"`)}, HostCert, false},
		{" user ", args{[]byte(`" user "`)}, 0, true},
		{"number", args{[]byte(`1`)}, 0, true},
		{"object", args{[]byte(`{}`)}, 0, true},
		{"badJSON", args{[]byte(`"user`)}, 0, true},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			var ct CertType
			if err := ct.UnmarshalJSON(tt.args.data); (err != nil) != tt.wantErr {
				t.Errorf("CertType.UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
			}
			if !reflect.DeepEqual(ct, tt.want) {
				t.Errorf("CertType.UnmarshalJSON() = %v, want %v", ct, tt.want)
			}
		})
	}
}