key.go 11.9 KB
Newer Older
1
package tests
2 3 4 5 6 7

import (
	"context"
	"strings"
	"testing"

8 9 10
	cid "github.com/ipfs/go-cid"
	coreiface "github.com/ipfs/interface-go-ipfs-core"
	iface "github.com/ipfs/interface-go-ipfs-core"
Łukasz Magiera's avatar
Łukasz Magiera committed
11
	opt "github.com/ipfs/interface-go-ipfs-core/options"
12
	mbase "github.com/multiformats/go-multibase"
13 14
)

Łukasz Magiera's avatar
Łukasz Magiera committed
15
func (tp *TestSuite) TestKey(t *testing.T) {
16 17 18 19 20 21 22
	tp.hasApi(t, func(api iface.CoreAPI) error {
		if api.Key() == nil {
			return apiNotImplemented
		}
		return nil
	})

23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
	t.Run("TestListSelf", tp.TestListSelf)
	t.Run("TestRenameSelf", tp.TestRenameSelf)
	t.Run("TestRemoveSelf", tp.TestRemoveSelf)
	t.Run("TestGenerate", tp.TestGenerate)
	t.Run("TestGenerateSize", tp.TestGenerateSize)
	t.Run("TestGenerateType", tp.TestGenerateType)
	t.Run("TestGenerateExisting", tp.TestGenerateExisting)
	t.Run("TestList", tp.TestList)
	t.Run("TestRename", tp.TestRename)
	t.Run("TestRenameToSelf", tp.TestRenameToSelf)
	t.Run("TestRenameToSelfForce", tp.TestRenameToSelfForce)
	t.Run("TestRenameOverwriteNoForce", tp.TestRenameOverwriteNoForce)
	t.Run("TestRenameOverwrite", tp.TestRenameOverwrite)
	t.Run("TestRenameSameNameNoForce", tp.TestRenameSameNameNoForce)
	t.Run("TestRenameSameName", tp.TestRenameSameName)
	t.Run("TestRemove", tp.TestRemove)
39 40
}

Łukasz Magiera's avatar
Łukasz Magiera committed
41
func (tp *TestSuite) TestListSelf(t *testing.T) {
42 43
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
44
	api, err := tp.makeAPI(ctx)
45 46 47 48 49
	if err != nil {
		t.Fatal(err)
		return
	}

50 51 52 53 54
	self, err := api.Key().Self(ctx)
	if err != nil {
		t.Fatal(err)
	}

55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
	keys, err := api.Key().List(ctx)
	if err != nil {
		t.Fatalf("failed to list keys: %s", err)
		return
	}

	if len(keys) != 1 {
		t.Fatalf("there should be 1 key (self), got %d", len(keys))
		return
	}

	if keys[0].Name() != "self" {
		t.Errorf("expected the key to be called 'self', got '%s'", keys[0].Name())
	}

70 71
	if keys[0].Path().String() != "/ipns/"+coreiface.FormatKeyID(self.ID()) {
		t.Errorf("expected the key to have path '/ipns/%s', got '%s'", coreiface.FormatKeyID(self.ID()), keys[0].Path().String())
72 73 74
	}
}

Łukasz Magiera's avatar
Łukasz Magiera committed
75
func (tp *TestSuite) TestRenameSelf(t *testing.T) {
76 77
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
78
	api, err := tp.makeAPI(ctx)
79 80 81 82 83 84 85 86 87
	if err != nil {
		t.Fatal(err)
		return
	}

	_, _, err = api.Key().Rename(ctx, "self", "foo")
	if err == nil {
		t.Error("expected error to not be nil")
	} else {
88
		if !strings.Contains(err.Error(), "cannot rename key with name 'self'") {
89 90 91 92 93 94 95 96
			t.Fatalf("expected error 'cannot rename key with name 'self'', got '%s'", err.Error())
		}
	}

	_, _, err = api.Key().Rename(ctx, "self", "foo", opt.Key.Force(true))
	if err == nil {
		t.Error("expected error to not be nil")
	} else {
97
		if !strings.Contains(err.Error(), "cannot rename key with name 'self'") {
98 99 100 101 102
			t.Fatalf("expected error 'cannot rename key with name 'self'', got '%s'", err.Error())
		}
	}
}

