Newer
Older
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
import (
"crypto/rsa"
"dfss/auth"
"fmt"
"os"
"path/filepath"
"testing"
)
var pkey *rsa.PrivateKey
func TestMain(m *testing.M) {
pkey, _ = auth.GeneratePrivateKey(512)
os.Exit(m.Run())
}
func TestGetHomeDir(t *testing.T) {
res := GetHomeDir()
if res == "" {
t.Fatal("Result is empty")
}
}
func TestGenerateRootCA(t *testing.T) {
res, err := GenerateRootCA(365, "country", "organization", "unit", "cn", pkey)
if res == nil || err != nil {
t.Fatal(err)
}
if res[0] != '-' {
t.Fatalf("Bad format\n%s", res)
}
}
func TestInitialize(t *testing.T) {
path := os.TempDir()
keyPath := filepath.Join(path, PkeyFileName)
certPath := filepath.Join(path, RootCAFileName)
err := Initialize(1024, 365, "country", "organization", "unit", "cn", path)
if err != nil {
t.Fatal(err)
}
if _, err = os.Stat(keyPath); os.IsNotExist(err) {
t.Fatal("Private key file couldn't be found")
} else {
}
if _, err = os.Stat(certPath); os.IsNotExist(err) {
t.Fatal("Root certificate file couldn't be found")
} else {
}
}
func ExampleInitialize() {
path := os.TempDir()
keyPath := filepath.Join(path, PkeyFileName)
certPath := filepath.Join(path, RootCAFileName)
err := Initialize(1024, 365, "country", "organization", "unit", "cn", path)
if err != nil {
fmt.Println(err)
}
checkFile(keyPath, "Private key")
checkFile(certPath, "Certificate")
// Output:
// Private key file has been found
// Private key file has been deleted
// Certificate file has been found
// Certificate file has been deleted
}
func checkFile(path, name string) {
if _, err := os.Stat(path); os.IsNotExist(err) {
fmt.Println(name + " file couldn't be found")
} else {
fmt.Println(name + " file has been found")
err = os.Remove(path)
if err != nil {
fmt.Println(err)
} else {
fmt.Println(name + " file has been deleted")
}
}
}
func TestStart(t *testing.T) {
path := os.TempDir()
keyPath := filepath.Join(path, PkeyFileName)
certPath := filepath.Join(path, RootCAFileName)
_ = Initialize(1024, 365, "country", "organization", "unit", "cn", path)
pid, err := Start(path)
if err != nil {
t.Fatal(err)
}
if pid == nil || pid.pkey == nil || pid.rootCA == nil {
t.Fatal("Data was not recovered from saved files")
}
_ = os.Remove(keyPath)
_ = os.Remove(certPath)