| 1 |
package bolt |
| 2 |
|
| 3 |
import ( |
| 4 |
"io/ioutil" |
| 5 |
"math/big" |
| 6 |
"os" |
| 7 |
"reflect" |
| 8 |
"testing" |
| 9 |
|
| 10 |
"github.com/boltdb/bolt" |
| 11 |
) |
| 12 |
|
| 13 |
// createDepot creates a Bolt database in a temporary location. |
| 14 |
func createDB(mode os.FileMode, options *bolt.Options) *Depot { |
| 15 |
// Create temporary path. |
| 16 |
f, _ := ioutil.TempFile("", "bolt-") |
| 17 |
f.Close() |
| 18 |
os.Remove(f.Name()) |
| 19 |
|
| 20 |
db, err := bolt.Open(f.Name(), mode, options) |
| 21 |
if err != nil { |
| 22 |
panic(err.Error()) |
| 23 |
} |
| 24 |
d, err := NewBoltDepot(db) |
| 25 |
if err != nil { |
| 26 |
panic(err.Error()) |
| 27 |
} |
| 28 |
return d |
| 29 |
} |
| 30 |
|
| 31 |
func TestDepot_Serial(t *testing.T) { |
| 32 |
db := createDB(0666, nil) |
| 33 |
tests := []struct { |
| 34 |
name string |
| 35 |
want *big.Int |
| 36 |
wantErr bool |
| 37 |
}{ |
| 38 |
{ |
| 39 |
name: "two is the default value.", |
| 40 |
want: big.NewInt(2), |
| 41 |
}, |
| 42 |
} |
| 43 |
for _, tt := range tests { |
| 44 |
got, err := db.Serial() |
| 45 |
if (err != nil) != tt.wantErr { |
| 46 |
t.Errorf("%q. Depot.Serial() error = %v, wantErr %v", tt.name, err, tt.wantErr) |
| 47 |
continue |
| 48 |
} |
| 49 |
if !reflect.DeepEqual(got, tt.want) { |
| 50 |
t.Errorf("%q. Depot.Serial() = %v, want %v", tt.name, got, tt.want) |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
func TestDepot_writeSerial(t *testing.T) { |
| 56 |
db := createDB(0666, nil) |
| 57 |
type args struct { |
| 58 |
s *big.Int |
| 59 |
} |
| 60 |
tests := []struct { |
| 61 |
name string |
| 62 |
args *big.Int |
| 63 |
wantErr bool |
| 64 |
}{ |
| 65 |
{ |
| 66 |
args: big.NewInt(5), |
| 67 |
}, |
| 68 |
{ |
| 69 |
args: big.NewInt(3), |
| 70 |
}, |
| 71 |
} |
| 72 |
for _, tt := range tests { |
| 73 |
if err := db.writeSerial(tt.args); (err != nil) != tt.wantErr { |
| 74 |
t.Errorf("%q. Depot.writeSerial() error = %v, wantErr %v", tt.name, err, tt.wantErr) |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
func TestDepot_incrementSerial(t *testing.T) { |
| 80 |
db := createDB(0666, nil) |
| 81 |
type args struct { |
| 82 |
s *big.Int |
| 83 |
} |
| 84 |
tests := []struct { |
| 85 |
name string |
| 86 |
args *big.Int |
| 87 |
want *big.Int |
| 88 |
wantErr bool |
| 89 |
}{ |
| 90 |
{ |
| 91 |
args: big.NewInt(2), |
| 92 |
want: big.NewInt(3), |
| 93 |
}, |
| 94 |
{ |
| 95 |
args: big.NewInt(3), |
| 96 |
want: big.NewInt(4), |
| 97 |
}, |
| 98 |
} |
| 99 |
for _, tt := range tests { |
| 100 |
if err := db.incrementSerial(tt.args); (err != nil) != tt.wantErr { |
| 101 |
t.Errorf("%q. Depot.incrementSerial() error = %v, wantErr %v", tt.name, err, tt.wantErr) |
| 102 |
} |
| 103 |
got, _ := db.readSerial() |
| 104 |
if !reflect.DeepEqual(got, tt.want) { |
| 105 |
t.Errorf("%q. Depot.Serial() = %v, want %v", tt.name, got, tt.want) |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
func TestDepot_CreateOrLoadKey(t *testing.T) { |
| 111 |
db := createDB(0666, nil) |
| 112 |
tests := []struct { |
| 113 |
bits int |
| 114 |
wantErr bool |
| 115 |
}{ |
| 116 |
{ |
| 117 |
bits: 1024, |
| 118 |
}, |
| 119 |
{ |
| 120 |
bits: 2048, |
| 121 |
}, |
| 122 |
} |
| 123 |
for i, tt := range tests { |
| 124 |
if _, err := db.CreateOrLoadKey(tt.bits); (err != nil) != tt.wantErr { |
| 125 |
t.Errorf("%d. Depot.CreateOrLoadKey() error = %v, wantErr %v", i, err, tt.wantErr) |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
func TestDepot_CreateOrLoadCA(t *testing.T) { |
| 131 |
db := createDB(0666, nil) |
| 132 |
tests := []struct { |
| 133 |
wantErr bool |
| 134 |
}{ |
| 135 |
{}, |
| 136 |
{}, |
| 137 |
} |
| 138 |
for i, tt := range tests { |
| 139 |
key, err := db.CreateOrLoadKey(1024) |
| 140 |
if err != nil { |
| 141 |
t.Fatalf("%d. Depot.CreateOrLoadKey() error = %v", i, err) |
| 142 |
} |
| 143 |
|
| 144 |
if _, err := db.CreateOrLoadCA(key, 10, "MicroMDM", "US"); (err != nil) != tt.wantErr { |
| 145 |
t.Errorf("%d. Depot.CreateOrLoadCA() error = %v, wantErr %v", i, err, tt.wantErr) |
| 146 |
} |
| 147 |
} |
| 148 |
} |