master
go 171 lines 4.33 KB
Raw
1 package config
2
3 import (
4 "testing"
5 )
6
7 func TestClone(t *testing.T) {
8 c := new(Config)
9 c.Identity.PeerID = "faketest"
10 c.API.HTTPHeaders = map[string][]string{"foo": {"bar"}}
11
12 newCfg, err := c.Clone()
13 if err != nil {
14 t.Fatal(err)
15 }
16 if newCfg.Identity.PeerID != c.Identity.PeerID {
17 t.Fatal("peer ID not preserved")
18 }
19
20 c.API.HTTPHeaders["foo"] = []string{"baz"}
21 if newCfg.API.HTTPHeaders["foo"][0] != "bar" {
22 t.Fatal("HTTP headers not preserved")
23 }
24
25 delete(c.API.HTTPHeaders, "foo")
26 if newCfg.API.HTTPHeaders["foo"][0] != "bar" {
27 t.Fatal("HTTP headers not preserved")
28 }
29 }
30
31 func TestReflectToMap(t *testing.T) {
32 // Helper function to create a test config with various field types
33 reflectedConfig := ReflectToMap(new(Config))
34
35 mapConfig, ok := reflectedConfig.(map[string]any)
36 if !ok {
37 t.Fatal("Config didn't convert to map")
38 }
39
40 reflectedIdentity, ok := mapConfig["Identity"]
41 if !ok {
42 t.Fatal("Identity field not found")
43 }
44
45 mapIdentity, ok := reflectedIdentity.(map[string]any)
46 if !ok {
47 t.Fatal("Identity field didn't convert to map")
48 }
49
50 // Test string field reflection
51 reflectedPeerID, ok := mapIdentity["PeerID"]
52 if !ok {
53 t.Fatal("PeerID field not found in Identity")
54 }
55 if _, ok := reflectedPeerID.(string); !ok {
56 t.Fatal("PeerID field didn't convert to string")
57 }
58
59 // Test omitempty json string field
60 reflectedPrivKey, ok := mapIdentity["PrivKey"]
61 if !ok {
62 t.Fatal("PrivKey omitempty field not found in Identity")
63 }
64 if _, ok := reflectedPrivKey.(string); !ok {
65 t.Fatal("PrivKey omitempty field didn't convert to string")
66 }
67
68 // Test slices field
69 reflectedBootstrap, ok := mapConfig["Bootstrap"]
70 if !ok {
71 t.Fatal("Bootstrap field not found in config")
72 }
73 bootstrap, ok := reflectedBootstrap.([]any)
74 if !ok {
75 t.Fatal("Bootstrap field didn't convert to []string")
76 }
77 if len(bootstrap) != 0 {
78 t.Fatal("Bootstrap len is incorrect")
79 }
80
81 reflectedDatastore, ok := mapConfig["Datastore"]
82 if !ok {
83 t.Fatal("Datastore field not found in config")
84 }
85 datastore, ok := reflectedDatastore.(map[string]any)
86 if !ok {
87 t.Fatal("Datastore field didn't convert to map")
88 }
89 storageGCWatermark, ok := datastore["StorageGCWatermark"]
90 if !ok {
91 t.Fatal("StorageGCWatermark field not found in Datastore")
92 }
93 // Test int field
94 if _, ok := storageGCWatermark.(int64); !ok {
95 t.Fatal("StorageGCWatermark field didn't convert to int64")
96 }
97 noSync, ok := datastore["NoSync"]
98 if !ok {
99 t.Fatal("NoSync field not found in Datastore")
100 }
101 // Test bool field
102 if _, ok := noSync.(bool); !ok {
103 t.Fatal("NoSync field didn't convert to bool")
104 }
105
106 reflectedDNS, ok := mapConfig["DNS"]
107 if !ok {
108 t.Fatal("DNS field not found in config")
109 }
110 DNS, ok := reflectedDNS.(map[string]any)
111 if !ok {
112 t.Fatal("DNS field didn't convert to map")
113 }
114 reflectedResolvers, ok := DNS["Resolvers"]
115 if !ok {
116 t.Fatal("Resolvers field not found in DNS")
117 }
118 // Test map field
119 if _, ok := reflectedResolvers.(map[string]any); !ok {
120 t.Fatal("Resolvers field didn't convert to map")
121 }
122
123 // Test pointer field
124 if _, ok := DNS["MaxCacheTTL"].(map[string]any); !ok {
125 // Since OptionalDuration only field is private, we cannot test it
126 t.Fatal("MaxCacheTTL field didn't convert to map")
127 }
128 }
129
130 // Test validation of options set through "ipfs config"
131 func TestCheckKey(t *testing.T) {
132 err := CheckKey("Foo.Bar")
133 if err == nil {
134 t.Fatal("Foo.Bar isn't a valid key in the config")
135 }
136
137 err = CheckKey("Provide.Strategy")
138 if err != nil {
139 t.Fatalf("%s: %s", err, "Provide.Strategy is a valid key in the config")
140 }
141
142 err = CheckKey("Provide.DHT.MaxWorkers")
143 if err != nil {
144 t.Fatalf("%s: %s", err, "Provide.DHT.MaxWorkers is a valid key in the config")
145 }
146
147 err = CheckKey("Provide.DHT.Interval")
148 if err != nil {
149 t.Fatalf("%s: %s", err, "Provide.DHT.Interval is a valid key in the config")
150 }
151
152 err = CheckKey("Provide.Foo")
153 if err == nil {
154 t.Fatal("Provide.Foo isn't a valid key in the config")
155 }
156
157 err = CheckKey("Gateway.PublicGateways.Foo.Paths")
158 if err != nil {
159 t.Fatalf("%s: %s", err, "Gateway.PublicGateways.Foo.Paths is a valid key in the config")
160 }
161
162 err = CheckKey("Gateway.PublicGateways.Foo.Bar")
163 if err == nil {
164 t.Fatal("Gateway.PublicGateways.Foo.Bar isn't a valid key in the config")
165 }
166
167 err = CheckKey("Plugins.Plugins.peerlog.Config.Enabled")
168 if err != nil {
169 t.Fatalf("%s: %s", err, "Plugins.Plugins.peerlog.Config.Enabled is a valid key in the config")
170 }
171 }