| 1 | package test |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/base64" |
| 6 | "fmt" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "testing" |
| 10 | |
| 11 | "github.com/ipfs/boxo/bootstrap" |
| 12 | "github.com/ipfs/boxo/filestore" |
| 13 | keystore "github.com/ipfs/boxo/keystore" |
| 14 | "github.com/ipfs/kubo/core" |
| 15 | "github.com/ipfs/kubo/core/coreapi" |
| 16 | mock "github.com/ipfs/kubo/core/mock" |
| 17 | "github.com/ipfs/kubo/core/node/libp2p" |
| 18 | "github.com/ipfs/kubo/repo" |
| 19 | |
| 20 | "github.com/ipfs/go-datastore" |
| 21 | syncds "github.com/ipfs/go-datastore/sync" |
| 22 | "github.com/ipfs/kubo/config" |
| 23 | coreiface "github.com/ipfs/kubo/core/coreiface" |
| 24 | "github.com/ipfs/kubo/core/coreiface/tests" |
| 25 | "github.com/libp2p/go-libp2p/core/crypto" |
| 26 | "github.com/libp2p/go-libp2p/core/peer" |
| 27 | mocknet "github.com/libp2p/go-libp2p/p2p/net/mock" |
| 28 | ) |
| 29 | |
| 30 | const testPeerID = "QmTFauExutTsy4XP6JbMFcw2Wa9645HJt2bTqL6qYDCKfe" |
| 31 | |
| 32 | type NodeProvider struct{} |
| 33 | |
| 34 | func (NodeProvider) MakeAPISwarm(t *testing.T, ctx context.Context, fullIdentity bool, online bool, n int) ([]coreiface.CoreAPI, error) { |
| 35 | mn := mocknet.New() |
| 36 | |
| 37 | nodes := make([]*core.IpfsNode, n) |
| 38 | apis := make([]coreiface.CoreAPI, n) |
| 39 | |
| 40 | for i := range n { |
| 41 | var ident config.Identity |
| 42 | if fullIdentity { |
| 43 | sk, pk, err := crypto.GenerateKeyPair(crypto.RSA, 2048) |
| 44 | if err != nil { |
| 45 | return nil, err |
| 46 | } |
| 47 | |
| 48 | id, err := peer.IDFromPublicKey(pk) |
| 49 | if err != nil { |
| 50 | return nil, err |
| 51 | } |
| 52 | |
| 53 | kbytes, err := crypto.MarshalPrivateKey(sk) |
| 54 | if err != nil { |
| 55 | return nil, err |
| 56 | } |
| 57 | |
| 58 | ident = config.Identity{ |
| 59 | PeerID: id.String(), |
| 60 | PrivKey: base64.StdEncoding.EncodeToString(kbytes), |
| 61 | } |
| 62 | } else { |
| 63 | ident = config.Identity{ |
| 64 | PeerID: testPeerID, |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | c := config.Config{} |
| 69 | c.Addresses.Swarm = []string{fmt.Sprintf("/ip4/18.0.%d.1/tcp/4001", i)} |
| 70 | c.Identity = ident |
| 71 | c.Experimental.FilestoreEnabled = true |
| 72 | c.AutoTLS.Enabled = config.False // disable so no /ws listener is added |
| 73 | // For provider tests, avoid that content gets |
| 74 | // auto-provided without calling "provide" (unless pinned). |
| 75 | c.Provide.Strategy = config.NewOptionalString("roots") |
| 76 | |
| 77 | ds := syncds.MutexWrap(datastore.NewMapDatastore()) |
| 78 | r := &repo.Mock{ |
| 79 | C: c, |
| 80 | D: ds, |
| 81 | K: keystore.NewMemKeystore(), |
| 82 | F: filestore.NewFileManager(ds, filepath.Dir(os.TempDir())), |
| 83 | } |
| 84 | |
| 85 | node, err := core.NewNode(ctx, &core.BuildCfg{ |
| 86 | Routing: libp2p.DHTServerOption, |
| 87 | Repo: r, |
| 88 | Host: mock.MockHostOption(mn), |
| 89 | Online: online, |
| 90 | ExtraOpts: map[string]bool{ |
| 91 | "pubsub": true, |
| 92 | }, |
| 93 | }) |
| 94 | if err != nil { |
| 95 | return nil, err |
| 96 | } |
| 97 | nodes[i] = node |
| 98 | apis[i], err = coreapi.NewCoreAPI(node) |
| 99 | if err != nil { |
| 100 | return nil, err |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | err := mn.LinkAll() |
| 105 | if err != nil { |
| 106 | return nil, err |
| 107 | } |
| 108 | |
| 109 | if online { |
| 110 | bsinf := bootstrap.BootstrapConfigWithPeers( |
| 111 | []peer.AddrInfo{ |
| 112 | nodes[0].Peerstore.PeerInfo(nodes[0].Identity), |
| 113 | }, |
| 114 | ) |
| 115 | |
| 116 | for _, n := range nodes[1:] { |
| 117 | if err := n.Bootstrap(bsinf); err != nil { |
| 118 | return nil, err |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | return apis, nil |
| 124 | } |
| 125 | |
| 126 | func TestIface(t *testing.T) { |
| 127 | tests.TestApi(NodeProvider{})(t) |
| 128 | } |