|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/ipfs/kubo/config" |
| 8 | + "github.com/ipfs/kubo/test/cli/harness" |
| 9 | + "github.com/ipfs/kubo/test/cli/testutils" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | +) |
| 12 | + |
| 13 | +func TestBitswapConfig(t *testing.T) { |
| 14 | + t.Parallel() |
| 15 | + |
| 16 | + // Create test data that will be shared between nodes |
| 17 | + testData := testutils.RandomBytes(100) |
| 18 | + |
| 19 | + t.Run("server enabled (default)", func(t *testing.T) { |
| 20 | + t.Parallel() |
| 21 | + h := harness.NewT(t) |
| 22 | + provider := h.NewNode().Init().StartDaemon() |
| 23 | + requester := h.NewNode().Init().StartDaemon() |
| 24 | + |
| 25 | + hash := provider.IPFSAddStr(string(testData)) |
| 26 | + requester.Connect(provider) |
| 27 | + |
| 28 | + res := requester.IPFS("cat", hash) |
| 29 | + assert.Equal(t, testData, res.Stdout.Bytes(), "retrieved data should match original") |
| 30 | + }) |
| 31 | + |
| 32 | + t.Run("server disabled", func(t *testing.T) { |
| 33 | + t.Parallel() |
| 34 | + h := harness.NewT(t) |
| 35 | + |
| 36 | + provider := h.NewNode().Init() |
| 37 | + provider.SetIPFSConfig("Bitswap.ServerEnabled", false) |
| 38 | + provider = provider.StartDaemon() |
| 39 | + |
| 40 | + requester := h.NewNode().Init().StartDaemon() |
| 41 | + |
| 42 | + hash := provider.IPFSAddStr(string(testData)) |
| 43 | + requester.Connect(provider) |
| 44 | + |
| 45 | + // If the data was available, it would be retrieved immediately. |
| 46 | + // Therefore, after the timeout, we can assume the data is not available |
| 47 | + // i.e. the server is disabled |
| 48 | + timeout := time.After(3 * time.Second) |
| 49 | + dataChan := make(chan []byte) |
| 50 | + |
| 51 | + go func() { |
| 52 | + res := requester.RunIPFS("cat", hash) |
| 53 | + dataChan <- res.Stdout.Bytes() |
| 54 | + }() |
| 55 | + |
| 56 | + select { |
| 57 | + case data := <-dataChan: |
| 58 | + assert.NotEqual(t, testData, data, "retrieved data should not match original") |
| 59 | + case <-timeout: |
| 60 | + t.Log("Test passed: operation timed out after 3 seconds as expected") |
| 61 | + } |
| 62 | + }) |
| 63 | + |
| 64 | + t.Run("server disabled and client enabled", func(t *testing.T) { |
| 65 | + t.Parallel() |
| 66 | + h := harness.NewT(t) |
| 67 | + |
| 68 | + provider := h.NewNode().Init() |
| 69 | + provider.SetIPFSConfig("Bitswap.ServerEnabled", false) |
| 70 | + provider.StartDaemon() |
| 71 | + |
| 72 | + requester := h.NewNode().Init().StartDaemon() |
| 73 | + hash := requester.IPFSAddStr(string(testData)) |
| 74 | + |
| 75 | + provider.Connect(requester) |
| 76 | + |
| 77 | + // Even when the server is disabled, the client should be able to retrieve data |
| 78 | + res := provider.RunIPFS("cat", hash) |
| 79 | + assert.Equal(t, testData, res.Stdout.Bytes(), "retrieved data should match original") |
| 80 | + }) |
| 81 | + |
| 82 | + t.Run("bitswap completely disabled", func(t *testing.T) { |
| 83 | + t.Parallel() |
| 84 | + h := harness.NewT(t) |
| 85 | + |
| 86 | + requester := h.NewNode().Init() |
| 87 | + requester.UpdateConfig(func(cfg *config.Config) { |
| 88 | + cfg.Bitswap.Enabled = config.False |
| 89 | + cfg.Bitswap.ServerEnabled = config.False |
| 90 | + }) |
| 91 | + requester.StartDaemon() |
| 92 | + |
| 93 | + provider := h.NewNode().Init().StartDaemon() |
| 94 | + hash := provider.IPFSAddStr(string(testData)) |
| 95 | + |
| 96 | + requester.Connect(provider) |
| 97 | + res := requester.RunIPFS("cat", hash) |
| 98 | + assert.Equal(t, []byte{}, res.Stdout.Bytes(), "cat should not return any data") |
| 99 | + assert.Contains(t, res.Stderr.String(), "Error: ipld: could not find") |
| 100 | + |
| 101 | + // Verify that basic operations still work with bitswap disabled |
| 102 | + res = requester.IPFS("id") |
| 103 | + assert.Equal(t, 0, res.ExitCode(), "basic IPFS operations should work") |
| 104 | + res = requester.IPFS("bitswap", "stat") |
| 105 | + assert.Equal(t, 0, res.ExitCode(), "bitswap stat should work even with bitswap disabled") |
| 106 | + res = requester.IPFS("bitswap", "wantlist") |
| 107 | + assert.Equal(t, 0, res.ExitCode(), "bitswap wantlist should work even with bitswap disabled") |
| 108 | + |
| 109 | + // Verify local operations still work |
| 110 | + hashNew := requester.IPFSAddStr("random") |
| 111 | + res = requester.IPFS("cat", hashNew) |
| 112 | + assert.Equal(t, []byte("random"), res.Stdout.Bytes(), "cat should return the added data") |
| 113 | + }) |
| 114 | + |
| 115 | + // TODO: Disabling Bitswap.Enabled should remove /ifps/bitswap* protocols from `ipfs id` |
| 116 | + // t.Run("bitswap protocols disabled", func(t *testing.T) { |
| 117 | + // t.Parallel() |
| 118 | + // harness.EnableDebugLogging() |
| 119 | + // h := harness.NewT(t) |
| 120 | + |
| 121 | + // provider := h.NewNode().Init() |
| 122 | + // provider.SetIPFSConfig("Bitswap.ServerEnabled", false) |
| 123 | + // provider = provider.StartDaemon() |
| 124 | + // requester := h.NewNode().Init().StartDaemon() |
| 125 | + // requester.Connect(provider) |
| 126 | + // // Parse and check ID output |
| 127 | + // res := provider.IPFS("id", "-f", "<protocols>") |
| 128 | + // protocols := strings.Split(strings.TrimSpace(res.Stdout.String()), "\n") |
| 129 | + |
| 130 | + // // No bitswap protocols should be present |
| 131 | + // for _, proto := range protocols { |
| 132 | + // assert.NotContains(t, proto, bsnet.ProtocolBitswap, "bitswap protocol %s should not be advertised when server is disabled", proto) |
| 133 | + // assert.NotContains(t, proto, bsnet.ProtocolBitswapNoVers, "bitswap protocol %s should not be advertised when server is disabled", proto) |
| 134 | + // assert.NotContains(t, proto, bsnet.ProtocolBitswapOneOne, "bitswap protocol %s should not be advertised when server is disabled", proto) |
| 135 | + // assert.NotContains(t, proto, bsnet.ProtocolBitswapOneZero, "bitswap protocol %s should not be advertised when server is disabled", proto) |
| 136 | + // } |
| 137 | + // }) |
| 138 | +} |
0 commit comments