Skip to content

Commit

Permalink
feat: add gateway tests
Browse files Browse the repository at this point in the history
Signed-off-by: gfanton <8671905+gfanton@users.noreply.github.com>
  • Loading branch information
gfanton committed Jun 28, 2021
1 parent ef70e9a commit 9b10c62
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 37 deletions.
31 changes: 24 additions & 7 deletions go/bind/core/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,24 +69,32 @@ func (n *Node) Close() error {
}

func (n *Node) ServeUnixSocketAPI(sockpath string) (err error) {
_, err = n.ServeMultiaddr("/unix/" + sockpath)
_, err = n.ServeAPIMultiaddr("/unix/" + sockpath)
return
}

// ServeTCPAPI on the given port and return the current listening maddr
func (n *Node) ServeTCPAPI(port string) (string, error) {
return n.ServeMultiaddr("/ip4/127.0.0.1/tcp/" + port)
return n.ServeAPIMultiaddr("/ip4/127.0.0.1/tcp/" + port)
}

func (n *Node) ServeConfigAPI() error {
func (n *Node) ServeConfig() error {
cfg, err := n.ipfsMobile.Repo.Config()
if err != nil {
return err
}

if len(cfg.Addresses.API) > 0 {
for _, maddr := range cfg.Addresses.API {
if _, err := n.ServeMultiaddr(maddr); err != nil {
if _, err := n.ServeAPIMultiaddr(maddr); err != nil {
log.Printf("cannot serve `%s`: %s", maddr, err.Error())
}
}
}

if len(cfg.Addresses.Gateway) > 0 {
for _, maddr := range cfg.Addresses.Gateway {
if _, err := n.ServeAPIMultiaddr(maddr); err != nil {
log.Printf("cannot serve `%s`: %s", maddr, err.Error())
}
}
Expand All @@ -95,7 +103,16 @@ func (n *Node) ServeConfigAPI() error {
return nil
}

func (n *Node) ServeGateway(smaddr string, writable bool) (string, error) {
func (n *Node) ServeUnixSocketGateway(sockpath string, writable bool) (err error) {
_, err = n.ServeGatewayMultiaddr("/unix/"+sockpath, writable)
return
}

func (n *Node) ServeTCPGateway(port string, writable bool) (string, error) {
return n.ServeGatewayMultiaddr("/ip4/127.0.0.1/tcp/"+port, writable)
}

func (n *Node) ServeGatewayMultiaddr(smaddr string, writable bool) (string, error) {
maddr, err := ma.NewMultiaddr(smaddr)
if err != nil {
return "", err
Expand All @@ -116,10 +133,10 @@ func (n *Node) ServeGateway(smaddr string, writable bool) (string, error) {
}
}(manet.NetListener(ml))

return "", nil
return ml.Multiaddr().String(), nil
}

func (n *Node) ServeMultiaddr(smaddr string) (string, error) {
func (n *Node) ServeAPIMultiaddr(smaddr string) (string, error) {
maddr, err := ma.NewMultiaddr(smaddr)
if err != nil {
return "", err
Expand Down
117 changes: 117 additions & 0 deletions go/bind/core/node_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package core

import (
"bytes"
"context"
"fmt"
"io"
"net"
"net/http"
"path/filepath"
"testing"
"time"

ipfs_files "github.com/ipfs/go-ipfs-files"
ipfs_coreapi "github.com/ipfs/go-ipfs/core/coreapi"

ma "github.com/multiformats/go-multiaddr"
manet "github.com/multiformats/go-multiaddr/net"
)
Expand Down Expand Up @@ -93,3 +98,115 @@ func TestNodeServeAPI(t *testing.T) {
}
})
}

func TestNodeServeGateway(t *testing.T) {
var testcontent = []byte("hello world\n")

t.Run("tpc gateway", func(t *testing.T) {
path, clean := testingTempDir(t, "tpc_repo")
defer clean()

node, clean := testingNode(t, path)
defer clean()

smaddr, err := node.ServeTCPGateway("0", true)
if err != nil {
t.Fatal(err)
}

maddr, err := ma.NewMultiaddr(smaddr)
if err != nil {
t.Fatal(err)
}

addr, err := manet.ToNetAddr(maddr)
if err != nil {
t.Fatal(err)
}

api, err := ipfs_coreapi.NewCoreAPI(node.ipfsMobile.IpfsNode)
if err != nil {
t.Fatal(err)
}

file := ipfs_files.NewBytesFile(testcontent)
resolved, err := api.Unixfs().Add(context.Background(), file)
if err != nil {
t.Fatal(err)
}

cid := resolved.Cid()

url := fmt.Sprintf("http://%s/ipfs/%s", addr.String(), cid.String())
client := http.Client{Timeout: 5 * time.Second}

resp, err := client.Get(url)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()

b, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}

if bytes.Compare(b, testcontent) != 0 {
t.Fatalf("content `%s` are different from `%s`", b, testcontent)
}
})

t.Run("uds gateway", func(t *testing.T) {
path, clean := testingTempDir(t, "uds_repo")
defer clean()

sockdir, clean := testingTempDir(t, "uds_gateway")
defer clean()

node, clean := testingNode(t, path)
defer clean()

sock := filepath.Join(sockdir, "sock")
err := node.ServeUnixSocketGateway(sock, true)
if err != nil {
t.Fatal(err)
}

client := http.Client{
Timeout: 5 * time.Second,
Transport: &http.Transport{
DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
return net.Dial("unix", sock)
},
},
}

api, err := ipfs_coreapi.NewCoreAPI(node.ipfsMobile.IpfsNode)
if err != nil {
t.Fatal(err)
}

file := ipfs_files.NewBytesFile(testcontent)
resolved, err := api.Unixfs().Add(context.Background(), file)
if err != nil {
t.Fatal(err)
}

cid := resolved.Cid()
url := fmt.Sprintf("http://unix/ipfs/%s", cid.String())
resp, err := client.Get(url)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()

b, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}

if bytes.Compare(b, testcontent) != 0 {
t.Fatalf("content `%s` are different from `%s`", b, testcontent)
}
})
}
6 changes: 4 additions & 2 deletions go/go.mod
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
module github.com/ipfs-shipyard/gomobile-ipfs/go

go 1.14
go 1.16

require (
github.com/ipfs/go-datastore v0.4.5
github.com/ipfs/go-ipfs v0.7.0
github.com/ipfs/go-ipfs-api v0.2.0
github.com/ipfs/go-ipfs-config v0.9.0
github.com/ipfs/go-ipfs-files v0.0.8
github.com/libp2p/go-libp2p v0.11.0
github.com/libp2p/go-libp2p-core v0.6.1
github.com/libp2p/go-libp2p-record v0.1.3
github.com/multiformats/go-multiaddr v0.3.1
github.com/pkg/errors v0.9.1
)

replace github.com/lucas-clemente/quic-go => github.com/lucas-clemente/quic-go v0.18.0 // required by go1.15
// replace github.com/lucas-clemente/quic-go => github.com/lucas-clemente/quic-go v0.18.0
// required by go1.15
Loading

0 comments on commit 9b10c62

Please sign in to comment.