Łukasz Magiera's avatar
Łukasz Magiera committed
103
func (tp *TestSuite) TestRemoveSelf(t *testing.T) {
104 105
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
106
	api, err := tp.makeAPI(ctx)
107 108 109 110 111 112 113 114 115
	if err != nil {
		t.Fatal(err)
		return
	}

	_, err = api.Key().Remove(ctx, "self")
	if err == nil {
		t.Error("expected error to not be nil")
	} else {
116
		if !strings.Contains(err.Error(), "cannot remove key with name 'self'") {
117 118 119 120 121
			t.Fatalf("expected error 'cannot remove key with name 'self'', got '%s'", err.Error())
		}
	}
}

Łukasz Magiera's avatar
Łukasz Magiera committed
122
func (tp *TestSuite) TestGenerate(t *testing.T) {
123 124
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
125
	api, err := tp.makeAPI(ctx)
126
	if err != nil {
127
		t.Fatal(err)
128 129 130 131 132 133 134 135 136 137 138 139
	}

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

	if k.Name() != "foo" {
		t.Errorf("expected the key to be called 'foo', got '%s'", k.Name())
	}

140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
	verifyIPNSPath(t, k.Path().String())
}

func verifyIPNSPath(t *testing.T, p string) bool {
	t.Helper()
	if !strings.HasPrefix(p, "/ipns/") {
		t.Errorf("path %q does not look like an IPNS path", p)
		return false
	}
	k := p[len("/ipns/"):]
	c, err := cid.Decode(k)
	if err != nil {
		t.Errorf("failed to decode IPNS key %q (%v)", k, err)
		return false
	}
	b36, err := c.StringOfBase(mbase.Base36)
	if err != nil {
		t.Fatalf("cid cannot format itself in b36")
		return false
	}
	if b36 != k {
		t.Errorf("IPNS key is not base36")
162
	}
163
	return true
164 165
}

Łukasz Magiera's avatar
Łukasz Magiera committed
166
func (tp *TestSuite) TestGenerateSize(t *testing.T) {
167 168
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
169
	api, err := tp.makeAPI(ctx)
170
	if err != nil {
171
		t.Fatal(err)
172 173
	}

174
	k, err := api.Key().Generate(ctx, "foo", opt.Key.Size(2048))
175 176 177 178 179 180 181 182 183
	if err != nil {
		t.Fatal(err)
		return
	}

	if k.Name() != "foo" {
		t.Errorf("expected the key to be called 'foo', got '%s'", k.Name())
	}

184
	verifyIPNSPath(t, k.Path().String())
185 186
}

Łukasz Magiera's avatar
Łukasz Magiera committed
187
func (tp *TestSuite) TestGenerateType(t *testing.T) {
188 189
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
190 191
	t.Skip("disabled until libp2p/specs#111 is fixed")

192
	api, err := tp.makeAPI(ctx)
193
	if err != nil {
194
		t.Fatal(err)
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
	}

	k, err := api.Key().Generate(ctx, "bar", opt.Key.Type(opt.Ed25519Key))
	if err != nil {
		t.Fatal(err)
		return
	}

	if k.Name() != "bar" {
		t.Errorf("expected the key to be called 'foo', got '%s'", k.Name())
	}

	// Expected to be an inlined identity hash.
	if !strings.HasPrefix(k.Path().String(), "/ipns/12") {
		t.Errorf("expected the key to be prefixed with '/ipns/12', got '%s'", k.Path().String())
	}
}

