Skip to content

Commit

Permalink
feat(test): Add mobile test
Browse files Browse the repository at this point in the history
  • Loading branch information
gfanton authored and aeddi committed Dec 1, 2019
1 parent d91426f commit 1daa641
Show file tree
Hide file tree
Showing 10 changed files with 757 additions and 69 deletions.
26 changes: 24 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,41 @@ module github.com/berty/gomobile-ipfs
go 1.12

require (
berty.tech/go-ipfs-log v0.0.0-20190805145225-165c94541925
github.com/AndreasBriese/bbloom v0.0.0-20190306092124-e2d15f34fcf9 // indirect
github.com/btcsuite/goleveldb v1.0.0 // indirect
github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2 // indirect
github.com/gopherjs/gopherjs v0.0.0-20190430165422-3e4dfb77656c // indirect
github.com/ipfs/go-datastore v0.0.5
github.com/ipfs/go-filestore v0.0.2
github.com/ipfs/go-ipfs v0.4.21
github.com/ipfs/go-ipfs-api v0.0.2
github.com/ipfs/go-ipfs-config v0.0.3
github.com/jtolds/gls v4.2.2-0.20181110203027-b4936e06046b+incompatible // indirect
github.com/kisielk/errcheck v1.2.0 // indirect
github.com/kkdai/bstream v0.0.0-20181106074824-b3251f7901ec // indirect
github.com/libp2p/go-libp2p v0.3.0
github.com/libp2p/go-libp2p-connmgr v0.1.0 // indirect
github.com/libp2p/go-libp2p-core v0.2.0
github.com/libp2p/go-libp2p-host v0.1.0 // indirect
github.com/libp2p/go-libp2p-interface-connmgr v0.1.0 // indirect
github.com/libp2p/go-libp2p-kad-dht v0.1.0 // indirect
github.com/libp2p/go-libp2p-metrics v0.1.0 // indirect
github.com/libp2p/go-libp2p-net v0.1.0 // indirect
github.com/libp2p/go-libp2p-pnet v0.1.0 // indirect
github.com/libp2p/go-libp2p-protocol v0.1.0 // indirect
github.com/libp2p/go-libp2p-quic-transport v0.1.2-0.20190602113809-9400928a835e // indirect
github.com/libp2p/go-libp2p-tls v0.1.0 // indirect
github.com/libp2p/go-stream-muxer v0.1.0 // indirect
github.com/multiformats/go-multiaddr v0.0.4
github.com/multiformats/go-multiaddr-net v0.0.1
github.com/smartystreets/assertions v0.0.0-20190401211740-f487f9de1cd3 // indirect
github.com/smartystreets/goconvey v0.0.0-20190222223459-a17d461953aa
github.com/spf13/cobra v0.0.5 // indirect
golang.org/x/net v0.0.0-20190620200207-3b0461eec859 // indirect
go.opencensus.io v0.22.0 // indirect
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4 // indirect
golang.org/x/net v0.0.0-20190628185345-da137c7871d7 // indirect
golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb // indirect
golang.org/x/tools v0.0.0-20190521203540-521d6ed310dd // indirect
google.golang.org/appengine v1.4.0 // indirect
)

