aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/conf/store_test.go
blob: fdef7ea7ee5a86b6cce5711fe6cf04ad871781bf (plain) (blame)
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
/* SPDX-License-Identifier: MIT
 *
 * Copyright (C) 2019 WireGuard LLC. All Rights Reserved.
 */

package conf

import (
	"reflect"
	"testing"
)

func TestStorage(t *testing.T) {
	c, err := FromWgQuick(testInput, "golangTest")
	if err != nil {
		t.Errorf("Unable to parse test config: %s", err.Error())
		return
	}

	err = c.Save()
	if err != nil {
		t.Errorf("Unable to save config: %s", err.Error())
	}

	configs, err := ListConfigNames()
	if err != nil {
		t.Errorf("Unable to list configs: %s", err.Error())
	}

	found := false
	for _, name := range configs {
		if name == "golangTest" {
			found = true
			break
		}
	}
	if !found {
		t.Error("Unable to find saved config in list")
	}

	loaded, err := LoadFromName("golangTest")
	if err != nil {
		t.Errorf("Unable to load config: %s", err.Error())
		return
	}

	if !reflect.DeepEqual(loaded, c) {
		t.Error("Loaded config is not the same as saved config")
	}

	k, err := NewPrivateKey()
	if err != nil {
		t.Errorf("Unable to generate new private key: %s", err.Error())
	}
	c.Interface.PrivateKey = *k

	err = c.Save()
	if err != nil {
		t.Errorf("Unable to save config a second time: %s", err.Error())
	}

	loaded, err = LoadFromName("golangTest")
	if err != nil {
		t.Errorf("Unable to load config a second time: %s", err.Error())
		return
	}

	if !reflect.DeepEqual(loaded, c) {
		t.Error("Second loaded config is not the same as second saved config")
	}

	err = DeleteName("golangTest")
	if err != nil {
		t.Errorf("Unable to delete config: %s", err.Error())
	}

	configs, err = ListConfigNames()
	if err != nil {
		t.Errorf("Unable to list configs: %s", err.Error())
	}
	found = false
	for _, name := range configs {
		if name == "golangTest" {
			found = true
			break
		}
	}
	if found {
		t.Error("Config wasn't actually deleted")
	}
}