Łukasz Magiera's avatar
Łukasz Magiera committed
213
func (tp *TestSuite) TestGenerateExisting(t *testing.T) {
214 215
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
216
	api, err := tp.makeAPI(ctx)
217
	if err != nil {
218
		t.Fatal(err)
219 220 221 222 223 224 225 226 227 228 229 230
	}

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

	_, err = api.Key().Generate(ctx, "foo")
	if err == nil {
		t.Error("expected error to not be nil")
	} else {
231
		if !strings.Contains(err.Error(), "key with name 'foo' already exists") {
232 233 234 235 236 237 238 239
			t.Fatalf("expected error 'key with name 'foo' already exists', got '%s'", err.Error())
		}
	}

	_, err = api.Key().Generate(ctx, "self")
	if err == nil {
		t.Error("expected error to not be nil")
	} else {
240
		if !strings.Contains(err.Error(), "cannot create key with name 'self'") {
241 242 243 244 245
			t.Fatalf("expected error 'cannot create key with name 'self'', got '%s'", err.Error())
		}
	}
}

Łukasz Magiera's avatar
Łukasz Magiera committed
246
func (tp *TestSuite) TestList(t *testing.T) {
247 248
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
249
	api, err := tp.makeAPI(ctx)
250
	if err != nil {
251
		t.Fatal(err)
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
	}

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

	l, err := api.Key().List(ctx)
	if err != nil {
		t.Fatal(err)
		return
	}

	if len(l) != 2 {
		t.Fatalf("expected to get 2 keys, got %d", len(l))
		return
	}

	if l[0].Name() != "self" {
		t.Fatalf("expected key 0 to be called 'self', got '%s'", l[0].Name())
		return
	}

	if l[1].Name() != "foo" {
		t.Fatalf("expected key 1 to be called 'foo', got '%s'", l[1].Name())
		return
	}

281 282
	verifyIPNSPath(t, l[0].Path().String())
	verifyIPNSPath(t, l[1].Path().String())
283 284
}

Łukasz Magiera's avatar
Łukasz Magiera committed
285
func (tp *TestSuite) TestRename(t *testing.T) {
286 287
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
288
	api, err := tp.makeAPI(ctx)
289
	if err != nil {
290
		t.Fatal(err)
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
	}

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

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

	if overwrote {
		t.Error("overwrote should be false")
	}

	if k.Name() != "bar" {
		t.Errorf("returned key should be called 'bar', got '%s'", k.Name())
	}
}

Łukasz Magiera's avatar
Łukasz Magiera committed
314
func (tp *TestSuite) TestRenameToSelf(t *testing.T) {
315 316
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
317
	api, err := tp.makeAPI(ctx)
318
	if err != nil {
319
		t.Fatal(err)
320 321 322 323 324 325 326 327 328 329 330 331
	}

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

	_, _, err = api.Key().Rename(ctx, "foo", "self")
	if err == nil {
		t.Error("expected error to not be nil")
	} else {
332
		if !strings.Contains(err.Error(), "cannot overwrite key with name 'self'") {
333 334 335 336 337
			t.Fatalf("expected error 'cannot overwrite key with name 'self'', got '%s'", err.Error())
		}
	}
}

Łukasz Magiera's avatar
Łukasz Magiera committed
338
func (tp *TestSuite) TestRenameToSelfForce(t *testing.T) {
339 340
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
341
	api, err := tp.makeAPI(ctx)
342
	if err != nil {
343
		t.Fatal(err)
344 345 346 347 348 349 350 351 352 353 354 355
	}

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

	_, _, err = api.Key().Rename(ctx, "foo", "self", opt.Key.Force(true))
	if err == nil {
		t.Error("expected error to not be nil")
	} else {
356
		if !strings.Contains(err.Error(), "cannot overwrite key with name 'self'") {
357 358 359 360 361
			t.Fatalf("expected error 'cannot overwrite key with name 'self'', got '%s'", err.Error())
		}
	}
}