Expand Down
77 changes: 71 additions & 6 deletions go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion host/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func (mh *MobileHost) Close() error {

// ConnManager returns this hosts connection manager
func (mh *MobileHost) ConnManager() p2p_connmgr.ConnManager {
return mh.ConnManager()
return mh.Host.ConnManager()
}

// EventBus returns the hosts eventbus
Expand Down
31 changes: 1 addition & 30 deletions host/host_config.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,5 @@
package host

import (
"bytes"
"encoding/json"
"fmt"
)

// @TODO: add custom mobile option here
type MobileConfig struct {
}

func FromMap(v map[string]interface{}) (*Config, error) {
buf := new(bytes.Buffer)
if err := json.NewEncoder(buf).Encode(v); err != nil {
return nil, err
}
var conf MobileConfig
if err := json.NewDecoder(buf).Decode(&conf); err != nil {
return nil, fmt.Errorf("failure to decode config: %s", err)
}
return &conf, nil
}

func ToMap(conf *MobileConfig) (map[string]interface{}, error) {
buf := new(bytes.Buffer)
if err := json.NewEncoder(buf).Encode(conf); err != nil {
return nil, err
}
var m map[string]interface{}
if err := json.NewDecoder(buf).Decode(&m); err != nil {
return nil, fmt.Errorf("failure to decode config: %s", err)
}
return m, nil
}
197 changes: 197 additions & 0 deletions init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
package mobile

import (
"encoding/base64"
"errors"
"fmt"
"io"
"time"

ipfs_config "github.com/ipfs/go-ipfs-config"
libp2p_ci "github.com/libp2p/go-libp2p-core/crypto"
libp2p_peer "github.com/libp2p/go-libp2p-core/peer"
)

func Init(out io.Writer, nBitsForKeypair int) (*ipfs_config.Config, error) {
identity, err := identityConfig(out, nBitsForKeypair)
if err != nil {
return nil, err
}

bootstrapPeers, err := ipfs_config.DefaultBootstrapPeers()
if err != nil {
return nil, err
}

datastore := ipfs_config.DefaultDatastoreConfig()

conf := &ipfs_config.Config{
API: ipfs_config.API{
HTTPHeaders: map[string][]string{
"Access-Control-Allow-Credentials": []string{"true"},
"Access-Control-Allow-Origin": []string{"*"},
"Access-Control-Allow-Methods": []string{"GET", "PUT", "POST"},
},
},

// setup the node's default addresses.
// NOTE: two swarm listen addrs, one tcp, one utp.
Addresses: addressesConfig(),

Datastore: datastore,
Bootstrap: ipfs_config.BootstrapPeerStrings(bootstrapPeers),
Identity: identity,
Discovery: ipfs_config.Discovery{
MDNS: ipfs_config.MDNS{
Enabled: true,
Interval: 10,
},
},

Routing: ipfs_config.Routing{
Type: "dhtclient",
},

// setup the node mount points.
Mounts: ipfs_config.Mounts{
IPFS: "/ipfs",
IPNS: "/ipns",
},

Ipns: ipfs_config.Ipns{
ResolveCacheSize: 128,
},

Gateway: ipfs_config.Gateway{
RootRedirect: "",
Writable: false,
NoFetch: false,
PathPrefixes: []string{},
HTTPHeaders: map[string][]string{
"Access-Control-Allow-Origin": []string{"*"},
"Access-Control-Allow-Methods": []string{"GET"},
"Access-Control-Allow-Headers": []string{"X-Requested-With", "Range", "User-Agent"},
},
APICommands: []string{},
},
Reprovider: ipfs_config.Reprovider{
Interval: "12h",
Strategy: "all",
},
Swarm: ipfs_config.SwarmConfig{
ConnMgr: ipfs_config.ConnMgr{
LowWater: DefaultConnMgrLowWater,
HighWater: DefaultConnMgrHighWater,
GracePeriod: DefaultConnMgrGracePeriod.String(),
Type: "basic",
},
},
Experimental: ipfs_config.Experiments{
FilestoreEnabled: false,
Libp2pStreamMounting: false,
P2pHttpProxy: false,
QUIC: true,
ShardingEnabled: false,
UrlstoreEnabled: false,
PreferTLS: true,
},
}

return conf, nil
}

// DefaultConnMgrHighWater is the default value for the connection managers
// 'high water' mark
const DefaultConnMgrHighWater = 200

// DefaultConnMgrLowWater is the default value for the connection managers 'low
// water' mark
const DefaultConnMgrLowWater = 100

// DefaultConnMgrGracePeriod is the default value for the connection managers
// grace period
const DefaultConnMgrGracePeriod = time.Second * 20

func addressesConfig() ipfs_config.Addresses {
return ipfs_config.Addresses{
Swarm: []string{
"/ip4/0.0.0.0/tcp/0",
"/ip6/::/tcp/0",

"/ip4/0.0.0.0/udp/0/quic",
"/ip6/::/udp/0/quic",
},
Announce: []string{},
NoAnnounce: []string{},
API: ipfs_config.Strings{"/ip4/127.0.0.1/tcp/5001"},
Gateway: ipfs_config.Strings{"/ip4/127.0.0.1/tcp/8080"},
}
}

// DefaultDatastoreConfig is an internal function exported to aid in testing.
func DefaultDatastoreConfig() ipfs_config.Datastore {
return ipfs_config.Datastore{
StorageMax: "10GB",
StorageGCWatermark: 90, // 90%
GCPeriod: "1h",
BloomFilterSize: 0,
Spec: map[string]interface{}{
"type": "mount",
"mounts": []interface{}{
map[string]interface{}{
"mountpoint": "/blocks",
"type": "measure",
"prefix": "flatfs.datastore",
"child": map[string]interface{}{
"type": "flatfs",
"path": "blocks",
"sync": true,
"shardFunc": "/repo/flatfs/shard/v1/next-to-last/2",
},
},
map[string]interface{}{
"mountpoint": "/",
"type": "measure",
"prefix": "leveldb.datastore",
"child": map[string]interface{}{
"type": "levelds",
"path": "datastore",
"compression": "none",
},
},
},
},
}
}

// identityConfig initializes a new identity.
func identityConfig(out io.Writer, nbits int) (ipfs_config.Identity, error) {
// TODO guard higher up
ident := ipfs_config.Identity{}
if nbits < 1024 {
return ident, errors.New("bitsize less than 1024 is considered unsafe")
}

fmt.Fprintf(out, "generating %v-bit RSA keypair...", nbits)
sk, pk, err := libp2p_ci.GenerateKeyPair(libp2p_ci.RSA, nbits)
if err != nil {
return ident, err
}
fmt.Fprintf(out, "done\n")

// currently storing key unencrypted. in the future we need to encrypt it.
// TODO(security)
skbytes, err := sk.Bytes()
if err != nil {
return ident, err
}
ident.PrivKey = base64.StdEncoding.EncodeToString(skbytes)

id, err := libp2p_peer.IDFromPublicKey(pk)
if err != nil {
return ident, err
}
ident.PeerID = id.Pretty()
fmt.Fprintf(out, "libp2p_peer identity: %s\n", ident.PeerID)
return ident, nil
}
Loading

0 comments on commit 1daa641

Please sign in to comment.