File: depot_test.go

package info (click to toggle)
golang-github-micromdm-scep 2.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 404 kB
  • sloc: makefile: 50; sh: 27
file content (148 lines) | stat: -rw-r--r-- 2,972 bytes parent folder | download | duplicates (2)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package bolt

import (
	"io/ioutil"
	"math/big"
	"os"
	"reflect"
	"testing"

	bolt "go.etcd.io/bbolt"
)

// createDepot creates a Bolt database in a temporary location.
func createDB(mode os.FileMode, options *bolt.Options) *Depot {
	// Create temporary path.
	f, _ := ioutil.TempFile("", "bolt-")
	f.Close()
	os.Remove(f.Name())

	db, err := bolt.Open(f.Name(), mode, options)
	if err != nil {
		panic(err.Error())
	}
	d, err := NewBoltDepot(db)
	if err != nil {
		panic(err.Error())
	}
	return d
}

func TestDepot_Serial(t *testing.T) {
	db := createDB(0666, nil)
	tests := []struct {
		name    string
		want    *big.Int
		wantErr bool
	}{
		{
			name: "two is the default value.",
			want: big.NewInt(2),
		},
	}
	for _, tt := range tests {
		got, err := db.Serial()
		if (err != nil) != tt.wantErr {
			t.Errorf("%q. Depot.Serial() error = %v, wantErr %v", tt.name, err, tt.wantErr)
			continue
		}
		if !reflect.DeepEqual(got, tt.want) {
			t.Errorf("%q. Depot.Serial() = %v, want %v", tt.name, got, tt.want)
		}
	}
}

func TestDepot_writeSerial(t *testing.T) {
	db := createDB(0666, nil)
	type args struct {
		s *big.Int
	}
	tests := []struct {
		name    string
		args    *big.Int
		wantErr bool
	}{
		{
			args: big.NewInt(5),
		},
		{
			args: big.NewInt(3),
		},
	}
	for _, tt := range tests {
		if err := db.writeSerial(tt.args); (err != nil) != tt.wantErr {
			t.Errorf("%q. Depot.writeSerial() error = %v, wantErr %v", tt.name, err, tt.wantErr)
		}
	}
}

func TestDepot_incrementSerial(t *testing.T) {
	db := createDB(0666, nil)
	type args struct {
		s *big.Int
	}
	tests := []struct {
		name    string
		args    *big.Int
		want    *big.Int
		wantErr bool
	}{
		{
			args: big.NewInt(2),
			want: big.NewInt(3),
		},
		{
			args: big.NewInt(3),
			want: big.NewInt(4),
		},
	}
	for _, tt := range tests {
		if err := db.incrementSerial(tt.args); (err != nil) != tt.wantErr {
			t.Errorf("%q. Depot.incrementSerial() error = %v, wantErr %v", tt.name, err, tt.wantErr)
		}
		got, _ := db.readSerial()
		if !reflect.DeepEqual(got, tt.want) {
			t.Errorf("%q. Depot.Serial() = %v, want %v", tt.name, got, tt.want)
		}
	}
}

func TestDepot_CreateOrLoadKey(t *testing.T) {
	db := createDB(0666, nil)
	tests := []struct {
		bits    int
		wantErr bool
	}{
		{
			bits: 1024,
		},
		{
			bits: 2048,
		},
	}
	for i, tt := range tests {
		if _, err := db.CreateOrLoadKey(tt.bits); (err != nil) != tt.wantErr {
			t.Errorf("%d. Depot.CreateOrLoadKey() error = %v, wantErr %v", i, err, tt.wantErr)
		}
	}
}

func TestDepot_CreateOrLoadCA(t *testing.T) {
	db := createDB(0666, nil)
	tests := []struct {
		wantErr bool
	}{
		{},
		{},
	}
	for i, tt := range tests {
		key, err := db.CreateOrLoadKey(1024)
		if err != nil {
			t.Fatalf("%d. Depot.CreateOrLoadKey() error = %v", i, err)
		}

		if _, err := db.CreateOrLoadCA(key, 10, "MicroMDM", "US"); (err != nil) != tt.wantErr {
			t.Errorf("%d. Depot.CreateOrLoadCA() error = %v, wantErr %v", i, err, tt.wantErr)
		}
	}
}