name.go 6.48 KB
Newer Older
1
package tests
2 3 4

import (
	"context"
Łukasz Magiera's avatar
Łukasz Magiera committed
5
	path "github.com/ipfs/interface-go-ipfs-core/path"
6 7
	"io"
	"math/rand"
Łukasz Magiera's avatar
Łukasz Magiera committed
8
	gopath "path"
9 10 11
	"testing"
	"time"

Łukasz Magiera's avatar
Łukasz Magiera committed
12 13
	"github.com/ipfs/go-ipfs-files"
	ipath "github.com/ipfs/go-path"
14

Łukasz Magiera's avatar
Łukasz Magiera committed
15 16
	coreiface "github.com/ipfs/interface-go-ipfs-core"
	opt "github.com/ipfs/interface-go-ipfs-core/options"
17 18
)

19
func (tp *provider) TestName(t *testing.T) {
20 21 22 23 24 25 26
	tp.hasApi(t, func(api coreiface.CoreAPI) error {
		if api.Name() == nil {
			return apiNotImplemented
		}
		return nil
	})

27 28 29
	t.Run("TestPublishResolve", tp.TestPublishResolve)
	t.Run("TestBasicPublishResolveKey", tp.TestBasicPublishResolveKey)
	t.Run("TestBasicPublishResolveTimeout", tp.TestBasicPublishResolveTimeout)
30 31
}

32 33
var rnd = rand.New(rand.NewSource(0x62796532303137))

Łukasz Magiera's avatar
Łukasz Magiera committed
34
func addTestObject(ctx context.Context, api coreiface.CoreAPI) (path.Path, error) {
35 36 37
	return api.Unixfs().Add(ctx, files.NewReaderFile(&io.LimitedReader{R: rnd, N: 4092}))
}

Łukasz Magiera's avatar
Łukasz Magiera committed
38
func appendPath(p path.Path, sub string) path.Path {
39
	return path.New(gopath.Join(p.String(), sub))
40 41
}

42
func (tp *provider) TestPublishResolve(t *testing.T) {
43 44
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
Łukasz Magiera's avatar
Łukasz Magiera committed
45
	init := func() (coreiface.CoreAPI, path.Path) {
46
		apis, err := tp.MakeAPISwarm(ctx, true, 5)
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
		if err != nil {
			t.Fatal(err)
			return nil, nil
		}
		api := apis[0]

		p, err := addTestObject(ctx, api)
		if err != nil {
			t.Fatal(err)
			return nil, nil
		}
		return api, p
	}
	run := func(t *testing.T, ropts []opt.NameResolveOption) {
		t.Run("basic", func(t *testing.T) {
			api, p := init()
			e, err := api.Name().Publish(ctx, p)
			if err != nil {
				t.Fatal(err)
			}

			self, err := api.Key().Self(ctx)
			if err != nil {
				t.Fatal(err)
			}

			if e.Name() != self.ID().Pretty() {
				t.Errorf("expected e.Name to equal '%s', got '%s'", self.ID().Pretty(), e.Name())
			}

			if e.Value().String() != p.String() {
				t.Errorf("expected paths to match, '%s'!='%s'", e.Value().String(), p.String())
			}

			resPath, err := api.Name().Resolve(ctx, e.Name(), ropts...)
			if err != nil {
				t.Fatal(err)
			}

			if resPath.String() != p.String() {
				t.Errorf("expected paths to match, '%s'!='%s'", resPath.String(), p.String())
			}
		})

		t.Run("publishPath", func(t *testing.T) {
			api, p := init()
			e, err := api.Name().Publish(ctx, appendPath(p, "/test"))
			if err != nil {
				t.Fatal(err)
			}

			self, err := api.Key().Self(ctx)
			if err != nil {
				t.Fatal(err)
			}

			if e.Name() != self.ID().Pretty() {
				t.Errorf("expected e.Name to equal '%s', got '%s'", self.ID().Pretty(), e.Name())
			}

			if e.Value().String() != p.String()+"/test" {
				t.Errorf("expected paths to match, '%s'!='%s'", e.Value().String(), p.String())
			}

			resPath, err := api.Name().Resolve(ctx, e.Name(), ropts...)
			if err != nil {
				t.Fatal(err)
			}

			if resPath.String() != p.String()+"/test" {
				t.Errorf("expected paths to match, '%s'!='%s'", resPath.String(), p.String()+"/test")
			}
		})

		t.Run("revolvePath", func(t *testing.T) {
			api, p := init()
			e, err := api.Name().Publish(ctx, p)
			if err != nil {
				t.Fatal(err)
			}

			self, err := api.Key().Self(ctx)
			if err != nil {
				t.Fatal(err)
			}

			if e.Name() != self.ID().Pretty() {
				t.Errorf("expected e.Name to equal '%s', got '%s'", self.ID().Pretty(), e.Name())
			}

			if e.Value().String() != p.String() {
				t.Errorf("expected paths to match, '%s'!='%s'", e.Value().String(), p.String())
			}

			resPath, err := api.Name().Resolve(ctx, e.Name()+"/test", ropts...)
			if err != nil {
				t.Fatal(err)
			}

			if resPath.String() != p.String()+"/test" {
				t.Errorf("expected paths to match, '%s'!='%s'", resPath.String(), p.String()+"/test")
			}
		})

		t.Run("publishRevolvePath", func(t *testing.T) {
			api, p := init()
			e, err := api.Name().Publish(ctx, appendPath(p, "/a"))
			if err != nil {
				t.Fatal(err)
			}

			self, err := api.Key().Self(ctx)
			if err != nil {
				t.Fatal(err)
			}

			if e.Name() != self.ID().Pretty() {
				t.Errorf("expected e.Name to equal '%s', got '%s'", self.ID().Pretty(), e.Name())
			}

			if e.Value().String() != p.String()+"/a" {
				t.Errorf("expected paths to match, '%s'!='%s'", e.Value().String(), p.String())
			}

			resPath, err := api.Name().Resolve(ctx, e.Name()+"/b", ropts...)
			if err != nil {
				t.Fatal(err)
			}

			if resPath.String() != p.String()+"/a/b" {
				t.Errorf("expected paths to match, '%s'!='%s'", resPath.String(), p.String()+"/a/b")
			}
		})
	}

	t.Run("default", func(t *testing.T) {
		run(t, []opt.NameResolveOption{})
	})

	t.Run("nocache", func(t *testing.T) {
		run(t, []opt.NameResolveOption{opt.Name.Cache(false)})
	})
}

