@@ -27,3 +27,135 @@ func TestClone(t *testing.T) {
27
27
t .Fatal ("HTTP headers not preserved" )
28
28
}
29
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 ]interface {})
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 ]interface {})
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 .([]interface {})
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 ]interface {})
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 ]interface {})
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 ]interface {}); ! ok {
120
+ t .Fatal ("Resolvers field didn't convert to map" )
121
+ }
122
+
123
+ // Test pointer field
124
+ if _ , ok := DNS ["MaxCacheTTL" ].(map [string ]interface {}); ! 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 ("Provider.Strategy" )
138
+ if err != nil {
139
+ t .Fatalf ("%s: %s" , err , "Provider.Strategy is a valid key in the config" )
140
+ }
141
+
142
+ err = CheckKey ("Provider.Foo" )
143
+ if err == nil {
144
+ t .Fatal ("Provider.Foo isn't a valid key in the config" )
145
+ }
146
+
147
+ err = CheckKey ("Gateway.PublicGateways.Foo.Paths" )
148
+ if err != nil {
149
+ t .Fatalf ("%s: %s" , err , "Gateway.PublicGateways.Foo.Paths is a valid key in the config" )
150
+ }
151
+
152
+ err = CheckKey ("Gateway.PublicGateways.Foo.Bar" )
153
+ if err == nil {
154
+ t .Fatal ("Gateway.PublicGateways.Foo.Bar isn't a valid key in the config" )
155
+ }
156
+
157
+ err = CheckKey ("Plugins.Plugins.peerlog.Config.Enabled" )
158
+ if err != nil {
159
+ t .Fatalf ("%s: %s" , err , "Plugins.Plugins.peerlog.Config.Enabled is a valid key in the config" )
160
+ }
161
+ }
0 commit comments