master
go 144 lines 4.35 KB
Raw
1 package autoconf
2
3 import (
4 "net/http"
5 "net/http/httptest"
6 "testing"
7
8 "github.com/ipfs/kubo/test/cli/harness"
9 "github.com/stretchr/testify/assert"
10 )
11
12 func TestAutoConfValidation(t *testing.T) {
13 t.Parallel()
14
15 t.Run("invalid autoconf JSON prevents caching", func(t *testing.T) {
16 t.Parallel()
17 testInvalidAutoConfJSONPreventsCaching(t)
18 })
19
20 t.Run("malformed multiaddr in autoconf", func(t *testing.T) {
21 t.Parallel()
22 testMalformedMultiaddrInAutoConf(t)
23 })
24
25 t.Run("malformed URL in autoconf", func(t *testing.T) {
26 t.Parallel()
27 testMalformedURLInAutoConf(t)
28 })
29 }
30
31 func testInvalidAutoConfJSONPreventsCaching(t *testing.T) {
32 // Create server that serves invalid autoconf JSON
33 invalidAutoConfData := `{
34 "AutoConfVersion": 123,
35 "AutoConfSchema": 1,
36 "SystemRegistry": {
37 "AminoDHT": {
38 "NativeConfig": {
39 "Bootstrap": [
40 "invalid-multiaddr-that-should-fail"
41 ]
42 }
43 }
44 }
45 }`
46
47 requestCount := 0
48 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
49 requestCount++
50 t.Logf("Invalid autoconf server request #%d: %s %s", requestCount, r.Method, r.URL.Path)
51 w.Header().Set("Content-Type", "application/json")
52 w.Header().Set("ETag", `"invalid-config-123"`)
53 _, _ = w.Write([]byte(invalidAutoConfData))
54 }))
55 defer server.Close()
56
57 // Create IPFS node and try to start daemon with invalid autoconf
58 node := harness.NewT(t).NewNode().Init("--profile=test")
59 node.SetIPFSConfig("AutoConf.URL", server.URL)
60 node.SetIPFSConfig("AutoConf.Enabled", true)
61 node.SetIPFSConfig("Bootstrap", []string{"auto"})
62
63 // Start daemon to trigger autoconf fetch - this should start but log validation errors
64 node.StartDaemon()
65 defer node.StopDaemon()
66
67 // Give autoconf some time to attempt fetch and fail validation
68 // The daemon should still start but autoconf should fail
69 result := node.RunIPFS("version")
70 assert.Equal(t, 0, result.ExitCode(), "Daemon should start even with invalid autoconf")
71
72 // Verify server was called (autoconf was attempted even though validation failed)
73 assert.Greater(t, requestCount, 0, "Invalid autoconf server should have been called")
74 }
75
76 func testMalformedMultiaddrInAutoConf(t *testing.T) {
77 // Create server that serves autoconf with malformed multiaddr
78 invalidAutoConfData := `{
79 "AutoConfVersion": 456,
80 "AutoConfSchema": 1,
81 "SystemRegistry": {
82 "AminoDHT": {
83 "NativeConfig": {
84 "Bootstrap": [
85 "/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN",
86 "not-a-valid-multiaddr"
87 ]
88 }
89 }
90 }
91 }`
92
93 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
94 w.Header().Set("Content-Type", "application/json")
95 _, _ = w.Write([]byte(invalidAutoConfData))
96 }))
97 defer server.Close()
98
99 // Create IPFS node
100 node := harness.NewT(t).NewNode().Init("--profile=test")
101 node.SetIPFSConfig("AutoConf.URL", server.URL)
102 node.SetIPFSConfig("AutoConf.Enabled", true)
103 node.SetIPFSConfig("Bootstrap", []string{"auto"})
104
105 // Start daemon to trigger autoconf fetch - daemon should start but autoconf validation should fail
106 node.StartDaemon()
107 defer node.StopDaemon()
108
109 // Daemon should still be functional even with invalid autoconf
110 result := node.RunIPFS("version")
111 assert.Equal(t, 0, result.ExitCode(), "Daemon should start even with invalid autoconf")
112 }
113
114 func testMalformedURLInAutoConf(t *testing.T) {
115 // Create server that serves autoconf with malformed URL
116 invalidAutoConfData := `{
117 "AutoConfVersion": 789,
118 "AutoConfSchema": 1,
119 "DNSResolvers": {
120 "eth.": ["https://valid.example.com"],
121 "bad.": ["://malformed-url-missing-scheme"]
122 }
123 }`
124
125 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
126 w.Header().Set("Content-Type", "application/json")
127 _, _ = w.Write([]byte(invalidAutoConfData))
128 }))
129 defer server.Close()
130
131 // Create IPFS node
132 node := harness.NewT(t).NewNode().Init("--profile=test")
133 node.SetIPFSConfig("AutoConf.URL", server.URL)
134 node.SetIPFSConfig("AutoConf.Enabled", true)
135 node.SetIPFSConfig("DNS.Resolvers", map[string]string{"foo.": "auto"})
136
137 // Start daemon to trigger autoconf fetch - daemon should start but autoconf validation should fail
138 node.StartDaemon()
139 defer node.StopDaemon()
140
141 // Daemon should still be functional even with invalid autoconf
142 result := node.RunIPFS("version")
143 assert.Equal(t, 0, result.ExitCode(), "Daemon should start even with invalid autoconf")
144 }