191
func (tp *provider) TestBasicPublishResolveKey(t *testing.T) {
192 193
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
194
	apis, err := tp.MakeAPISwarm(ctx, true, 5)
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
	if err != nil {
		t.Fatal(err)
	}
	api := apis[0]

	k, err := api.Key().Generate(ctx, "foo")
	if err != nil {
		t.Fatal(err)
	}

	p, err := addTestObject(ctx, api)
	if err != nil {
		t.Fatal(err)
	}

	e, err := api.Name().Publish(ctx, p, opt.Name.Key(k.Name()))
	if err != nil {
		t.Fatal(err)
	}

	if ipath.Join([]string{"/ipns", e.Name()}) != k.Path().String() {
		t.Errorf("expected e.Name to equal '%s', got '%s'", e.Name(), k.Path().String())
	}

	if e.Value().String() != p.String() {
		t.Errorf("expected paths to match, '%s'!='%s'", e.Value().String(), p.String())
	}

	resPath, err := api.Name().Resolve(ctx, e.Name())
	if err != nil {
		t.Fatal(err)
	}

	if resPath.String() != p.String() {
		t.Errorf("expected paths to match, '%s'!='%s'", resPath.String(), p.String())
	}
}

233
func (tp *provider) TestBasicPublishResolveTimeout(t *testing.T) {
234 235
	t.Skip("ValidTime doesn't appear to work at this time resolution")

236 237
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
238
	apis, err := tp.MakeAPISwarm(ctx, true, 5)
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
	if err != nil {
		t.Fatal(err)
	}
	api := apis[0]
	p, err := addTestObject(ctx, api)
	if err != nil {
		t.Fatal(err)
	}

	e, err := api.Name().Publish(ctx, p, opt.Name.ValidTime(time.Millisecond*100))
	if err != nil {
		t.Fatal(err)
	}

	self, err := api.Key().Self(ctx)
	if err != nil {
		t.Fatal(err)
	}

	if e.Name() != self.ID().Pretty() {
		t.Errorf("expected e.Name to equal '%s', got '%s'", self.ID().Pretty(), e.Name())
	}

	if e.Value().String() != p.String() {
		t.Errorf("expected paths to match, '%s'!='%s'", e.Value().String(), p.String())
	}

	time.Sleep(time.Second)

	_, err = api.Name().Resolve(ctx, e.Name())
	if err == nil {
		t.Fatal("Expected an error")
	}
}

//TODO: When swarm api is created, add multinode tests