| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "os" |
| 6 | fp "path/filepath" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | "github.com/ipfs/kubo/test/cli/harness" |
| 11 | . "github.com/ipfs/kubo/test/cli/testutils" |
| 12 | pb "github.com/libp2p/go-libp2p/core/crypto/pb" |
| 13 | "github.com/libp2p/go-libp2p/core/peer" |
| 14 | "github.com/stretchr/testify/assert" |
| 15 | "github.com/stretchr/testify/require" |
| 16 | ) |
| 17 | |
| 18 | func validatePeerID(t *testing.T, peerID peer.ID, expErr error, expAlgo pb.KeyType) { |
| 19 | assert.NoError(t, peerID.Validate()) |
| 20 | pub, err := peerID.ExtractPublicKey() |
| 21 | assert.ErrorIs(t, expErr, err) |
| 22 | if expAlgo != 0 { |
| 23 | assert.Equal(t, expAlgo, pub.Type()) |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | func testInitAlgo(t *testing.T, initFlags []string, expOutputName string, expPeerIDPubKeyErr error, expPeerIDPubKeyType pb.KeyType) { |
| 28 | t.Run("init", func(t *testing.T) { |
| 29 | t.Parallel() |
| 30 | node := harness.NewT(t).NewNode() |
| 31 | initRes := node.IPFS(StrCat("init", initFlags)...) |
| 32 | |
| 33 | lines := []string{ |
| 34 | fmt.Sprintf("generating %s keypair...done", expOutputName), |
| 35 | fmt.Sprintf("peer identity: %s", node.PeerID().String()), |
| 36 | fmt.Sprintf("initializing IPFS node at %s\n", node.Dir), |
| 37 | } |
| 38 | expectedInitOutput := strings.Join(lines, "\n") |
| 39 | assert.Equal(t, expectedInitOutput, initRes.Stdout.String()) |
| 40 | |
| 41 | assert.DirExists(t, node.Dir) |
| 42 | assert.FileExists(t, fp.Join(node.Dir, "config")) |
| 43 | assert.DirExists(t, fp.Join(node.Dir, "datastore")) |
| 44 | assert.DirExists(t, fp.Join(node.Dir, "blocks")) |
| 45 | assert.NoFileExists(t, fp.Join(node.Dir, "._check_writeable")) |
| 46 | |
| 47 | _, err := os.ReadDir(node.Dir) |
| 48 | assert.NoError(t, err, "ipfs dir should be listable") |
| 49 | |
| 50 | validatePeerID(t, node.PeerID(), expPeerIDPubKeyErr, expPeerIDPubKeyType) |
| 51 | |
| 52 | res := node.IPFS("config", "Mounts.IPFS") |
| 53 | assert.Equal(t, "/ipfs", res.Stdout.Trimmed()) |
| 54 | |
| 55 | catRes := node.RunIPFS("cat", fmt.Sprintf("/ipfs/%s/readme", CIDWelcomeDocs)) |
| 56 | assert.NotEqual(t, 0, catRes.ExitErr.ExitCode(), "welcome readme doesn't exist") |
| 57 | }) |
| 58 | |
| 59 | t.Run("init without empty repo", func(t *testing.T) { |
| 60 | t.Parallel() |
| 61 | node := harness.NewT(t).NewNode() |
| 62 | initRes := node.IPFS(StrCat("init", "--empty-repo=false", initFlags)...) |
| 63 | |
| 64 | validatePeerID(t, node.PeerID(), expPeerIDPubKeyErr, expPeerIDPubKeyType) |
| 65 | |
| 66 | lines := []string{ |
| 67 | fmt.Sprintf("generating %s keypair...done", expOutputName), |
| 68 | fmt.Sprintf("peer identity: %s", node.PeerID().String()), |
| 69 | fmt.Sprintf("initializing IPFS node at %s", node.Dir), |
| 70 | "to get started, enter:", |
| 71 | fmt.Sprintf("\n\tipfs cat /ipfs/%s/readme\n\n", CIDWelcomeDocs), |
| 72 | } |
| 73 | expectedEmptyInitOutput := strings.Join(lines, "\n") |
| 74 | assert.Equal(t, expectedEmptyInitOutput, initRes.Stdout.String()) |
| 75 | |
| 76 | node.IPFS("cat", fmt.Sprintf("/ipfs/%s/readme", CIDWelcomeDocs)) |
| 77 | |
| 78 | idRes := node.IPFS("id", "-f", "<aver>") |
| 79 | version := node.IPFS("version", "-n").Stdout.Trimmed() |
| 80 | assert.Contains(t, idRes.Stdout.String(), version) |
| 81 | }) |
| 82 | } |
| 83 | |
| 84 | func TestInit(t *testing.T) { |
| 85 | t.Parallel() |
| 86 | |
| 87 | t.Run("init fails if the repo dir has no perms", func(t *testing.T) { |
| 88 | t.Parallel() |
| 89 | node := harness.NewT(t).NewNode() |
| 90 | badDir := fp.Join(node.Dir, ".badipfs") |
| 91 | err := os.Mkdir(badDir, 0o000) |
| 92 | require.NoError(t, err) |
| 93 | |
| 94 | res := node.RunIPFS("init", "--repo-dir", badDir) |
| 95 | assert.NotEqual(t, 0, res.Cmd.ProcessState.ExitCode()) |
| 96 | assert.Contains(t, res.Stderr.String(), "permission denied") |
| 97 | }) |
| 98 | |
| 99 | t.Run("init with ed25519", func(t *testing.T) { |
| 100 | t.Parallel() |
| 101 | testInitAlgo(t, []string{"--algorithm=ed25519"}, "ED25519", nil, pb.KeyType_Ed25519) |
| 102 | }) |
| 103 | |
| 104 | t.Run("init with rsa", func(t *testing.T) { |
| 105 | t.Parallel() |
| 106 | testInitAlgo(t, []string{"--bits=2048", "--algorithm=rsa"}, "2048-bit RSA", peer.ErrNoPublicKey, 0) |
| 107 | }) |
| 108 | |
| 109 | t.Run("init with default algorithm", func(t *testing.T) { |
| 110 | t.Parallel() |
| 111 | testInitAlgo(t, []string{}, "ED25519", nil, pb.KeyType_Ed25519) |
| 112 | }) |
| 113 | |
| 114 | t.Run("ipfs init --profile with invalid profile fails", func(t *testing.T) { |
| 115 | t.Parallel() |
| 116 | node := harness.NewT(t).NewNode() |
| 117 | res := node.RunIPFS("init", "--profile=invalid_profile") |
| 118 | assert.NotEqual(t, 0, res.ExitErr.ExitCode()) |
| 119 | assert.Equal(t, "Error: invalid configuration profile: invalid_profile", res.Stderr.Trimmed()) |
| 120 | }) |
| 121 | |
| 122 | t.Run("ipfs init --profile with valid profile succeeds", func(t *testing.T) { |
| 123 | t.Parallel() |
| 124 | node := harness.NewT(t).NewNode() |
| 125 | node.IPFS("init", "--profile=server") |
| 126 | }) |
| 127 | |
| 128 | t.Run("ipfs config looks good", func(t *testing.T) { |
| 129 | t.Parallel() |
| 130 | node := harness.NewT(t).NewNode().Init("--profile=server") |
| 131 | |
| 132 | lines := node.IPFS("config", "Swarm.AddrFilters").Stdout.Lines() |
| 133 | assert.Len(t, lines, 21) |
| 134 | |
| 135 | out := node.IPFS("config", "Bootstrap").Stdout.Trimmed() |
| 136 | assert.Equal(t, "[]", out) |
| 137 | |
| 138 | out = node.IPFS("config", "Addresses.API").Stdout.Trimmed() |
| 139 | assert.Equal(t, "/ip4/127.0.0.1/tcp/0", out) |
| 140 | }) |
| 141 | |
| 142 | t.Run("ipfs init from existing config succeeds", func(t *testing.T) { |
| 143 | t.Parallel() |
| 144 | nodes := harness.NewT(t).NewNodes(2) |
| 145 | node1 := nodes[0] |
| 146 | node2 := nodes[1] |
| 147 | |
| 148 | node1.Init("--profile=server") |
| 149 | |
| 150 | node2.IPFS("init", fp.Join(node1.Dir, "config")) |
| 151 | out := node2.IPFS("config", "Addresses.API").Stdout.Trimmed() |
| 152 | assert.Equal(t, "/ip4/127.0.0.1/tcp/0", out) |
| 153 | }) |
| 154 | |
| 155 | t.Run("ipfs init should not run while daemon is running", func(t *testing.T) { |
| 156 | t.Parallel() |
| 157 | node := harness.NewT(t).NewNode().Init().StartDaemon() |
| 158 | defer node.StopDaemon() |
| 159 | res := node.RunIPFS("init") |
| 160 | assert.NotEqual(t, 0, res.ExitErr.ExitCode()) |
| 161 | assert.Contains(t, res.Stderr.String(), "Error: ipfs daemon is running. please stop it to run this command") |
| 162 | }) |
| 163 | } |