├── staticcheck.conf ├── version.json ├── .gx └── lastpubver ├── .github ├── workflows │ ├── stale.yml │ ├── releaser.yml │ ├── tagpush.yml │ ├── automerge.yml │ ├── release-check.yml │ ├── go-check.yml │ └── go-test.yml ├── ISSUE_TEMPLATE │ ├── config.yml │ └── open_an_issue.md └── config.yml ├── dag.go ├── routing.go ├── util.go ├── idfmt.go ├── errors.go ├── README.md ├── dht.go ├── LICENSE ├── block.go ├── key.go ├── options ├── global.go ├── pubsub.go ├── dht.go ├── key.go ├── name.go ├── object.go ├── block.go ├── namesys │ └── opts.go ├── unixfs.go └── pin.go ├── pubsub.go ├── coreapi.go ├── name.go ├── swarm.go ├── pin.go ├── tests ├── routing.go ├── api.go ├── pubsub.go ├── dht.go ├── dag.go ├── path.go ├── name.go ├── block.go ├── object.go ├── key.go ├── pin.go └── unixfs.go ├── unixfs.go ├── go.mod ├── object.go └── path └── path.go /staticcheck.conf: -------------------------------------------------------------------------------- 1 | checks = ["inherit", "-SA1019"] 2 | -------------------------------------------------------------------------------- /version.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "v0.11.2" 3 | } 4 | -------------------------------------------------------------------------------- /.gx/lastpubver: -------------------------------------------------------------------------------- 1 | 0.1.16: QmaWreJcDyf2BEAtGDfL1ETGrGAHR8KS8UCCPWd3QtDg8R 2 | -------------------------------------------------------------------------------- /.github/workflows/stale.yml: -------------------------------------------------------------------------------- 1 | name: Close and mark stale issue 2 | 3 | on: 4 | schedule: 5 | - cron: '0 0 * * *' 6 | 7 | jobs: 8 | stale: 9 | uses: pl-strflt/.github/.github/workflows/reusable-stale-issue.yml@v0.3 10 | -------------------------------------------------------------------------------- /.github/workflows/releaser.yml: -------------------------------------------------------------------------------- 1 | # File managed by web3-bot. DO NOT EDIT. 2 | # See https://github.com/protocol/.github/ for details. 3 | 4 | name: Releaser 5 | on: 6 | push: 7 | paths: [ 'version.json' ] 8 | 9 | jobs: 10 | releaser: 11 | uses: protocol/.github/.github/workflows/releaser.yml@master 12 | -------------------------------------------------------------------------------- /.github/workflows/tagpush.yml: -------------------------------------------------------------------------------- 1 | # File managed by web3-bot. DO NOT EDIT. 2 | # See https://github.com/protocol/.github/ for details. 3 | 4 | name: Tag Push Checker 5 | on: 6 | push: 7 | tags: 8 | - v* 9 | 10 | jobs: 11 | releaser: 12 | uses: protocol/.github/.github/workflows/tagpush.yml@master 13 | -------------------------------------------------------------------------------- /.github/workflows/automerge.yml: -------------------------------------------------------------------------------- 1 | # File managed by web3-bot. DO NOT EDIT. 2 | # See https://github.com/protocol/.github/ for details. 3 | 4 | name: Automerge 5 | on: [ pull_request ] 6 | 7 | jobs: 8 | automerge: 9 | uses: protocol/.github/.github/workflows/automerge.yml@master 10 | with: 11 | job: 'automerge' 12 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/config.yml: -------------------------------------------------------------------------------- 1 | blank_issues_enabled: false 2 | contact_links: 3 | - name: Getting Help on IPFS 4 | url: https://ipfs.io/help 5 | about: All information about how and where to get help on IPFS. 6 | - name: IPFS Official Forum 7 | url: https://discuss.ipfs.io 8 | about: Please post general questions, support requests, and discussions here. 9 | -------------------------------------------------------------------------------- /.github/workflows/release-check.yml: -------------------------------------------------------------------------------- 1 | # File managed by web3-bot. DO NOT EDIT. 2 | # See https://github.com/protocol/.github/ for details. 3 | 4 | name: Release Checker 5 | on: 6 | pull_request_target: 7 | paths: [ 'version.json' ] 8 | 9 | jobs: 10 | release-check: 11 | uses: protocol/.github/.github/workflows/release-check.yml@master 12 | with: 13 | go-version: 1.20.x 14 | -------------------------------------------------------------------------------- /dag.go: -------------------------------------------------------------------------------- 1 | package iface 2 | 3 | import ( 4 | ipld "github.com/ipfs/go-ipld-format" 5 | ) 6 | 7 | // APIDagService extends ipld.DAGService 8 | // 9 | // Deprecated: use github.com/ipfs/boxo/coreiface.APIDagService 10 | type APIDagService interface { 11 | ipld.DAGService 12 | 13 | // Pinning returns special NodeAdder which recursively pins added nodes 14 | Pinning() ipld.NodeAdder 15 | } 16 | -------------------------------------------------------------------------------- /routing.go: -------------------------------------------------------------------------------- 1 | package iface 2 | 3 | import ( 4 | "context" 5 | ) 6 | 7 | // RoutingAPI specifies the interface to the routing layer. 8 | // 9 | // Deprecated: use github.com/ipfs/boxo/coreiface.RoutingAPI 10 | type RoutingAPI interface { 11 | // Get retrieves the best value for a given key 12 | Get(context.Context, string) ([]byte, error) 13 | 14 | // Put sets a value for a given key 15 | Put(ctx context.Context, key string, value []byte) error 16 | } 17 | -------------------------------------------------------------------------------- /util.go: -------------------------------------------------------------------------------- 1 | package iface 2 | 3 | import ( 4 | "context" 5 | "io" 6 | ) 7 | 8 | // Deprecated: use github.com/ipfs/boxo/coreiface.Reader 9 | type Reader interface { 10 | ReadSeekCloser 11 | Size() uint64 12 | CtxReadFull(context.Context, []byte) (int, error) 13 | } 14 | 15 | // A ReadSeekCloser implements interfaces to read, copy, seek and close. 16 | // 17 | // Deprecated: use github.com/ipfs/boxo/coreiface.ReadSeekCloser 18 | type ReadSeekCloser interface { 19 | io.Reader 20 | io.Seeker 21 | io.Closer 22 | io.WriterTo 23 | } 24 | -------------------------------------------------------------------------------- /idfmt.go: -------------------------------------------------------------------------------- 1 | package iface 2 | 3 | import ( 4 | "github.com/libp2p/go-libp2p/core/peer" 5 | mbase "github.com/multiformats/go-multibase" 6 | ) 7 | 8 | // Deprecated: use github.com/ipfs/boxo/coreiface.FormatKeyID 9 | func FormatKeyID(id peer.ID) string { 10 | if s, err := peer.ToCid(id).StringOfBase(mbase.Base36); err != nil { 11 | panic(err) 12 | } else { 13 | return s 14 | } 15 | } 16 | 17 | // FormatKey formats the given IPNS key in a canonical way. 18 | // 19 | // Deprecated: use github.com/ipfs/boxo/coreiface.FormatKey 20 | func FormatKey(key Key) string { 21 | return FormatKeyID(key.ID()) 22 | } 23 | -------------------------------------------------------------------------------- /errors.go: -------------------------------------------------------------------------------- 1 | package iface 2 | 3 | import "errors" 4 | 5 | var ( 6 | // Deprecated: use github.com/ipfs/boxo/coreiface.ErrIsDir 7 | ErrIsDir = errors.New("this dag node is a directory") 8 | // Deprecated: use github.com/ipfs/boxo/coreiface.ErrNotFile 9 | ErrNotFile = errors.New("this dag node is not a regular file") 10 | // Deprecated: use github.com/ipfs/boxo/coreiface.ErrOffline 11 | ErrOffline = errors.New("this action must be run in online mode, try running 'ipfs daemon' first") 12 | // Deprecated: use github.com/ipfs/boxo/coreiface.ErrNotSupported 13 | ErrNotSupported = errors.New("operation not supported") 14 | ) 15 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ### interface-go-ipfs-core 2 | 3 | > Legacy CoreAPI interfaces from Kubo (go-ipfs) 4 | 5 | # ❗ This repo is no longer maintained. 6 | 7 | 👉 We highly recommend switching to the maintained version at https://github.com/ipfs/boxo/tree/main/coreiface. 8 | 9 | 🏎️ Good news! There is [tooling and documentation](https://github.com/ipfs/boxo#migrating-to-boxo) to expedite a switch in your repo to boxo version, if you depend on this one. 10 | 11 | ⚠️ If you continue using this repo, please note that security fixes will not be provided (unless someone steps in to maintain it). 12 | 13 | 📚 Learn more, including how to take the maintainership mantle or ask questions, [here](https://github.com/ipfs/boxo/wiki/Copied-or-Migrated-Repos-FAQ). 14 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/open_an_issue.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Open an issue 3 | about: Only for actionable issues relevant to this repository. 4 | title: '' 5 | labels: need/triage 6 | assignees: '' 7 | 8 | --- 9 | 20 | -------------------------------------------------------------------------------- /dht.go: -------------------------------------------------------------------------------- 1 | package iface 2 | 3 | import ( 4 | "context" 5 | 6 | "github.com/ipfs/interface-go-ipfs-core/path" 7 | 8 | "github.com/ipfs/interface-go-ipfs-core/options" 9 | 10 | "github.com/libp2p/go-libp2p/core/peer" 11 | ) 12 | 13 | // DhtAPI specifies the interface to the DHT 14 | // Note: This API will likely get deprecated in near future, see 15 | // https://github.com/ipfs/interface-ipfs-core/issues/249 for more context. 16 | // 17 | // Deprecated: use github.com/ipfs/boxo/coreiface.DhtAPI 18 | type DhtAPI interface { 19 | // FindPeer queries the DHT for all of the multiaddresses associated with a 20 | // Peer ID 21 | FindPeer(context.Context, peer.ID) (peer.AddrInfo, error) 22 | 23 | // FindProviders finds peers in the DHT who can provide a specific value 24 | // given a key. 25 | FindProviders(context.Context, path.Path, ...options.DhtFindProvidersOption) (<-chan peer.AddrInfo, error) 26 | 27 | // Provide announces to the network that you are providing given values 28 | Provide(context.Context, path.Path, ...options.DhtProvideOption) error 29 | } 30 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2019 Protocol Labs 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /block.go: -------------------------------------------------------------------------------- 1 | package iface 2 | 3 | import ( 4 | "context" 5 | "io" 6 | 7 | path "github.com/ipfs/interface-go-ipfs-core/path" 8 | 9 | "github.com/ipfs/interface-go-ipfs-core/options" 10 | ) 11 | 12 | // BlockStat contains information about a block 13 | // 14 | // Deprecated: use github.com/ipfs/boxo/coreiface.BlockStat 15 | type BlockStat interface { 16 | // Size is the size of a block 17 | Size() int 18 | 19 | // Path returns path to the block 20 | Path() path.Resolved 21 | } 22 | 23 | // BlockAPI specifies the interface to the block layer 24 | // 25 | // Deprecated: use github.com/ipfs/boxo/coreiface.BlockAPI 26 | type BlockAPI interface { 27 | // Put imports raw block data, hashing it using specified settings. 28 | Put(context.Context, io.Reader, ...options.BlockPutOption) (BlockStat, error) 29 | 30 | // Get attempts to resolve the path and return a reader for data in the block 31 | Get(context.Context, path.Path) (io.Reader, error) 32 | 33 | // Rm removes the block specified by the path from local blockstore. 34 | // By default an error will be returned if the block can't be found locally. 35 | // 36 | // NOTE: If the specified block is pinned it won't be removed and no error 37 | // will be returned 38 | Rm(context.Context, path.Path, ...options.BlockRmOption) error 39 | 40 | // Stat returns information on 41 | Stat(context.Context, path.Path) (BlockStat, error) 42 | } 43 | -------------------------------------------------------------------------------- /key.go: -------------------------------------------------------------------------------- 1 | package iface 2 | 3 | import ( 4 | "context" 5 | 6 | "github.com/ipfs/interface-go-ipfs-core/path" 7 | 8 | "github.com/ipfs/interface-go-ipfs-core/options" 9 | 10 | "github.com/libp2p/go-libp2p/core/peer" 11 | ) 12 | 13 | // Key specifies the interface to Keys in KeyAPI Keystore 14 | // 15 | // Deprecated: use github.com/ipfs/boxo/coreiface.Key 16 | type Key interface { 17 | // Key returns key name 18 | Name() string 19 | 20 | // Path returns key path 21 | Path() path.Path 22 | 23 | // ID returns key PeerID 24 | ID() peer.ID 25 | } 26 | 27 | // KeyAPI specifies the interface to Keystore 28 | // 29 | // Deprecated: use github.com/ipfs/boxo/coreiface.KeyAPI 30 | type KeyAPI interface { 31 | // Generate generates new key, stores it in the keystore under the specified 32 | // name and returns a base58 encoded multihash of it's public key 33 | Generate(ctx context.Context, name string, opts ...options.KeyGenerateOption) (Key, error) 34 | 35 | // Rename renames oldName key to newName. Returns the key and whether another 36 | // key was overwritten, or an error 37 | Rename(ctx context.Context, oldName string, newName string, opts ...options.KeyRenameOption) (Key, bool, error) 38 | 39 | // List lists keys stored in keystore 40 | List(ctx context.Context) ([]Key, error) 41 | 42 | // Self returns the 'main' node key 43 | Self(ctx context.Context) (Key, error) 44 | 45 | // Remove removes keys from keystore. Returns ipns path of the removed key 46 | Remove(ctx context.Context, name string) (Key, error) 47 | } 48 | -------------------------------------------------------------------------------- /options/global.go: -------------------------------------------------------------------------------- 1 | package options 2 | 3 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.ApiSettings 4 | type ApiSettings struct { 5 | Offline bool 6 | FetchBlocks bool 7 | } 8 | 9 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.ApiOption 10 | type ApiOption func(*ApiSettings) error 11 | 12 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.ApiOptions 13 | func ApiOptions(opts ...ApiOption) (*ApiSettings, error) { 14 | options := &ApiSettings{ 15 | Offline: false, 16 | FetchBlocks: true, 17 | } 18 | 19 | return ApiOptionsTo(options, opts...) 20 | } 21 | 22 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.ApiOptionsTo 23 | func ApiOptionsTo(options *ApiSettings, opts ...ApiOption) (*ApiSettings, error) { 24 | for _, opt := range opts { 25 | err := opt(options) 26 | if err != nil { 27 | return nil, err 28 | } 29 | } 30 | return options, nil 31 | } 32 | 33 | type apiOpts struct{} 34 | 35 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.Api 36 | var Api apiOpts 37 | 38 | func (apiOpts) Offline(offline bool) ApiOption { 39 | return func(settings *ApiSettings) error { 40 | settings.Offline = offline 41 | return nil 42 | } 43 | } 44 | 45 | // FetchBlocks when set to false prevents api from fetching blocks from the 46 | // network while allowing other services such as IPNS to still be online 47 | func (apiOpts) FetchBlocks(fetch bool) ApiOption { 48 | return func(settings *ApiSettings) error { 49 | settings.FetchBlocks = fetch 50 | return nil 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /pubsub.go: -------------------------------------------------------------------------------- 1 | package iface 2 | 3 | import ( 4 | "context" 5 | "io" 6 | 7 | "github.com/ipfs/interface-go-ipfs-core/options" 8 | 9 | "github.com/libp2p/go-libp2p/core/peer" 10 | ) 11 | 12 | // PubSubSubscription is an active PubSub subscription 13 | // 14 | // Deprecated: use github.com/ipfs/boxo/coreiface.PubSubSubscription 15 | type PubSubSubscription interface { 16 | io.Closer 17 | 18 | // Next return the next incoming message 19 | Next(context.Context) (PubSubMessage, error) 20 | } 21 | 22 | // PubSubMessage is a single PubSub message 23 | // 24 | // Deprecated: use github.com/ipfs/boxo/coreiface.PubSubMessage 25 | type PubSubMessage interface { 26 | // From returns id of a peer from which the message has arrived 27 | From() peer.ID 28 | 29 | // Data returns the message body 30 | Data() []byte 31 | 32 | // Seq returns message identifier 33 | Seq() []byte 34 | 35 | // Topics returns list of topics this message was set to 36 | Topics() []string 37 | } 38 | 39 | // PubSubAPI specifies the interface to PubSub 40 | // 41 | // Deprecated: use github.com/ipfs/boxo/coreiface.PubSubAPI 42 | type PubSubAPI interface { 43 | // Ls lists subscribed topics by name 44 | Ls(context.Context) ([]string, error) 45 | 46 | // Peers list peers we are currently pubsubbing with 47 | Peers(context.Context, ...options.PubSubPeersOption) ([]peer.ID, error) 48 | 49 | // Publish a message to a given pubsub topic 50 | Publish(context.Context, string, []byte) error 51 | 52 | // Subscribe to messages on a given topic 53 | Subscribe(context.Context, string, ...options.PubSubSubscribeOption) (PubSubSubscription, error) 54 | } 55 | -------------------------------------------------------------------------------- /coreapi.go: -------------------------------------------------------------------------------- 1 | // Package iface defines IPFS Core API which is a set of interfaces used to 2 | // interact with IPFS nodes. 3 | package iface 4 | 5 | import ( 6 | "context" 7 | 8 | path "github.com/ipfs/interface-go-ipfs-core/path" 9 | 10 | "github.com/ipfs/interface-go-ipfs-core/options" 11 | 12 | ipld "github.com/ipfs/go-ipld-format" 13 | ) 14 | 15 | // CoreAPI defines an unified interface to IPFS for Go programs 16 | // 17 | // Deprecated: use github.com/ipfs/boxo/coreiface.CoreAPI 18 | type CoreAPI interface { 19 | // Unixfs returns an implementation of Unixfs API 20 | Unixfs() UnixfsAPI 21 | 22 | // Block returns an implementation of Block API 23 | Block() BlockAPI 24 | 25 | // Dag returns an implementation of Dag API 26 | Dag() APIDagService 27 | 28 | // Name returns an implementation of Name API 29 | Name() NameAPI 30 | 31 | // Key returns an implementation of Key API 32 | Key() KeyAPI 33 | 34 | // Pin returns an implementation of Pin API 35 | Pin() PinAPI 36 | 37 | // Object returns an implementation of Object API 38 | Object() ObjectAPI 39 | 40 | // Dht returns an implementation of Dht API 41 | Dht() DhtAPI 42 | 43 | // Swarm returns an implementation of Swarm API 44 | Swarm() SwarmAPI 45 | 46 | // PubSub returns an implementation of PubSub API 47 | PubSub() PubSubAPI 48 | 49 | // Routing returns an implementation of Routing API 50 | Routing() RoutingAPI 51 | 52 | // ResolvePath resolves the path using Unixfs resolver 53 | ResolvePath(context.Context, path.Path) (path.Resolved, error) 54 | 55 | // ResolveNode resolves the path (if not resolved already) using Unixfs 56 | // resolver, gets and returns the resolved Node 57 | ResolveNode(context.Context, path.Path) (ipld.Node, error) 58 | 59 | // WithOptions creates new instance of CoreAPI based on this instance with 60 | // a set of options applied 61 | WithOptions(...options.ApiOption) (CoreAPI, error) 62 | } 63 | -------------------------------------------------------------------------------- /options/pubsub.go: -------------------------------------------------------------------------------- 1 | package options 2 | 3 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.PubSubPeersSettings 4 | type PubSubPeersSettings struct { 5 | Topic string 6 | } 7 | 8 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.PubSubSubscribeSettings 9 | type PubSubSubscribeSettings struct { 10 | Discover bool 11 | } 12 | 13 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.PubSubPeersOption 14 | type PubSubPeersOption func(*PubSubPeersSettings) error 15 | 16 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.PubSubSubscribeOption 17 | type PubSubSubscribeOption func(*PubSubSubscribeSettings) error 18 | 19 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.PubSubPeersOptions 20 | func PubSubPeersOptions(opts ...PubSubPeersOption) (*PubSubPeersSettings, error) { 21 | options := &PubSubPeersSettings{ 22 | Topic: "", 23 | } 24 | 25 | for _, opt := range opts { 26 | err := opt(options) 27 | if err != nil { 28 | return nil, err 29 | } 30 | } 31 | return options, nil 32 | } 33 | 34 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.PubSubSubscribeOptions 35 | func PubSubSubscribeOptions(opts ...PubSubSubscribeOption) (*PubSubSubscribeSettings, error) { 36 | options := &PubSubSubscribeSettings{ 37 | Discover: false, 38 | } 39 | 40 | for _, opt := range opts { 41 | err := opt(options) 42 | if err != nil { 43 | return nil, err 44 | } 45 | } 46 | return options, nil 47 | } 48 | 49 | type pubsubOpts struct{} 50 | 51 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.PubSub 52 | var PubSub pubsubOpts 53 | 54 | func (pubsubOpts) Topic(topic string) PubSubPeersOption { 55 | return func(settings *PubSubPeersSettings) error { 56 | settings.Topic = topic 57 | return nil 58 | } 59 | } 60 | 61 | func (pubsubOpts) Discover(discover bool) PubSubSubscribeOption { 62 | return func(settings *PubSubSubscribeSettings) error { 63 | settings.Discover = discover 64 | return nil 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /name.go: -------------------------------------------------------------------------------- 1 | package iface 2 | 3 | import ( 4 | "context" 5 | "errors" 6 | 7 | path "github.com/ipfs/interface-go-ipfs-core/path" 8 | 9 | "github.com/ipfs/interface-go-ipfs-core/options" 10 | ) 11 | 12 | // Deprecated: use github.com/ipfs/boxo/coreiface.ErrResolveFailed 13 | var ErrResolveFailed = errors.New("could not resolve name") 14 | 15 | // IpnsEntry specifies the interface to IpnsEntries 16 | // 17 | // Deprecated: use github.com/ipfs/boxo/coreiface.IpnsEntry 18 | type IpnsEntry interface { 19 | // Name returns IpnsEntry name 20 | Name() string 21 | // Value returns IpnsEntry value 22 | Value() path.Path 23 | } 24 | 25 | // Deprecated: use github.com/ipfs/boxo/coreiface.IpnsResult 26 | type IpnsResult struct { 27 | path.Path 28 | Err error 29 | } 30 | 31 | // NameAPI specifies the interface to IPNS. 32 | // 33 | // IPNS is a PKI namespace, where names are the hashes of public keys, and the 34 | // private key enables publishing new (signed) values. In both publish and 35 | // resolve, the default name used is the node's own PeerID, which is the hash of 36 | // its public key. 37 | // 38 | // You can use .Key API to list and generate more names and their respective keys. 39 | // 40 | // Deprecated: use github.com/ipfs/boxo/coreiface.NameAPI 41 | type NameAPI interface { 42 | // Publish announces new IPNS name 43 | Publish(ctx context.Context, path path.Path, opts ...options.NamePublishOption) (IpnsEntry, error) 44 | 45 | // Resolve attempts to resolve the newest version of the specified name 46 | Resolve(ctx context.Context, name string, opts ...options.NameResolveOption) (path.Path, error) 47 | 48 | // Search is a version of Resolve which outputs paths as they are discovered, 49 | // reducing the time to first entry 50 | // 51 | // Note: by default, all paths read from the channel are considered unsafe, 52 | // except the latest (last path in channel read buffer). 53 | Search(ctx context.Context, name string, opts ...options.NameResolveOption) (<-chan IpnsResult, error) 54 | } 55 | -------------------------------------------------------------------------------- /swarm.go: -------------------------------------------------------------------------------- 1 | package iface 2 | 3 | import ( 4 | "context" 5 | "errors" 6 | "time" 7 | 8 | "github.com/libp2p/go-libp2p/core/network" 9 | "github.com/libp2p/go-libp2p/core/peer" 10 | "github.com/libp2p/go-libp2p/core/protocol" 11 | 12 | ma "github.com/multiformats/go-multiaddr" 13 | ) 14 | 15 | var ( 16 | // Deprecated: use github.com/ipfs/boxo/coreiface.ErrNotConnected 17 | ErrNotConnected = errors.New("not connected") 18 | // Deprecated: use github.com/ipfs/boxo/coreiface.ErrConnNotFound 19 | ErrConnNotFound = errors.New("conn not found") 20 | ) 21 | 22 | // ConnectionInfo contains information about a peer 23 | // 24 | // Deprecated: use github.com/ipfs/boxo/coreiface.ConnectionInfo 25 | type ConnectionInfo interface { 26 | // ID returns PeerID 27 | ID() peer.ID 28 | 29 | // Address returns the multiaddress via which we are connected with the peer 30 | Address() ma.Multiaddr 31 | 32 | // Direction returns which way the connection was established 33 | Direction() network.Direction 34 | 35 | // Latency returns last known round trip time to the peer 36 | Latency() (time.Duration, error) 37 | 38 | // Streams returns list of streams established with the peer 39 | Streams() ([]protocol.ID, error) 40 | } 41 | 42 | // SwarmAPI specifies the interface to libp2p swarm 43 | // 44 | // Deprecated: use github.com/ipfs/boxo/coreiface.SwarmAPI 45 | type SwarmAPI interface { 46 | // Connect to a given peer 47 | Connect(context.Context, peer.AddrInfo) error 48 | 49 | // Disconnect from a given address 50 | Disconnect(context.Context, ma.Multiaddr) error 51 | 52 | // Peers returns the list of peers we are connected to 53 | Peers(context.Context) ([]ConnectionInfo, error) 54 | 55 | // KnownAddrs returns the list of all addresses this node is aware of 56 | KnownAddrs(context.Context) (map[peer.ID][]ma.Multiaddr, error) 57 | 58 | // LocalAddrs returns the list of announced listening addresses 59 | LocalAddrs(context.Context) ([]ma.Multiaddr, error) 60 | 61 | // ListenAddrs returns the list of all listening addresses 62 | ListenAddrs(context.Context) ([]ma.Multiaddr, error) 63 | } 64 | -------------------------------------------------------------------------------- /options/dht.go: -------------------------------------------------------------------------------- 1 | package options 2 | 3 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.DhtProvideSettings 4 | type DhtProvideSettings struct { 5 | Recursive bool 6 | } 7 | 8 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.DhtFindProvidersSettings 9 | type DhtFindProvidersSettings struct { 10 | NumProviders int 11 | } 12 | 13 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.DhtProvideOption 14 | type DhtProvideOption func(*DhtProvideSettings) error 15 | 16 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.DhtFindProvidersOption 17 | type DhtFindProvidersOption func(*DhtFindProvidersSettings) error 18 | 19 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.DhtProvideOptions 20 | func DhtProvideOptions(opts ...DhtProvideOption) (*DhtProvideSettings, error) { 21 | options := &DhtProvideSettings{ 22 | Recursive: false, 23 | } 24 | 25 | for _, opt := range opts { 26 | err := opt(options) 27 | if err != nil { 28 | return nil, err 29 | } 30 | } 31 | return options, nil 32 | } 33 | 34 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.DhtFindProvidersOptions 35 | func DhtFindProvidersOptions(opts ...DhtFindProvidersOption) (*DhtFindProvidersSettings, error) { 36 | options := &DhtFindProvidersSettings{ 37 | NumProviders: 20, 38 | } 39 | 40 | for _, opt := range opts { 41 | err := opt(options) 42 | if err != nil { 43 | return nil, err 44 | } 45 | } 46 | return options, nil 47 | } 48 | 49 | type dhtOpts struct{} 50 | 51 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.Dht 52 | var Dht dhtOpts 53 | 54 | // Recursive is an option for Dht.Provide which specifies whether to provide 55 | // the given path recursively 56 | func (dhtOpts) Recursive(recursive bool) DhtProvideOption { 57 | return func(settings *DhtProvideSettings) error { 58 | settings.Recursive = recursive 59 | return nil 60 | } 61 | } 62 | 63 | // NumProviders is an option for Dht.FindProviders which specifies the 64 | // number of peers to look for. Default is 20 65 | func (dhtOpts) NumProviders(numProviders int) DhtFindProvidersOption { 66 | return func(settings *DhtFindProvidersSettings) error { 67 | settings.NumProviders = numProviders 68 | return nil 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /pin.go: -------------------------------------------------------------------------------- 1 | package iface 2 | 3 | import ( 4 | "context" 5 | 6 | path "github.com/ipfs/interface-go-ipfs-core/path" 7 | 8 | "github.com/ipfs/interface-go-ipfs-core/options" 9 | ) 10 | 11 | // Pin holds information about pinned resource 12 | // 13 | // Deprecated: use github.com/ipfs/boxo/coreiface.Pin 14 | type Pin interface { 15 | // Path to the pinned object 16 | Path() path.Resolved 17 | 18 | // Type of the pin 19 | Type() string 20 | 21 | // if not nil, an error happened. Everything else should be ignored. 22 | Err() error 23 | } 24 | 25 | // PinStatus holds information about pin health 26 | // 27 | // Deprecated: use github.com/ipfs/boxo/coreiface.PinStatus 28 | type PinStatus interface { 29 | // Ok indicates whether the pin has been verified to be correct 30 | Ok() bool 31 | 32 | // BadNodes returns any bad (usually missing) nodes from the pin 33 | BadNodes() []BadPinNode 34 | } 35 | 36 | // BadPinNode is a node that has been marked as bad by Pin.Verify 37 | // 38 | // Deprecated: use github.com/ipfs/boxo/coreiface.BadPinNode 39 | type BadPinNode interface { 40 | // Path is the path of the node 41 | Path() path.Resolved 42 | 43 | // Err is the reason why the node has been marked as bad 44 | Err() error 45 | } 46 | 47 | // PinAPI specifies the interface to pining 48 | // 49 | // Deprecated: use github.com/ipfs/boxo/coreiface.PinAPI 50 | type PinAPI interface { 51 | // Add creates new pin, be default recursive - pinning the whole referenced 52 | // tree 53 | Add(context.Context, path.Path, ...options.PinAddOption) error 54 | 55 | // Ls returns list of pinned objects on this node 56 | Ls(context.Context, ...options.PinLsOption) (<-chan Pin, error) 57 | 58 | // IsPinned returns whether or not the given cid is pinned 59 | // and an explanation of why its pinned 60 | IsPinned(context.Context, path.Path, ...options.PinIsPinnedOption) (string, bool, error) 61 | 62 | // Rm removes pin for object specified by the path 63 | Rm(context.Context, path.Path, ...options.PinRmOption) error 64 | 65 | // Update changes one pin to another, skipping checks for matching paths in 66 | // the old tree 67 | Update(ctx context.Context, from path.Path, to path.Path, opts ...options.PinUpdateOption) error 68 | 69 | // Verify verifies the integrity of pinned objects 70 | Verify(context.Context) (<-chan PinStatus, error) 71 | } 72 | -------------------------------------------------------------------------------- /tests/routing.go: -------------------------------------------------------------------------------- 1 | package tests 2 | 3 | import ( 4 | "context" 5 | "testing" 6 | "time" 7 | 8 | "github.com/gogo/protobuf/proto" 9 | ipns_pb "github.com/ipfs/go-ipns/pb" 10 | iface "github.com/ipfs/interface-go-ipfs-core" 11 | ) 12 | 13 | func (tp *TestSuite) TestRouting(t *testing.T) { 14 | tp.hasApi(t, func(api iface.CoreAPI) error { 15 | if api.Routing() == nil { 16 | return errAPINotImplemented 17 | } 18 | return nil 19 | }) 20 | 21 | t.Run("TestRoutingGet", tp.TestRoutingGet) 22 | t.Run("TestRoutingPut", tp.TestRoutingPut) 23 | } 24 | 25 | func (tp *TestSuite) testRoutingPublishKey(t *testing.T, ctx context.Context, api iface.CoreAPI) iface.IpnsEntry { 26 | p, err := addTestObject(ctx, api) 27 | if err != nil { 28 | t.Fatal(err) 29 | } 30 | 31 | entry, err := api.Name().Publish(ctx, p) 32 | if err != nil { 33 | t.Fatal(err) 34 | } 35 | 36 | time.Sleep(3 * time.Second) 37 | return entry 38 | } 39 | 40 | func (tp *TestSuite) TestRoutingGet(t *testing.T) { 41 | ctx, cancel := context.WithCancel(context.Background()) 42 | defer cancel() 43 | 44 | apis, err := tp.MakeAPISwarm(ctx, true, 2) 45 | if err != nil { 46 | t.Fatal(err) 47 | } 48 | 49 | // Node 1: publishes an IPNS name 50 | ipnsEntry := tp.testRoutingPublishKey(t, ctx, apis[0]) 51 | 52 | // Node 2: retrieves the best value for the IPNS name. 53 | data, err := apis[1].Routing().Get(ctx, "/ipns/"+ipnsEntry.Name()) 54 | if err != nil { 55 | t.Fatal(err) 56 | } 57 | 58 | // Checks if values match. 59 | var entry ipns_pb.IpnsEntry 60 | err = proto.Unmarshal(data, &entry) 61 | if err != nil { 62 | t.Fatal(err) 63 | } 64 | 65 | if string(entry.GetValue()) != ipnsEntry.Value().String() { 66 | t.Fatalf("routing key has wrong value, expected %s, got %s", ipnsEntry.Value().String(), string(entry.GetValue())) 67 | } 68 | } 69 | 70 | func (tp *TestSuite) TestRoutingPut(t *testing.T) { 71 | ctx, cancel := context.WithCancel(context.Background()) 72 | defer cancel() 73 | apis, err := tp.MakeAPISwarm(ctx, true, 2) 74 | if err != nil { 75 | t.Fatal(err) 76 | } 77 | 78 | // Create and publish IPNS entry. 79 | ipnsEntry := tp.testRoutingPublishKey(t, ctx, apis[0]) 80 | 81 | // Get valid routing value. 82 | data, err := apis[0].Routing().Get(ctx, "/ipns/"+ipnsEntry.Name()) 83 | if err != nil { 84 | t.Fatal(err) 85 | } 86 | 87 | // Put routing value. 88 | err = apis[1].Routing().Put(ctx, "/ipns/"+ipnsEntry.Name(), data) 89 | if err != nil { 90 | t.Fatal(err) 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /tests/api.go: -------------------------------------------------------------------------------- 1 | package tests 2 | 3 | import ( 4 | "context" 5 | "errors" 6 | "testing" 7 | "time" 8 | 9 | coreiface "github.com/ipfs/interface-go-ipfs-core" 10 | ) 11 | 12 | var errAPINotImplemented = errors.New("api not implemented") 13 | 14 | func (tp *TestSuite) makeAPI(ctx context.Context) (coreiface.CoreAPI, error) { 15 | api, err := tp.MakeAPISwarm(ctx, false, 1) 16 | if err != nil { 17 | return nil, err 18 | } 19 | 20 | return api[0], nil 21 | } 22 | 23 | // Deprecated: use github.com/ipfs/boxo/coreiface/tests.Provider 24 | type Provider interface { 25 | // Make creates n nodes. fullIdentity set to false can be ignored 26 | MakeAPISwarm(ctx context.Context, fullIdentity bool, n int) ([]coreiface.CoreAPI, error) 27 | } 28 | 29 | func (tp *TestSuite) MakeAPISwarm(ctx context.Context, fullIdentity bool, n int) ([]coreiface.CoreAPI, error) { 30 | if tp.apis != nil { 31 | tp.apis <- 1 32 | go func() { 33 | <-ctx.Done() 34 | tp.apis <- -1 35 | }() 36 | } 37 | 38 | return tp.Provider.MakeAPISwarm(ctx, fullIdentity, n) 39 | } 40 | 41 | // Deprecated: use github.com/ipfs/boxo/coreiface/tests.TestSuite 42 | type TestSuite struct { 43 | Provider 44 | 45 | apis chan int 46 | } 47 | 48 | // Deprecated: use github.com/ipfs/boxo/coreiface/tests.TestApi 49 | func TestApi(p Provider) func(t *testing.T) { 50 | running := 1 51 | apis := make(chan int) 52 | zeroRunning := make(chan struct{}) 53 | go func() { 54 | for i := range apis { 55 | running += i 56 | if running < 1 { 57 | close(zeroRunning) 58 | return 59 | } 60 | } 61 | }() 62 | 63 | tp := &TestSuite{Provider: p, apis: apis} 64 | 65 | return func(t *testing.T) { 66 | t.Run("Block", tp.TestBlock) 67 | t.Run("Dag", tp.TestDag) 68 | t.Run("Dht", tp.TestDht) 69 | t.Run("Key", tp.TestKey) 70 | t.Run("Name", tp.TestName) 71 | t.Run("Object", tp.TestObject) 72 | t.Run("Path", tp.TestPath) 73 | t.Run("Pin", tp.TestPin) 74 | t.Run("PubSub", tp.TestPubSub) 75 | t.Run("Routing", tp.TestRouting) 76 | t.Run("Unixfs", tp.TestUnixfs) 77 | 78 | apis <- -1 79 | t.Run("TestsCancelCtx", func(t *testing.T) { 80 | select { 81 | case <-zeroRunning: 82 | case <-time.After(time.Second): 83 | t.Errorf("%d test swarms(s) not closed", running) 84 | } 85 | }) 86 | } 87 | } 88 | 89 | func (tp *TestSuite) hasApi(t *testing.T, tf func(coreiface.CoreAPI) error) { 90 | ctx, cancel := context.WithCancel(context.Background()) 91 | defer cancel() 92 | api, err := tp.makeAPI(ctx) 93 | if err != nil { 94 | t.Fatal(err) 95 | } 96 | 97 | if err := tf(api); err != nil { 98 | t.Fatal(api) 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /.github/workflows/go-check.yml: -------------------------------------------------------------------------------- 1 | # File managed by web3-bot. DO NOT EDIT. 2 | # See https://github.com/protocol/.github/ for details. 3 | 4 | on: [push, pull_request] 5 | name: Go Checks 6 | 7 | jobs: 8 | unit: 9 | runs-on: ubuntu-latest 10 | name: All 11 | steps: 12 | - uses: actions/checkout@v3 13 | with: 14 | submodules: recursive 15 | - id: config 16 | uses: protocol/.github/.github/actions/read-config@master 17 | - uses: actions/setup-go@v3 18 | with: 19 | go-version: 1.20.x 20 | - name: Run repo-specific setup 21 | uses: ./.github/actions/go-check-setup 22 | if: hashFiles('./.github/actions/go-check-setup') != '' 23 | - name: Install staticcheck 24 | run: go install honnef.co/go/tools/cmd/staticcheck@4970552d932f48b71485287748246cf3237cebdf # 2023.1 (v0.4.0) 25 | - name: Check that go.mod is tidy 26 | uses: protocol/multiple-go-modules@v1.2 27 | with: 28 | run: | 29 | go mod tidy 30 | if [[ -n $(git ls-files --other --exclude-standard --directory -- go.sum) ]]; then 31 | echo "go.sum was added by go mod tidy" 32 | exit 1 33 | fi 34 | git diff --exit-code -- go.sum go.mod 35 | - name: gofmt 36 | if: success() || failure() # run this step even if the previous one failed 37 | run: | 38 | out=$(gofmt -s -l .) 39 | if [[ -n "$out" ]]; then 40 | echo $out | awk '{print "::error file=" $0 ",line=0,col=0::File is not gofmt-ed."}' 41 | exit 1 42 | fi 43 | - name: go vet 44 | if: success() || failure() # run this step even if the previous one failed 45 | uses: protocol/multiple-go-modules@v1.2 46 | with: 47 | run: go vet ./... 48 | - name: staticcheck 49 | if: success() || failure() # run this step even if the previous one failed 50 | uses: protocol/multiple-go-modules@v1.2 51 | with: 52 | run: | 53 | set -o pipefail 54 | staticcheck ./... | sed -e 's@\(.*\)\.go@./\1.go@g' 55 | - name: go generate 56 | uses: protocol/multiple-go-modules@v1.2 57 | if: (success() || failure()) && fromJSON(steps.config.outputs.json).gogenerate == true 58 | with: 59 | run: | 60 | git clean -fd # make sure there aren't untracked files / directories 61 | go generate -x ./... 62 | # check if go generate modified or added any files 63 | if ! $(git add . && git diff-index HEAD --exit-code --quiet); then 64 | echo "go generated caused changes to the repository:" 65 | git status --short 66 | exit 1 67 | fi 68 | -------------------------------------------------------------------------------- /options/key.go: -------------------------------------------------------------------------------- 1 | package options 2 | 3 | const ( 4 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.RSAKey 5 | RSAKey = "rsa" 6 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.Ed25519Key 7 | Ed25519Key = "ed25519" 8 | 9 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.DefaultRSALen 10 | DefaultRSALen = 2048 11 | ) 12 | 13 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.KeyGenerateSettings 14 | type KeyGenerateSettings struct { 15 | Algorithm string 16 | Size int 17 | } 18 | 19 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.KeyRenameSettings 20 | type KeyRenameSettings struct { 21 | Force bool 22 | } 23 | 24 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.KeyGenerateOption 25 | type KeyGenerateOption func(*KeyGenerateSettings) error 26 | 27 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.KeyRenameOption 28 | type KeyRenameOption func(*KeyRenameSettings) error 29 | 30 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.KeyGenerateOptions 31 | func KeyGenerateOptions(opts ...KeyGenerateOption) (*KeyGenerateSettings, error) { 32 | options := &KeyGenerateSettings{ 33 | Algorithm: RSAKey, 34 | Size: -1, 35 | } 36 | 37 | for _, opt := range opts { 38 | err := opt(options) 39 | if err != nil { 40 | return nil, err 41 | } 42 | } 43 | return options, nil 44 | } 45 | 46 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.KeyRenameOptions 47 | func KeyRenameOptions(opts ...KeyRenameOption) (*KeyRenameSettings, error) { 48 | options := &KeyRenameSettings{ 49 | Force: false, 50 | } 51 | 52 | for _, opt := range opts { 53 | err := opt(options) 54 | if err != nil { 55 | return nil, err 56 | } 57 | } 58 | return options, nil 59 | } 60 | 61 | type keyOpts struct{} 62 | 63 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.Key 64 | var Key keyOpts 65 | 66 | // Type is an option for Key.Generate which specifies which algorithm 67 | // should be used for the key. Default is options.RSAKey 68 | // 69 | // Supported key types: 70 | // * options.RSAKey 71 | // * options.Ed25519Key 72 | func (keyOpts) Type(algorithm string) KeyGenerateOption { 73 | return func(settings *KeyGenerateSettings) error { 74 | settings.Algorithm = algorithm 75 | return nil 76 | } 77 | } 78 | 79 | // Size is an option for Key.Generate which specifies the size of the key to 80 | // generated. Default is -1 81 | // 82 | // value of -1 means 'use default size for key type': 83 | // - 2048 for RSA 84 | func (keyOpts) Size(size int) KeyGenerateOption { 85 | return func(settings *KeyGenerateSettings) error { 86 | settings.Size = size 87 | return nil 88 | } 89 | } 90 | 91 | // Force is an option for Key.Rename which specifies whether to allow to 92 | // replace existing keys. 93 | func (keyOpts) Force(force bool) KeyRenameOption { 94 | return func(settings *KeyRenameSettings) error { 95 | settings.Force = force 96 | return nil 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /unixfs.go: -------------------------------------------------------------------------------- 1 | package iface 2 | 3 | import ( 4 | "context" 5 | 6 | "github.com/ipfs/interface-go-ipfs-core/options" 7 | path "github.com/ipfs/interface-go-ipfs-core/path" 8 | 9 | "github.com/ipfs/go-cid" 10 | "github.com/ipfs/go-libipfs/files" 11 | ) 12 | 13 | // Deprecated: use github.com/ipfs/boxo/coreiface.AddEvent 14 | type AddEvent struct { 15 | Name string 16 | Path path.Resolved `json:",omitempty"` 17 | Bytes int64 `json:",omitempty"` 18 | Size string `json:",omitempty"` 19 | } 20 | 21 | // FileType is an enum of possible UnixFS file types. 22 | // 23 | // Deprecated: use github.com/ipfs/boxo/coreiface.FileType 24 | type FileType int32 25 | 26 | const ( 27 | // TUnknown means the file type isn't known (e.g., it hasn't been 28 | // resolved). 29 | // 30 | // Deprecated: use github.com/ipfs/boxo/coreiface.TUnknown 31 | TUnknown FileType = iota 32 | // TFile is a regular file. 33 | // 34 | // Deprecated: use github.com/ipfs/boxo/coreiface.TFile 35 | TFile 36 | // TDirectory is a directory. 37 | // 38 | // Deprecated: use github.com/ipfs/boxo/coreiface.TDirectory 39 | TDirectory 40 | // TSymlink is a symlink. 41 | // 42 | // Deprecated: use github.com/ipfs/boxo/coreiface.TSymlink 43 | TSymlink 44 | ) 45 | 46 | func (t FileType) String() string { 47 | switch t { 48 | case TUnknown: 49 | return "unknown" 50 | case TFile: 51 | return "file" 52 | case TDirectory: 53 | return "directory" 54 | case TSymlink: 55 | return "symlink" 56 | default: 57 | return "" 58 | } 59 | } 60 | 61 | // DirEntry is a directory entry returned by `Ls`. 62 | // 63 | // Deprecated: use github.com/ipfs/boxo/coreiface.DirEntry 64 | type DirEntry struct { 65 | Name string 66 | Cid cid.Cid 67 | 68 | // Only filled when asked to resolve the directory entry. 69 | Size uint64 // The size of the file in bytes (or the size of the symlink). 70 | Type FileType // The type of the file. 71 | Target string // The symlink target (if a symlink). 72 | 73 | Err error 74 | } 75 | 76 | // UnixfsAPI is the basic interface to immutable files in IPFS 77 | // NOTE: This API is heavily WIP, things are guaranteed to break frequently 78 | // 79 | // Deprecated: use github.com/ipfs/boxo/coreiface.UnixfsAPI 80 | type UnixfsAPI interface { 81 | // Add imports the data from the reader into merkledag file 82 | // 83 | // TODO: a long useful comment on how to use this for many different scenarios 84 | Add(context.Context, files.Node, ...options.UnixfsAddOption) (path.Resolved, error) 85 | 86 | // Get returns a read-only handle to a file tree referenced by a path 87 | // 88 | // Note that some implementations of this API may apply the specified context 89 | // to operations performed on the returned file 90 | Get(context.Context, path.Path) (files.Node, error) 91 | 92 | // Ls returns the list of links in a directory. Links aren't guaranteed to be 93 | // returned in order 94 | Ls(context.Context, path.Path, ...options.UnixfsLsOption) (<-chan DirEntry, error) 95 | } 96 | -------------------------------------------------------------------------------- /.github/config.yml: -------------------------------------------------------------------------------- 1 | # Configuration for welcome - https://github.com/behaviorbot/welcome 2 | 3 | # Configuration for new-issue-welcome - https://github.com/behaviorbot/new-issue-welcome 4 | # Comment to be posted to on first time issues 5 | newIssueWelcomeComment: > 6 | Thank you for submitting your first issue to this repository! A maintainer 7 | will be here shortly to triage and review. 8 | 9 | In the meantime, please double-check that you have provided all the 10 | necessary information to make this process easy! Any information that can 11 | help save additional round trips is useful! We currently aim to give 12 | initial feedback within **two business days**. If this does not happen, feel 13 | free to leave a comment. 14 | 15 | Please keep an eye on how this issue will be labeled, as labels give an 16 | overview of priorities, assignments and additional actions requested by the 17 | maintainers: 18 | 19 | - "Priority" labels will show how urgent this is for the team. 20 | - "Status" labels will show if this is ready to be worked on, blocked, or in progress. 21 | - "Need" labels will indicate if additional input or analysis is required. 22 | 23 | Finally, remember to use https://discuss.ipfs.io if you just need general 24 | support. 25 | 26 | # Configuration for new-pr-welcome - https://github.com/behaviorbot/new-pr-welcome 27 | # Comment to be posted to on PRs from first time contributors in your repository 28 | newPRWelcomeComment: > 29 | Thank you for submitting this PR! 30 | 31 | A maintainer will be here shortly to review it. 32 | 33 | We are super grateful, but we are also overloaded! Help us by making sure 34 | that: 35 | 36 | * The context for this PR is clear, with relevant discussion, decisions 37 | and stakeholders linked/mentioned. 38 | 39 | * Your contribution itself is clear (code comments, self-review for the 40 | rest) and in its best form. Follow the [code contribution 41 | guidelines](https://github.com/ipfs/community/blob/master/CONTRIBUTING.md#code-contribution-guidelines) 42 | if they apply. 43 | 44 | Getting other community members to do a review would be great help too on 45 | complex PRs (you can ask in the chats/forums). If you are unsure about 46 | something, just leave us a comment. 47 | 48 | Next steps: 49 | 50 | * A maintainer will triage and assign priority to this PR, commenting on 51 | any missing things and potentially assigning a reviewer for high 52 | priority items. 53 | 54 | * The PR gets reviews, discussed and approvals as needed. 55 | 56 | * The PR is merged by maintainers when it has been approved and comments addressed. 57 | 58 | We currently aim to provide initial feedback/triaging within **two business 59 | days**. Please keep an eye on any labelling actions, as these will indicate 60 | priorities and status of your contribution. 61 | 62 | We are very grateful for your contribution! 63 | 64 | 65 | # Configuration for first-pr-merge - https://github.com/behaviorbot/first-pr-merge 66 | # Comment to be posted to on pull requests merged by a first time user 67 | # Currently disabled 68 | #firstPRMergeComment: "" 69 | -------------------------------------------------------------------------------- /tests/pubsub.go: -------------------------------------------------------------------------------- 1 | package tests 2 | 3 | import ( 4 | "context" 5 | "testing" 6 | "time" 7 | 8 | iface "github.com/ipfs/interface-go-ipfs-core" 9 | "github.com/ipfs/interface-go-ipfs-core/options" 10 | ) 11 | 12 | func (tp *TestSuite) TestPubSub(t *testing.T) { 13 | tp.hasApi(t, func(api iface.CoreAPI) error { 14 | if api.PubSub() == nil { 15 | return errAPINotImplemented 16 | } 17 | return nil 18 | }) 19 | 20 | t.Run("TestBasicPubSub", tp.TestBasicPubSub) 21 | } 22 | 23 | func (tp *TestSuite) TestBasicPubSub(t *testing.T) { 24 | ctx, cancel := context.WithCancel(context.Background()) 25 | defer cancel() 26 | 27 | apis, err := tp.MakeAPISwarm(ctx, true, 2) 28 | if err != nil { 29 | t.Fatal(err) 30 | } 31 | 32 | sub, err := apis[0].PubSub().Subscribe(ctx, "testch") 33 | if err != nil { 34 | t.Fatal(err) 35 | } 36 | 37 | done := make(chan struct{}) 38 | go func() { 39 | defer close(done) 40 | 41 | ticker := time.NewTicker(100 * time.Millisecond) 42 | defer ticker.Stop() 43 | 44 | for { 45 | err := apis[1].PubSub().Publish(ctx, "testch", []byte("hello world")) 46 | switch err { 47 | case nil: 48 | case context.Canceled: 49 | return 50 | default: 51 | t.Error(err) 52 | cancel() 53 | return 54 | } 55 | select { 56 | case <-ticker.C: 57 | case <-ctx.Done(): 58 | return 59 | } 60 | } 61 | }() 62 | 63 | // Wait for the sender to finish before we return. 64 | // Otherwise, we can get random errors as publish fails. 65 | defer func() { 66 | cancel() 67 | <-done 68 | }() 69 | 70 | m, err := sub.Next(ctx) 71 | if err != nil { 72 | t.Fatal(err) 73 | } 74 | 75 | if string(m.Data()) != "hello world" { 76 | t.Errorf("got invalid data: %s", string(m.Data())) 77 | } 78 | 79 | self1, err := apis[1].Key().Self(ctx) 80 | if err != nil { 81 | t.Fatal(err) 82 | } 83 | 84 | if m.From() != self1.ID() { 85 | t.Errorf("m.From didn't match") 86 | } 87 | 88 | peers, err := apis[1].PubSub().Peers(ctx, options.PubSub.Topic("testch")) 89 | if err != nil { 90 | t.Fatal(err) 91 | } 92 | 93 | if len(peers) != 1 { 94 | t.Fatalf("got incorrect number of peers: %d", len(peers)) 95 | } 96 | 97 | self0, err := apis[0].Key().Self(ctx) 98 | if err != nil { 99 | t.Fatal(err) 100 | } 101 | 102 | if peers[0] != self0.ID() { 103 | t.Errorf("peer didn't match") 104 | } 105 | 106 | peers, err = apis[1].PubSub().Peers(ctx, options.PubSub.Topic("nottestch")) 107 | if err != nil { 108 | t.Fatal(err) 109 | } 110 | 111 | if len(peers) != 0 { 112 | t.Fatalf("got incorrect number of peers: %d", len(peers)) 113 | } 114 | 115 | topics, err := apis[0].PubSub().Ls(ctx) 116 | if err != nil { 117 | t.Fatal(err) 118 | } 119 | 120 | if len(topics) != 1 { 121 | t.Fatalf("got incorrect number of topics: %d", len(peers)) 122 | } 123 | 124 | if topics[0] != "testch" { 125 | t.Errorf("topic didn't match") 126 | } 127 | 128 | topics, err = apis[1].PubSub().Ls(ctx) 129 | if err != nil { 130 | t.Fatal(err) 131 | } 132 | 133 | if len(topics) != 0 { 134 | t.Fatalf("got incorrect number of topics: %d", len(peers)) 135 | } 136 | } 137 | -------------------------------------------------------------------------------- /.github/workflows/go-test.yml: -------------------------------------------------------------------------------- 1 | # File managed by web3-bot. DO NOT EDIT. 2 | # See https://github.com/protocol/.github/ for details. 3 | 4 | on: [push, pull_request] 5 | name: Go Test 6 | 7 | jobs: 8 | unit: 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | os: [ "ubuntu", "windows", "macos" ] 13 | go: ["1.19.x","1.20.x"] 14 | env: 15 | COVERAGES: "" 16 | runs-on: ${{ fromJSON(vars[format('UCI_GO_TEST_RUNNER_{0}', matrix.os)] || format('"{0}-latest"', matrix.os)) }} 17 | name: ${{ matrix.os }} (go ${{ matrix.go }}) 18 | steps: 19 | - uses: actions/checkout@v3 20 | with: 21 | submodules: recursive 22 | - id: config 23 | uses: protocol/.github/.github/actions/read-config@master 24 | - uses: actions/setup-go@v3 25 | with: 26 | go-version: ${{ matrix.go }} 27 | - name: Go information 28 | run: | 29 | go version 30 | go env 31 | - name: Use msys2 on windows 32 | if: matrix.os == 'windows' 33 | shell: bash 34 | # The executable for msys2 is also called bash.cmd 35 | # https://github.com/actions/virtual-environments/blob/main/images/win/Windows2019-Readme.md#shells 36 | # If we prepend its location to the PATH 37 | # subsequent 'shell: bash' steps will use msys2 instead of gitbash 38 | run: echo "C:/msys64/usr/bin" >> $GITHUB_PATH 39 | - name: Run repo-specific setup 40 | uses: ./.github/actions/go-test-setup 41 | if: hashFiles('./.github/actions/go-test-setup') != '' 42 | - name: Run tests 43 | if: contains(fromJSON(steps.config.outputs.json).skipOSes, matrix.os) == false 44 | uses: protocol/multiple-go-modules@v1.2 45 | with: 46 | # Use -coverpkg=./..., so that we include cross-package coverage. 47 | # If package ./A imports ./B, and ./A's tests also cover ./B, 48 | # this means ./B's coverage will be significantly higher than 0%. 49 | run: go test -v -shuffle=on -coverprofile=module-coverage.txt -coverpkg=./... ./... 50 | - name: Run tests (32 bit) 51 | # can't run 32 bit tests on OSX. 52 | if: matrix.os != 'macos' && 53 | fromJSON(steps.config.outputs.json).skip32bit != true && 54 | contains(fromJSON(steps.config.outputs.json).skipOSes, matrix.os) == false 55 | uses: protocol/multiple-go-modules@v1.2 56 | env: 57 | GOARCH: 386 58 | with: 59 | run: | 60 | export "PATH=$PATH_386:$PATH" 61 | go test -v -shuffle=on ./... 62 | - name: Run tests with race detector 63 | # speed things up. Windows and OSX VMs are slow 64 | if: matrix.os == 'ubuntu' && 65 | contains(fromJSON(steps.config.outputs.json).skipOSes, matrix.os) == false 66 | uses: protocol/multiple-go-modules@v1.2 67 | with: 68 | run: go test -v -race ./... 69 | - name: Collect coverage files 70 | shell: bash 71 | run: echo "COVERAGES=$(find . -type f -name 'module-coverage.txt' | tr -s '\n' ',' | sed 's/,$//')" >> $GITHUB_ENV 72 | - name: Upload coverage to Codecov 73 | uses: codecov/codecov-action@d9f34f8cd5cb3b3eb79b3e4b5dae3a16df499a70 # v3.1.1 74 | with: 75 | files: '${{ env.COVERAGES }}' 76 | env_vars: OS=${{ matrix.os }}, GO=${{ matrix.go }} 77 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/ipfs/interface-go-ipfs-core 2 | 3 | require ( 4 | github.com/gogo/protobuf v1.3.2 5 | github.com/ipfs/go-cid v0.3.2 6 | github.com/ipfs/go-ipld-cbor v0.0.5 7 | github.com/ipfs/go-ipld-format v0.3.0 8 | github.com/ipfs/go-ipns v0.3.0 9 | github.com/ipfs/go-libipfs v0.1.0 10 | github.com/ipfs/go-merkledag v0.6.0 11 | github.com/ipfs/go-path v0.1.1 12 | github.com/ipfs/go-unixfs v0.2.4 13 | github.com/libp2p/go-libp2p v0.23.4 14 | github.com/multiformats/go-multiaddr v0.8.0 15 | github.com/multiformats/go-multibase v0.1.1 16 | github.com/multiformats/go-multicodec v0.6.0 17 | github.com/multiformats/go-multihash v0.2.1 18 | ) 19 | 20 | require ( 21 | github.com/crackcomm/go-gitignore v0.0.0-20170627025303-887ab5e44cc3 // indirect 22 | github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0 // indirect 23 | github.com/google/uuid v1.3.0 // indirect 24 | github.com/hashicorp/golang-lru v0.5.4 // indirect 25 | github.com/ipfs/bbloom v0.0.4 // indirect 26 | github.com/ipfs/go-block-format v0.0.3 // indirect 27 | github.com/ipfs/go-blockservice v0.3.0 // indirect 28 | github.com/ipfs/go-datastore v0.6.0 // indirect 29 | github.com/ipfs/go-ipfs-blockstore v1.2.0 // indirect 30 | github.com/ipfs/go-ipfs-chunker v0.0.1 // indirect 31 | github.com/ipfs/go-ipfs-ds-help v1.1.0 // indirect 32 | github.com/ipfs/go-ipfs-exchange-interface v0.1.0 // indirect 33 | github.com/ipfs/go-ipfs-files v0.0.8 // indirect 34 | github.com/ipfs/go-ipfs-posinfo v0.0.1 // indirect 35 | github.com/ipfs/go-ipfs-util v0.0.2 // indirect 36 | github.com/ipfs/go-ipld-legacy v0.1.0 // indirect 37 | github.com/ipfs/go-log v1.0.5 // indirect 38 | github.com/ipfs/go-log/v2 v2.5.1 // indirect 39 | github.com/ipfs/go-metrics-interface v0.0.1 // indirect 40 | github.com/ipfs/go-verifcid v0.0.1 // indirect 41 | github.com/ipld/go-codec-dagpb v1.3.0 // indirect 42 | github.com/ipld/go-ipld-prime v0.11.0 // indirect 43 | github.com/jbenet/goprocess v0.1.4 // indirect 44 | github.com/klauspost/cpuid/v2 v2.1.1 // indirect 45 | github.com/libp2p/go-buffer-pool v0.1.0 // indirect 46 | github.com/libp2p/go-openssl v0.1.0 // indirect 47 | github.com/mattn/go-isatty v0.0.16 // indirect 48 | github.com/mattn/go-pointer v0.0.1 // indirect 49 | github.com/minio/sha256-simd v1.0.0 // indirect 50 | github.com/mr-tron/base58 v1.2.0 // indirect 51 | github.com/multiformats/go-base32 v0.1.0 // indirect 52 | github.com/multiformats/go-base36 v0.1.0 // indirect 53 | github.com/multiformats/go-varint v0.0.6 // indirect 54 | github.com/opentracing/opentracing-go v1.2.0 // indirect 55 | github.com/polydawn/refmt v0.0.0-20201211092308-30ac6d18308e // indirect 56 | github.com/spacemonkeygo/spacelog v0.0.0-20180420211403-2296661a0572 // indirect 57 | github.com/spaolacci/murmur3 v1.1.0 // indirect 58 | github.com/whyrusleeping/cbor-gen v0.0.0-20200123233031-1cdf64d27158 // indirect 59 | github.com/whyrusleeping/chunker v0.0.0-20181014151217-fe64bd25879f // indirect 60 | go.uber.org/atomic v1.10.0 // indirect 61 | go.uber.org/multierr v1.8.0 // indirect 62 | go.uber.org/zap v1.23.0 // indirect 63 | golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e // indirect 64 | golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab // indirect 65 | golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect 66 | google.golang.org/protobuf v1.28.1 // indirect 67 | lukechampine.com/blake3 v1.1.7 // indirect 68 | ) 69 | 70 | go 1.19 71 | -------------------------------------------------------------------------------- /options/name.go: -------------------------------------------------------------------------------- 1 | package options 2 | 3 | import ( 4 | "time" 5 | 6 | ropts "github.com/ipfs/interface-go-ipfs-core/options/namesys" 7 | ) 8 | 9 | const ( 10 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.DefaultNameValidTime 11 | DefaultNameValidTime = 24 * time.Hour 12 | ) 13 | 14 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.NamePublishSettings 15 | type NamePublishSettings struct { 16 | ValidTime time.Duration 17 | Key string 18 | 19 | TTL *time.Duration 20 | 21 | AllowOffline bool 22 | } 23 | 24 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.NameResolveSettings 25 | type NameResolveSettings struct { 26 | Cache bool 27 | 28 | ResolveOpts []ropts.ResolveOpt 29 | } 30 | 31 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.NamePublishOption 32 | type NamePublishOption func(*NamePublishSettings) error 33 | 34 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.NameResolveOption 35 | type NameResolveOption func(*NameResolveSettings) error 36 | 37 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.NamePublishOptions 38 | func NamePublishOptions(opts ...NamePublishOption) (*NamePublishSettings, error) { 39 | options := &NamePublishSettings{ 40 | ValidTime: DefaultNameValidTime, 41 | Key: "self", 42 | 43 | AllowOffline: false, 44 | } 45 | 46 | for _, opt := range opts { 47 | err := opt(options) 48 | if err != nil { 49 | return nil, err 50 | } 51 | } 52 | 53 | return options, nil 54 | } 55 | 56 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.NameResolveOptions 57 | func NameResolveOptions(opts ...NameResolveOption) (*NameResolveSettings, error) { 58 | options := &NameResolveSettings{ 59 | Cache: true, 60 | } 61 | 62 | for _, opt := range opts { 63 | err := opt(options) 64 | if err != nil { 65 | return nil, err 66 | } 67 | } 68 | 69 | return options, nil 70 | } 71 | 72 | type nameOpts struct{} 73 | 74 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.Name 75 | var Name nameOpts 76 | 77 | // ValidTime is an option for Name.Publish which specifies for how long the 78 | // entry will remain valid. Default value is 24h 79 | func (nameOpts) ValidTime(validTime time.Duration) NamePublishOption { 80 | return func(settings *NamePublishSettings) error { 81 | settings.ValidTime = validTime 82 | return nil 83 | } 84 | } 85 | 86 | // Key is an option for Name.Publish which specifies the key to use for 87 | // publishing. Default value is "self" which is the node's own PeerID. 88 | // The key parameter must be either PeerID or keystore key alias. 89 | // 90 | // You can use KeyAPI to list and generate more names and their respective keys. 91 | func (nameOpts) Key(key string) NamePublishOption { 92 | return func(settings *NamePublishSettings) error { 93 | settings.Key = key 94 | return nil 95 | } 96 | } 97 | 98 | // AllowOffline is an option for Name.Publish which specifies whether to allow 99 | // publishing when the node is offline. Default value is false 100 | func (nameOpts) AllowOffline(allow bool) NamePublishOption { 101 | return func(settings *NamePublishSettings) error { 102 | settings.AllowOffline = allow 103 | return nil 104 | } 105 | } 106 | 107 | // TTL is an option for Name.Publish which specifies the time duration the 108 | // published record should be cached for (caution: experimental). 109 | func (nameOpts) TTL(ttl time.Duration) NamePublishOption { 110 | return func(settings *NamePublishSettings) error { 111 | settings.TTL = &ttl 112 | return nil 113 | } 114 | } 115 | 116 | // Cache is an option for Name.Resolve which specifies if cache should be used. 117 | // Default value is true 118 | func (nameOpts) Cache(cache bool) NameResolveOption { 119 | return func(settings *NameResolveSettings) error { 120 | settings.Cache = cache 121 | return nil 122 | } 123 | } 124 | 125 | func (nameOpts) ResolveOption(opt ropts.ResolveOpt) NameResolveOption { 126 | return func(settings *NameResolveSettings) error { 127 | settings.ResolveOpts = append(settings.ResolveOpts, opt) 128 | return nil 129 | } 130 | } 131 | -------------------------------------------------------------------------------- /options/object.go: -------------------------------------------------------------------------------- 1 | package options 2 | 3 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.ObjectNewSettings 4 | type ObjectNewSettings struct { 5 | Type string 6 | } 7 | 8 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.ObjectPutSettings 9 | type ObjectPutSettings struct { 10 | InputEnc string 11 | DataType string 12 | Pin bool 13 | } 14 | 15 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.ObjectAddLinkSettings 16 | type ObjectAddLinkSettings struct { 17 | Create bool 18 | } 19 | 20 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.ObjectNewOption 21 | type ObjectNewOption func(*ObjectNewSettings) error 22 | 23 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.ObjectPutOption 24 | type ObjectPutOption func(*ObjectPutSettings) error 25 | 26 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.ObjectAddLinkOption 27 | type ObjectAddLinkOption func(*ObjectAddLinkSettings) error 28 | 29 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.ObjectNewOptions 30 | func ObjectNewOptions(opts ...ObjectNewOption) (*ObjectNewSettings, error) { 31 | options := &ObjectNewSettings{ 32 | Type: "empty", 33 | } 34 | 35 | for _, opt := range opts { 36 | err := opt(options) 37 | if err != nil { 38 | return nil, err 39 | } 40 | } 41 | return options, nil 42 | } 43 | 44 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.ObjectPutOptions 45 | func ObjectPutOptions(opts ...ObjectPutOption) (*ObjectPutSettings, error) { 46 | options := &ObjectPutSettings{ 47 | InputEnc: "json", 48 | DataType: "text", 49 | Pin: false, 50 | } 51 | 52 | for _, opt := range opts { 53 | err := opt(options) 54 | if err != nil { 55 | return nil, err 56 | } 57 | } 58 | return options, nil 59 | } 60 | 61 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.ObjectAddLinkOptions 62 | func ObjectAddLinkOptions(opts ...ObjectAddLinkOption) (*ObjectAddLinkSettings, error) { 63 | options := &ObjectAddLinkSettings{ 64 | Create: false, 65 | } 66 | 67 | for _, opt := range opts { 68 | err := opt(options) 69 | if err != nil { 70 | return nil, err 71 | } 72 | } 73 | return options, nil 74 | } 75 | 76 | type objectOpts struct{} 77 | 78 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.Object 79 | var Object objectOpts 80 | 81 | // Type is an option for Object.New which allows to change the type of created 82 | // dag node. 83 | // 84 | // Supported types: 85 | // * 'empty' - Empty node 86 | // * 'unixfs-dir' - Empty UnixFS directory 87 | func (objectOpts) Type(t string) ObjectNewOption { 88 | return func(settings *ObjectNewSettings) error { 89 | settings.Type = t 90 | return nil 91 | } 92 | } 93 | 94 | // InputEnc is an option for Object.Put which specifies the input encoding of the 95 | // data. Default is "json". 96 | // 97 | // Supported encodings: 98 | // * "protobuf" 99 | // * "json" 100 | func (objectOpts) InputEnc(e string) ObjectPutOption { 101 | return func(settings *ObjectPutSettings) error { 102 | settings.InputEnc = e 103 | return nil 104 | } 105 | } 106 | 107 | // DataType is an option for Object.Put which specifies the encoding of data 108 | // field when using Json or XML input encoding. 109 | // 110 | // Supported types: 111 | // * "text" (default) 112 | // * "base64" 113 | func (objectOpts) DataType(t string) ObjectPutOption { 114 | return func(settings *ObjectPutSettings) error { 115 | settings.DataType = t 116 | return nil 117 | } 118 | } 119 | 120 | // Pin is an option for Object.Put which specifies whether to pin the added 121 | // objects, default is false 122 | func (objectOpts) Pin(pin bool) ObjectPutOption { 123 | return func(settings *ObjectPutSettings) error { 124 | settings.Pin = pin 125 | return nil 126 | } 127 | } 128 | 129 | // Create is an option for Object.AddLink which specifies whether create required 130 | // directories for the child 131 | func (objectOpts) Create(create bool) ObjectAddLinkOption { 132 | return func(settings *ObjectAddLinkSettings) error { 133 | settings.Create = create 134 | return nil 135 | } 136 | } 137 | -------------------------------------------------------------------------------- /object.go: -------------------------------------------------------------------------------- 1 | package iface 2 | 3 | import ( 4 | "context" 5 | "io" 6 | 7 | path "github.com/ipfs/interface-go-ipfs-core/path" 8 | 9 | "github.com/ipfs/interface-go-ipfs-core/options" 10 | 11 | "github.com/ipfs/go-cid" 12 | ipld "github.com/ipfs/go-ipld-format" 13 | ) 14 | 15 | // ObjectStat provides information about dag nodes 16 | // 17 | // Deprecated: use github.com/ipfs/boxo/coreiface.ObjectStat 18 | type ObjectStat struct { 19 | // Cid is the CID of the node 20 | Cid cid.Cid 21 | 22 | // NumLinks is number of links the node contains 23 | NumLinks int 24 | 25 | // BlockSize is size of the raw serialized node 26 | BlockSize int 27 | 28 | // LinksSize is size of the links block section 29 | LinksSize int 30 | 31 | // DataSize is the size of data block section 32 | DataSize int 33 | 34 | // CumulativeSize is size of the tree (BlockSize + link sizes) 35 | CumulativeSize int 36 | } 37 | 38 | // ChangeType denotes type of change in ObjectChange 39 | // 40 | // Deprecated: use github.com/ipfs/boxo/coreiface.ChangeType 41 | type ChangeType int 42 | 43 | const ( 44 | // DiffAdd is set when a link was added to the graph 45 | // 46 | // Deprecated: use github.com/ipfs/boxo/coreiface.DiffAdd 47 | DiffAdd ChangeType = iota 48 | 49 | // DiffRemove is set when a link was removed from the graph 50 | // 51 | // Deprecated: use github.com/ipfs/boxo/coreiface.DiffRemove 52 | DiffRemove 53 | 54 | // DiffMod is set when a link was changed in the graph 55 | // 56 | // Deprecated: use github.com/ipfs/boxo/coreiface.DiffMod 57 | DiffMod 58 | ) 59 | 60 | // ObjectChange represents a change ia a graph 61 | // 62 | // Deprecated: use github.com/ipfs/boxo/coreiface.ObjectChange 63 | type ObjectChange struct { 64 | // Type of the change, either: 65 | // * DiffAdd - Added a link 66 | // * DiffRemove - Removed a link 67 | // * DiffMod - Modified a link 68 | Type ChangeType 69 | 70 | // Path to the changed link 71 | Path string 72 | 73 | // Before holds the link path before the change. Note that when a link is 74 | // added, this will be nil. 75 | Before path.Resolved 76 | 77 | // After holds the link path after the change. Note that when a link is 78 | // removed, this will be nil. 79 | After path.Resolved 80 | } 81 | 82 | // ObjectAPI specifies the interface to MerkleDAG and contains useful utilities 83 | // for manipulating MerkleDAG data structures. 84 | // 85 | // Deprecated: use github.com/ipfs/boxo/coreiface.ObjectAPI 86 | type ObjectAPI interface { 87 | // New creates new, empty (by default) dag-node. 88 | New(context.Context, ...options.ObjectNewOption) (ipld.Node, error) 89 | 90 | // Put imports the data into merkledag 91 | Put(context.Context, io.Reader, ...options.ObjectPutOption) (path.Resolved, error) 92 | 93 | // Get returns the node for the path 94 | Get(context.Context, path.Path) (ipld.Node, error) 95 | 96 | // Data returns reader for data of the node 97 | Data(context.Context, path.Path) (io.Reader, error) 98 | 99 | // Links returns lint or links the node contains 100 | Links(context.Context, path.Path) ([]*ipld.Link, error) 101 | 102 | // Stat returns information about the node 103 | Stat(context.Context, path.Path) (*ObjectStat, error) 104 | 105 | // AddLink adds a link under the specified path. child path can point to a 106 | // subdirectory within the patent which must be present (can be overridden 107 | // with WithCreate option). 108 | AddLink(ctx context.Context, base path.Path, name string, child path.Path, opts ...options.ObjectAddLinkOption) (path.Resolved, error) 109 | 110 | // RmLink removes a link from the node 111 | RmLink(ctx context.Context, base path.Path, link string) (path.Resolved, error) 112 | 113 | // AppendData appends data to the node 114 | AppendData(context.Context, path.Path, io.Reader) (path.Resolved, error) 115 | 116 | // SetData sets the data contained in the node 117 | SetData(context.Context, path.Path, io.Reader) (path.Resolved, error) 118 | 119 | // Diff returns a set of changes needed to transform the first object into the 120 | // second. 121 | Diff(context.Context, path.Path, path.Path) ([]ObjectChange, error) 122 | } 123 | -------------------------------------------------------------------------------- /tests/dht.go: -------------------------------------------------------------------------------- 1 | package tests 2 | 3 | import ( 4 | "context" 5 | "io" 6 | "testing" 7 | "time" 8 | 9 | iface "github.com/ipfs/interface-go-ipfs-core" 10 | "github.com/ipfs/interface-go-ipfs-core/options" 11 | ) 12 | 13 | func (tp *TestSuite) TestDht(t *testing.T) { 14 | tp.hasApi(t, func(api iface.CoreAPI) error { 15 | if api.Dht() == nil { 16 | return errAPINotImplemented 17 | } 18 | return nil 19 | }) 20 | 21 | t.Run("TestDhtFindPeer", tp.TestDhtFindPeer) 22 | t.Run("TestDhtFindProviders", tp.TestDhtFindProviders) 23 | t.Run("TestDhtProvide", tp.TestDhtProvide) 24 | } 25 | 26 | func (tp *TestSuite) TestDhtFindPeer(t *testing.T) { 27 | ctx, cancel := context.WithCancel(context.Background()) 28 | defer cancel() 29 | apis, err := tp.MakeAPISwarm(ctx, true, 5) 30 | if err != nil { 31 | t.Fatal(err) 32 | } 33 | 34 | self0, err := apis[0].Key().Self(ctx) 35 | if err != nil { 36 | t.Fatal(err) 37 | } 38 | 39 | laddrs0, err := apis[0].Swarm().LocalAddrs(ctx) 40 | if err != nil { 41 | t.Fatal(err) 42 | } 43 | if len(laddrs0) != 1 { 44 | t.Fatal("unexpected number of local addrs") 45 | } 46 | 47 | time.Sleep(3 * time.Second) 48 | 49 | pi, err := apis[2].Dht().FindPeer(ctx, self0.ID()) 50 | if err != nil { 51 | t.Fatal(err) 52 | } 53 | 54 | if pi.Addrs[0].String() != laddrs0[0].String() { 55 | t.Errorf("got unexpected address from FindPeer: %s", pi.Addrs[0].String()) 56 | } 57 | 58 | self2, err := apis[2].Key().Self(ctx) 59 | if err != nil { 60 | t.Fatal(err) 61 | } 62 | 63 | pi, err = apis[1].Dht().FindPeer(ctx, self2.ID()) 64 | if err != nil { 65 | t.Fatal(err) 66 | } 67 | 68 | laddrs2, err := apis[2].Swarm().LocalAddrs(ctx) 69 | if err != nil { 70 | t.Fatal(err) 71 | } 72 | if len(laddrs2) != 1 { 73 | t.Fatal("unexpected number of local addrs") 74 | } 75 | 76 | if pi.Addrs[0].String() != laddrs2[0].String() { 77 | t.Errorf("got unexpected address from FindPeer: %s", pi.Addrs[0].String()) 78 | } 79 | } 80 | 81 | func (tp *TestSuite) TestDhtFindProviders(t *testing.T) { 82 | ctx, cancel := context.WithCancel(context.Background()) 83 | defer cancel() 84 | apis, err := tp.MakeAPISwarm(ctx, true, 5) 85 | if err != nil { 86 | t.Fatal(err) 87 | } 88 | 89 | p, err := addTestObject(ctx, apis[0]) 90 | if err != nil { 91 | t.Fatal(err) 92 | } 93 | 94 | time.Sleep(3 * time.Second) 95 | 96 | out, err := apis[2].Dht().FindProviders(ctx, p, options.Dht.NumProviders(1)) 97 | if err != nil { 98 | t.Fatal(err) 99 | } 100 | 101 | provider := <-out 102 | 103 | self0, err := apis[0].Key().Self(ctx) 104 | if err != nil { 105 | t.Fatal(err) 106 | } 107 | 108 | if provider.ID.String() != self0.ID().String() { 109 | t.Errorf("got wrong provider: %s != %s", provider.ID.String(), self0.ID().String()) 110 | } 111 | } 112 | 113 | func (tp *TestSuite) TestDhtProvide(t *testing.T) { 114 | ctx, cancel := context.WithCancel(context.Background()) 115 | defer cancel() 116 | apis, err := tp.MakeAPISwarm(ctx, true, 5) 117 | if err != nil { 118 | t.Fatal(err) 119 | } 120 | 121 | off0, err := apis[0].WithOptions(options.Api.Offline(true)) 122 | if err != nil { 123 | t.Fatal(err) 124 | } 125 | 126 | s, err := off0.Block().Put(ctx, &io.LimitedReader{R: rnd, N: 4092}) 127 | if err != nil { 128 | t.Fatal(err) 129 | } 130 | 131 | p := s.Path() 132 | 133 | time.Sleep(3 * time.Second) 134 | 135 | out, err := apis[2].Dht().FindProviders(ctx, p, options.Dht.NumProviders(1)) 136 | if err != nil { 137 | t.Fatal(err) 138 | } 139 | 140 | _, ok := <-out 141 | 142 | if ok { 143 | t.Fatal("did not expect to find any providers") 144 | } 145 | 146 | self0, err := apis[0].Key().Self(ctx) 147 | if err != nil { 148 | t.Fatal(err) 149 | } 150 | 151 | err = apis[0].Dht().Provide(ctx, p) 152 | if err != nil { 153 | t.Fatal(err) 154 | } 155 | 156 | out, err = apis[2].Dht().FindProviders(ctx, p, options.Dht.NumProviders(1)) 157 | if err != nil { 158 | t.Fatal(err) 159 | } 160 | 161 | provider := <-out 162 | 163 | if provider.ID.String() != self0.ID().String() { 164 | t.Errorf("got wrong provider: %s != %s", provider.ID.String(), self0.ID().String()) 165 | } 166 | } 167 | -------------------------------------------------------------------------------- /tests/dag.go: -------------------------------------------------------------------------------- 1 | package tests 2 | 3 | import ( 4 | "context" 5 | "math" 6 | gopath "path" 7 | "strings" 8 | "testing" 9 | 10 | path "github.com/ipfs/interface-go-ipfs-core/path" 11 | 12 | coreiface "github.com/ipfs/interface-go-ipfs-core" 13 | 14 | ipldcbor "github.com/ipfs/go-ipld-cbor" 15 | ipld "github.com/ipfs/go-ipld-format" 16 | mh "github.com/multiformats/go-multihash" 17 | ) 18 | 19 | func (tp *TestSuite) TestDag(t *testing.T) { 20 | tp.hasApi(t, func(api coreiface.CoreAPI) error { 21 | if api.Dag() == nil { 22 | return errAPINotImplemented 23 | } 24 | return nil 25 | }) 26 | 27 | t.Run("TestPut", tp.TestPut) 28 | t.Run("TestPutWithHash", tp.TestPutWithHash) 29 | t.Run("TestPath", tp.TestDagPath) 30 | t.Run("TestTree", tp.TestTree) 31 | t.Run("TestBatch", tp.TestBatch) 32 | } 33 | 34 | var ( 35 | treeExpected = map[string]struct{}{ 36 | "a": {}, 37 | "b": {}, 38 | "c": {}, 39 | "c/d": {}, 40 | "c/e": {}, 41 | } 42 | ) 43 | 44 | func (tp *TestSuite) TestPut(t *testing.T) { 45 | ctx, cancel := context.WithCancel(context.Background()) 46 | defer cancel() 47 | api, err := tp.makeAPI(ctx) 48 | if err != nil { 49 | t.Fatal(err) 50 | } 51 | 52 | nd, err := ipldcbor.FromJSON(strings.NewReader(`"Hello"`), math.MaxUint64, -1) 53 | if err != nil { 54 | t.Fatal(err) 55 | } 56 | 57 | err = api.Dag().Add(ctx, nd) 58 | if err != nil { 59 | t.Fatal(err) 60 | } 61 | 62 | if nd.Cid().String() != "bafyreicnga62zhxnmnlt6ymq5hcbsg7gdhqdu6z4ehu3wpjhvqnflfy6nm" { 63 | t.Errorf("got wrong cid: %s", nd.Cid().String()) 64 | } 65 | } 66 | 67 | func (tp *TestSuite) TestPutWithHash(t *testing.T) { 68 | ctx, cancel := context.WithCancel(context.Background()) 69 | defer cancel() 70 | api, err := tp.makeAPI(ctx) 71 | if err != nil { 72 | t.Fatal(err) 73 | } 74 | 75 | nd, err := ipldcbor.FromJSON(strings.NewReader(`"Hello"`), mh.SHA3_256, -1) 76 | if err != nil { 77 | t.Fatal(err) 78 | } 79 | 80 | err = api.Dag().Add(ctx, nd) 81 | if err != nil { 82 | t.Fatal(err) 83 | } 84 | 85 | if nd.Cid().String() != "bafyrmifu7haikttpqqgc5ewvmp76z3z4ebp7h2ph4memw7dq4nt6btmxny" { 86 | t.Errorf("got wrong cid: %s", nd.Cid().String()) 87 | } 88 | } 89 | 90 | func (tp *TestSuite) TestDagPath(t *testing.T) { 91 | ctx, cancel := context.WithCancel(context.Background()) 92 | defer cancel() 93 | api, err := tp.makeAPI(ctx) 94 | if err != nil { 95 | t.Fatal(err) 96 | } 97 | 98 | snd, err := ipldcbor.FromJSON(strings.NewReader(`"foo"`), math.MaxUint64, -1) 99 | if err != nil { 100 | t.Fatal(err) 101 | } 102 | 103 | err = api.Dag().Add(ctx, snd) 104 | if err != nil { 105 | t.Fatal(err) 106 | } 107 | 108 | nd, err := ipldcbor.FromJSON(strings.NewReader(`{"lnk": {"/": "`+snd.Cid().String()+`"}}`), math.MaxUint64, -1) 109 | if err != nil { 110 | t.Fatal(err) 111 | } 112 | 113 | err = api.Dag().Add(ctx, nd) 114 | if err != nil { 115 | t.Fatal(err) 116 | } 117 | 118 | p := path.New(gopath.Join(nd.Cid().String(), "lnk")) 119 | 120 | rp, err := api.ResolvePath(ctx, p) 121 | if err != nil { 122 | t.Fatal(err) 123 | } 124 | 125 | ndd, err := api.Dag().Get(ctx, rp.Cid()) 126 | if err != nil { 127 | t.Fatal(err) 128 | } 129 | 130 | if ndd.Cid().String() != snd.Cid().String() { 131 | t.Errorf("got unexpected cid %s, expected %s", ndd.Cid().String(), snd.Cid().String()) 132 | } 133 | } 134 | 135 | func (tp *TestSuite) TestTree(t *testing.T) { 136 | ctx, cancel := context.WithCancel(context.Background()) 137 | defer cancel() 138 | api, err := tp.makeAPI(ctx) 139 | if err != nil { 140 | t.Fatal(err) 141 | } 142 | 143 | nd, err := ipldcbor.FromJSON(strings.NewReader(`{"a": 123, "b": "foo", "c": {"d": 321, "e": 111}}`), math.MaxUint64, -1) 144 | if err != nil { 145 | t.Fatal(err) 146 | } 147 | 148 | err = api.Dag().Add(ctx, nd) 149 | if err != nil { 150 | t.Fatal(err) 151 | } 152 | 153 | res, err := api.Dag().Get(ctx, nd.Cid()) 154 | if err != nil { 155 | t.Fatal(err) 156 | } 157 | 158 | lst := res.Tree("", -1) 159 | if len(lst) != len(treeExpected) { 160 | t.Errorf("tree length of %d doesn't match expected %d", len(lst), len(treeExpected)) 161 | } 162 | 163 | for _, ent := range lst { 164 | if _, ok := treeExpected[ent]; !ok { 165 | t.Errorf("unexpected tree entry %s", ent) 166 | } 167 | } 168 | } 169 | 170 | func (tp *TestSuite) TestBatch(t *testing.T) { 171 | ctx, cancel := context.WithCancel(context.Background()) 172 | defer cancel() 173 | api, err := tp.makeAPI(ctx) 174 | if err != nil { 175 | t.Fatal(err) 176 | } 177 | 178 | nd, err := ipldcbor.FromJSON(strings.NewReader(`"Hello"`), math.MaxUint64, -1) 179 | if err != nil { 180 | t.Fatal(err) 181 | } 182 | 183 | if nd.Cid().String() != "bafyreicnga62zhxnmnlt6ymq5hcbsg7gdhqdu6z4ehu3wpjhvqnflfy6nm" { 184 | t.Errorf("got wrong cid: %s", nd.Cid().String()) 185 | } 186 | 187 | _, err = api.Dag().Get(ctx, nd.Cid()) 188 | if err == nil || !strings.Contains(err.Error(), "not found") { 189 | t.Fatal(err) 190 | } 191 | 192 | if err := api.Dag().AddMany(ctx, []ipld.Node{nd}); err != nil { 193 | t.Fatal(err) 194 | } 195 | 196 | _, err = api.Dag().Get(ctx, nd.Cid()) 197 | if err != nil { 198 | t.Fatal(err) 199 | } 200 | } 201 | -------------------------------------------------------------------------------- /tests/path.go: -------------------------------------------------------------------------------- 1 | package tests 2 | 3 | import ( 4 | "context" 5 | "math" 6 | "strings" 7 | "testing" 8 | 9 | "github.com/ipfs/interface-go-ipfs-core/path" 10 | 11 | "github.com/ipfs/interface-go-ipfs-core/options" 12 | 13 | ipldcbor "github.com/ipfs/go-ipld-cbor" 14 | ) 15 | 16 | func (tp *TestSuite) TestPath(t *testing.T) { 17 | t.Run("TestMutablePath", tp.TestMutablePath) 18 | t.Run("TestPathRemainder", tp.TestPathRemainder) 19 | t.Run("TestEmptyPathRemainder", tp.TestEmptyPathRemainder) 20 | t.Run("TestInvalidPathRemainder", tp.TestInvalidPathRemainder) 21 | t.Run("TestPathRoot", tp.TestPathRoot) 22 | t.Run("TestPathJoin", tp.TestPathJoin) 23 | } 24 | 25 | func (tp *TestSuite) TestMutablePath(t *testing.T) { 26 | ctx, cancel := context.WithCancel(context.Background()) 27 | defer cancel() 28 | api, err := tp.makeAPI(ctx) 29 | if err != nil { 30 | t.Fatal(err) 31 | } 32 | 33 | blk, err := api.Block().Put(ctx, strings.NewReader(`foo`)) 34 | if err != nil { 35 | t.Fatal(err) 36 | } 37 | 38 | if blk.Path().Mutable() { 39 | t.Error("expected /ipld path to be immutable") 40 | } 41 | 42 | // get self /ipns path 43 | 44 | if api.Key() == nil { 45 | t.Fatal(".Key not implemented") 46 | } 47 | 48 | keys, err := api.Key().List(ctx) 49 | if err != nil { 50 | t.Fatal(err) 51 | } 52 | 53 | if !keys[0].Path().Mutable() { 54 | t.Error("expected self /ipns path to be mutable") 55 | } 56 | } 57 | 58 | func (tp *TestSuite) TestPathRemainder(t *testing.T) { 59 | ctx, cancel := context.WithCancel(context.Background()) 60 | defer cancel() 61 | api, err := tp.makeAPI(ctx) 62 | if err != nil { 63 | t.Fatal(err) 64 | } 65 | 66 | if api.Dag() == nil { 67 | t.Fatal(".Dag not implemented") 68 | } 69 | 70 | nd, err := ipldcbor.FromJSON(strings.NewReader(`{"foo": {"bar": "baz"}}`), math.MaxUint64, -1) 71 | if err != nil { 72 | t.Fatal(err) 73 | } 74 | 75 | if err := api.Dag().Add(ctx, nd); err != nil { 76 | t.Fatal(err) 77 | } 78 | 79 | rp1, err := api.ResolvePath(ctx, path.New(nd.String()+"/foo/bar")) 80 | if err != nil { 81 | t.Fatal(err) 82 | } 83 | 84 | if rp1.Remainder() != "foo/bar" { 85 | t.Error("expected to get path remainder") 86 | } 87 | } 88 | 89 | func (tp *TestSuite) TestEmptyPathRemainder(t *testing.T) { 90 | ctx, cancel := context.WithCancel(context.Background()) 91 | defer cancel() 92 | api, err := tp.makeAPI(ctx) 93 | if err != nil { 94 | t.Fatal(err) 95 | } 96 | 97 | if api.Dag() == nil { 98 | t.Fatal(".Dag not implemented") 99 | } 100 | 101 | nd, err := ipldcbor.FromJSON(strings.NewReader(`{"foo": {"bar": "baz"}}`), math.MaxUint64, -1) 102 | if err != nil { 103 | t.Fatal(err) 104 | } 105 | 106 | if err := api.Dag().Add(ctx, nd); err != nil { 107 | t.Fatal(err) 108 | } 109 | 110 | rp1, err := api.ResolvePath(ctx, path.New(nd.Cid().String())) 111 | if err != nil { 112 | t.Fatal(err) 113 | } 114 | 115 | if rp1.Remainder() != "" { 116 | t.Error("expected the resolved path to not have a remainder") 117 | } 118 | } 119 | 120 | func (tp *TestSuite) TestInvalidPathRemainder(t *testing.T) { 121 | ctx, cancel := context.WithCancel(context.Background()) 122 | defer cancel() 123 | api, err := tp.makeAPI(ctx) 124 | if err != nil { 125 | t.Fatal(err) 126 | } 127 | 128 | if api.Dag() == nil { 129 | t.Fatal(".Dag not implemented") 130 | } 131 | 132 | nd, err := ipldcbor.FromJSON(strings.NewReader(`{"foo": {"bar": "baz"}}`), math.MaxUint64, -1) 133 | if err != nil { 134 | t.Fatal(err) 135 | } 136 | 137 | if err := api.Dag().Add(ctx, nd); err != nil { 138 | t.Fatal(err) 139 | } 140 | 141 | _, err = api.ResolvePath(ctx, path.New("/ipld/"+nd.Cid().String()+"/bar/baz")) 142 | if err == nil || !strings.Contains(err.Error(), `no link named "bar"`) { 143 | t.Fatalf("unexpected error: %s", err) 144 | } 145 | } 146 | 147 | func (tp *TestSuite) TestPathRoot(t *testing.T) { 148 | ctx, cancel := context.WithCancel(context.Background()) 149 | defer cancel() 150 | api, err := tp.makeAPI(ctx) 151 | if err != nil { 152 | t.Fatal(err) 153 | } 154 | 155 | if api.Block() == nil { 156 | t.Fatal(".Block not implemented") 157 | } 158 | 159 | blk, err := api.Block().Put(ctx, strings.NewReader(`foo`), options.Block.Format("raw")) 160 | if err != nil { 161 | t.Fatal(err) 162 | } 163 | 164 | if api.Dag() == nil { 165 | t.Fatal(".Dag not implemented") 166 | } 167 | 168 | nd, err := ipldcbor.FromJSON(strings.NewReader(`{"foo": {"/": "`+blk.Path().Cid().String()+`"}}`), math.MaxUint64, -1) 169 | if err != nil { 170 | t.Fatal(err) 171 | } 172 | 173 | if err := api.Dag().Add(ctx, nd); err != nil { 174 | t.Fatal(err) 175 | } 176 | 177 | rp, err := api.ResolvePath(ctx, path.New("/ipld/"+nd.Cid().String()+"/foo")) 178 | if err != nil { 179 | t.Fatal(err) 180 | } 181 | 182 | if rp.Root().String() != nd.Cid().String() { 183 | t.Error("unexpected path root") 184 | } 185 | 186 | if rp.Cid().String() != blk.Path().Cid().String() { 187 | t.Error("unexpected path cid") 188 | } 189 | } 190 | 191 | func (tp *TestSuite) TestPathJoin(t *testing.T) { 192 | p1 := path.New("/ipfs/QmYNmQKp6SuaVrpgWRsPTgCQCnpxUYGq76YEKBXuj2N4H6/bar/baz") 193 | 194 | if path.Join(p1, "foo").String() != "/ipfs/QmYNmQKp6SuaVrpgWRsPTgCQCnpxUYGq76YEKBXuj2N4H6/bar/baz/foo" { 195 | t.Error("unexpected path") 196 | } 197 | } 198 | -------------------------------------------------------------------------------- /options/block.go: -------------------------------------------------------------------------------- 1 | package options 2 | 3 | import ( 4 | "fmt" 5 | 6 | cid "github.com/ipfs/go-cid" 7 | mc "github.com/multiformats/go-multicodec" 8 | mh "github.com/multiformats/go-multihash" 9 | ) 10 | 11 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.BlockPutSettings 12 | type BlockPutSettings struct { 13 | CidPrefix cid.Prefix 14 | Pin bool 15 | } 16 | 17 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.BlockRmSettings 18 | type BlockRmSettings struct { 19 | Force bool 20 | } 21 | 22 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.BlockPutOption 23 | type BlockPutOption func(*BlockPutSettings) error 24 | 25 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.BlockRmOption 26 | type BlockRmOption func(*BlockRmSettings) error 27 | 28 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.BlockPutOptions 29 | func BlockPutOptions(opts ...BlockPutOption) (*BlockPutSettings, error) { 30 | var cidPrefix cid.Prefix 31 | 32 | // Baseline is CIDv1 raw sha2-255-32 (can be tweaked later via opts) 33 | cidPrefix.Version = 1 34 | cidPrefix.Codec = uint64(mc.Raw) 35 | cidPrefix.MhType = mh.SHA2_256 36 | cidPrefix.MhLength = -1 // -1 means len is to be calculated during mh.Sum() 37 | 38 | options := &BlockPutSettings{ 39 | CidPrefix: cidPrefix, 40 | Pin: false, 41 | } 42 | 43 | // Apply any overrides 44 | for _, opt := range opts { 45 | err := opt(options) 46 | if err != nil { 47 | return nil, err 48 | } 49 | } 50 | 51 | return options, nil 52 | } 53 | 54 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.BlockRmOptions 55 | func BlockRmOptions(opts ...BlockRmOption) (*BlockRmSettings, error) { 56 | options := &BlockRmSettings{ 57 | Force: false, 58 | } 59 | 60 | for _, opt := range opts { 61 | err := opt(options) 62 | if err != nil { 63 | return nil, err 64 | } 65 | } 66 | return options, nil 67 | } 68 | 69 | type blockOpts struct{} 70 | 71 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.Block 72 | var Block blockOpts 73 | 74 | // CidCodec is the modern option for Block.Put which specifies the multicodec to use 75 | // in the CID returned by the Block.Put operation. 76 | // It uses correct codes from go-multicodec and replaces the old Format now with CIDv1 as the default. 77 | func (blockOpts) CidCodec(codecName string) BlockPutOption { 78 | return func(settings *BlockPutSettings) error { 79 | if codecName == "" { 80 | return nil 81 | } 82 | code, err := codeFromName(codecName) 83 | if err != nil { 84 | return err 85 | } 86 | settings.CidPrefix.Codec = uint64(code) 87 | return nil 88 | } 89 | } 90 | 91 | // Map string to code from go-multicodec 92 | func codeFromName(codecName string) (mc.Code, error) { 93 | var cidCodec mc.Code 94 | err := cidCodec.Set(codecName) 95 | return cidCodec, err 96 | } 97 | 98 | // Format is a legacy option for Block.Put which specifies the multicodec to 99 | // use to serialize the object. 100 | // Provided for backward-compatibility only. Use CidCodec instead. 101 | func (blockOpts) Format(format string) BlockPutOption { 102 | return func(settings *BlockPutSettings) error { 103 | if format == "" { 104 | return nil 105 | } 106 | // Opt-in CIDv0 support for backward-compatibility 107 | if format == "v0" { 108 | settings.CidPrefix.Version = 0 109 | } 110 | 111 | // Fixup a legacy (invalid) names for dag-pb (0x70) 112 | if format == "v0" || format == "protobuf" { 113 | format = "dag-pb" 114 | } 115 | 116 | // Fixup invalid name for dag-cbor (0x71) 117 | if format == "cbor" { 118 | format = "dag-cbor" 119 | } 120 | 121 | // Set code based on name passed as "format" 122 | code, err := codeFromName(format) 123 | if err != nil { 124 | return err 125 | } 126 | settings.CidPrefix.Codec = uint64(code) 127 | 128 | // If CIDv0, ensure all parameters are compatible 129 | // (in theory go-cid would validate this anyway, but we want to provide better errors) 130 | pref := settings.CidPrefix 131 | if pref.Version == 0 { 132 | if pref.Codec != uint64(mc.DagPb) { 133 | return fmt.Errorf("only dag-pb is allowed with CIDv0") 134 | } 135 | if pref.MhType != mh.SHA2_256 || (pref.MhLength != -1 && pref.MhLength != 32) { 136 | return fmt.Errorf("only sha2-255-32 is allowed with CIDv0") 137 | } 138 | } 139 | 140 | return nil 141 | } 142 | 143 | } 144 | 145 | // Hash is an option for Block.Put which specifies the multihash settings to use 146 | // when hashing the object. Default is mh.SHA2_256 (0x12). 147 | // If mhLen is set to -1, default length for the hash will be used 148 | func (blockOpts) Hash(mhType uint64, mhLen int) BlockPutOption { 149 | return func(settings *BlockPutSettings) error { 150 | settings.CidPrefix.MhType = mhType 151 | settings.CidPrefix.MhLength = mhLen 152 | return nil 153 | } 154 | } 155 | 156 | // Pin is an option for Block.Put which specifies whether to (recursively) pin 157 | // added blocks 158 | func (blockOpts) Pin(pin bool) BlockPutOption { 159 | return func(settings *BlockPutSettings) error { 160 | settings.Pin = pin 161 | return nil 162 | } 163 | } 164 | 165 | // Force is an option for Block.Rm which, when set to true, will ignore 166 | // non-existing blocks 167 | func (blockOpts) Force(force bool) BlockRmOption { 168 | return func(settings *BlockRmSettings) error { 169 | settings.Force = force 170 | return nil 171 | } 172 | } 173 | -------------------------------------------------------------------------------- /options/namesys/opts.go: -------------------------------------------------------------------------------- 1 | package nsopts 2 | 3 | import ( 4 | "time" 5 | ) 6 | 7 | const ( 8 | // DefaultDepthLimit is the default depth limit used by Resolve. 9 | // 10 | // Deprecated: use github.com/ipfs/boxo/coreiface/options/namesys.DefaultDepthLimit 11 | DefaultDepthLimit = 32 12 | 13 | // UnlimitedDepth allows infinite recursion in Resolve. You 14 | // probably don't want to use this, but it's here if you absolutely 15 | // trust resolution to eventually complete and can't put an upper 16 | // limit on how many steps it will take. 17 | // 18 | // Deprecated: use github.com/ipfs/boxo/coreiface/options/namesys.UnlimitedDepth 19 | UnlimitedDepth = 0 20 | 21 | // DefaultIPNSRecordTTL specifies the time that the record can be cached 22 | // before checking if its validity again. 23 | // 24 | // Deprecated: use github.com/ipfs/boxo/coreiface/options/namesys.DefaultIPNSRecordTTL 25 | DefaultIPNSRecordTTL = time.Minute 26 | 27 | // DefaultIPNSRecordEOL specifies the time that the network will cache IPNS 28 | // records after being published. Records should be re-published before this 29 | // interval expires. We use the same default expiration as the DHT. 30 | // 31 | // Deprecated: use github.com/ipfs/boxo/coreiface/options/namesys.DefaultIPNSRecordEOL 32 | DefaultIPNSRecordEOL = 48 * time.Hour 33 | ) 34 | 35 | // ResolveOpts specifies options for resolving an IPNS path 36 | // 37 | // Deprecated: use github.com/ipfs/boxo/coreiface/options/namesys.ResolveOpts 38 | type ResolveOpts struct { 39 | // Recursion depth limit 40 | Depth uint 41 | // The number of IPNS records to retrieve from the DHT 42 | // (the best record is selected from this set) 43 | DhtRecordCount uint 44 | // The amount of time to wait for DHT records to be fetched 45 | // and verified. A zero value indicates that there is no explicit 46 | // timeout (although there is an implicit timeout due to dial 47 | // timeouts within the DHT) 48 | DhtTimeout time.Duration 49 | } 50 | 51 | // DefaultResolveOpts returns the default options for resolving 52 | // an IPNS path 53 | // 54 | // Deprecated: use github.com/ipfs/boxo/coreiface/options/namesys.DefaultResolveOpts 55 | func DefaultResolveOpts() ResolveOpts { 56 | return ResolveOpts{ 57 | Depth: DefaultDepthLimit, 58 | DhtRecordCount: 16, 59 | DhtTimeout: time.Minute, 60 | } 61 | } 62 | 63 | // ResolveOpt is used to set an option 64 | // 65 | // Deprecated: use github.com/ipfs/boxo/coreiface/options/namesys.ResolveOpt 66 | type ResolveOpt func(*ResolveOpts) 67 | 68 | // Depth is the recursion depth limit 69 | // 70 | // Deprecated: use github.com/ipfs/boxo/coreiface/options/namesys.Depth 71 | func Depth(depth uint) ResolveOpt { 72 | return func(o *ResolveOpts) { 73 | o.Depth = depth 74 | } 75 | } 76 | 77 | // DhtRecordCount is the number of IPNS records to retrieve from the DHT 78 | // 79 | // Deprecated: use github.com/ipfs/boxo/coreiface/options/namesys.DhtRecordCount 80 | func DhtRecordCount(count uint) ResolveOpt { 81 | return func(o *ResolveOpts) { 82 | o.DhtRecordCount = count 83 | } 84 | } 85 | 86 | // DhtTimeout is the amount of time to wait for DHT records to be fetched 87 | // and verified. A zero value indicates that there is no explicit timeout 88 | // 89 | // Deprecated: use github.com/ipfs/boxo/coreiface/options/namesys.DhtTimeout 90 | func DhtTimeout(timeout time.Duration) ResolveOpt { 91 | return func(o *ResolveOpts) { 92 | o.DhtTimeout = timeout 93 | } 94 | } 95 | 96 | // ProcessOpts converts an array of ResolveOpt into a ResolveOpts object 97 | // 98 | // Deprecated: use github.com/ipfs/boxo/coreiface/options/namesys.ProcessOpts 99 | func ProcessOpts(opts []ResolveOpt) ResolveOpts { 100 | rsopts := DefaultResolveOpts() 101 | for _, option := range opts { 102 | option(&rsopts) 103 | } 104 | return rsopts 105 | } 106 | 107 | // PublishOptions specifies options for publishing an IPNS record. 108 | // 109 | // Deprecated: use github.com/ipfs/boxo/coreiface/options/namesys.PublishOptions 110 | type PublishOptions struct { 111 | EOL time.Time 112 | TTL time.Duration 113 | } 114 | 115 | // DefaultPublishOptions returns the default options for publishing an IPNS record. 116 | // 117 | // Deprecated: use github.com/ipfs/boxo/coreiface/options/namesys.DefaultPublishOptions 118 | func DefaultPublishOptions() PublishOptions { 119 | return PublishOptions{ 120 | EOL: time.Now().Add(DefaultIPNSRecordEOL), 121 | TTL: DefaultIPNSRecordTTL, 122 | } 123 | } 124 | 125 | // PublishOption is used to set an option for PublishOpts. 126 | // 127 | // Deprecated: use github.com/ipfs/boxo/coreiface/options/namesys.PublishOption 128 | type PublishOption func(*PublishOptions) 129 | 130 | // PublishWithEOL sets an EOL. 131 | // 132 | // Deprecated: use github.com/ipfs/boxo/coreiface/options/namesys.PublishWithEOL 133 | func PublishWithEOL(eol time.Time) PublishOption { 134 | return func(o *PublishOptions) { 135 | o.EOL = eol 136 | } 137 | } 138 | 139 | // PublishWithEOL sets a TTL. 140 | // 141 | // Deprecated: use github.com/ipfs/boxo/coreiface/options/namesys.PublishWithTTL 142 | func PublishWithTTL(ttl time.Duration) PublishOption { 143 | return func(o *PublishOptions) { 144 | o.TTL = ttl 145 | } 146 | } 147 | 148 | // ProcessPublishOptions converts an array of PublishOpt into a PublishOpts object. 149 | // 150 | // Deprecated: use github.com/ipfs/boxo/coreiface/options/namesys.ProcessPublishOptions 151 | func ProcessPublishOptions(opts []PublishOption) PublishOptions { 152 | rsopts := DefaultPublishOptions() 153 | for _, option := range opts { 154 | option(&rsopts) 155 | } 156 | return rsopts 157 | } 158 | -------------------------------------------------------------------------------- /path/path.go: -------------------------------------------------------------------------------- 1 | package path 2 | 3 | import ( 4 | "strings" 5 | 6 | cid "github.com/ipfs/go-cid" 7 | ipfspath "github.com/ipfs/go-path" 8 | ) 9 | 10 | // Path is a generic wrapper for paths used in the API. A path can be resolved 11 | // to a CID using one of Resolve functions in the API. 12 | // 13 | // Paths must be prefixed with a valid prefix: 14 | // 15 | // * /ipfs - Immutable unixfs path (files) 16 | // * /ipld - Immutable ipld path (data) 17 | // * /ipns - Mutable names. Usually resolves to one of the immutable paths 18 | // TODO: /local (MFS) 19 | // 20 | // Deprecated: use github.com/ipfs/boxo/coreiface/path.Path 21 | type Path interface { 22 | // String returns the path as a string. 23 | String() string 24 | 25 | // Namespace returns the first component of the path. 26 | // 27 | // For example path "/ipfs/QmHash", calling Namespace() will return "ipfs" 28 | // 29 | // Calling this method on invalid paths (IsValid() != nil) will result in 30 | // empty string 31 | Namespace() string 32 | 33 | // Mutable returns false if the data pointed to by this path in guaranteed 34 | // to not change. 35 | // 36 | // Note that resolved mutable path can be immutable. 37 | Mutable() bool 38 | 39 | // IsValid checks if this path is a valid ipfs Path, returning nil iff it is 40 | // valid 41 | IsValid() error 42 | } 43 | 44 | // Resolved is a path which was resolved to the last resolvable node. 45 | // ResolvedPaths are guaranteed to return nil from `IsValid` 46 | // 47 | // Deprecated: use github.com/ipfs/boxo/coreiface/path.Resolved 48 | type Resolved interface { 49 | // Cid returns the CID of the node referenced by the path. Remainder of the 50 | // path is guaranteed to be within the node. 51 | // 52 | // Examples: 53 | // If you have 3 linked objects: QmRoot -> A -> B: 54 | // 55 | // cidB := {"foo": {"bar": 42 }} 56 | // cidA := {"B": {"/": cidB }} 57 | // cidRoot := {"A": {"/": cidA }} 58 | // 59 | // And resolve paths: 60 | // 61 | // * "/ipfs/${cidRoot}" 62 | // * Calling Cid() will return `cidRoot` 63 | // * Calling Root() will return `cidRoot` 64 | // * Calling Remainder() will return `` 65 | // 66 | // * "/ipfs/${cidRoot}/A" 67 | // * Calling Cid() will return `cidA` 68 | // * Calling Root() will return `cidRoot` 69 | // * Calling Remainder() will return `` 70 | // 71 | // * "/ipfs/${cidRoot}/A/B/foo" 72 | // * Calling Cid() will return `cidB` 73 | // * Calling Root() will return `cidRoot` 74 | // * Calling Remainder() will return `foo` 75 | // 76 | // * "/ipfs/${cidRoot}/A/B/foo/bar" 77 | // * Calling Cid() will return `cidB` 78 | // * Calling Root() will return `cidRoot` 79 | // * Calling Remainder() will return `foo/bar` 80 | Cid() cid.Cid 81 | 82 | // Root returns the CID of the root object of the path 83 | // 84 | // Example: 85 | // If you have 3 linked objects: QmRoot -> A -> B, and resolve path 86 | // "/ipfs/QmRoot/A/B", the Root method will return the CID of object QmRoot 87 | // 88 | // For more examples see the documentation of Cid() method 89 | Root() cid.Cid 90 | 91 | // Remainder returns unresolved part of the path 92 | // 93 | // Example: 94 | // If you have 2 linked objects: QmRoot -> A, where A is a CBOR node 95 | // containing the following data: 96 | // 97 | // {"foo": {"bar": 42 }} 98 | // 99 | // When resolving "/ipld/QmRoot/A/foo/bar", Remainder will return "foo/bar" 100 | // 101 | // For more examples see the documentation of Cid() method 102 | Remainder() string 103 | 104 | Path 105 | } 106 | 107 | // path implements coreiface.Path 108 | type path struct { 109 | path string 110 | } 111 | 112 | // resolvedPath implements coreiface.resolvedPath 113 | type resolvedPath struct { 114 | path 115 | cid cid.Cid 116 | root cid.Cid 117 | remainder string 118 | } 119 | 120 | // Join appends provided segments to the base path 121 | // 122 | // Deprecated: use github.com/ipfs/boxo/coreiface/path.Join 123 | func Join(base Path, a ...string) Path { 124 | s := strings.Join(append([]string{base.String()}, a...), "/") 125 | return &path{path: s} 126 | } 127 | 128 | // IpfsPath creates new /ipfs path from the provided CID 129 | // 130 | // Deprecated: use github.com/ipfs/boxo/coreiface/path.IpfsPath 131 | func IpfsPath(c cid.Cid) Resolved { 132 | return &resolvedPath{ 133 | path: path{"/ipfs/" + c.String()}, 134 | cid: c, 135 | root: c, 136 | remainder: "", 137 | } 138 | } 139 | 140 | // IpldPath creates new /ipld path from the provided CID 141 | // 142 | // Deprecated: use github.com/ipfs/boxo/coreiface/path.IpldPath 143 | func IpldPath(c cid.Cid) Resolved { 144 | return &resolvedPath{ 145 | path: path{"/ipld/" + c.String()}, 146 | cid: c, 147 | root: c, 148 | remainder: "", 149 | } 150 | } 151 | 152 | // New parses string path to a Path 153 | // 154 | // Deprecated: use github.com/ipfs/boxo/coreiface/path.New 155 | func New(p string) Path { 156 | if pp, err := ipfspath.ParsePath(p); err == nil { 157 | p = pp.String() 158 | } 159 | 160 | return &path{path: p} 161 | } 162 | 163 | // NewResolvedPath creates new Resolved path. This function performs no checks 164 | // and is intended to be used by resolver implementations. Incorrect inputs may 165 | // cause panics. Handle with care. 166 | // 167 | // Deprecated: use github.com/ipfs/boxo/coreiface/path.NewResolvedPath 168 | func NewResolvedPath(ipath ipfspath.Path, c cid.Cid, root cid.Cid, remainder string) Resolved { 169 | return &resolvedPath{ 170 | path: path{ipath.String()}, 171 | cid: c, 172 | root: root, 173 | remainder: remainder, 174 | } 175 | } 176 | 177 | func (p *path) String() string { 178 | return p.path 179 | } 180 | 181 | func (p *path) Namespace() string { 182 | ip, err := ipfspath.ParsePath(p.path) 183 | if err != nil { 184 | return "" 185 | } 186 | 187 | if len(ip.Segments()) < 1 { 188 | panic("path without namespace") // this shouldn't happen under any scenario 189 | } 190 | return ip.Segments()[0] 191 | } 192 | 193 | func (p *path) Mutable() bool { 194 | // TODO: MFS: check for /local 195 | return p.Namespace() == "ipns" 196 | } 197 | 198 | func (p *path) IsValid() error { 199 | _, err := ipfspath.ParsePath(p.path) 200 | return err 201 | } 202 | 203 | func (p *resolvedPath) Cid() cid.Cid { 204 | return p.cid 205 | } 206 | 207 | func (p *resolvedPath) Root() cid.Cid { 208 | return p.root 209 | } 210 | 211 | func (p *resolvedPath) Remainder() string { 212 | return p.remainder 213 | } 214 | -------------------------------------------------------------------------------- /tests/name.go: -------------------------------------------------------------------------------- 1 | package tests 2 | 3 | import ( 4 | "context" 5 | "io" 6 | "math/rand" 7 | gopath "path" 8 | "testing" 9 | "time" 10 | 11 | path "github.com/ipfs/interface-go-ipfs-core/path" 12 | 13 | "github.com/ipfs/go-libipfs/files" 14 | 15 | coreiface "github.com/ipfs/interface-go-ipfs-core" 16 | opt "github.com/ipfs/interface-go-ipfs-core/options" 17 | ) 18 | 19 | func (tp *TestSuite) TestName(t *testing.T) { 20 | tp.hasApi(t, func(api coreiface.CoreAPI) error { 21 | if api.Name() == nil { 22 | return errAPINotImplemented 23 | } 24 | return nil 25 | }) 26 | 27 | t.Run("TestPublishResolve", tp.TestPublishResolve) 28 | t.Run("TestBasicPublishResolveKey", tp.TestBasicPublishResolveKey) 29 | t.Run("TestBasicPublishResolveTimeout", tp.TestBasicPublishResolveTimeout) 30 | } 31 | 32 | var rnd = rand.New(rand.NewSource(0x62796532303137)) 33 | 34 | func addTestObject(ctx context.Context, api coreiface.CoreAPI) (path.Path, error) { 35 | return api.Unixfs().Add(ctx, files.NewReaderFile(&io.LimitedReader{R: rnd, N: 4092})) 36 | } 37 | 38 | func appendPath(p path.Path, sub string) path.Path { 39 | return path.New(gopath.Join(p.String(), sub)) 40 | } 41 | 42 | func (tp *TestSuite) TestPublishResolve(t *testing.T) { 43 | ctx, cancel := context.WithCancel(context.Background()) 44 | defer cancel() 45 | init := func() (coreiface.CoreAPI, path.Path) { 46 | apis, err := tp.MakeAPISwarm(ctx, true, 5) 47 | if err != nil { 48 | t.Fatal(err) 49 | return nil, nil 50 | } 51 | api := apis[0] 52 | 53 | p, err := addTestObject(ctx, api) 54 | if err != nil { 55 | t.Fatal(err) 56 | return nil, nil 57 | } 58 | return api, p 59 | } 60 | run := func(t *testing.T, ropts []opt.NameResolveOption) { 61 | t.Run("basic", func(t *testing.T) { 62 | api, p := init() 63 | e, err := api.Name().Publish(ctx, p) 64 | if err != nil { 65 | t.Fatal(err) 66 | } 67 | 68 | self, err := api.Key().Self(ctx) 69 | if err != nil { 70 | t.Fatal(err) 71 | } 72 | 73 | if e.Name() != coreiface.FormatKeyID(self.ID()) { 74 | t.Errorf("expected e.Name to equal '%s', got '%s'", coreiface.FormatKeyID(self.ID()), e.Name()) 75 | } 76 | 77 | if e.Value().String() != p.String() { 78 | t.Errorf("expected paths to match, '%s'!='%s'", e.Value().String(), p.String()) 79 | } 80 | 81 | resPath, err := api.Name().Resolve(ctx, e.Name(), ropts...) 82 | if err != nil { 83 | t.Fatal(err) 84 | } 85 | 86 | if resPath.String() != p.String() { 87 | t.Errorf("expected paths to match, '%s'!='%s'", resPath.String(), p.String()) 88 | } 89 | }) 90 | 91 | t.Run("publishPath", func(t *testing.T) { 92 | api, p := init() 93 | e, err := api.Name().Publish(ctx, appendPath(p, "/test")) 94 | if err != nil { 95 | t.Fatal(err) 96 | } 97 | 98 | self, err := api.Key().Self(ctx) 99 | if err != nil { 100 | t.Fatal(err) 101 | } 102 | 103 | if e.Name() != coreiface.FormatKeyID(self.ID()) { 104 | t.Errorf("expected e.Name to equal '%s', got '%s'", coreiface.FormatKeyID(self.ID()), e.Name()) 105 | } 106 | 107 | if e.Value().String() != p.String()+"/test" { 108 | t.Errorf("expected paths to match, '%s'!='%s'", e.Value().String(), p.String()) 109 | } 110 | 111 | resPath, err := api.Name().Resolve(ctx, e.Name(), ropts...) 112 | if err != nil { 113 | t.Fatal(err) 114 | } 115 | 116 | if resPath.String() != p.String()+"/test" { 117 | t.Errorf("expected paths to match, '%s'!='%s'", resPath.String(), p.String()+"/test") 118 | } 119 | }) 120 | 121 | t.Run("revolvePath", func(t *testing.T) { 122 | api, p := init() 123 | e, err := api.Name().Publish(ctx, p) 124 | if err != nil { 125 | t.Fatal(err) 126 | } 127 | 128 | self, err := api.Key().Self(ctx) 129 | if err != nil { 130 | t.Fatal(err) 131 | } 132 | 133 | if e.Name() != coreiface.FormatKeyID(self.ID()) { 134 | t.Errorf("expected e.Name to equal '%s', got '%s'", coreiface.FormatKeyID(self.ID()), e.Name()) 135 | } 136 | 137 | if e.Value().String() != p.String() { 138 | t.Errorf("expected paths to match, '%s'!='%s'", e.Value().String(), p.String()) 139 | } 140 | 141 | resPath, err := api.Name().Resolve(ctx, e.Name()+"/test", ropts...) 142 | if err != nil { 143 | t.Fatal(err) 144 | } 145 | 146 | if resPath.String() != p.String()+"/test" { 147 | t.Errorf("expected paths to match, '%s'!='%s'", resPath.String(), p.String()+"/test") 148 | } 149 | }) 150 | 151 | t.Run("publishRevolvePath", func(t *testing.T) { 152 | api, p := init() 153 | e, err := api.Name().Publish(ctx, appendPath(p, "/a")) 154 | if err != nil { 155 | t.Fatal(err) 156 | } 157 | 158 | self, err := api.Key().Self(ctx) 159 | if err != nil { 160 | t.Fatal(err) 161 | } 162 | 163 | if e.Name() != coreiface.FormatKeyID(self.ID()) { 164 | t.Errorf("expected e.Name to equal '%s', got '%s'", coreiface.FormatKeyID(self.ID()), e.Name()) 165 | } 166 | 167 | if e.Value().String() != p.String()+"/a" { 168 | t.Errorf("expected paths to match, '%s'!='%s'", e.Value().String(), p.String()) 169 | } 170 | 171 | resPath, err := api.Name().Resolve(ctx, e.Name()+"/b", ropts...) 172 | if err != nil { 173 | t.Fatal(err) 174 | } 175 | 176 | if resPath.String() != p.String()+"/a/b" { 177 | t.Errorf("expected paths to match, '%s'!='%s'", resPath.String(), p.String()+"/a/b") 178 | } 179 | }) 180 | } 181 | 182 | t.Run("default", func(t *testing.T) { 183 | run(t, []opt.NameResolveOption{}) 184 | }) 185 | 186 | t.Run("nocache", func(t *testing.T) { 187 | run(t, []opt.NameResolveOption{opt.Name.Cache(false)}) 188 | }) 189 | } 190 | 191 | func (tp *TestSuite) TestBasicPublishResolveKey(t *testing.T) { 192 | ctx, cancel := context.WithCancel(context.Background()) 193 | defer cancel() 194 | apis, err := tp.MakeAPISwarm(ctx, true, 5) 195 | if err != nil { 196 | t.Fatal(err) 197 | } 198 | api := apis[0] 199 | 200 | k, err := api.Key().Generate(ctx, "foo") 201 | if err != nil { 202 | t.Fatal(err) 203 | } 204 | 205 | p, err := addTestObject(ctx, api) 206 | if err != nil { 207 | t.Fatal(err) 208 | } 209 | 210 | e, err := api.Name().Publish(ctx, p, opt.Name.Key(k.Name())) 211 | if err != nil { 212 | t.Fatal(err) 213 | } 214 | 215 | if e.Name() != coreiface.FormatKey(k) { 216 | t.Errorf("expected e.Name to equal %s, got '%s'", e.Name(), coreiface.FormatKey(k)) 217 | } 218 | 219 | if e.Value().String() != p.String() { 220 | t.Errorf("expected paths to match, '%s'!='%s'", e.Value().String(), p.String()) 221 | } 222 | 223 | resPath, err := api.Name().Resolve(ctx, e.Name()) 224 | if err != nil { 225 | t.Fatal(err) 226 | } 227 | 228 | if resPath.String() != p.String() { 229 | t.Errorf("expected paths to match, '%s'!='%s'", resPath.String(), p.String()) 230 | } 231 | } 232 | 233 | func (tp *TestSuite) TestBasicPublishResolveTimeout(t *testing.T) { 234 | t.Skip("ValidTime doesn't appear to work at this time resolution") 235 | 236 | ctx, cancel := context.WithCancel(context.Background()) 237 | defer cancel() 238 | apis, err := tp.MakeAPISwarm(ctx, true, 5) 239 | if err != nil { 240 | t.Fatal(err) 241 | } 242 | api := apis[0] 243 | p, err := addTestObject(ctx, api) 244 | if err != nil { 245 | t.Fatal(err) 246 | } 247 | 248 | e, err := api.Name().Publish(ctx, p, opt.Name.ValidTime(time.Millisecond*100)) 249 | if err != nil { 250 | t.Fatal(err) 251 | } 252 | 253 | self, err := api.Key().Self(ctx) 254 | if err != nil { 255 | t.Fatal(err) 256 | } 257 | 258 | if e.Name() != coreiface.FormatKeyID(self.ID()) { 259 | t.Errorf("expected e.Name to equal '%s', got '%s'", coreiface.FormatKeyID(self.ID()), e.Name()) 260 | } 261 | 262 | if e.Value().String() != p.String() { 263 | t.Errorf("expected paths to match, '%s'!='%s'", e.Value().String(), p.String()) 264 | } 265 | 266 | time.Sleep(time.Second) 267 | 268 | _, err = api.Name().Resolve(ctx, e.Name()) 269 | if err == nil { 270 | t.Fatal("Expected an error") 271 | } 272 | } 273 | 274 | //TODO: When swarm api is created, add multinode tests 275 | -------------------------------------------------------------------------------- /options/unixfs.go: -------------------------------------------------------------------------------- 1 | package options 2 | 3 | import ( 4 | "errors" 5 | "fmt" 6 | 7 | cid "github.com/ipfs/go-cid" 8 | dag "github.com/ipfs/go-merkledag" 9 | mh "github.com/multiformats/go-multihash" 10 | ) 11 | 12 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.Layout 13 | type Layout int 14 | 15 | const ( 16 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.BalancedLayout 17 | BalancedLayout Layout = iota 18 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.TrickleLayout 19 | TrickleLayout 20 | ) 21 | 22 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.UnixfsAddSettings 23 | type UnixfsAddSettings struct { 24 | CidVersion int 25 | MhType uint64 26 | 27 | Inline bool 28 | InlineLimit int 29 | RawLeaves bool 30 | RawLeavesSet bool 31 | 32 | Chunker string 33 | Layout Layout 34 | 35 | Pin bool 36 | OnlyHash bool 37 | FsCache bool 38 | NoCopy bool 39 | 40 | Events chan<- interface{} 41 | Silent bool 42 | Progress bool 43 | } 44 | 45 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.UnixfsLsSettings 46 | type UnixfsLsSettings struct { 47 | ResolveChildren bool 48 | UseCumulativeSize bool 49 | } 50 | 51 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.UnixfsAddOption 52 | type UnixfsAddOption func(*UnixfsAddSettings) error 53 | 54 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.UnixfsLsOption 55 | type UnixfsLsOption func(*UnixfsLsSettings) error 56 | 57 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.UnixfsAddOptions 58 | func UnixfsAddOptions(opts ...UnixfsAddOption) (*UnixfsAddSettings, cid.Prefix, error) { 59 | options := &UnixfsAddSettings{ 60 | CidVersion: -1, 61 | MhType: mh.SHA2_256, 62 | 63 | Inline: false, 64 | InlineLimit: 32, 65 | RawLeaves: false, 66 | RawLeavesSet: false, 67 | 68 | Chunker: "size-262144", 69 | Layout: BalancedLayout, 70 | 71 | Pin: false, 72 | OnlyHash: false, 73 | FsCache: false, 74 | NoCopy: false, 75 | 76 | Events: nil, 77 | Silent: false, 78 | Progress: false, 79 | } 80 | 81 | for _, opt := range opts { 82 | err := opt(options) 83 | if err != nil { 84 | return nil, cid.Prefix{}, err 85 | } 86 | } 87 | 88 | // nocopy -> rawblocks 89 | if options.NoCopy && !options.RawLeaves { 90 | // fixed? 91 | if options.RawLeavesSet { 92 | return nil, cid.Prefix{}, fmt.Errorf("nocopy option requires '--raw-leaves' to be enabled as well") 93 | } 94 | 95 | // No, satisfy mandatory constraint. 96 | options.RawLeaves = true 97 | } 98 | 99 | // (hash != "sha2-256") -> CIDv1 100 | if options.MhType != mh.SHA2_256 { 101 | switch options.CidVersion { 102 | case 0: 103 | return nil, cid.Prefix{}, errors.New("CIDv0 only supports sha2-256") 104 | case 1, -1: 105 | options.CidVersion = 1 106 | default: 107 | return nil, cid.Prefix{}, fmt.Errorf("unknown CID version: %d", options.CidVersion) 108 | } 109 | } else { 110 | if options.CidVersion < 0 { 111 | // Default to CIDv0 112 | options.CidVersion = 0 113 | } 114 | } 115 | 116 | // cidV1 -> raw blocks (by default) 117 | if options.CidVersion > 0 && !options.RawLeavesSet { 118 | options.RawLeaves = true 119 | } 120 | 121 | prefix, err := dag.PrefixForCidVersion(options.CidVersion) 122 | if err != nil { 123 | return nil, cid.Prefix{}, err 124 | } 125 | 126 | prefix.MhType = options.MhType 127 | prefix.MhLength = -1 128 | 129 | return options, prefix, nil 130 | } 131 | 132 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.UnixfsLsOptions 133 | func UnixfsLsOptions(opts ...UnixfsLsOption) (*UnixfsLsSettings, error) { 134 | options := &UnixfsLsSettings{ 135 | ResolveChildren: true, 136 | } 137 | 138 | for _, opt := range opts { 139 | err := opt(options) 140 | if err != nil { 141 | return nil, err 142 | } 143 | } 144 | 145 | return options, nil 146 | } 147 | 148 | type unixfsOpts struct{} 149 | 150 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.Unixfs 151 | var Unixfs unixfsOpts 152 | 153 | // CidVersion specifies which CID version to use. Defaults to 0 unless an option 154 | // that depends on CIDv1 is passed. 155 | func (unixfsOpts) CidVersion(version int) UnixfsAddOption { 156 | return func(settings *UnixfsAddSettings) error { 157 | settings.CidVersion = version 158 | return nil 159 | } 160 | } 161 | 162 | // Hash function to use. Implies CIDv1 if not set to sha2-256 (default). 163 | // 164 | // Table of functions is declared in https://github.com/multiformats/go-multihash/blob/master/multihash.go 165 | func (unixfsOpts) Hash(mhtype uint64) UnixfsAddOption { 166 | return func(settings *UnixfsAddSettings) error { 167 | settings.MhType = mhtype 168 | return nil 169 | } 170 | } 171 | 172 | // RawLeaves specifies whether to use raw blocks for leaves (data nodes with no 173 | // links) instead of wrapping them with unixfs structures. 174 | func (unixfsOpts) RawLeaves(enable bool) UnixfsAddOption { 175 | return func(settings *UnixfsAddSettings) error { 176 | settings.RawLeaves = enable 177 | settings.RawLeavesSet = true 178 | return nil 179 | } 180 | } 181 | 182 | // Inline tells the adder to inline small blocks into CIDs 183 | func (unixfsOpts) Inline(enable bool) UnixfsAddOption { 184 | return func(settings *UnixfsAddSettings) error { 185 | settings.Inline = enable 186 | return nil 187 | } 188 | } 189 | 190 | // InlineLimit sets the amount of bytes below which blocks will be encoded 191 | // directly into CID instead of being stored and addressed by it's hash. 192 | // Specifying this option won't enable block inlining. For that use `Inline` 193 | // option. Default: 32 bytes 194 | // 195 | // Note that while there is no hard limit on the number of bytes, it should be 196 | // kept at a reasonably low value, such as 64; implementations may choose to 197 | // reject anything larger. 198 | func (unixfsOpts) InlineLimit(limit int) UnixfsAddOption { 199 | return func(settings *UnixfsAddSettings) error { 200 | settings.InlineLimit = limit 201 | return nil 202 | } 203 | } 204 | 205 | // Chunker specifies settings for the chunking algorithm to use. 206 | // 207 | // Default: size-262144, formats: 208 | // size-[bytes] - Simple chunker splitting data into blocks of n bytes 209 | // rabin-[min]-[avg]-[max] - Rabin chunker 210 | func (unixfsOpts) Chunker(chunker string) UnixfsAddOption { 211 | return func(settings *UnixfsAddSettings) error { 212 | settings.Chunker = chunker 213 | return nil 214 | } 215 | } 216 | 217 | // Layout tells the adder how to balance data between leaves. 218 | // options.BalancedLayout is the default, it's optimized for static seekable 219 | // files. 220 | // options.TrickleLayout is optimized for streaming data, 221 | func (unixfsOpts) Layout(layout Layout) UnixfsAddOption { 222 | return func(settings *UnixfsAddSettings) error { 223 | settings.Layout = layout 224 | return nil 225 | } 226 | } 227 | 228 | // Pin tells the adder to pin the file root recursively after adding 229 | func (unixfsOpts) Pin(pin bool) UnixfsAddOption { 230 | return func(settings *UnixfsAddSettings) error { 231 | settings.Pin = pin 232 | return nil 233 | } 234 | } 235 | 236 | // HashOnly will make the adder calculate data hash without storing it in the 237 | // blockstore or announcing it to the network 238 | func (unixfsOpts) HashOnly(hashOnly bool) UnixfsAddOption { 239 | return func(settings *UnixfsAddSettings) error { 240 | settings.OnlyHash = hashOnly 241 | return nil 242 | } 243 | } 244 | 245 | // Events specifies channel which will be used to report events about ongoing 246 | // Add operation. 247 | // 248 | // Note that if this channel blocks it may slowdown the adder 249 | func (unixfsOpts) Events(sink chan<- interface{}) UnixfsAddOption { 250 | return func(settings *UnixfsAddSettings) error { 251 | settings.Events = sink 252 | return nil 253 | } 254 | } 255 | 256 | // Silent reduces event output 257 | func (unixfsOpts) Silent(silent bool) UnixfsAddOption { 258 | return func(settings *UnixfsAddSettings) error { 259 | settings.Silent = silent 260 | return nil 261 | } 262 | } 263 | 264 | // Progress tells the adder whether to enable progress events 265 | func (unixfsOpts) Progress(enable bool) UnixfsAddOption { 266 | return func(settings *UnixfsAddSettings) error { 267 | settings.Progress = enable 268 | return nil 269 | } 270 | } 271 | 272 | // FsCache tells the adder to check the filestore for pre-existing blocks 273 | // 274 | // Experimental 275 | func (unixfsOpts) FsCache(enable bool) UnixfsAddOption { 276 | return func(settings *UnixfsAddSettings) error { 277 | settings.FsCache = enable 278 | return nil 279 | } 280 | } 281 | 282 | // NoCopy tells the adder to add the files using filestore. Implies RawLeaves. 283 | // 284 | // Experimental 285 | func (unixfsOpts) Nocopy(enable bool) UnixfsAddOption { 286 | return func(settings *UnixfsAddSettings) error { 287 | settings.NoCopy = enable 288 | return nil 289 | } 290 | } 291 | 292 | func (unixfsOpts) ResolveChildren(resolve bool) UnixfsLsOption { 293 | return func(settings *UnixfsLsSettings) error { 294 | settings.ResolveChildren = resolve 295 | return nil 296 | } 297 | } 298 | 299 | func (unixfsOpts) UseCumulativeSize(use bool) UnixfsLsOption { 300 | return func(settings *UnixfsLsSettings) error { 301 | settings.UseCumulativeSize = use 302 | return nil 303 | } 304 | } 305 | -------------------------------------------------------------------------------- /tests/block.go: -------------------------------------------------------------------------------- 1 | package tests 2 | 3 | import ( 4 | "bytes" 5 | "context" 6 | "io" 7 | "strings" 8 | "testing" 9 | 10 | ipld "github.com/ipfs/go-ipld-format" 11 | coreiface "github.com/ipfs/interface-go-ipfs-core" 12 | opt "github.com/ipfs/interface-go-ipfs-core/options" 13 | "github.com/ipfs/interface-go-ipfs-core/path" 14 | 15 | mh "github.com/multiformats/go-multihash" 16 | ) 17 | 18 | var ( 19 | pbCidV0 = "QmZULkCELmmk5XNfCgTnCyFgAVxBRBXyDHGGMVoLFLiXEN" // dag-pb 20 | pbCid = "bafybeiffndsajwhk3lwjewwdxqntmjm4b5wxaaanokonsggenkbw6slwk4" // dag-pb 21 | rawCid = "bafkreiffndsajwhk3lwjewwdxqntmjm4b5wxaaanokonsggenkbw6slwk4" // raw bytes 22 | cborCid = "bafyreicnga62zhxnmnlt6ymq5hcbsg7gdhqdu6z4ehu3wpjhvqnflfy6nm" // dag-cbor 23 | cborKCid = "bafyr2qgsohbwdlk7ajmmbb4lhoytmest4wdbe5xnexfvtxeatuyqqmwv3fgxp3pmhpc27gwey2cct56gloqefoqwcf3yqiqzsaqb7p4jefhcw" // dag-cbor keccak-512 24 | ) 25 | 26 | // dag-pb 27 | func pbBlock() io.Reader { 28 | return bytes.NewReader([]byte{10, 12, 8, 2, 18, 6, 104, 101, 108, 108, 111, 10, 24, 6}) 29 | } 30 | 31 | // dag-cbor 32 | func cborBlock() io.Reader { 33 | return bytes.NewReader([]byte{101, 72, 101, 108, 108, 111}) 34 | } 35 | 36 | func (tp *TestSuite) TestBlock(t *testing.T) { 37 | tp.hasApi(t, func(api coreiface.CoreAPI) error { 38 | if api.Block() == nil { 39 | return errAPINotImplemented 40 | } 41 | return nil 42 | }) 43 | 44 | t.Run("TestBlockPut (get raw CIDv1)", tp.TestBlockPut) 45 | t.Run("TestBlockPutCidCodec: dag-pb", tp.TestBlockPutCidCodecDagPb) 46 | t.Run("TestBlockPutCidCodec: dag-cbor", tp.TestBlockPutCidCodecDagCbor) 47 | t.Run("TestBlockPutFormat (legacy): cbor → dag-cbor", tp.TestBlockPutFormatDagCbor) 48 | t.Run("TestBlockPutFormat (legacy): protobuf → dag-pb", tp.TestBlockPutFormatDagPb) 49 | t.Run("TestBlockPutFormat (legacy): v0 → CIDv0", tp.TestBlockPutFormatV0) 50 | t.Run("TestBlockPutHash", tp.TestBlockPutHash) 51 | t.Run("TestBlockGet", tp.TestBlockGet) 52 | t.Run("TestBlockRm", tp.TestBlockRm) 53 | t.Run("TestBlockStat", tp.TestBlockStat) 54 | t.Run("TestBlockPin", tp.TestBlockPin) 55 | } 56 | 57 | // when no opts are passed, produced CID has 'raw' codec 58 | func (tp *TestSuite) TestBlockPut(t *testing.T) { 59 | ctx, cancel := context.WithCancel(context.Background()) 60 | defer cancel() 61 | api, err := tp.makeAPI(ctx) 62 | if err != nil { 63 | t.Fatal(err) 64 | } 65 | 66 | res, err := api.Block().Put(ctx, pbBlock()) 67 | if err != nil { 68 | t.Fatal(err) 69 | } 70 | 71 | if res.Path().Cid().String() != rawCid { 72 | t.Errorf("got wrong cid: %s", res.Path().Cid().String()) 73 | } 74 | } 75 | 76 | // Format is deprecated, it used invalid codec names. 77 | // Confirm 'cbor' gets fixed to 'dag-cbor' 78 | func (tp *TestSuite) TestBlockPutFormatDagCbor(t *testing.T) { 79 | ctx, cancel := context.WithCancel(context.Background()) 80 | defer cancel() 81 | api, err := tp.makeAPI(ctx) 82 | if err != nil { 83 | t.Fatal(err) 84 | } 85 | 86 | res, err := api.Block().Put(ctx, cborBlock(), opt.Block.Format("cbor")) 87 | if err != nil { 88 | t.Fatal(err) 89 | } 90 | 91 | if res.Path().Cid().String() != cborCid { 92 | t.Errorf("got wrong cid: %s", res.Path().Cid().String()) 93 | } 94 | } 95 | 96 | // Format is deprecated, it used invalid codec names. 97 | // Confirm 'protobuf' got fixed to 'dag-pb' 98 | func (tp *TestSuite) TestBlockPutFormatDagPb(t *testing.T) { 99 | ctx, cancel := context.WithCancel(context.Background()) 100 | defer cancel() 101 | api, err := tp.makeAPI(ctx) 102 | if err != nil { 103 | t.Fatal(err) 104 | } 105 | 106 | res, err := api.Block().Put(ctx, pbBlock(), opt.Block.Format("protobuf")) 107 | if err != nil { 108 | t.Fatal(err) 109 | } 110 | 111 | if res.Path().Cid().String() != pbCid { 112 | t.Errorf("got wrong cid: %s", res.Path().Cid().String()) 113 | } 114 | } 115 | 116 | // Format is deprecated, it used invalid codec names. 117 | // Confirm fake codec 'v0' got fixed to CIDv0 (with implicit dag-pb codec) 118 | func (tp *TestSuite) TestBlockPutFormatV0(t *testing.T) { 119 | ctx, cancel := context.WithCancel(context.Background()) 120 | defer cancel() 121 | api, err := tp.makeAPI(ctx) 122 | if err != nil { 123 | t.Fatal(err) 124 | } 125 | 126 | res, err := api.Block().Put(ctx, pbBlock(), opt.Block.Format("v0")) 127 | if err != nil { 128 | t.Fatal(err) 129 | } 130 | 131 | if res.Path().Cid().String() != pbCidV0 { 132 | t.Errorf("got wrong cid: %s", res.Path().Cid().String()) 133 | } 134 | } 135 | 136 | func (tp *TestSuite) TestBlockPutCidCodecDagCbor(t *testing.T) { 137 | ctx, cancel := context.WithCancel(context.Background()) 138 | defer cancel() 139 | api, err := tp.makeAPI(ctx) 140 | if err != nil { 141 | t.Fatal(err) 142 | } 143 | 144 | res, err := api.Block().Put(ctx, cborBlock(), opt.Block.CidCodec("dag-cbor")) 145 | if err != nil { 146 | t.Fatal(err) 147 | } 148 | 149 | if res.Path().Cid().String() != cborCid { 150 | t.Errorf("got wrong cid: %s", res.Path().Cid().String()) 151 | } 152 | } 153 | 154 | func (tp *TestSuite) TestBlockPutCidCodecDagPb(t *testing.T) { 155 | ctx, cancel := context.WithCancel(context.Background()) 156 | defer cancel() 157 | api, err := tp.makeAPI(ctx) 158 | if err != nil { 159 | t.Fatal(err) 160 | } 161 | 162 | res, err := api.Block().Put(ctx, pbBlock(), opt.Block.CidCodec("dag-pb")) 163 | if err != nil { 164 | t.Fatal(err) 165 | } 166 | 167 | if res.Path().Cid().String() != pbCid { 168 | t.Errorf("got wrong cid: %s", res.Path().Cid().String()) 169 | } 170 | } 171 | 172 | func (tp *TestSuite) TestBlockPutHash(t *testing.T) { 173 | ctx, cancel := context.WithCancel(context.Background()) 174 | defer cancel() 175 | api, err := tp.makeAPI(ctx) 176 | if err != nil { 177 | t.Fatal(err) 178 | } 179 | 180 | res, err := api.Block().Put( 181 | ctx, 182 | cborBlock(), 183 | opt.Block.Hash(mh.KECCAK_512, -1), 184 | opt.Block.CidCodec("dag-cbor"), 185 | ) 186 | if err != nil { 187 | t.Fatal(err) 188 | } 189 | 190 | if res.Path().Cid().String() != cborKCid { 191 | t.Errorf("got wrong cid: %s", res.Path().Cid().String()) 192 | } 193 | } 194 | 195 | func (tp *TestSuite) TestBlockGet(t *testing.T) { 196 | ctx, cancel := context.WithCancel(context.Background()) 197 | defer cancel() 198 | api, err := tp.makeAPI(ctx) 199 | if err != nil { 200 | t.Fatal(err) 201 | } 202 | 203 | res, err := api.Block().Put(ctx, strings.NewReader(`Hello`), opt.Block.Format("raw")) 204 | if err != nil { 205 | t.Fatal(err) 206 | } 207 | 208 | r, err := api.Block().Get(ctx, res.Path()) 209 | if err != nil { 210 | t.Fatal(err) 211 | } 212 | 213 | d, err := io.ReadAll(r) 214 | if err != nil { 215 | t.Fatal(err) 216 | } 217 | 218 | if string(d) != "Hello" { 219 | t.Error("didn't get correct data back") 220 | } 221 | 222 | p := path.New("/ipfs/" + res.Path().Cid().String()) 223 | 224 | rp, err := api.ResolvePath(ctx, p) 225 | if err != nil { 226 | t.Fatal(err) 227 | } 228 | if rp.Cid().String() != res.Path().Cid().String() { 229 | t.Error("paths didn't match") 230 | } 231 | } 232 | 233 | func (tp *TestSuite) TestBlockRm(t *testing.T) { 234 | ctx, cancel := context.WithCancel(context.Background()) 235 | defer cancel() 236 | api, err := tp.makeAPI(ctx) 237 | if err != nil { 238 | t.Fatal(err) 239 | } 240 | 241 | res, err := api.Block().Put(ctx, strings.NewReader(`Hello`), opt.Block.Format("raw")) 242 | if err != nil { 243 | t.Fatal(err) 244 | } 245 | 246 | r, err := api.Block().Get(ctx, res.Path()) 247 | if err != nil { 248 | t.Fatal(err) 249 | } 250 | 251 | d, err := io.ReadAll(r) 252 | if err != nil { 253 | t.Fatal(err) 254 | } 255 | 256 | if string(d) != "Hello" { 257 | t.Error("didn't get correct data back") 258 | } 259 | 260 | err = api.Block().Rm(ctx, res.Path()) 261 | if err != nil { 262 | t.Fatal(err) 263 | } 264 | 265 | _, err = api.Block().Get(ctx, res.Path()) 266 | if err == nil { 267 | t.Fatal("expected err to exist") 268 | } 269 | if !ipld.IsNotFound(err) { 270 | t.Errorf("unexpected error; %s", err.Error()) 271 | } 272 | 273 | err = api.Block().Rm(ctx, res.Path()) 274 | if err == nil { 275 | t.Fatal("expected err to exist") 276 | } 277 | if !ipld.IsNotFound(err) { 278 | t.Errorf("unexpected error; %s", err.Error()) 279 | } 280 | 281 | err = api.Block().Rm(ctx, res.Path(), opt.Block.Force(true)) 282 | if err != nil { 283 | t.Fatal(err) 284 | } 285 | } 286 | 287 | func (tp *TestSuite) TestBlockStat(t *testing.T) { 288 | ctx, cancel := context.WithCancel(context.Background()) 289 | defer cancel() 290 | api, err := tp.makeAPI(ctx) 291 | if err != nil { 292 | t.Fatal(err) 293 | } 294 | 295 | res, err := api.Block().Put(ctx, strings.NewReader(`Hello`), opt.Block.Format("raw")) 296 | if err != nil { 297 | t.Fatal(err) 298 | } 299 | 300 | stat, err := api.Block().Stat(ctx, res.Path()) 301 | if err != nil { 302 | t.Fatal(err) 303 | } 304 | 305 | if stat.Path().String() != res.Path().String() { 306 | t.Error("paths don't match") 307 | } 308 | 309 | if stat.Size() != len("Hello") { 310 | t.Error("length doesn't match") 311 | } 312 | } 313 | 314 | func (tp *TestSuite) TestBlockPin(t *testing.T) { 315 | ctx, cancel := context.WithCancel(context.Background()) 316 | defer cancel() 317 | api, err := tp.makeAPI(ctx) 318 | if err != nil { 319 | t.Fatal(err) 320 | } 321 | 322 | _, err = api.Block().Put(ctx, strings.NewReader(`Hello`), opt.Block.Format("raw")) 323 | if err != nil { 324 | t.Fatal(err) 325 | } 326 | 327 | if pins, err := api.Pin().Ls(ctx); err != nil || len(pins) != 0 { 328 | t.Fatal("expected 0 pins") 329 | } 330 | 331 | res, err := api.Block().Put( 332 | ctx, 333 | strings.NewReader(`Hello`), 334 | opt.Block.Pin(true), 335 | opt.Block.Format("raw"), 336 | ) 337 | if err != nil { 338 | t.Fatal(err) 339 | } 340 | 341 | pins, err := accPins(api.Pin().Ls(ctx)) 342 | if err != nil { 343 | t.Fatal(err) 344 | } 345 | if len(pins) != 1 { 346 | t.Fatal("expected 1 pin") 347 | } 348 | if pins[0].Type() != "recursive" { 349 | t.Error("expected a recursive pin") 350 | } 351 | if pins[0].Path().String() != res.Path().String() { 352 | t.Error("pin path didn't match") 353 | } 354 | } 355 | -------------------------------------------------------------------------------- /options/pin.go: -------------------------------------------------------------------------------- 1 | package options 2 | 3 | import "fmt" 4 | 5 | // PinAddSettings represent the settings for PinAPI.Add 6 | // 7 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.PinAddSettings 8 | type PinAddSettings struct { 9 | Recursive bool 10 | } 11 | 12 | // PinLsSettings represent the settings for PinAPI.Ls 13 | // 14 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.PinLsSettings 15 | type PinLsSettings struct { 16 | Type string 17 | } 18 | 19 | // PinIsPinnedSettings represent the settings for PinAPI.IsPinned 20 | // 21 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.PinIsPinnedSettings 22 | type PinIsPinnedSettings struct { 23 | WithType string 24 | } 25 | 26 | // PinRmSettings represents the settings for PinAPI.Rm 27 | // 28 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.PinRmSettings 29 | type PinRmSettings struct { 30 | Recursive bool 31 | } 32 | 33 | // PinUpdateSettings represent the settings for PinAPI.Update 34 | // 35 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.PinUpdateSettings 36 | type PinUpdateSettings struct { 37 | Unpin bool 38 | } 39 | 40 | // PinAddOption is the signature of an option for PinAPI.Add 41 | // 42 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.PinAddOption 43 | type PinAddOption func(*PinAddSettings) error 44 | 45 | // PinLsOption is the signature of an option for PinAPI.Ls 46 | // 47 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.PinLsOption 48 | type PinLsOption func(*PinLsSettings) error 49 | 50 | // PinIsPinnedOption is the signature of an option for PinAPI.IsPinned 51 | // 52 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.PinIsPinnedOption 53 | type PinIsPinnedOption func(*PinIsPinnedSettings) error 54 | 55 | // PinRmOption is the signature of an option for PinAPI.Rm 56 | // 57 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.PinRmOption 58 | type PinRmOption func(*PinRmSettings) error 59 | 60 | // PinUpdateOption is the signature of an option for PinAPI.Update 61 | // 62 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.PinUpdateOption 63 | type PinUpdateOption func(*PinUpdateSettings) error 64 | 65 | // PinAddOptions compile a series of PinAddOption into a ready to use 66 | // PinAddSettings and set the default values. 67 | // 68 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.PinAddOptions 69 | func PinAddOptions(opts ...PinAddOption) (*PinAddSettings, error) { 70 | options := &PinAddSettings{ 71 | Recursive: true, 72 | } 73 | 74 | for _, opt := range opts { 75 | err := opt(options) 76 | if err != nil { 77 | return nil, err 78 | } 79 | } 80 | 81 | return options, nil 82 | } 83 | 84 | // PinLsOptions compile a series of PinLsOption into a ready to use 85 | // PinLsSettings and set the default values. 86 | // 87 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.PinLsOptions 88 | func PinLsOptions(opts ...PinLsOption) (*PinLsSettings, error) { 89 | options := &PinLsSettings{ 90 | Type: "all", 91 | } 92 | 93 | for _, opt := range opts { 94 | err := opt(options) 95 | if err != nil { 96 | return nil, err 97 | } 98 | } 99 | 100 | return options, nil 101 | } 102 | 103 | // PinIsPinnedOptions compile a series of PinIsPinnedOption into a ready to use 104 | // PinIsPinnedSettings and set the default values. 105 | // 106 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.PinIsPinnedOptions 107 | func PinIsPinnedOptions(opts ...PinIsPinnedOption) (*PinIsPinnedSettings, error) { 108 | options := &PinIsPinnedSettings{ 109 | WithType: "all", 110 | } 111 | 112 | for _, opt := range opts { 113 | err := opt(options) 114 | if err != nil { 115 | return nil, err 116 | } 117 | } 118 | 119 | return options, nil 120 | } 121 | 122 | // PinRmOptions compile a series of PinRmOption into a ready to use 123 | // PinRmSettings and set the default values. 124 | // 125 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.PinRmOptions 126 | func PinRmOptions(opts ...PinRmOption) (*PinRmSettings, error) { 127 | options := &PinRmSettings{ 128 | Recursive: true, 129 | } 130 | 131 | for _, opt := range opts { 132 | if err := opt(options); err != nil { 133 | return nil, err 134 | } 135 | } 136 | 137 | return options, nil 138 | } 139 | 140 | // PinUpdateOptions compile a series of PinUpdateOption into a ready to use 141 | // PinUpdateSettings and set the default values. 142 | // 143 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.PinUpdateOptions 144 | func PinUpdateOptions(opts ...PinUpdateOption) (*PinUpdateSettings, error) { 145 | options := &PinUpdateSettings{ 146 | Unpin: true, 147 | } 148 | 149 | for _, opt := range opts { 150 | err := opt(options) 151 | if err != nil { 152 | return nil, err 153 | } 154 | } 155 | 156 | return options, nil 157 | } 158 | 159 | type pinOpts struct { 160 | Ls pinLsOpts 161 | IsPinned pinIsPinnedOpts 162 | } 163 | 164 | // Pin provide an access to all the options for the Pin API. 165 | // 166 | // Deprecated: use github.com/ipfs/boxo/coreiface/options.Pin 167 | var Pin pinOpts 168 | 169 | type pinLsOpts struct{} 170 | 171 | // All is an option for Pin.Ls which will make it return all pins. It is 172 | // the default 173 | func (pinLsOpts) All() PinLsOption { 174 | return Pin.Ls.pinType("all") 175 | } 176 | 177 | // Recursive is an option for Pin.Ls which will make it only return recursive 178 | // pins 179 | func (pinLsOpts) Recursive() PinLsOption { 180 | return Pin.Ls.pinType("recursive") 181 | } 182 | 183 | // Direct is an option for Pin.Ls which will make it only return direct (non 184 | // recursive) pins 185 | func (pinLsOpts) Direct() PinLsOption { 186 | return Pin.Ls.pinType("direct") 187 | } 188 | 189 | // Indirect is an option for Pin.Ls which will make it only return indirect pins 190 | // (objects referenced by other recursively pinned objects) 191 | func (pinLsOpts) Indirect() PinLsOption { 192 | return Pin.Ls.pinType("indirect") 193 | } 194 | 195 | // Type is an option for Pin.Ls which will make it only return pins of the given 196 | // type. 197 | // 198 | // Supported values: 199 | // - "direct" - directly pinned objects 200 | // - "recursive" - roots of recursive pins 201 | // - "indirect" - indirectly pinned objects (referenced by recursively pinned 202 | // objects) 203 | // - "all" - all pinned objects (default) 204 | func (pinLsOpts) Type(typeStr string) (PinLsOption, error) { 205 | switch typeStr { 206 | case "all", "direct", "indirect", "recursive": 207 | return Pin.Ls.pinType(typeStr), nil 208 | default: 209 | return nil, fmt.Errorf("invalid type '%s', must be one of {direct, indirect, recursive, all}", typeStr) 210 | } 211 | } 212 | 213 | // pinType is an option for Pin.Ls which allows to specify which pin types should 214 | // be returned 215 | // 216 | // Supported values: 217 | // - "direct" - directly pinned objects 218 | // - "recursive" - roots of recursive pins 219 | // - "indirect" - indirectly pinned objects (referenced by recursively pinned 220 | // objects) 221 | // - "all" - all pinned objects (default) 222 | func (pinLsOpts) pinType(t string) PinLsOption { 223 | return func(settings *PinLsSettings) error { 224 | settings.Type = t 225 | return nil 226 | } 227 | } 228 | 229 | type pinIsPinnedOpts struct{} 230 | 231 | // All is an option for Pin.IsPinned which will make it search in all type of pins. 232 | // It is the default 233 | func (pinIsPinnedOpts) All() PinIsPinnedOption { 234 | return Pin.IsPinned.pinType("all") 235 | } 236 | 237 | // Recursive is an option for Pin.IsPinned which will make it only search in 238 | // recursive pins 239 | func (pinIsPinnedOpts) Recursive() PinIsPinnedOption { 240 | return Pin.IsPinned.pinType("recursive") 241 | } 242 | 243 | // Direct is an option for Pin.IsPinned which will make it only search in direct 244 | // (non recursive) pins 245 | func (pinIsPinnedOpts) Direct() PinIsPinnedOption { 246 | return Pin.IsPinned.pinType("direct") 247 | } 248 | 249 | // Indirect is an option for Pin.IsPinned which will make it only search indirect 250 | // pins (objects referenced by other recursively pinned objects) 251 | func (pinIsPinnedOpts) Indirect() PinIsPinnedOption { 252 | return Pin.IsPinned.pinType("indirect") 253 | } 254 | 255 | // Type is an option for Pin.IsPinned which will make it only search pins of the given 256 | // type. 257 | // 258 | // Supported values: 259 | // - "direct" - directly pinned objects 260 | // - "recursive" - roots of recursive pins 261 | // - "indirect" - indirectly pinned objects (referenced by recursively pinned 262 | // objects) 263 | // - "all" - all pinned objects (default) 264 | func (pinIsPinnedOpts) Type(typeStr string) (PinIsPinnedOption, error) { 265 | switch typeStr { 266 | case "all", "direct", "indirect", "recursive": 267 | return Pin.IsPinned.pinType(typeStr), nil 268 | default: 269 | return nil, fmt.Errorf("invalid type '%s', must be one of {direct, indirect, recursive, all}", typeStr) 270 | } 271 | } 272 | 273 | // pinType is an option for Pin.IsPinned which allows to specify which pin type the given 274 | // pin is expected to be, speeding up the research. 275 | // 276 | // Supported values: 277 | // - "direct" - directly pinned objects 278 | // - "recursive" - roots of recursive pins 279 | // - "indirect" - indirectly pinned objects (referenced by recursively pinned 280 | // objects) 281 | // - "all" - all pinned objects (default) 282 | func (pinIsPinnedOpts) pinType(t string) PinIsPinnedOption { 283 | return func(settings *PinIsPinnedSettings) error { 284 | settings.WithType = t 285 | return nil 286 | } 287 | } 288 | 289 | // Recursive is an option for Pin.Add which specifies whether to pin an entire 290 | // object tree or just one object. Default: true 291 | func (pinOpts) Recursive(recursive bool) PinAddOption { 292 | return func(settings *PinAddSettings) error { 293 | settings.Recursive = recursive 294 | return nil 295 | } 296 | } 297 | 298 | // RmRecursive is an option for Pin.Rm which specifies whether to recursively 299 | // unpin the object linked to by the specified object(s). This does not remove 300 | // indirect pins referenced by other recursive pins. 301 | func (pinOpts) RmRecursive(recursive bool) PinRmOption { 302 | return func(settings *PinRmSettings) error { 303 | settings.Recursive = recursive 304 | return nil 305 | } 306 | } 307 | 308 | // Unpin is an option for Pin.Update which specifies whether to remove the old pin. 309 | // Default is true. 310 | func (pinOpts) Unpin(unpin bool) PinUpdateOption { 311 | return func(settings *PinUpdateSettings) error { 312 | settings.Unpin = unpin 313 | return nil 314 | } 315 | } 316 | -------------------------------------------------------------------------------- /tests/object.go: -------------------------------------------------------------------------------- 1 | package tests 2 | 3 | import ( 4 | "bytes" 5 | "context" 6 | "encoding/hex" 7 | "io" 8 | "strings" 9 | "testing" 10 | 11 | iface "github.com/ipfs/interface-go-ipfs-core" 12 | opt "github.com/ipfs/interface-go-ipfs-core/options" 13 | ) 14 | 15 | func (tp *TestSuite) TestObject(t *testing.T) { 16 | tp.hasApi(t, func(api iface.CoreAPI) error { 17 | if api.Object() == nil { 18 | return errAPINotImplemented 19 | } 20 | return nil 21 | }) 22 | 23 | t.Run("TestNew", tp.TestNew) 24 | t.Run("TestObjectPut", tp.TestObjectPut) 25 | t.Run("TestObjectGet", tp.TestObjectGet) 26 | t.Run("TestObjectData", tp.TestObjectData) 27 | t.Run("TestObjectLinks", tp.TestObjectLinks) 28 | t.Run("TestObjectStat", tp.TestObjectStat) 29 | t.Run("TestObjectAddLink", tp.TestObjectAddLink) 30 | t.Run("TestObjectAddLinkCreate", tp.TestObjectAddLinkCreate) 31 | t.Run("TestObjectRmLink", tp.TestObjectRmLink) 32 | t.Run("TestObjectAddData", tp.TestObjectAddData) 33 | t.Run("TestObjectSetData", tp.TestObjectSetData) 34 | t.Run("TestDiffTest", tp.TestDiffTest) 35 | } 36 | 37 | func (tp *TestSuite) TestNew(t *testing.T) { 38 | ctx, cancel := context.WithCancel(context.Background()) 39 | defer cancel() 40 | api, err := tp.makeAPI(ctx) 41 | if err != nil { 42 | t.Fatal(err) 43 | } 44 | 45 | emptyNode, err := api.Object().New(ctx) 46 | if err != nil { 47 | t.Fatal(err) 48 | } 49 | 50 | dirNode, err := api.Object().New(ctx, opt.Object.Type("unixfs-dir")) 51 | if err != nil { 52 | t.Fatal(err) 53 | } 54 | 55 | if emptyNode.String() != "QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n" { 56 | t.Errorf("Unexpected emptyNode path: %s", emptyNode.String()) 57 | } 58 | 59 | if dirNode.String() != "QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn" { 60 | t.Errorf("Unexpected dirNode path: %s", dirNode.String()) 61 | } 62 | } 63 | 64 | func (tp *TestSuite) TestObjectPut(t *testing.T) { 65 | ctx, cancel := context.WithCancel(context.Background()) 66 | defer cancel() 67 | api, err := tp.makeAPI(ctx) 68 | if err != nil { 69 | t.Fatal(err) 70 | } 71 | 72 | p1, err := api.Object().Put(ctx, strings.NewReader(`{"Data":"foo"}`)) 73 | if err != nil { 74 | t.Fatal(err) 75 | } 76 | 77 | p2, err := api.Object().Put(ctx, strings.NewReader(`{"Data":"YmFy"}`), opt.Object.DataType("base64")) //bar 78 | if err != nil { 79 | t.Fatal(err) 80 | } 81 | 82 | pbBytes, err := hex.DecodeString("0a0362617a") 83 | if err != nil { 84 | t.Fatal(err) 85 | } 86 | 87 | p3, err := api.Object().Put(ctx, bytes.NewReader(pbBytes), opt.Object.InputEnc("protobuf")) 88 | if err != nil { 89 | t.Fatal(err) 90 | } 91 | 92 | if p1.String() != "/ipfs/QmQeGyS87nyijii7kFt1zbe4n2PsXTFimzsdxyE9qh9TST" { 93 | t.Errorf("unexpected path: %s", p1.String()) 94 | } 95 | 96 | if p2.String() != "/ipfs/QmNeYRbCibmaMMK6Du6ChfServcLqFvLJF76PzzF76SPrZ" { 97 | t.Errorf("unexpected path: %s", p2.String()) 98 | } 99 | 100 | if p3.String() != "/ipfs/QmZreR7M2t7bFXAdb1V5FtQhjk4t36GnrvueLJowJbQM9m" { 101 | t.Errorf("unexpected path: %s", p3.String()) 102 | } 103 | } 104 | 105 | func (tp *TestSuite) TestObjectGet(t *testing.T) { 106 | ctx, cancel := context.WithCancel(context.Background()) 107 | defer cancel() 108 | api, err := tp.makeAPI(ctx) 109 | if err != nil { 110 | t.Fatal(err) 111 | } 112 | 113 | p1, err := api.Object().Put(ctx, strings.NewReader(`{"Data":"foo"}`)) 114 | if err != nil { 115 | t.Fatal(err) 116 | } 117 | 118 | nd, err := api.Object().Get(ctx, p1) 119 | if err != nil { 120 | t.Fatal(err) 121 | } 122 | 123 | if string(nd.RawData()[len(nd.RawData())-3:]) != "foo" { 124 | t.Fatal("got non-matching data") 125 | } 126 | } 127 | 128 | func (tp *TestSuite) TestObjectData(t *testing.T) { 129 | ctx, cancel := context.WithCancel(context.Background()) 130 | defer cancel() 131 | api, err := tp.makeAPI(ctx) 132 | if err != nil { 133 | t.Fatal(err) 134 | } 135 | 136 | p1, err := api.Object().Put(ctx, strings.NewReader(`{"Data":"foo"}`)) 137 | if err != nil { 138 | t.Fatal(err) 139 | } 140 | 141 | r, err := api.Object().Data(ctx, p1) 142 | if err != nil { 143 | t.Fatal(err) 144 | } 145 | 146 | data, err := io.ReadAll(r) 147 | if err != nil { 148 | t.Fatal(err) 149 | } 150 | 151 | if string(data) != "foo" { 152 | t.Fatal("got non-matching data") 153 | } 154 | } 155 | 156 | func (tp *TestSuite) TestObjectLinks(t *testing.T) { 157 | ctx, cancel := context.WithCancel(context.Background()) 158 | defer cancel() 159 | api, err := tp.makeAPI(ctx) 160 | if err != nil { 161 | t.Fatal(err) 162 | } 163 | 164 | p1, err := api.Object().Put(ctx, strings.NewReader(`{"Data":"foo"}`)) 165 | if err != nil { 166 | t.Fatal(err) 167 | } 168 | 169 | p2, err := api.Object().Put(ctx, strings.NewReader(`{"Links":[{"Name":"bar", "Hash":"`+p1.Cid().String()+`"}]}`)) 170 | if err != nil { 171 | t.Fatal(err) 172 | } 173 | 174 | links, err := api.Object().Links(ctx, p2) 175 | if err != nil { 176 | t.Fatal(err) 177 | } 178 | 179 | if len(links) != 1 { 180 | t.Errorf("unexpected number of links: %d", len(links)) 181 | } 182 | 183 | if links[0].Cid.String() != p1.Cid().String() { 184 | t.Fatal("cids didn't batch") 185 | } 186 | 187 | if links[0].Name != "bar" { 188 | t.Fatal("unexpected link name") 189 | } 190 | } 191 | 192 | func (tp *TestSuite) TestObjectStat(t *testing.T) { 193 | ctx, cancel := context.WithCancel(context.Background()) 194 | defer cancel() 195 | api, err := tp.makeAPI(ctx) 196 | if err != nil { 197 | t.Fatal(err) 198 | } 199 | 200 | p1, err := api.Object().Put(ctx, strings.NewReader(`{"Data":"foo"}`)) 201 | if err != nil { 202 | t.Fatal(err) 203 | } 204 | 205 | p2, err := api.Object().Put(ctx, strings.NewReader(`{"Data":"bazz", "Links":[{"Name":"bar", "Hash":"`+p1.Cid().String()+`", "Size":3}]}`)) 206 | if err != nil { 207 | t.Fatal(err) 208 | } 209 | 210 | stat, err := api.Object().Stat(ctx, p2) 211 | if err != nil { 212 | t.Fatal(err) 213 | } 214 | 215 | if stat.Cid.String() != p2.Cid().String() { 216 | t.Error("unexpected stat.Cid") 217 | } 218 | 219 | if stat.NumLinks != 1 { 220 | t.Errorf("unexpected stat.NumLinks") 221 | } 222 | 223 | if stat.BlockSize != 51 { 224 | t.Error("unexpected stat.BlockSize") 225 | } 226 | 227 | if stat.LinksSize != 47 { 228 | t.Errorf("unexpected stat.LinksSize: %d", stat.LinksSize) 229 | } 230 | 231 | if stat.DataSize != 4 { 232 | t.Error("unexpected stat.DataSize") 233 | } 234 | 235 | if stat.CumulativeSize != 54 { 236 | t.Error("unexpected stat.DataSize") 237 | } 238 | } 239 | 240 | func (tp *TestSuite) TestObjectAddLink(t *testing.T) { 241 | ctx, cancel := context.WithCancel(context.Background()) 242 | defer cancel() 243 | api, err := tp.makeAPI(ctx) 244 | if err != nil { 245 | t.Fatal(err) 246 | } 247 | 248 | p1, err := api.Object().Put(ctx, strings.NewReader(`{"Data":"foo"}`)) 249 | if err != nil { 250 | t.Fatal(err) 251 | } 252 | 253 | p2, err := api.Object().Put(ctx, strings.NewReader(`{"Data":"bazz", "Links":[{"Name":"bar", "Hash":"`+p1.Cid().String()+`", "Size":3}]}`)) 254 | if err != nil { 255 | t.Fatal(err) 256 | } 257 | 258 | p3, err := api.Object().AddLink(ctx, p2, "abc", p2) 259 | if err != nil { 260 | t.Fatal(err) 261 | } 262 | 263 | links, err := api.Object().Links(ctx, p3) 264 | if err != nil { 265 | t.Fatal(err) 266 | } 267 | 268 | if len(links) != 2 { 269 | t.Errorf("unexpected number of links: %d", len(links)) 270 | } 271 | 272 | if links[0].Name != "abc" { 273 | t.Errorf("unexpected link 0 name: %s", links[0].Name) 274 | } 275 | 276 | if links[1].Name != "bar" { 277 | t.Errorf("unexpected link 1 name: %s", links[1].Name) 278 | } 279 | } 280 | 281 | func (tp *TestSuite) TestObjectAddLinkCreate(t *testing.T) { 282 | ctx, cancel := context.WithCancel(context.Background()) 283 | defer cancel() 284 | api, err := tp.makeAPI(ctx) 285 | if err != nil { 286 | t.Fatal(err) 287 | } 288 | 289 | p1, err := api.Object().Put(ctx, strings.NewReader(`{"Data":"foo"}`)) 290 | if err != nil { 291 | t.Fatal(err) 292 | } 293 | 294 | p2, err := api.Object().Put(ctx, strings.NewReader(`{"Data":"bazz", "Links":[{"Name":"bar", "Hash":"`+p1.Cid().String()+`", "Size":3}]}`)) 295 | if err != nil { 296 | t.Fatal(err) 297 | } 298 | 299 | _, err = api.Object().AddLink(ctx, p2, "abc/d", p2) 300 | if err == nil { 301 | t.Fatal("expected an error") 302 | } 303 | if !strings.Contains(err.Error(), "no link by that name") { 304 | t.Fatalf("unexpected error: %s", err.Error()) 305 | } 306 | 307 | p3, err := api.Object().AddLink(ctx, p2, "abc/d", p2, opt.Object.Create(true)) 308 | if err != nil { 309 | t.Fatal(err) 310 | } 311 | 312 | links, err := api.Object().Links(ctx, p3) 313 | if err != nil { 314 | t.Fatal(err) 315 | } 316 | 317 | if len(links) != 2 { 318 | t.Errorf("unexpected number of links: %d", len(links)) 319 | } 320 | 321 | if links[0].Name != "abc" { 322 | t.Errorf("unexpected link 0 name: %s", links[0].Name) 323 | } 324 | 325 | if links[1].Name != "bar" { 326 | t.Errorf("unexpected link 1 name: %s", links[1].Name) 327 | } 328 | } 329 | 330 | func (tp *TestSuite) TestObjectRmLink(t *testing.T) { 331 | ctx, cancel := context.WithCancel(context.Background()) 332 | defer cancel() 333 | api, err := tp.makeAPI(ctx) 334 | if err != nil { 335 | t.Fatal(err) 336 | } 337 | 338 | p1, err := api.Object().Put(ctx, strings.NewReader(`{"Data":"foo"}`)) 339 | if err != nil { 340 | t.Fatal(err) 341 | } 342 | 343 | p2, err := api.Object().Put(ctx, strings.NewReader(`{"Data":"bazz", "Links":[{"Name":"bar", "Hash":"`+p1.Cid().String()+`", "Size":3}]}`)) 344 | if err != nil { 345 | t.Fatal(err) 346 | } 347 | 348 | p3, err := api.Object().RmLink(ctx, p2, "bar") 349 | if err != nil { 350 | t.Fatal(err) 351 | } 352 | 353 | links, err := api.Object().Links(ctx, p3) 354 | if err != nil { 355 | t.Fatal(err) 356 | } 357 | 358 | if len(links) != 0 { 359 | t.Errorf("unexpected number of links: %d", len(links)) 360 | } 361 | } 362 | 363 | func (tp *TestSuite) TestObjectAddData(t *testing.T) { 364 | ctx, cancel := context.WithCancel(context.Background()) 365 | defer cancel() 366 | api, err := tp.makeAPI(ctx) 367 | if err != nil { 368 | t.Fatal(err) 369 | } 370 | 371 | p1, err := api.Object().Put(ctx, strings.NewReader(`{"Data":"foo"}`)) 372 | if err != nil { 373 | t.Fatal(err) 374 | } 375 | 376 | p2, err := api.Object().AppendData(ctx, p1, strings.NewReader("bar")) 377 | if err != nil { 378 | t.Fatal(err) 379 | } 380 | 381 | r, err := api.Object().Data(ctx, p2) 382 | if err != nil { 383 | t.Fatal(err) 384 | } 385 | 386 | data, err := io.ReadAll(r) 387 | if err != nil { 388 | t.Fatal(err) 389 | } 390 | 391 | if string(data) != "foobar" { 392 | t.Error("unexpected data") 393 | } 394 | } 395 | 396 | func (tp *TestSuite) TestObjectSetData(t *testing.T) { 397 | ctx, cancel := context.WithCancel(context.Background()) 398 | defer cancel() 399 | api, err := tp.makeAPI(ctx) 400 | if err != nil { 401 | t.Fatal(err) 402 | } 403 | 404 | p1, err := api.Object().Put(ctx, strings.NewReader(`{"Data":"foo"}`)) 405 | if err != nil { 406 | t.Fatal(err) 407 | } 408 | 409 | p2, err := api.Object().SetData(ctx, p1, strings.NewReader("bar")) 410 | if err != nil { 411 | t.Fatal(err) 412 | } 413 | 414 | r, err := api.Object().Data(ctx, p2) 415 | if err != nil { 416 | t.Fatal(err) 417 | } 418 | 419 | data, err := io.ReadAll(r) 420 | if err != nil { 421 | t.Fatal(err) 422 | } 423 | 424 | if string(data) != "bar" { 425 | t.Error("unexpected data") 426 | } 427 | } 428 | 429 | func (tp *TestSuite) TestDiffTest(t *testing.T) { 430 | ctx, cancel := context.WithCancel(context.Background()) 431 | defer cancel() 432 | api, err := tp.makeAPI(ctx) 433 | if err != nil { 434 | t.Fatal(err) 435 | } 436 | 437 | p1, err := api.Object().Put(ctx, strings.NewReader(`{"Data":"foo"}`)) 438 | if err != nil { 439 | t.Fatal(err) 440 | } 441 | 442 | p2, err := api.Object().Put(ctx, strings.NewReader(`{"Data":"bar"}`)) 443 | if err != nil { 444 | t.Fatal(err) 445 | } 446 | 447 | changes, err := api.Object().Diff(ctx, p1, p2) 448 | if err != nil { 449 | t.Fatal(err) 450 | } 451 | 452 | if len(changes) != 1 { 453 | t.Fatal("unexpected changes len") 454 | } 455 | 456 | if changes[0].Type != iface.DiffMod { 457 | t.Fatal("unexpected change type") 458 | } 459 | 460 | if changes[0].Before.String() != p1.String() { 461 | t.Fatal("unexpected before path") 462 | } 463 | 464 | if changes[0].After.String() != p2.String() { 465 | t.Fatal("unexpected before path") 466 | } 467 | } 468 | -------------------------------------------------------------------------------- /tests/key.go: -------------------------------------------------------------------------------- 1 | package tests 2 | 3 | import ( 4 | "context" 5 | "strings" 6 | "testing" 7 | 8 | "github.com/ipfs/go-cid" 9 | iface "github.com/ipfs/interface-go-ipfs-core" 10 | opt "github.com/ipfs/interface-go-ipfs-core/options" 11 | mbase "github.com/multiformats/go-multibase" 12 | ) 13 | 14 | func (tp *TestSuite) TestKey(t *testing.T) { 15 | tp.hasApi(t, func(api iface.CoreAPI) error { 16 | if api.Key() == nil { 17 | return errAPINotImplemented 18 | } 19 | return nil 20 | }) 21 | 22 | t.Run("TestListSelf", tp.TestListSelf) 23 | t.Run("TestRenameSelf", tp.TestRenameSelf) 24 | t.Run("TestRemoveSelf", tp.TestRemoveSelf) 25 | t.Run("TestGenerate", tp.TestGenerate) 26 | t.Run("TestGenerateSize", tp.TestGenerateSize) 27 | t.Run("TestGenerateType", tp.TestGenerateType) 28 | t.Run("TestGenerateExisting", tp.TestGenerateExisting) 29 | t.Run("TestList", tp.TestList) 30 | t.Run("TestRename", tp.TestRename) 31 | t.Run("TestRenameToSelf", tp.TestRenameToSelf) 32 | t.Run("TestRenameToSelfForce", tp.TestRenameToSelfForce) 33 | t.Run("TestRenameOverwriteNoForce", tp.TestRenameOverwriteNoForce) 34 | t.Run("TestRenameOverwrite", tp.TestRenameOverwrite) 35 | t.Run("TestRenameSameNameNoForce", tp.TestRenameSameNameNoForce) 36 | t.Run("TestRenameSameName", tp.TestRenameSameName) 37 | t.Run("TestRemove", tp.TestRemove) 38 | } 39 | 40 | func (tp *TestSuite) TestListSelf(t *testing.T) { 41 | ctx, cancel := context.WithCancel(context.Background()) 42 | defer cancel() 43 | api, err := tp.makeAPI(ctx) 44 | if err != nil { 45 | t.Fatal(err) 46 | return 47 | } 48 | 49 | self, err := api.Key().Self(ctx) 50 | if err != nil { 51 | t.Fatal(err) 52 | } 53 | 54 | keys, err := api.Key().List(ctx) 55 | if err != nil { 56 | t.Fatalf("failed to list keys: %s", err) 57 | return 58 | } 59 | 60 | if len(keys) != 1 { 61 | t.Fatalf("there should be 1 key (self), got %d", len(keys)) 62 | return 63 | } 64 | 65 | if keys[0].Name() != "self" { 66 | t.Errorf("expected the key to be called 'self', got '%s'", keys[0].Name()) 67 | } 68 | 69 | if keys[0].Path().String() != "/ipns/"+iface.FormatKeyID(self.ID()) { 70 | t.Errorf("expected the key to have path '/ipns/%s', got '%s'", iface.FormatKeyID(self.ID()), keys[0].Path().String()) 71 | } 72 | } 73 | 74 | func (tp *TestSuite) TestRenameSelf(t *testing.T) { 75 | ctx, cancel := context.WithCancel(context.Background()) 76 | defer cancel() 77 | api, err := tp.makeAPI(ctx) 78 | if err != nil { 79 | t.Fatal(err) 80 | return 81 | } 82 | 83 | _, _, err = api.Key().Rename(ctx, "self", "foo") 84 | if err == nil { 85 | t.Error("expected error to not be nil") 86 | } else { 87 | if !strings.Contains(err.Error(), "cannot rename key with name 'self'") { 88 | t.Fatalf("expected error 'cannot rename key with name 'self'', got '%s'", err.Error()) 89 | } 90 | } 91 | 92 | _, _, err = api.Key().Rename(ctx, "self", "foo", opt.Key.Force(true)) 93 | if err == nil { 94 | t.Error("expected error to not be nil") 95 | } else { 96 | if !strings.Contains(err.Error(), "cannot rename key with name 'self'") { 97 | t.Fatalf("expected error 'cannot rename key with name 'self'', got '%s'", err.Error()) 98 | } 99 | } 100 | } 101 | 102 | func (tp *TestSuite) TestRemoveSelf(t *testing.T) { 103 | ctx, cancel := context.WithCancel(context.Background()) 104 | defer cancel() 105 | api, err := tp.makeAPI(ctx) 106 | if err != nil { 107 | t.Fatal(err) 108 | return 109 | } 110 | 111 | _, err = api.Key().Remove(ctx, "self") 112 | if err == nil { 113 | t.Error("expected error to not be nil") 114 | } else { 115 | if !strings.Contains(err.Error(), "cannot remove key with name 'self'") { 116 | t.Fatalf("expected error 'cannot remove key with name 'self'', got '%s'", err.Error()) 117 | } 118 | } 119 | } 120 | 121 | func (tp *TestSuite) TestGenerate(t *testing.T) { 122 | ctx, cancel := context.WithCancel(context.Background()) 123 | defer cancel() 124 | api, err := tp.makeAPI(ctx) 125 | if err != nil { 126 | t.Fatal(err) 127 | } 128 | 129 | k, err := api.Key().Generate(ctx, "foo") 130 | if err != nil { 131 | t.Fatal(err) 132 | return 133 | } 134 | 135 | if k.Name() != "foo" { 136 | t.Errorf("expected the key to be called 'foo', got '%s'", k.Name()) 137 | } 138 | 139 | verifyIPNSPath(t, k.Path().String()) 140 | } 141 | 142 | func verifyIPNSPath(t *testing.T, p string) bool { 143 | t.Helper() 144 | if !strings.HasPrefix(p, "/ipns/") { 145 | t.Errorf("path %q does not look like an IPNS path", p) 146 | return false 147 | } 148 | k := p[len("/ipns/"):] 149 | c, err := cid.Decode(k) 150 | if err != nil { 151 | t.Errorf("failed to decode IPNS key %q (%v)", k, err) 152 | return false 153 | } 154 | b36, err := c.StringOfBase(mbase.Base36) 155 | if err != nil { 156 | t.Fatalf("cid cannot format itself in b36") 157 | return false 158 | } 159 | if b36 != k { 160 | t.Errorf("IPNS key is not base36") 161 | } 162 | return true 163 | } 164 | 165 | func (tp *TestSuite) TestGenerateSize(t *testing.T) { 166 | ctx, cancel := context.WithCancel(context.Background()) 167 | defer cancel() 168 | api, err := tp.makeAPI(ctx) 169 | if err != nil { 170 | t.Fatal(err) 171 | } 172 | 173 | k, err := api.Key().Generate(ctx, "foo", opt.Key.Size(2048)) 174 | if err != nil { 175 | t.Fatal(err) 176 | return 177 | } 178 | 179 | if k.Name() != "foo" { 180 | t.Errorf("expected the key to be called 'foo', got '%s'", k.Name()) 181 | } 182 | 183 | verifyIPNSPath(t, k.Path().String()) 184 | } 185 | 186 | func (tp *TestSuite) TestGenerateType(t *testing.T) { 187 | t.Skip("disabled until libp2p/specs#111 is fixed") 188 | 189 | ctx, cancel := context.WithCancel(context.Background()) 190 | defer cancel() 191 | 192 | api, err := tp.makeAPI(ctx) 193 | if err != nil { 194 | t.Fatal(err) 195 | } 196 | 197 | k, err := api.Key().Generate(ctx, "bar", opt.Key.Type(opt.Ed25519Key)) 198 | if err != nil { 199 | t.Fatal(err) 200 | return 201 | } 202 | 203 | if k.Name() != "bar" { 204 | t.Errorf("expected the key to be called 'foo', got '%s'", k.Name()) 205 | } 206 | 207 | // Expected to be an inlined identity hash. 208 | if !strings.HasPrefix(k.Path().String(), "/ipns/12") { 209 | t.Errorf("expected the key to be prefixed with '/ipns/12', got '%s'", k.Path().String()) 210 | } 211 | } 212 | 213 | func (tp *TestSuite) TestGenerateExisting(t *testing.T) { 214 | ctx, cancel := context.WithCancel(context.Background()) 215 | defer cancel() 216 | api, err := tp.makeAPI(ctx) 217 | if err != nil { 218 | t.Fatal(err) 219 | } 220 | 221 | _, err = api.Key().Generate(ctx, "foo") 222 | if err != nil { 223 | t.Fatal(err) 224 | return 225 | } 226 | 227 | _, err = api.Key().Generate(ctx, "foo") 228 | if err == nil { 229 | t.Error("expected error to not be nil") 230 | } else { 231 | if !strings.Contains(err.Error(), "key with name 'foo' already exists") { 232 | t.Fatalf("expected error 'key with name 'foo' already exists', got '%s'", err.Error()) 233 | } 234 | } 235 | 236 | _, err = api.Key().Generate(ctx, "self") 237 | if err == nil { 238 | t.Error("expected error to not be nil") 239 | } else { 240 | if !strings.Contains(err.Error(), "cannot create key with name 'self'") { 241 | t.Fatalf("expected error 'cannot create key with name 'self'', got '%s'", err.Error()) 242 | } 243 | } 244 | } 245 | 246 | func (tp *TestSuite) TestList(t *testing.T) { 247 | ctx, cancel := context.WithCancel(context.Background()) 248 | defer cancel() 249 | api, err := tp.makeAPI(ctx) 250 | if err != nil { 251 | t.Fatal(err) 252 | } 253 | 254 | _, err = api.Key().Generate(ctx, "foo") 255 | if err != nil { 256 | t.Fatal(err) 257 | return 258 | } 259 | 260 | l, err := api.Key().List(ctx) 261 | if err != nil { 262 | t.Fatal(err) 263 | return 264 | } 265 | 266 | if len(l) != 2 { 267 | t.Fatalf("expected to get 2 keys, got %d", len(l)) 268 | return 269 | } 270 | 271 | if l[0].Name() != "self" { 272 | t.Fatalf("expected key 0 to be called 'self', got '%s'", l[0].Name()) 273 | return 274 | } 275 | 276 | if l[1].Name() != "foo" { 277 | t.Fatalf("expected key 1 to be called 'foo', got '%s'", l[1].Name()) 278 | return 279 | } 280 | 281 | verifyIPNSPath(t, l[0].Path().String()) 282 | verifyIPNSPath(t, l[1].Path().String()) 283 | } 284 | 285 | func (tp *TestSuite) TestRename(t *testing.T) { 286 | ctx, cancel := context.WithCancel(context.Background()) 287 | defer cancel() 288 | api, err := tp.makeAPI(ctx) 289 | if err != nil { 290 | t.Fatal(err) 291 | } 292 | 293 | _, err = api.Key().Generate(ctx, "foo") 294 | if err != nil { 295 | t.Fatal(err) 296 | return 297 | } 298 | 299 | k, overwrote, err := api.Key().Rename(ctx, "foo", "bar") 300 | if err != nil { 301 | t.Fatal(err) 302 | return 303 | } 304 | 305 | if overwrote { 306 | t.Error("overwrote should be false") 307 | } 308 | 309 | if k.Name() != "bar" { 310 | t.Errorf("returned key should be called 'bar', got '%s'", k.Name()) 311 | } 312 | } 313 | 314 | func (tp *TestSuite) TestRenameToSelf(t *testing.T) { 315 | ctx, cancel := context.WithCancel(context.Background()) 316 | defer cancel() 317 | api, err := tp.makeAPI(ctx) 318 | if err != nil { 319 | t.Fatal(err) 320 | } 321 | 322 | _, err = api.Key().Generate(ctx, "foo") 323 | if err != nil { 324 | t.Fatal(err) 325 | return 326 | } 327 | 328 | _, _, err = api.Key().Rename(ctx, "foo", "self") 329 | if err == nil { 330 | t.Error("expected error to not be nil") 331 | } else { 332 | if !strings.Contains(err.Error(), "cannot overwrite key with name 'self'") { 333 | t.Fatalf("expected error 'cannot overwrite key with name 'self'', got '%s'", err.Error()) 334 | } 335 | } 336 | } 337 | 338 | func (tp *TestSuite) TestRenameToSelfForce(t *testing.T) { 339 | ctx, cancel := context.WithCancel(context.Background()) 340 | defer cancel() 341 | api, err := tp.makeAPI(ctx) 342 | if err != nil { 343 | t.Fatal(err) 344 | } 345 | 346 | _, err = api.Key().Generate(ctx, "foo") 347 | if err != nil { 348 | t.Fatal(err) 349 | return 350 | } 351 | 352 | _, _, err = api.Key().Rename(ctx, "foo", "self", opt.Key.Force(true)) 353 | if err == nil { 354 | t.Error("expected error to not be nil") 355 | } else { 356 | if !strings.Contains(err.Error(), "cannot overwrite key with name 'self'") { 357 | t.Fatalf("expected error 'cannot overwrite key with name 'self'', got '%s'", err.Error()) 358 | } 359 | } 360 | } 361 | 362 | func (tp *TestSuite) TestRenameOverwriteNoForce(t *testing.T) { 363 | ctx, cancel := context.WithCancel(context.Background()) 364 | defer cancel() 365 | api, err := tp.makeAPI(ctx) 366 | if err != nil { 367 | t.Fatal(err) 368 | } 369 | 370 | _, err = api.Key().Generate(ctx, "foo") 371 | if err != nil { 372 | t.Fatal(err) 373 | return 374 | } 375 | 376 | _, err = api.Key().Generate(ctx, "bar") 377 | if err != nil { 378 | t.Fatal(err) 379 | return 380 | } 381 | 382 | _, _, err = api.Key().Rename(ctx, "foo", "bar") 383 | if err == nil { 384 | t.Error("expected error to not be nil") 385 | } else { 386 | if !strings.Contains(err.Error(), "key by that name already exists, refusing to overwrite") { 387 | t.Fatalf("expected error 'key by that name already exists, refusing to overwrite', got '%s'", err.Error()) 388 | } 389 | } 390 | } 391 | 392 | func (tp *TestSuite) TestRenameOverwrite(t *testing.T) { 393 | ctx, cancel := context.WithCancel(context.Background()) 394 | defer cancel() 395 | api, err := tp.makeAPI(ctx) 396 | if err != nil { 397 | t.Fatal(err) 398 | } 399 | 400 | kfoo, err := api.Key().Generate(ctx, "foo") 401 | if err != nil { 402 | t.Fatal(err) 403 | return 404 | } 405 | 406 | _, err = api.Key().Generate(ctx, "bar") 407 | if err != nil { 408 | t.Fatal(err) 409 | return 410 | } 411 | 412 | k, overwrote, err := api.Key().Rename(ctx, "foo", "bar", opt.Key.Force(true)) 413 | if err != nil { 414 | t.Fatal(err) 415 | return 416 | } 417 | 418 | if !overwrote { 419 | t.Error("overwrote should be true") 420 | } 421 | 422 | if k.Name() != "bar" { 423 | t.Errorf("returned key should be called 'bar', got '%s'", k.Name()) 424 | } 425 | 426 | if k.Path().String() != kfoo.Path().String() { 427 | t.Errorf("k and kfoo should have equal paths, '%s'!='%s'", k.Path().String(), kfoo.Path().String()) 428 | } 429 | } 430 | 431 | func (tp *TestSuite) TestRenameSameNameNoForce(t *testing.T) { 432 | ctx, cancel := context.WithCancel(context.Background()) 433 | defer cancel() 434 | api, err := tp.makeAPI(ctx) 435 | if err != nil { 436 | t.Fatal(err) 437 | } 438 | 439 | _, err = api.Key().Generate(ctx, "foo") 440 | if err != nil { 441 | t.Fatal(err) 442 | return 443 | } 444 | 445 | k, overwrote, err := api.Key().Rename(ctx, "foo", "foo") 446 | if err != nil { 447 | t.Fatal(err) 448 | return 449 | } 450 | 451 | if overwrote { 452 | t.Error("overwrote should be false") 453 | } 454 | 455 | if k.Name() != "foo" { 456 | t.Errorf("returned key should be called 'foo', got '%s'", k.Name()) 457 | } 458 | } 459 | 460 | func (tp *TestSuite) TestRenameSameName(t *testing.T) { 461 | ctx, cancel := context.WithCancel(context.Background()) 462 | defer cancel() 463 | api, err := tp.makeAPI(ctx) 464 | if err != nil { 465 | t.Fatal(err) 466 | } 467 | 468 | _, err = api.Key().Generate(ctx, "foo") 469 | if err != nil { 470 | t.Fatal(err) 471 | return 472 | } 473 | 474 | k, overwrote, err := api.Key().Rename(ctx, "foo", "foo", opt.Key.Force(true)) 475 | if err != nil { 476 | t.Fatal(err) 477 | return 478 | } 479 | 480 | if overwrote { 481 | t.Error("overwrote should be false") 482 | } 483 | 484 | if k.Name() != "foo" { 485 | t.Errorf("returned key should be called 'foo', got '%s'", k.Name()) 486 | } 487 | } 488 | 489 | func (tp *TestSuite) TestRemove(t *testing.T) { 490 | ctx, cancel := context.WithCancel(context.Background()) 491 | defer cancel() 492 | api, err := tp.makeAPI(ctx) 493 | if err != nil { 494 | t.Fatal(err) 495 | } 496 | 497 | k, err := api.Key().Generate(ctx, "foo") 498 | if err != nil { 499 | t.Fatal(err) 500 | return 501 | } 502 | 503 | l, err := api.Key().List(ctx) 504 | if err != nil { 505 | t.Fatal(err) 506 | return 507 | } 508 | 509 | if len(l) != 2 { 510 | t.Fatalf("expected to get 2 keys, got %d", len(l)) 511 | return 512 | } 513 | 514 | p, err := api.Key().Remove(ctx, "foo") 515 | if err != nil { 516 | t.Fatal(err) 517 | return 518 | } 519 | 520 | if k.Path().String() != p.Path().String() { 521 | t.Errorf("k and p should have equal paths, '%s'!='%s'", k.Path().String(), p.Path().String()) 522 | } 523 | 524 | l, err = api.Key().List(ctx) 525 | if err != nil { 526 | t.Fatal(err) 527 | return 528 | } 529 | 530 | if len(l) != 1 { 531 | t.Fatalf("expected to get 1 key, got %d", len(l)) 532 | return 533 | } 534 | 535 | if l[0].Name() != "self" { 536 | t.Errorf("expected the key to be called 'self', got '%s'", l[0].Name()) 537 | } 538 | } 539 | -------------------------------------------------------------------------------- /tests/pin.go: -------------------------------------------------------------------------------- 1 | package tests 2 | 3 | import ( 4 | "context" 5 | "math" 6 | "strings" 7 | "testing" 8 | 9 | iface "github.com/ipfs/interface-go-ipfs-core" 10 | opt "github.com/ipfs/interface-go-ipfs-core/options" 11 | "github.com/ipfs/interface-go-ipfs-core/path" 12 | 13 | "github.com/ipfs/go-cid" 14 | ipldcbor "github.com/ipfs/go-ipld-cbor" 15 | ipld "github.com/ipfs/go-ipld-format" 16 | ) 17 | 18 | func (tp *TestSuite) TestPin(t *testing.T) { 19 | tp.hasApi(t, func(api iface.CoreAPI) error { 20 | if api.Pin() == nil { 21 | return errAPINotImplemented 22 | } 23 | return nil 24 | }) 25 | 26 | t.Run("TestPinAdd", tp.TestPinAdd) 27 | t.Run("TestPinSimple", tp.TestPinSimple) 28 | t.Run("TestPinRecursive", tp.TestPinRecursive) 29 | t.Run("TestPinLsIndirect", tp.TestPinLsIndirect) 30 | t.Run("TestPinLsPrecedence", tp.TestPinLsPrecedence) 31 | t.Run("TestPinIsPinned", tp.TestPinIsPinned) 32 | } 33 | 34 | func (tp *TestSuite) TestPinAdd(t *testing.T) { 35 | ctx, cancel := context.WithCancel(context.Background()) 36 | defer cancel() 37 | api, err := tp.makeAPI(ctx) 38 | if err != nil { 39 | t.Fatal(err) 40 | } 41 | 42 | p, err := api.Unixfs().Add(ctx, strFile("foo")()) 43 | if err != nil { 44 | t.Fatal(err) 45 | } 46 | 47 | err = api.Pin().Add(ctx, p) 48 | if err != nil { 49 | t.Fatal(err) 50 | } 51 | } 52 | 53 | func (tp *TestSuite) TestPinSimple(t *testing.T) { 54 | ctx, cancel := context.WithCancel(context.Background()) 55 | defer cancel() 56 | api, err := tp.makeAPI(ctx) 57 | if err != nil { 58 | t.Fatal(err) 59 | } 60 | 61 | p, err := api.Unixfs().Add(ctx, strFile("foo")()) 62 | if err != nil { 63 | t.Fatal(err) 64 | } 65 | 66 | err = api.Pin().Add(ctx, p) 67 | if err != nil { 68 | t.Fatal(err) 69 | } 70 | 71 | list, err := accPins(api.Pin().Ls(ctx)) 72 | if err != nil { 73 | t.Fatal(err) 74 | } 75 | 76 | if len(list) != 1 { 77 | t.Errorf("unexpected pin list len: %d", len(list)) 78 | } 79 | 80 | if list[0].Path().Cid().String() != p.Cid().String() { 81 | t.Error("paths don't match") 82 | } 83 | 84 | if list[0].Type() != "recursive" { 85 | t.Error("unexpected pin type") 86 | } 87 | 88 | assertIsPinned(t, ctx, api, p, "recursive") 89 | 90 | err = api.Pin().Rm(ctx, p) 91 | if err != nil { 92 | t.Fatal(err) 93 | } 94 | 95 | list, err = accPins(api.Pin().Ls(ctx)) 96 | if err != nil { 97 | t.Fatal(err) 98 | } 99 | 100 | if len(list) != 0 { 101 | t.Errorf("unexpected pin list len: %d", len(list)) 102 | } 103 | } 104 | 105 | func (tp *TestSuite) TestPinRecursive(t *testing.T) { 106 | ctx, cancel := context.WithCancel(context.Background()) 107 | defer cancel() 108 | api, err := tp.makeAPI(ctx) 109 | if err != nil { 110 | t.Fatal(err) 111 | } 112 | 113 | p0, err := api.Unixfs().Add(ctx, strFile("foo")()) 114 | if err != nil { 115 | t.Fatal(err) 116 | } 117 | 118 | p1, err := api.Unixfs().Add(ctx, strFile("bar")()) 119 | if err != nil { 120 | t.Fatal(err) 121 | } 122 | 123 | nd2, err := ipldcbor.FromJSON(strings.NewReader(`{"lnk": {"/": "`+p0.Cid().String()+`"}}`), math.MaxUint64, -1) 124 | if err != nil { 125 | t.Fatal(err) 126 | } 127 | 128 | nd3, err := ipldcbor.FromJSON(strings.NewReader(`{"lnk": {"/": "`+p1.Cid().String()+`"}}`), math.MaxUint64, -1) 129 | if err != nil { 130 | t.Fatal(err) 131 | } 132 | 133 | if err := api.Dag().AddMany(ctx, []ipld.Node{nd2, nd3}); err != nil { 134 | t.Fatal(err) 135 | } 136 | 137 | err = api.Pin().Add(ctx, path.IpldPath(nd2.Cid())) 138 | if err != nil { 139 | t.Fatal(err) 140 | } 141 | 142 | err = api.Pin().Add(ctx, path.IpldPath(nd3.Cid()), opt.Pin.Recursive(false)) 143 | if err != nil { 144 | t.Fatal(err) 145 | } 146 | 147 | list, err := accPins(api.Pin().Ls(ctx)) 148 | if err != nil { 149 | t.Fatal(err) 150 | } 151 | 152 | if len(list) != 3 { 153 | t.Errorf("unexpected pin list len: %d", len(list)) 154 | } 155 | 156 | list, err = accPins(api.Pin().Ls(ctx, opt.Pin.Ls.Direct())) 157 | if err != nil { 158 | t.Fatal(err) 159 | } 160 | 161 | if len(list) != 1 { 162 | t.Errorf("unexpected pin list len: %d", len(list)) 163 | } 164 | 165 | if list[0].Path().String() != path.IpldPath(nd3.Cid()).String() { 166 | t.Errorf("unexpected path, %s != %s", list[0].Path().String(), path.IpfsPath(nd3.Cid()).String()) 167 | } 168 | 169 | list, err = accPins(api.Pin().Ls(ctx, opt.Pin.Ls.Recursive())) 170 | if err != nil { 171 | t.Fatal(err) 172 | } 173 | 174 | if len(list) != 1 { 175 | t.Errorf("unexpected pin list len: %d", len(list)) 176 | } 177 | 178 | if list[0].Path().String() != path.IpldPath(nd2.Cid()).String() { 179 | t.Errorf("unexpected path, %s != %s", list[0].Path().String(), path.IpldPath(nd2.Cid()).String()) 180 | } 181 | 182 | list, err = accPins(api.Pin().Ls(ctx, opt.Pin.Ls.Indirect())) 183 | if err != nil { 184 | t.Fatal(err) 185 | } 186 | 187 | if len(list) != 1 { 188 | t.Errorf("unexpected pin list len: %d", len(list)) 189 | } 190 | 191 | if list[0].Path().Cid().String() != p0.Cid().String() { 192 | t.Errorf("unexpected path, %s != %s", list[0].Path().Cid().String(), p0.Cid().String()) 193 | } 194 | 195 | res, err := api.Pin().Verify(ctx) 196 | if err != nil { 197 | t.Fatal(err) 198 | } 199 | n := 0 200 | for r := range res { 201 | if !r.Ok() { 202 | t.Error("expected pin to be ok") 203 | } 204 | n++ 205 | } 206 | 207 | if n != 1 { 208 | t.Errorf("unexpected verify result count: %d", n) 209 | } 210 | 211 | //TODO: figure out a way to test verify without touching IpfsNode 212 | /* 213 | err = api.Block().Rm(ctx, p0, opt.Block.Force(true)) 214 | if err != nil { 215 | t.Fatal(err) 216 | } 217 | 218 | res, err = api.Pin().Verify(ctx) 219 | if err != nil { 220 | t.Fatal(err) 221 | } 222 | n = 0 223 | for r := range res { 224 | if r.Ok() { 225 | t.Error("expected pin to not be ok") 226 | } 227 | 228 | if len(r.BadNodes()) != 1 { 229 | t.Fatalf("unexpected badNodes len") 230 | } 231 | 232 | if r.BadNodes()[0].Path().Cid().String() != p0.Cid().String() { 233 | t.Error("unexpected badNode path") 234 | } 235 | 236 | if r.BadNodes()[0].Err().Error() != "merkledag: not found" { 237 | t.Errorf("unexpected badNode error: %s", r.BadNodes()[0].Err().Error()) 238 | } 239 | n++ 240 | } 241 | 242 | if n != 1 { 243 | t.Errorf("unexpected verify result count: %d", n) 244 | } 245 | */ 246 | } 247 | 248 | // TestPinLsIndirect verifies that indirect nodes are listed by pin ls even if a parent node is directly pinned 249 | func (tp *TestSuite) TestPinLsIndirect(t *testing.T) { 250 | ctx, cancel := context.WithCancel(context.Background()) 251 | defer cancel() 252 | api, err := tp.makeAPI(ctx) 253 | if err != nil { 254 | t.Fatal(err) 255 | } 256 | 257 | leaf, parent, grandparent := getThreeChainedNodes(t, ctx, api, "foo") 258 | 259 | err = api.Pin().Add(ctx, path.IpldPath(grandparent.Cid())) 260 | if err != nil { 261 | t.Fatal(err) 262 | } 263 | 264 | err = api.Pin().Add(ctx, path.IpldPath(parent.Cid()), opt.Pin.Recursive(false)) 265 | if err != nil { 266 | t.Fatal(err) 267 | } 268 | 269 | assertPinTypes(t, ctx, api, []cidContainer{grandparent}, []cidContainer{parent}, []cidContainer{leaf}) 270 | } 271 | 272 | // TestPinLsPrecedence verifies the precedence of pins (recursive > direct > indirect) 273 | func (tp *TestSuite) TestPinLsPrecedence(t *testing.T) { 274 | // Testing precedence of recursive, direct and indirect pins 275 | // Results should be recursive > indirect, direct > indirect, and recursive > direct 276 | 277 | t.Run("TestPinLsPredenceRecursiveIndirect", tp.TestPinLsPredenceRecursiveIndirect) 278 | t.Run("TestPinLsPrecedenceDirectIndirect", tp.TestPinLsPrecedenceDirectIndirect) 279 | t.Run("TestPinLsPrecedenceRecursiveDirect", tp.TestPinLsPrecedenceRecursiveDirect) 280 | } 281 | 282 | func (tp *TestSuite) TestPinLsPredenceRecursiveIndirect(t *testing.T) { 283 | ctx, cancel := context.WithCancel(context.Background()) 284 | defer cancel() 285 | api, err := tp.makeAPI(ctx) 286 | if err != nil { 287 | t.Fatal(err) 288 | } 289 | 290 | // Test recursive > indirect 291 | leaf, parent, grandparent := getThreeChainedNodes(t, ctx, api, "recursive > indirect") 292 | 293 | err = api.Pin().Add(ctx, path.IpldPath(grandparent.Cid())) 294 | if err != nil { 295 | t.Fatal(err) 296 | } 297 | 298 | err = api.Pin().Add(ctx, path.IpldPath(parent.Cid())) 299 | if err != nil { 300 | t.Fatal(err) 301 | } 302 | 303 | assertPinTypes(t, ctx, api, []cidContainer{grandparent, parent}, []cidContainer{}, []cidContainer{leaf}) 304 | } 305 | 306 | func (tp *TestSuite) TestPinLsPrecedenceDirectIndirect(t *testing.T) { 307 | ctx, cancel := context.WithCancel(context.Background()) 308 | defer cancel() 309 | api, err := tp.makeAPI(ctx) 310 | if err != nil { 311 | t.Fatal(err) 312 | } 313 | 314 | // Test direct > indirect 315 | leaf, parent, grandparent := getThreeChainedNodes(t, ctx, api, "direct > indirect") 316 | 317 | err = api.Pin().Add(ctx, path.IpldPath(grandparent.Cid())) 318 | if err != nil { 319 | t.Fatal(err) 320 | } 321 | 322 | err = api.Pin().Add(ctx, path.IpldPath(parent.Cid()), opt.Pin.Recursive(false)) 323 | if err != nil { 324 | t.Fatal(err) 325 | } 326 | 327 | assertPinTypes(t, ctx, api, []cidContainer{grandparent}, []cidContainer{parent}, []cidContainer{leaf}) 328 | } 329 | 330 | func (tp *TestSuite) TestPinLsPrecedenceRecursiveDirect(t *testing.T) { 331 | ctx, cancel := context.WithCancel(context.Background()) 332 | defer cancel() 333 | api, err := tp.makeAPI(ctx) 334 | if err != nil { 335 | t.Fatal(err) 336 | } 337 | 338 | // Test recursive > direct 339 | leaf, parent, grandparent := getThreeChainedNodes(t, ctx, api, "recursive + direct = error") 340 | 341 | err = api.Pin().Add(ctx, path.IpldPath(parent.Cid())) 342 | if err != nil { 343 | t.Fatal(err) 344 | } 345 | 346 | err = api.Pin().Add(ctx, path.IpldPath(parent.Cid()), opt.Pin.Recursive(false)) 347 | if err == nil { 348 | t.Fatal("expected error directly pinning a recursively pinned node") 349 | } 350 | 351 | assertPinTypes(t, ctx, api, []cidContainer{parent}, []cidContainer{}, []cidContainer{leaf}) 352 | 353 | err = api.Pin().Add(ctx, path.IpldPath(grandparent.Cid()), opt.Pin.Recursive(false)) 354 | if err != nil { 355 | t.Fatal(err) 356 | } 357 | 358 | err = api.Pin().Add(ctx, path.IpldPath(grandparent.Cid())) 359 | if err != nil { 360 | t.Fatal(err) 361 | } 362 | 363 | assertPinTypes(t, ctx, api, []cidContainer{grandparent, parent}, []cidContainer{}, []cidContainer{leaf}) 364 | } 365 | 366 | func (tp *TestSuite) TestPinIsPinned(t *testing.T) { 367 | ctx, cancel := context.WithCancel(context.Background()) 368 | defer cancel() 369 | api, err := tp.makeAPI(ctx) 370 | if err != nil { 371 | t.Fatal(err) 372 | } 373 | 374 | leaf, parent, grandparent := getThreeChainedNodes(t, ctx, api, "foofoo") 375 | 376 | assertNotPinned(t, ctx, api, path.IpldPath(grandparent.Cid())) 377 | assertNotPinned(t, ctx, api, path.IpldPath(parent.Cid())) 378 | assertNotPinned(t, ctx, api, path.IpldPath(leaf.Cid())) 379 | 380 | err = api.Pin().Add(ctx, path.IpldPath(parent.Cid()), opt.Pin.Recursive(true)) 381 | if err != nil { 382 | t.Fatal(err) 383 | } 384 | 385 | assertNotPinned(t, ctx, api, path.IpldPath(grandparent.Cid())) 386 | assertIsPinned(t, ctx, api, path.IpldPath(parent.Cid()), "recursive") 387 | assertIsPinned(t, ctx, api, path.IpldPath(leaf.Cid()), "indirect") 388 | 389 | err = api.Pin().Add(ctx, path.IpldPath(grandparent.Cid()), opt.Pin.Recursive(false)) 390 | if err != nil { 391 | t.Fatal(err) 392 | } 393 | 394 | assertIsPinned(t, ctx, api, path.IpldPath(grandparent.Cid()), "direct") 395 | assertIsPinned(t, ctx, api, path.IpldPath(parent.Cid()), "recursive") 396 | assertIsPinned(t, ctx, api, path.IpldPath(leaf.Cid()), "indirect") 397 | } 398 | 399 | type cidContainer interface { 400 | Cid() cid.Cid 401 | } 402 | 403 | func getThreeChainedNodes(t *testing.T, ctx context.Context, api iface.CoreAPI, leafData string) (cidContainer, cidContainer, cidContainer) { 404 | leaf, err := api.Unixfs().Add(ctx, strFile(leafData)()) 405 | if err != nil { 406 | t.Fatal(err) 407 | } 408 | 409 | parent, err := ipldcbor.FromJSON(strings.NewReader(`{"lnk": {"/": "`+leaf.Cid().String()+`"}}`), math.MaxUint64, -1) 410 | if err != nil { 411 | t.Fatal(err) 412 | } 413 | 414 | grandparent, err := ipldcbor.FromJSON(strings.NewReader(`{"lnk": {"/": "`+parent.Cid().String()+`"}}`), math.MaxUint64, -1) 415 | if err != nil { 416 | t.Fatal(err) 417 | } 418 | 419 | if err := api.Dag().AddMany(ctx, []ipld.Node{parent, grandparent}); err != nil { 420 | t.Fatal(err) 421 | } 422 | 423 | return leaf, parent, grandparent 424 | } 425 | 426 | func assertPinTypes(t *testing.T, ctx context.Context, api iface.CoreAPI, recusive, direct, indirect []cidContainer) { 427 | assertPinLsAllConsistency(t, ctx, api) 428 | 429 | list, err := accPins(api.Pin().Ls(ctx, opt.Pin.Ls.Recursive())) 430 | if err != nil { 431 | t.Fatal(err) 432 | } 433 | 434 | assertPinCids(t, list, recusive...) 435 | 436 | list, err = accPins(api.Pin().Ls(ctx, opt.Pin.Ls.Direct())) 437 | if err != nil { 438 | t.Fatal(err) 439 | } 440 | 441 | assertPinCids(t, list, direct...) 442 | 443 | list, err = accPins(api.Pin().Ls(ctx, opt.Pin.Ls.Indirect())) 444 | if err != nil { 445 | t.Fatal(err) 446 | } 447 | 448 | assertPinCids(t, list, indirect...) 449 | } 450 | 451 | // assertPinCids verifies that the pins match the expected cids 452 | func assertPinCids(t *testing.T, pins []iface.Pin, cids ...cidContainer) { 453 | t.Helper() 454 | 455 | if expected, actual := len(cids), len(pins); expected != actual { 456 | t.Fatalf("expected pin list to have len %d, was %d", expected, actual) 457 | } 458 | 459 | cSet := cid.NewSet() 460 | for _, c := range cids { 461 | cSet.Add(c.Cid()) 462 | } 463 | 464 | valid := true 465 | for _, p := range pins { 466 | c := p.Path().Cid() 467 | if cSet.Has(c) { 468 | cSet.Remove(c) 469 | } else { 470 | valid = false 471 | break 472 | } 473 | } 474 | 475 | valid = valid && cSet.Len() == 0 476 | 477 | if !valid { 478 | pinStrs := make([]string, len(pins)) 479 | for i, p := range pins { 480 | pinStrs[i] = p.Path().Cid().String() 481 | } 482 | pathStrs := make([]string, len(cids)) 483 | for i, c := range cids { 484 | pathStrs[i] = c.Cid().String() 485 | } 486 | t.Fatalf("expected: %s \nactual: %s", strings.Join(pathStrs, ", "), strings.Join(pinStrs, ", ")) 487 | } 488 | } 489 | 490 | // assertPinLsAllConsistency verifies that listing all pins gives the same result as listing the pin types individually 491 | func assertPinLsAllConsistency(t *testing.T, ctx context.Context, api iface.CoreAPI) { 492 | t.Helper() 493 | allPins, err := accPins(api.Pin().Ls(ctx)) 494 | if err != nil { 495 | t.Fatal(err) 496 | } 497 | 498 | type pinTypeProps struct { 499 | *cid.Set 500 | opt.PinLsOption 501 | } 502 | 503 | all, recursive, direct, indirect := cid.NewSet(), cid.NewSet(), cid.NewSet(), cid.NewSet() 504 | typeMap := map[string]*pinTypeProps{ 505 | "recursive": {recursive, opt.Pin.Ls.Recursive()}, 506 | "direct": {direct, opt.Pin.Ls.Direct()}, 507 | "indirect": {indirect, opt.Pin.Ls.Indirect()}, 508 | } 509 | 510 | for _, p := range allPins { 511 | if !all.Visit(p.Path().Cid()) { 512 | t.Fatalf("pin ls returned the same cid multiple times") 513 | } 514 | 515 | typeStr := p.Type() 516 | if typeSet, ok := typeMap[p.Type()]; ok { 517 | typeSet.Add(p.Path().Cid()) 518 | } else { 519 | t.Fatalf("unknown pin type: %s", typeStr) 520 | } 521 | } 522 | 523 | for typeStr, pinProps := range typeMap { 524 | pins, err := accPins(api.Pin().Ls(ctx, pinProps.PinLsOption)) 525 | if err != nil { 526 | t.Fatal(err) 527 | } 528 | 529 | if expected, actual := len(pins), pinProps.Set.Len(); expected != actual { 530 | t.Fatalf("pin ls all has %d pins of type %s, but pin ls for the type has %d", expected, typeStr, actual) 531 | } 532 | 533 | for _, p := range pins { 534 | if pinType := p.Type(); pinType != typeStr { 535 | t.Fatalf("returned wrong pin type: expected %s, got %s", typeStr, pinType) 536 | } 537 | 538 | if c := p.Path().Cid(); !pinProps.Has(c) { 539 | t.Fatalf("%s expected to be in pin ls all as type %s", c.String(), typeStr) 540 | } 541 | } 542 | } 543 | } 544 | 545 | func assertIsPinned(t *testing.T, ctx context.Context, api iface.CoreAPI, p path.Path, typeStr string) { 546 | t.Helper() 547 | withType, err := opt.Pin.IsPinned.Type(typeStr) 548 | if err != nil { 549 | t.Fatal("unhandled pin type") 550 | } 551 | 552 | whyPinned, pinned, err := api.Pin().IsPinned(ctx, p, withType) 553 | if err != nil { 554 | t.Fatal(err) 555 | } 556 | 557 | if !pinned { 558 | t.Fatalf("%s expected to be pinned with type %s", p, typeStr) 559 | } 560 | 561 | switch typeStr { 562 | case "recursive", "direct": 563 | if typeStr != whyPinned { 564 | t.Fatalf("reason for pinning expected to be %s for %s, got %s", typeStr, p, whyPinned) 565 | } 566 | case "indirect": 567 | if whyPinned == "" { 568 | t.Fatalf("expected to have a pin reason for %s", p) 569 | } 570 | } 571 | } 572 | 573 | func assertNotPinned(t *testing.T, ctx context.Context, api iface.CoreAPI, p path.Path) { 574 | t.Helper() 575 | 576 | _, pinned, err := api.Pin().IsPinned(ctx, p) 577 | if err != nil { 578 | t.Fatal(err) 579 | } 580 | 581 | if pinned { 582 | t.Fatalf("%s expected to not be pinned", p) 583 | } 584 | } 585 | 586 | func accPins(pins <-chan iface.Pin, err error) ([]iface.Pin, error) { 587 | if err != nil { 588 | return nil, err 589 | } 590 | 591 | var result []iface.Pin 592 | 593 | for pin := range pins { 594 | if pin.Err() != nil { 595 | return nil, pin.Err() 596 | } 597 | result = append(result, pin) 598 | } 599 | 600 | return result, nil 601 | } 602 | -------------------------------------------------------------------------------- /tests/unixfs.go: -------------------------------------------------------------------------------- 1 | package tests 2 | 3 | import ( 4 | "bytes" 5 | "context" 6 | "encoding/hex" 7 | "fmt" 8 | "io" 9 | "math" 10 | "math/rand" 11 | "os" 12 | "strconv" 13 | "strings" 14 | "sync" 15 | "testing" 16 | 17 | "github.com/ipfs/interface-go-ipfs-core/path" 18 | 19 | coreiface "github.com/ipfs/interface-go-ipfs-core" 20 | "github.com/ipfs/interface-go-ipfs-core/options" 21 | 22 | "github.com/ipfs/go-cid" 23 | cbor "github.com/ipfs/go-ipld-cbor" 24 | ipld "github.com/ipfs/go-ipld-format" 25 | "github.com/ipfs/go-libipfs/files" 26 | mdag "github.com/ipfs/go-merkledag" 27 | "github.com/ipfs/go-unixfs" 28 | "github.com/ipfs/go-unixfs/importer/helpers" 29 | mh "github.com/multiformats/go-multihash" 30 | ) 31 | 32 | func (tp *TestSuite) TestUnixfs(t *testing.T) { 33 | tp.hasApi(t, func(api coreiface.CoreAPI) error { 34 | if api.Unixfs() == nil { 35 | return errAPINotImplemented 36 | } 37 | return nil 38 | }) 39 | 40 | t.Run("TestAdd", tp.TestAdd) 41 | t.Run("TestAddPinned", tp.TestAddPinned) 42 | t.Run("TestAddHashOnly", tp.TestAddHashOnly) 43 | t.Run("TestGetEmptyFile", tp.TestGetEmptyFile) 44 | t.Run("TestGetDir", tp.TestGetDir) 45 | t.Run("TestGetNonUnixfs", tp.TestGetNonUnixfs) 46 | t.Run("TestLs", tp.TestLs) 47 | t.Run("TestEntriesExpired", tp.TestEntriesExpired) 48 | t.Run("TestLsEmptyDir", tp.TestLsEmptyDir) 49 | t.Run("TestLsNonUnixfs", tp.TestLsNonUnixfs) 50 | t.Run("TestAddCloses", tp.TestAddCloses) 51 | t.Run("TestGetSeek", tp.TestGetSeek) 52 | t.Run("TestGetReadAt", tp.TestGetReadAt) 53 | } 54 | 55 | // `echo -n 'hello, world!' | ipfs add` 56 | var hello = "/ipfs/QmQy2Dw4Wk7rdJKjThjYXzfFJNaRKRHhHP5gHHXroJMYxk" 57 | var helloStr = "hello, world!" 58 | 59 | // `echo -n | ipfs add` 60 | var emptyFile = "/ipfs/QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH" 61 | 62 | func strFile(data string) func() files.Node { 63 | return func() files.Node { 64 | return files.NewBytesFile([]byte(data)) 65 | } 66 | } 67 | 68 | func twoLevelDir() func() files.Node { 69 | return func() files.Node { 70 | return files.NewMapDirectory(map[string]files.Node{ 71 | "abc": files.NewMapDirectory(map[string]files.Node{ 72 | "def": files.NewBytesFile([]byte("world")), 73 | }), 74 | 75 | "bar": files.NewBytesFile([]byte("hello2")), 76 | "foo": files.NewBytesFile([]byte("hello1")), 77 | }) 78 | } 79 | } 80 | 81 | func flatDir() files.Node { 82 | return files.NewMapDirectory(map[string]files.Node{ 83 | "bar": files.NewBytesFile([]byte("hello2")), 84 | "foo": files.NewBytesFile([]byte("hello1")), 85 | }) 86 | } 87 | 88 | func wrapped(names ...string) func(f files.Node) files.Node { 89 | return func(f files.Node) files.Node { 90 | for i := range names { 91 | f = files.NewMapDirectory(map[string]files.Node{ 92 | names[len(names)-i-1]: f, 93 | }) 94 | } 95 | return f 96 | } 97 | } 98 | 99 | func (tp *TestSuite) TestAdd(t *testing.T) { 100 | ctx, cancel := context.WithCancel(context.Background()) 101 | defer cancel() 102 | api, err := tp.makeAPI(ctx) 103 | if err != nil { 104 | t.Fatal(err) 105 | } 106 | 107 | p := func(h string) path.Resolved { 108 | c, err := cid.Parse(h) 109 | if err != nil { 110 | t.Fatal(err) 111 | } 112 | return path.IpfsPath(c) 113 | } 114 | 115 | rf, err := os.CreateTemp(os.TempDir(), "unixfs-add-real") 116 | if err != nil { 117 | t.Fatal(err) 118 | } 119 | rfp := rf.Name() 120 | 121 | if _, err := rf.Write([]byte(helloStr)); err != nil { 122 | t.Fatal(err) 123 | } 124 | 125 | stat, err := rf.Stat() 126 | if err != nil { 127 | t.Fatal(err) 128 | } 129 | 130 | if err := rf.Close(); err != nil { 131 | t.Fatal(err) 132 | } 133 | defer os.Remove(rfp) 134 | 135 | realFile := func() files.Node { 136 | n, err := files.NewReaderPathFile(rfp, io.NopCloser(strings.NewReader(helloStr)), stat) 137 | if err != nil { 138 | t.Fatal(err) 139 | } 140 | return n 141 | } 142 | 143 | cases := []struct { 144 | name string 145 | data func() files.Node 146 | expect func(files.Node) files.Node 147 | 148 | apiOpts []options.ApiOption 149 | 150 | path string 151 | err string 152 | 153 | wrap string 154 | 155 | events []coreiface.AddEvent 156 | 157 | opts []options.UnixfsAddOption 158 | }{ 159 | // Simple cases 160 | { 161 | name: "simpleAdd", 162 | data: strFile(helloStr), 163 | path: hello, 164 | opts: []options.UnixfsAddOption{}, 165 | }, 166 | { 167 | name: "addEmpty", 168 | data: strFile(""), 169 | path: emptyFile, 170 | }, 171 | // CIDv1 version / rawLeaves 172 | { 173 | name: "addCidV1", 174 | data: strFile(helloStr), 175 | path: "/ipfs/bafkreidi4zlleupgp2bvrpxyja5lbvi4mym7hz5bvhyoowby2qp7g2hxfa", 176 | opts: []options.UnixfsAddOption{options.Unixfs.CidVersion(1)}, 177 | }, 178 | { 179 | name: "addCidV1NoLeaves", 180 | data: strFile(helloStr), 181 | path: "/ipfs/bafybeibhbcn7k7o2m6xsqkrlfiokod3nxwe47viteynhruh6uqx7hvkjfu", 182 | opts: []options.UnixfsAddOption{options.Unixfs.CidVersion(1), options.Unixfs.RawLeaves(false)}, 183 | }, 184 | // Non sha256 hash vs CID 185 | { 186 | name: "addCidSha3", 187 | data: strFile(helloStr), 188 | path: "/ipfs/bafkrmichjflejeh6aren53o7pig7zk3m3vxqcoc2i5dv326k3x6obh7jry", 189 | opts: []options.UnixfsAddOption{options.Unixfs.Hash(mh.SHA3_256)}, 190 | }, 191 | { 192 | name: "addCidSha3Cid0", 193 | data: strFile(helloStr), 194 | err: "CIDv0 only supports sha2-256", 195 | opts: []options.UnixfsAddOption{options.Unixfs.CidVersion(0), options.Unixfs.Hash(mh.SHA3_256)}, 196 | }, 197 | // Inline 198 | { 199 | name: "addInline", 200 | data: strFile(helloStr), 201 | path: "/ipfs/bafyaafikcmeaeeqnnbswy3dpfqqho33snrsccgan", 202 | opts: []options.UnixfsAddOption{options.Unixfs.Inline(true)}, 203 | }, 204 | { 205 | name: "addInlineLimit", 206 | data: strFile(helloStr), 207 | path: "/ipfs/bafyaafikcmeaeeqnnbswy3dpfqqho33snrsccgan", 208 | opts: []options.UnixfsAddOption{options.Unixfs.InlineLimit(32), options.Unixfs.Inline(true)}, 209 | }, 210 | { 211 | name: "addInlineZero", 212 | data: strFile(""), 213 | path: "/ipfs/bafkqaaa", 214 | opts: []options.UnixfsAddOption{options.Unixfs.InlineLimit(0), options.Unixfs.Inline(true), options.Unixfs.RawLeaves(true)}, 215 | }, 216 | { //TODO: after coreapi add is used in `ipfs add`, consider making this default for inline 217 | name: "addInlineRaw", 218 | data: strFile(helloStr), 219 | path: "/ipfs/bafkqadlimvwgy3zmeb3w64tmmqqq", 220 | opts: []options.UnixfsAddOption{options.Unixfs.InlineLimit(32), options.Unixfs.Inline(true), options.Unixfs.RawLeaves(true)}, 221 | }, 222 | // Chunker / Layout 223 | { 224 | name: "addChunks", 225 | data: strFile(strings.Repeat("aoeuidhtns", 200)), 226 | path: "/ipfs/QmRo11d4QJrST47aaiGVJYwPhoNA4ihRpJ5WaxBWjWDwbX", 227 | opts: []options.UnixfsAddOption{options.Unixfs.Chunker("size-4")}, 228 | }, 229 | { 230 | name: "addChunksTrickle", 231 | data: strFile(strings.Repeat("aoeuidhtns", 200)), 232 | path: "/ipfs/QmNNhDGttafX3M1wKWixGre6PrLFGjnoPEDXjBYpTv93HP", 233 | opts: []options.UnixfsAddOption{options.Unixfs.Chunker("size-4"), options.Unixfs.Layout(options.TrickleLayout)}, 234 | }, 235 | // Local 236 | { 237 | name: "addLocal", // better cases in sharness 238 | data: strFile(helloStr), 239 | path: hello, 240 | apiOpts: []options.ApiOption{options.Api.Offline(true)}, 241 | }, 242 | { 243 | name: "hashOnly", // test (non)fetchability 244 | data: strFile(helloStr), 245 | path: hello, 246 | opts: []options.UnixfsAddOption{options.Unixfs.HashOnly(true)}, 247 | }, 248 | // multi file 249 | { 250 | name: "simpleDirNoWrap", 251 | data: flatDir, 252 | path: "/ipfs/QmRKGpFfR32FVXdvJiHfo4WJ5TDYBsM1P9raAp1p6APWSp", 253 | }, 254 | { 255 | name: "simpleDir", 256 | data: flatDir, 257 | wrap: "t", 258 | expect: wrapped("t"), 259 | path: "/ipfs/Qmc3nGXm1HtUVCmnXLQHvWcNwfdZGpfg2SRm1CxLf7Q2Rm", 260 | }, 261 | { 262 | name: "twoLevelDir", 263 | data: twoLevelDir(), 264 | wrap: "t", 265 | expect: wrapped("t"), 266 | path: "/ipfs/QmPwsL3T5sWhDmmAWZHAzyjKtMVDS9a11aHNRqb3xoVnmg", 267 | }, 268 | // wrapped 269 | { 270 | name: "addWrapped", 271 | path: "/ipfs/QmVE9rNpj5doj7XHzp5zMUxD7BJgXEqx4pe3xZ3JBReWHE", 272 | data: func() files.Node { 273 | return files.NewBytesFile([]byte(helloStr)) 274 | }, 275 | wrap: "foo", 276 | expect: wrapped("foo"), 277 | }, 278 | // hidden 279 | { 280 | name: "hiddenFilesAdded", 281 | data: func() files.Node { 282 | return files.NewMapDirectory(map[string]files.Node{ 283 | ".bar": files.NewBytesFile([]byte("hello2")), 284 | "bar": files.NewBytesFile([]byte("hello2")), 285 | "foo": files.NewBytesFile([]byte("hello1")), 286 | }) 287 | }, 288 | wrap: "t", 289 | expect: wrapped("t"), 290 | path: "/ipfs/QmPXLSBX382vJDLrGakcbrZDkU3grfkjMox7EgSC9KFbtQ", 291 | }, 292 | // NoCopy 293 | { 294 | name: "simpleNoCopy", 295 | data: realFile, 296 | path: "/ipfs/bafkreidi4zlleupgp2bvrpxyja5lbvi4mym7hz5bvhyoowby2qp7g2hxfa", 297 | opts: []options.UnixfsAddOption{options.Unixfs.Nocopy(true)}, 298 | }, 299 | { 300 | name: "noCopyNoRaw", 301 | data: realFile, 302 | path: "/ipfs/bafkreidi4zlleupgp2bvrpxyja5lbvi4mym7hz5bvhyoowby2qp7g2hxfa", 303 | opts: []options.UnixfsAddOption{options.Unixfs.Nocopy(true), options.Unixfs.RawLeaves(false)}, 304 | err: "nocopy option requires '--raw-leaves' to be enabled as well", 305 | }, 306 | { 307 | name: "noCopyNoPath", 308 | data: strFile(helloStr), 309 | path: "/ipfs/bafkreidi4zlleupgp2bvrpxyja5lbvi4mym7hz5bvhyoowby2qp7g2hxfa", 310 | opts: []options.UnixfsAddOption{options.Unixfs.Nocopy(true)}, 311 | err: helpers.ErrMissingFsRef.Error(), 312 | }, 313 | // Events / Progress 314 | { 315 | name: "simpleAddEvent", 316 | data: strFile(helloStr), 317 | path: "/ipfs/bafkreidi4zlleupgp2bvrpxyja5lbvi4mym7hz5bvhyoowby2qp7g2hxfa", 318 | events: []coreiface.AddEvent{ 319 | {Name: "bafkreidi4zlleupgp2bvrpxyja5lbvi4mym7hz5bvhyoowby2qp7g2hxfa", Path: p("bafkreidi4zlleupgp2bvrpxyja5lbvi4mym7hz5bvhyoowby2qp7g2hxfa"), Size: strconv.Itoa(len(helloStr))}, 320 | }, 321 | opts: []options.UnixfsAddOption{options.Unixfs.RawLeaves(true)}, 322 | }, 323 | { 324 | name: "silentAddEvent", 325 | data: twoLevelDir(), 326 | path: "/ipfs/QmVG2ZYCkV1S4TK8URA3a4RupBF17A8yAr4FqsRDXVJASr", 327 | events: []coreiface.AddEvent{ 328 | {Name: "abc", Path: p("QmU7nuGs2djqK99UNsNgEPGh6GV4662p6WtsgccBNGTDxt"), Size: "62"}, 329 | {Name: "", Path: p("QmVG2ZYCkV1S4TK8URA3a4RupBF17A8yAr4FqsRDXVJASr"), Size: "229"}, 330 | }, 331 | opts: []options.UnixfsAddOption{options.Unixfs.Silent(true)}, 332 | }, 333 | { 334 | name: "dirAddEvents", 335 | data: twoLevelDir(), 336 | path: "/ipfs/QmVG2ZYCkV1S4TK8URA3a4RupBF17A8yAr4FqsRDXVJASr", 337 | events: []coreiface.AddEvent{ 338 | {Name: "abc/def", Path: p("QmNyJpQkU1cEkBwMDhDNFstr42q55mqG5GE5Mgwug4xyGk"), Size: "13"}, 339 | {Name: "bar", Path: p("QmS21GuXiRMvJKHos4ZkEmQDmRBqRaF5tQS2CQCu2ne9sY"), Size: "14"}, 340 | {Name: "foo", Path: p("QmfAjGiVpTN56TXi6SBQtstit5BEw3sijKj1Qkxn6EXKzJ"), Size: "14"}, 341 | {Name: "abc", Path: p("QmU7nuGs2djqK99UNsNgEPGh6GV4662p6WtsgccBNGTDxt"), Size: "62"}, 342 | {Name: "", Path: p("QmVG2ZYCkV1S4TK8URA3a4RupBF17A8yAr4FqsRDXVJASr"), Size: "229"}, 343 | }, 344 | }, 345 | { 346 | name: "progress1M", 347 | data: func() files.Node { 348 | return files.NewReaderFile(bytes.NewReader(bytes.Repeat([]byte{0}, 1000000))) 349 | }, 350 | path: "/ipfs/QmXXNNbwe4zzpdMg62ZXvnX1oU7MwSrQ3vAEtuwFKCm1oD", 351 | events: []coreiface.AddEvent{ 352 | {Name: "", Bytes: 262144}, 353 | {Name: "", Bytes: 524288}, 354 | {Name: "", Bytes: 786432}, 355 | {Name: "", Bytes: 1000000}, 356 | {Name: "QmXXNNbwe4zzpdMg62ZXvnX1oU7MwSrQ3vAEtuwFKCm1oD", Path: p("QmXXNNbwe4zzpdMg62ZXvnX1oU7MwSrQ3vAEtuwFKCm1oD"), Size: "1000256"}, 357 | }, 358 | wrap: "", 359 | opts: []options.UnixfsAddOption{options.Unixfs.Progress(true)}, 360 | }, 361 | } 362 | 363 | for _, testCase := range cases { 364 | t.Run(testCase.name, func(t *testing.T) { 365 | ctx, cancel := context.WithCancel(ctx) 366 | defer cancel() 367 | 368 | // recursive logic 369 | 370 | data := testCase.data() 371 | if testCase.wrap != "" { 372 | data = files.NewMapDirectory(map[string]files.Node{ 373 | testCase.wrap: data, 374 | }) 375 | } 376 | 377 | // handle events if relevant to test case 378 | 379 | opts := testCase.opts 380 | eventOut := make(chan interface{}) 381 | var evtWg sync.WaitGroup 382 | if len(testCase.events) > 0 { 383 | opts = append(opts, options.Unixfs.Events(eventOut)) 384 | evtWg.Add(1) 385 | 386 | go func() { 387 | defer evtWg.Done() 388 | expected := testCase.events 389 | 390 | for evt := range eventOut { 391 | event, ok := evt.(*coreiface.AddEvent) 392 | if !ok { 393 | t.Error("unexpected event type") 394 | continue 395 | } 396 | 397 | if len(expected) < 1 { 398 | t.Error("got more events than expected") 399 | continue 400 | } 401 | 402 | if expected[0].Size != event.Size { 403 | t.Errorf("Event.Size didn't match, %s != %s", expected[0].Size, event.Size) 404 | } 405 | 406 | if expected[0].Name != event.Name { 407 | t.Errorf("Event.Name didn't match, %s != %s", expected[0].Name, event.Name) 408 | } 409 | 410 | if expected[0].Path != nil && event.Path != nil { 411 | if expected[0].Path.Cid().String() != event.Path.Cid().String() { 412 | t.Errorf("Event.Hash didn't match, %s != %s", expected[0].Path, event.Path) 413 | } 414 | } else if event.Path != expected[0].Path { 415 | t.Errorf("Event.Hash didn't match, %s != %s", expected[0].Path, event.Path) 416 | } 417 | if expected[0].Bytes != event.Bytes { 418 | t.Errorf("Event.Bytes didn't match, %d != %d", expected[0].Bytes, event.Bytes) 419 | } 420 | 421 | expected = expected[1:] 422 | } 423 | 424 | if len(expected) > 0 { 425 | t.Errorf("%d event(s) didn't arrive", len(expected)) 426 | } 427 | }() 428 | } 429 | 430 | tapi, err := api.WithOptions(testCase.apiOpts...) 431 | if err != nil { 432 | t.Fatal(err) 433 | } 434 | 435 | // Add! 436 | 437 | p, err := tapi.Unixfs().Add(ctx, data, opts...) 438 | close(eventOut) 439 | evtWg.Wait() 440 | if testCase.err != "" { 441 | if err == nil { 442 | t.Fatalf("expected an error: %s", testCase.err) 443 | } 444 | if err.Error() != testCase.err { 445 | t.Fatalf("expected an error: '%s' != '%s'", err.Error(), testCase.err) 446 | } 447 | return 448 | } 449 | if err != nil { 450 | t.Fatal(err) 451 | } 452 | 453 | if p.String() != testCase.path { 454 | t.Errorf("expected path %s, got: %s", testCase.path, p) 455 | } 456 | 457 | // compare file structure with Unixfs().Get 458 | 459 | var cmpFile func(origName string, orig files.Node, gotName string, got files.Node) 460 | cmpFile = func(origName string, orig files.Node, gotName string, got files.Node) { 461 | _, origDir := orig.(files.Directory) 462 | _, gotDir := got.(files.Directory) 463 | 464 | if origName != gotName { 465 | t.Errorf("file name mismatch, orig='%s', got='%s'", origName, gotName) 466 | } 467 | 468 | if origDir != gotDir { 469 | t.Fatalf("file type mismatch on %s", origName) 470 | } 471 | 472 | if !gotDir { 473 | defer orig.Close() 474 | defer got.Close() 475 | 476 | do, err := io.ReadAll(orig.(files.File)) 477 | if err != nil { 478 | t.Fatal(err) 479 | } 480 | 481 | dg, err := io.ReadAll(got.(files.File)) 482 | if err != nil { 483 | t.Fatal(err) 484 | } 485 | 486 | if !bytes.Equal(do, dg) { 487 | t.Fatal("data not equal") 488 | } 489 | 490 | return 491 | } 492 | 493 | origIt := orig.(files.Directory).Entries() 494 | gotIt := got.(files.Directory).Entries() 495 | 496 | for { 497 | if origIt.Next() { 498 | if !gotIt.Next() { 499 | t.Fatal("gotIt out of entries before origIt") 500 | } 501 | } else { 502 | if gotIt.Next() { 503 | t.Fatal("origIt out of entries before gotIt") 504 | } 505 | break 506 | } 507 | 508 | cmpFile(origIt.Name(), origIt.Node(), gotIt.Name(), gotIt.Node()) 509 | } 510 | if origIt.Err() != nil { 511 | t.Fatal(origIt.Err()) 512 | } 513 | if gotIt.Err() != nil { 514 | t.Fatal(gotIt.Err()) 515 | } 516 | } 517 | 518 | f, err := tapi.Unixfs().Get(ctx, p) 519 | if err != nil { 520 | t.Fatal(err) 521 | } 522 | 523 | orig := testCase.data() 524 | if testCase.expect != nil { 525 | orig = testCase.expect(orig) 526 | } 527 | 528 | cmpFile("", orig, "", f) 529 | }) 530 | } 531 | } 532 | 533 | func (tp *TestSuite) TestAddPinned(t *testing.T) { 534 | ctx, cancel := context.WithCancel(context.Background()) 535 | defer cancel() 536 | api, err := tp.makeAPI(ctx) 537 | if err != nil { 538 | t.Fatal(err) 539 | } 540 | 541 | _, err = api.Unixfs().Add(ctx, strFile(helloStr)(), options.Unixfs.Pin(true)) 542 | if err != nil { 543 | t.Fatal(err) 544 | } 545 | 546 | pins, err := accPins(api.Pin().Ls(ctx)) 547 | if err != nil { 548 | t.Fatal(err) 549 | } 550 | if len(pins) != 1 { 551 | t.Fatalf("expected 1 pin, got %d", len(pins)) 552 | } 553 | 554 | if pins[0].Path().String() != "/ipld/QmQy2Dw4Wk7rdJKjThjYXzfFJNaRKRHhHP5gHHXroJMYxk" { 555 | t.Fatalf("got unexpected pin: %s", pins[0].Path().String()) 556 | } 557 | } 558 | 559 | func (tp *TestSuite) TestAddHashOnly(t *testing.T) { 560 | ctx, cancel := context.WithCancel(context.Background()) 561 | defer cancel() 562 | api, err := tp.makeAPI(ctx) 563 | if err != nil { 564 | t.Fatal(err) 565 | } 566 | 567 | p, err := api.Unixfs().Add(ctx, strFile(helloStr)(), options.Unixfs.HashOnly(true)) 568 | if err != nil { 569 | t.Fatal(err) 570 | } 571 | 572 | if p.String() != hello { 573 | t.Errorf("unxepected path: %s", p.String()) 574 | } 575 | 576 | _, err = api.Block().Get(ctx, p) 577 | if err == nil { 578 | t.Fatal("expected an error") 579 | } 580 | if !ipld.IsNotFound(err) { 581 | t.Errorf("unxepected error: %s", err.Error()) 582 | } 583 | } 584 | 585 | func (tp *TestSuite) TestGetEmptyFile(t *testing.T) { 586 | ctx, cancel := context.WithCancel(context.Background()) 587 | defer cancel() 588 | api, err := tp.makeAPI(ctx) 589 | if err != nil { 590 | t.Fatal(err) 591 | } 592 | 593 | _, err = api.Unixfs().Add(ctx, files.NewBytesFile([]byte{})) 594 | if err != nil { 595 | t.Fatal(err) 596 | } 597 | 598 | emptyFilePath := path.New(emptyFile) 599 | 600 | r, err := api.Unixfs().Get(ctx, emptyFilePath) 601 | if err != nil { 602 | t.Fatal(err) 603 | } 604 | 605 | buf := make([]byte, 1) // non-zero so that Read() actually tries to read 606 | n, err := io.ReadFull(r.(files.File), buf) 607 | if err != nil && err != io.EOF { 608 | t.Error(err) 609 | } 610 | if !bytes.HasPrefix(buf, []byte{0x00}) { 611 | t.Fatalf("expected empty data, got [%s] [read=%d]", buf, n) 612 | } 613 | } 614 | 615 | func (tp *TestSuite) TestGetDir(t *testing.T) { 616 | ctx, cancel := context.WithCancel(context.Background()) 617 | defer cancel() 618 | api, err := tp.makeAPI(ctx) 619 | if err != nil { 620 | t.Fatal(err) 621 | } 622 | edir := unixfs.EmptyDirNode() 623 | err = api.Dag().Add(ctx, edir) 624 | if err != nil { 625 | t.Fatal(err) 626 | } 627 | p := path.IpfsPath(edir.Cid()) 628 | 629 | emptyDir, err := api.Object().New(ctx, options.Object.Type("unixfs-dir")) 630 | if err != nil { 631 | t.Fatal(err) 632 | } 633 | 634 | if p.String() != path.IpfsPath(emptyDir.Cid()).String() { 635 | t.Fatalf("expected path %s, got: %s", emptyDir.Cid(), p.String()) 636 | } 637 | 638 | r, err := api.Unixfs().Get(ctx, path.IpfsPath(emptyDir.Cid())) 639 | if err != nil { 640 | t.Fatal(err) 641 | } 642 | 643 | if _, ok := r.(files.Directory); !ok { 644 | t.Fatalf("expected a directory") 645 | } 646 | } 647 | 648 | func (tp *TestSuite) TestGetNonUnixfs(t *testing.T) { 649 | ctx, cancel := context.WithCancel(context.Background()) 650 | defer cancel() 651 | api, err := tp.makeAPI(ctx) 652 | if err != nil { 653 | t.Fatal(err) 654 | } 655 | 656 | nd := new(mdag.ProtoNode) 657 | err = api.Dag().Add(ctx, nd) 658 | if err != nil { 659 | t.Fatal(err) 660 | } 661 | 662 | _, err = api.Unixfs().Get(ctx, path.IpfsPath(nd.Cid())) 663 | if !strings.Contains(err.Error(), "proto: required field") { 664 | t.Fatalf("expected protobuf error, got: %s", err) 665 | } 666 | } 667 | 668 | func (tp *TestSuite) TestLs(t *testing.T) { 669 | ctx, cancel := context.WithCancel(context.Background()) 670 | defer cancel() 671 | api, err := tp.makeAPI(ctx) 672 | if err != nil { 673 | t.Fatal(err) 674 | } 675 | 676 | r := strings.NewReader("content-of-file") 677 | p, err := api.Unixfs().Add(ctx, files.NewMapDirectory(map[string]files.Node{ 678 | "name-of-file": files.NewReaderFile(r), 679 | "name-of-symlink": files.NewLinkFile("/foo/bar", nil), 680 | })) 681 | if err != nil { 682 | t.Fatal(err) 683 | } 684 | 685 | entries, err := api.Unixfs().Ls(ctx, p) 686 | if err != nil { 687 | t.Fatal(err) 688 | } 689 | 690 | entry := <-entries 691 | if entry.Err != nil { 692 | t.Fatal(entry.Err) 693 | } 694 | if entry.Size != 15 { 695 | t.Errorf("expected size = 15, got %d", entry.Size) 696 | } 697 | if entry.Name != "name-of-file" { 698 | t.Errorf("expected name = name-of-file, got %s", entry.Name) 699 | } 700 | if entry.Type != coreiface.TFile { 701 | t.Errorf("wrong type %s", entry.Type) 702 | } 703 | if entry.Cid.String() != "QmX3qQVKxDGz3URVC3861Z3CKtQKGBn6ffXRBBWGMFz9Lr" { 704 | t.Errorf("expected cid = QmX3qQVKxDGz3URVC3861Z3CKtQKGBn6ffXRBBWGMFz9Lr, got %s", entry.Cid) 705 | } 706 | entry = <-entries 707 | if entry.Err != nil { 708 | t.Fatal(entry.Err) 709 | } 710 | if entry.Type != coreiface.TSymlink { 711 | t.Errorf("wrong type %s", entry.Type) 712 | } 713 | if entry.Name != "name-of-symlink" { 714 | t.Errorf("expected name = name-of-symlink, got %s", entry.Name) 715 | } 716 | if entry.Target != "/foo/bar" { 717 | t.Errorf("expected symlink target to be /foo/bar, got %s", entry.Target) 718 | } 719 | 720 | if l, ok := <-entries; ok { 721 | t.Errorf("didn't expect a second link") 722 | if l.Err != nil { 723 | t.Error(l.Err) 724 | } 725 | } 726 | } 727 | 728 | func (tp *TestSuite) TestEntriesExpired(t *testing.T) { 729 | ctx, cancel := context.WithCancel(context.Background()) 730 | defer cancel() 731 | api, err := tp.makeAPI(ctx) 732 | if err != nil { 733 | t.Fatal(err) 734 | } 735 | 736 | r := strings.NewReader("content-of-file") 737 | p, err := api.Unixfs().Add(ctx, files.NewMapDirectory(map[string]files.Node{ 738 | "name-of-file": files.NewReaderFile(r), 739 | })) 740 | if err != nil { 741 | t.Fatal(err) 742 | } 743 | 744 | ctx, cancel = context.WithCancel(ctx) 745 | 746 | nd, err := api.Unixfs().Get(ctx, p) 747 | if err != nil { 748 | t.Fatal(err) 749 | } 750 | cancel() 751 | 752 | it := files.ToDir(nd).Entries() 753 | if it == nil { 754 | t.Fatal("it was nil") 755 | } 756 | 757 | if it.Next() { 758 | t.Fatal("Next succeeded") 759 | } 760 | 761 | if it.Err() != context.Canceled { 762 | t.Fatalf("unexpected error %s", it.Err()) 763 | } 764 | 765 | if it.Next() { 766 | t.Fatal("Next succeeded") 767 | } 768 | } 769 | 770 | func (tp *TestSuite) TestLsEmptyDir(t *testing.T) { 771 | ctx, cancel := context.WithCancel(context.Background()) 772 | defer cancel() 773 | api, err := tp.makeAPI(ctx) 774 | if err != nil { 775 | t.Fatal(err) 776 | } 777 | 778 | _, err = api.Unixfs().Add(ctx, files.NewSliceDirectory([]files.DirEntry{})) 779 | if err != nil { 780 | t.Fatal(err) 781 | } 782 | 783 | emptyDir, err := api.Object().New(ctx, options.Object.Type("unixfs-dir")) 784 | if err != nil { 785 | t.Fatal(err) 786 | } 787 | 788 | links, err := api.Unixfs().Ls(ctx, path.IpfsPath(emptyDir.Cid())) 789 | if err != nil { 790 | t.Fatal(err) 791 | } 792 | 793 | if len(links) != 0 { 794 | t.Fatalf("expected 0 links, got %d", len(links)) 795 | } 796 | } 797 | 798 | // TODO(lgierth) this should test properly, with len(links) > 0 799 | func (tp *TestSuite) TestLsNonUnixfs(t *testing.T) { 800 | ctx, cancel := context.WithCancel(context.Background()) 801 | defer cancel() 802 | api, err := tp.makeAPI(ctx) 803 | if err != nil { 804 | t.Fatal(err) 805 | } 806 | 807 | nd, err := cbor.WrapObject(map[string]interface{}{"foo": "bar"}, math.MaxUint64, -1) 808 | if err != nil { 809 | t.Fatal(err) 810 | } 811 | 812 | err = api.Dag().Add(ctx, nd) 813 | if err != nil { 814 | t.Fatal(err) 815 | } 816 | 817 | links, err := api.Unixfs().Ls(ctx, path.IpfsPath(nd.Cid())) 818 | if err != nil { 819 | t.Fatal(err) 820 | } 821 | 822 | if len(links) != 0 { 823 | t.Fatalf("expected 0 links, got %d", len(links)) 824 | } 825 | } 826 | 827 | type closeTestF struct { 828 | files.File 829 | closed bool 830 | 831 | t *testing.T 832 | } 833 | 834 | type closeTestD struct { 835 | files.Directory 836 | closed bool 837 | 838 | t *testing.T 839 | } 840 | 841 | func (f *closeTestD) Close() error { 842 | f.t.Helper() 843 | if f.closed { 844 | f.t.Fatal("already closed") 845 | } 846 | f.closed = true 847 | return nil 848 | } 849 | 850 | func (f *closeTestF) Close() error { 851 | if f.closed { 852 | f.t.Fatal("already closed") 853 | } 854 | f.closed = true 855 | return nil 856 | } 857 | 858 | func (tp *TestSuite) TestAddCloses(t *testing.T) { 859 | ctx, cancel := context.WithCancel(context.Background()) 860 | defer cancel() 861 | api, err := tp.makeAPI(ctx) 862 | if err != nil { 863 | t.Fatal(err) 864 | } 865 | 866 | n4 := &closeTestF{files.NewBytesFile([]byte("foo")), false, t} 867 | d3 := &closeTestD{files.NewMapDirectory(map[string]files.Node{ 868 | "sub": n4, 869 | }), false, t} 870 | n2 := &closeTestF{files.NewBytesFile([]byte("bar")), false, t} 871 | n1 := &closeTestF{files.NewBytesFile([]byte("baz")), false, t} 872 | d0 := &closeTestD{files.NewMapDirectory(map[string]files.Node{ 873 | "a": d3, 874 | "b": n1, 875 | "c": n2, 876 | }), false, t} 877 | 878 | _, err = api.Unixfs().Add(ctx, d0) 879 | if err != nil { 880 | t.Fatal(err) 881 | } 882 | 883 | for i, n := range []*closeTestF{n1, n2, n4} { 884 | if !n.closed { 885 | t.Errorf("file %d not closed!", i) 886 | } 887 | } 888 | 889 | for i, n := range []*closeTestD{d0, d3} { 890 | if !n.closed { 891 | t.Errorf("dir %d not closed!", i) 892 | } 893 | } 894 | } 895 | 896 | func (tp *TestSuite) TestGetSeek(t *testing.T) { 897 | ctx, cancel := context.WithCancel(context.Background()) 898 | defer cancel() 899 | api, err := tp.makeAPI(ctx) 900 | if err != nil { 901 | t.Fatal(err) 902 | } 903 | 904 | dataSize := int64(100000) 905 | tf := files.NewReaderFile(io.LimitReader(rand.New(rand.NewSource(1403768328)), dataSize)) 906 | 907 | p, err := api.Unixfs().Add(ctx, tf, options.Unixfs.Chunker("size-100")) 908 | if err != nil { 909 | t.Fatal(err) 910 | } 911 | 912 | r, err := api.Unixfs().Get(ctx, p) 913 | if err != nil { 914 | t.Fatal(err) 915 | } 916 | 917 | f := files.ToFile(r) 918 | if f == nil { 919 | t.Fatal("not a file") 920 | } 921 | 922 | orig := make([]byte, dataSize) 923 | if _, err := io.ReadFull(f, orig); err != nil { 924 | t.Fatal(err) 925 | } 926 | f.Close() 927 | 928 | origR := bytes.NewReader(orig) 929 | 930 | r, err = api.Unixfs().Get(ctx, p) 931 | if err != nil { 932 | t.Fatal(err) 933 | } 934 | 935 | f = files.ToFile(r) 936 | if f == nil { 937 | t.Fatal("not a file") 938 | } 939 | 940 | test := func(offset int64, whence int, read int, expect int64, shouldEof bool) { 941 | t.Run(fmt.Sprintf("seek%d+%d-r%d-%d", whence, offset, read, expect), func(t *testing.T) { 942 | n, err := f.Seek(offset, whence) 943 | if err != nil { 944 | t.Fatal(err) 945 | } 946 | origN, err := origR.Seek(offset, whence) 947 | if err != nil { 948 | t.Fatal(err) 949 | } 950 | 951 | if n != origN { 952 | t.Fatalf("offsets didn't match, expected %d, got %d", origN, n) 953 | } 954 | 955 | buf := make([]byte, read) 956 | origBuf := make([]byte, read) 957 | origRead, err := origR.Read(origBuf) 958 | if err != nil { 959 | t.Fatalf("orig: %s", err) 960 | } 961 | r, err := io.ReadFull(f, buf) 962 | switch { 963 | case shouldEof && err != nil && err != io.ErrUnexpectedEOF: 964 | fallthrough 965 | case !shouldEof && err != nil: 966 | t.Fatalf("f: %s", err) 967 | case shouldEof: 968 | _, err := f.Read([]byte{0}) 969 | if err != io.EOF { 970 | t.Fatal("expected EOF") 971 | } 972 | _, err = origR.Read([]byte{0}) 973 | if err != io.EOF { 974 | t.Fatal("expected EOF (orig)") 975 | } 976 | } 977 | 978 | if int64(r) != expect { 979 | t.Fatal("read wrong amount of data") 980 | } 981 | if r != origRead { 982 | t.Fatal("read different amount of data than bytes.Reader") 983 | } 984 | if !bytes.Equal(buf, origBuf) { 985 | fmt.Fprintf(os.Stderr, "original:\n%s\n", hex.Dump(origBuf)) 986 | fmt.Fprintf(os.Stderr, "got:\n%s\n", hex.Dump(buf)) 987 | t.Fatal("data didn't match") 988 | } 989 | }) 990 | } 991 | 992 | test(3, io.SeekCurrent, 10, 10, false) 993 | test(3, io.SeekCurrent, 10, 10, false) 994 | test(500, io.SeekCurrent, 10, 10, false) 995 | test(350, io.SeekStart, 100, 100, false) 996 | test(-123, io.SeekCurrent, 100, 100, false) 997 | test(0, io.SeekStart, int(dataSize), dataSize, false) 998 | test(dataSize-50, io.SeekStart, 100, 50, true) 999 | test(-5, io.SeekEnd, 100, 5, true) 1000 | } 1001 | 1002 | func (tp *TestSuite) TestGetReadAt(t *testing.T) { 1003 | ctx, cancel := context.WithCancel(context.Background()) 1004 | defer cancel() 1005 | api, err := tp.makeAPI(ctx) 1006 | if err != nil { 1007 | t.Fatal(err) 1008 | } 1009 | 1010 | dataSize := int64(100000) 1011 | tf := files.NewReaderFile(io.LimitReader(rand.New(rand.NewSource(1403768328)), dataSize)) 1012 | 1013 | p, err := api.Unixfs().Add(ctx, tf, options.Unixfs.Chunker("size-100")) 1014 | if err != nil { 1015 | t.Fatal(err) 1016 | } 1017 | 1018 | r, err := api.Unixfs().Get(ctx, p) 1019 | if err != nil { 1020 | t.Fatal(err) 1021 | } 1022 | 1023 | f, ok := r.(interface { 1024 | files.File 1025 | io.ReaderAt 1026 | }) 1027 | if !ok { 1028 | t.Skip("ReaderAt not implemented") 1029 | } 1030 | 1031 | orig := make([]byte, dataSize) 1032 | if _, err := io.ReadFull(f, orig); err != nil { 1033 | t.Fatal(err) 1034 | } 1035 | f.Close() 1036 | 1037 | origR := bytes.NewReader(orig) 1038 | 1039 | if _, err := api.Unixfs().Get(ctx, p); err != nil { 1040 | t.Fatal(err) 1041 | } 1042 | 1043 | test := func(offset int64, read int, expect int64, shouldEof bool) { 1044 | t.Run(fmt.Sprintf("readat%d-r%d-%d", offset, read, expect), func(t *testing.T) { 1045 | origBuf := make([]byte, read) 1046 | origRead, err := origR.ReadAt(origBuf, offset) 1047 | if err != nil && err != io.EOF { 1048 | t.Fatalf("orig: %s", err) 1049 | } 1050 | buf := make([]byte, read) 1051 | r, err := f.ReadAt(buf, offset) 1052 | if shouldEof { 1053 | if err != io.EOF { 1054 | t.Fatal("expected EOF, got: ", err) 1055 | } 1056 | } else if err != nil { 1057 | t.Fatal("got: ", err) 1058 | } 1059 | 1060 | if int64(r) != expect { 1061 | t.Fatal("read wrong amount of data") 1062 | } 1063 | if r != origRead { 1064 | t.Fatal("read different amount of data than bytes.Reader") 1065 | } 1066 | if !bytes.Equal(buf, origBuf) { 1067 | fmt.Fprintf(os.Stderr, "original:\n%s\n", hex.Dump(origBuf)) 1068 | fmt.Fprintf(os.Stderr, "got:\n%s\n", hex.Dump(buf)) 1069 | t.Fatal("data didn't match") 1070 | } 1071 | }) 1072 | } 1073 | 1074 | test(3, 10, 10, false) 1075 | test(13, 10, 10, false) 1076 | test(513, 10, 10, false) 1077 | test(350, 100, 100, false) 1078 | test(0, int(dataSize), dataSize, false) 1079 | test(dataSize-50, 100, 50, true) 1080 | } 1081 | --------------------------------------------------------------------------------