Łukasz Magiera's avatar
Łukasz Magiera committed
362
func (tp *TestSuite) TestRenameOverwriteNoForce(t *testing.T) {
363 364
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
365
	api, err := tp.makeAPI(ctx)
366
	if err != nil {
367
		t.Fatal(err)
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
	}

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

	_, err = api.Key().Generate(ctx, "bar")
	if err != nil {
		t.Fatal(err)
		return
	}

	_, _, err = api.Key().Rename(ctx, "foo", "bar")
	if err == nil {
		t.Error("expected error to not be nil")
	} else {
386
		if !strings.Contains(err.Error(), "key by that name already exists, refusing to overwrite") {
387 388 389 390 391
			t.Fatalf("expected error 'key by that name already exists, refusing to overwrite', got '%s'", err.Error())
		}
	}
}

Łukasz Magiera's avatar
Łukasz Magiera committed
392
func (tp *TestSuite) TestRenameOverwrite(t *testing.T) {
393 394
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
395
	api, err := tp.makeAPI(ctx)
396
	if err != nil {
397
		t.Fatal(err)
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
	}

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

	_, err = api.Key().Generate(ctx, "bar")
	if err != nil {
		t.Fatal(err)
		return
	}

	k, overwrote, err := api.Key().Rename(ctx, "foo", "bar", opt.Key.Force(true))
	if err != nil {
		t.Fatal(err)
		return
	}

	if !overwrote {
		t.Error("overwrote should be true")
	}

	if k.Name() != "bar" {
		t.Errorf("returned key should be called 'bar', got '%s'", k.Name())
	}

	if k.Path().String() != kfoo.Path().String() {
		t.Errorf("k and kfoo should have equal paths, '%s'!='%s'", k.Path().String(), kfoo.Path().String())
	}
}

Łukasz Magiera's avatar
Łukasz Magiera committed
431
func (tp *TestSuite) TestRenameSameNameNoForce(t *testing.T) {
432 433
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
434
	api, err := tp.makeAPI(ctx)
435
	if err != nil {
436
		t.Fatal(err)
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
	}

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

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

	if overwrote {
		t.Error("overwrote should be false")
	}

	if k.Name() != "foo" {
		t.Errorf("returned key should be called 'foo', got '%s'", k.Name())
	}
}

Łukasz Magiera's avatar
Łukasz Magiera committed
460
func (tp *TestSuite) TestRenameSameName(t *testing.T) {
461 462
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
463
	api, err := tp.makeAPI(ctx)
464
	if err != nil {
465
		t.Fatal(err)
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
	}

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

	k, overwrote, err := api.Key().Rename(ctx, "foo", "foo", opt.Key.Force(true))
	if err != nil {
		t.Fatal(err)
		return
	}

	if overwrote {
		t.Error("overwrote should be false")
	}

	if k.Name() != "foo" {
		t.Errorf("returned key should be called 'foo', got '%s'", k.Name())
	}
}

Łukasz Magiera's avatar
Łukasz Magiera committed
489
func (tp *TestSuite) TestRemove(t *testing.T) {
490 491
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
492
	api, err := tp.makeAPI(ctx)
493
	if err != nil {
494
		t.Fatal(err)
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
	}

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

	l, err := api.Key().List(ctx)
	if err != nil {
		t.Fatal(err)
		return
	}

	if len(l) != 2 {
		t.Fatalf("expected to get 2 keys, got %d", len(l))
		return
	}

	p, err := api.Key().Remove(ctx, "foo")
	if err != nil {
		t.Fatal(err)
		return
	}

	if k.Path().String() != p.Path().String() {
		t.Errorf("k and p should have equal paths, '%s'!='%s'", k.Path().String(), p.Path().String())
	}

	l, err = api.Key().List(ctx)
	if err != nil {
		t.Fatal(err)
		return
	}

	if len(l) != 1 {
		t.Fatalf("expected to get 1 key, got %d", len(l))
		return
	}

	if l[0].Name() != "self" {
		t.Errorf("expected the key to be called 'self', got '%s'", l[0].Name())
	}
}