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

import (
	"context"
	"strings"
	"testing"

Łukasz Magiera's avatar
Łukasz Magiera committed
8 9
	"github.com/ipfs/interface-go-ipfs-core"
	opt "github.com/ipfs/interface-go-ipfs-core/options"
10 11
)

12
func (tp *provider) TestKey(t *testing.T) {
13 14 15 16 17 18 19
	tp.hasApi(t, func(api iface.CoreAPI) error {
		if api.Key() == nil {
			return apiNotImplemented
		}
		return nil
	})

20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
	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)
36 37
}

38
func (tp *provider) TestListSelf(t *testing.T) {
39 40
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
41
	api, err := tp.makeAPI(ctx)
42 43 44 45 46
	if err != nil {
		t.Fatal(err)
		return
	}

47 48 49 50 51
	self, err := api.Key().Self(ctx)
	if err != nil {
		t.Fatal(err)
	}

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
	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())
	}

67 68
	if keys[0].Path().String() != "/ipns/"+self.ID().Pretty() {
		t.Errorf("expected the key to have path '/ipns/%s', got '%s'", self.ID().Pretty(), keys[0].Path().String())
69 70 71
	}
}

72
func (tp *provider) TestRenameSelf(t *testing.T) {
73 74
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
75
	api, err := tp.makeAPI(ctx)
76 77 78 79 80 81 82 83 84
	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 {
85
		if !strings.Contains(err.Error(), "cannot rename key with name 'self'") {
86 87 88 89 90 91 92 93
			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 {
94
		if !strings.Contains(err.Error(), "cannot rename key with name 'self'") {
95 96 97 98 99
			t.Fatalf("expected error 'cannot rename key with name 'self'', got '%s'", err.Error())
		}
	}
}

100
func (tp *provider) TestRemoveSelf(t *testing.T) {
101 102
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
103
	api, err := tp.makeAPI(ctx)
104 105 106 107 108 109 110 111 112
	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 {
113
		if !strings.Contains(err.Error(), "cannot remove key with name 'self'") {
114 115 116 117 118
			t.Fatalf("expected error 'cannot remove key with name 'self'', got '%s'", err.Error())
		}
	}
}

119
func (tp *provider) TestGenerate(t *testing.T) {
120 121
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
122
	api, err := tp.makeAPI(ctx)
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
	if err != nil {
		t.Error(err)
	}

	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())
	}

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

142
func (tp *provider) TestGenerateSize(t *testing.T) {
143 144
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
145
	api, err := tp.makeAPI(ctx)
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
	if err != nil {
		t.Error(err)
	}

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

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

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

165
func (tp *provider) TestGenerateType(t *testing.T) {
166 167
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
168 169
	t.Skip("disabled until libp2p/specs#111 is fixed")

170
	api, err := tp.makeAPI(ctx)
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
	if err != nil {
		t.Error(err)
	}

	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())
	}
}

191
func (tp *provider) TestGenerateExisting(t *testing.T) {
192 193
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
194
	api, err := tp.makeAPI(ctx)
195 196 197 198 199 200 201 202 203 204 205 206 207 208
	if err != nil {
		t.Error(err)
	}

	_, 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 {
209
		if !strings.Contains(err.Error(), "key with name 'foo' already exists") {
210 211 212 213 214 215 216 217
			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 {
218
		if !strings.Contains(err.Error(), "cannot create key with name 'self'") {
219 220 221 222 223
			t.Fatalf("expected error 'cannot create key with name 'self'', got '%s'", err.Error())
		}
	}
}

224
func (tp *provider) TestList(t *testing.T) {
225 226
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
227
	api, err := tp.makeAPI(ctx)
228 229 230 231 232 233 234 235 236 237 238 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
	if err != nil {
		t.Error(err)
	}

	_, 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
	}

	if !strings.HasPrefix(l[0].Path().String(), "/ipns/Qm") {
		t.Fatalf("expected key 0 to be prefixed with '/ipns/Qm', got '%s'", l[0].Name())
		return
	}

	if !strings.HasPrefix(l[1].Path().String(), "/ipns/Qm") {
		t.Fatalf("expected key 1 to be prefixed with '/ipns/Qm', got '%s'", l[1].Name())
		return
	}
}

270
func (tp *provider) TestRename(t *testing.T) {
271 272
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
273
	api, err := tp.makeAPI(ctx)
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
	if err != nil {
		t.Error(err)
	}

	_, 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())
	}
}

299
func (tp *provider) TestRenameToSelf(t *testing.T) {
300 301
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
302
	api, err := tp.makeAPI(ctx)
303 304 305 306 307 308 309 310 311 312 313 314 315 316
	if err != nil {
		t.Error(err)
	}

	_, 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 {
317
		if !strings.Contains(err.Error(), "cannot overwrite key with name 'self'") {
318 319 320 321 322
			t.Fatalf("expected error 'cannot overwrite key with name 'self'', got '%s'", err.Error())
		}
	}
}

323
func (tp *provider) TestRenameToSelfForce(t *testing.T) {
324 325
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
326
	api, err := tp.makeAPI(ctx)
327 328 329 330 331 332 333 334 335 336 337 338 339 340
	if err != nil {
		t.Error(err)
	}

	_, 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 {
341
		if !strings.Contains(err.Error(), "cannot overwrite key with name 'self'") {
342 343 344 345 346
			t.Fatalf("expected error 'cannot overwrite key with name 'self'', got '%s'", err.Error())
		}
	}
}

347
func (tp *provider) TestRenameOverwriteNoForce(t *testing.T) {
348 349
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
350
	api, err := tp.makeAPI(ctx)
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
	if err != nil {
		t.Error(err)
	}

	_, 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 {
371
		if !strings.Contains(err.Error(), "key by that name already exists, refusing to overwrite") {
372 373 374 375 376
			t.Fatalf("expected error 'key by that name already exists, refusing to overwrite', got '%s'", err.Error())
		}
	}
}

377
func (tp *provider) TestRenameOverwrite(t *testing.T) {
378 379
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
380
	api, err := tp.makeAPI(ctx)
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
	if err != nil {
		t.Error(err)
	}

	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())
	}
}

416
func (tp *provider) TestRenameSameNameNoForce(t *testing.T) {
417 418
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
419
	api, err := tp.makeAPI(ctx)
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
	if err != nil {
		t.Error(err)
	}

	_, 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())
	}
}

445
func (tp *provider) TestRenameSameName(t *testing.T) {
446 447
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
448
	api, err := tp.makeAPI(ctx)
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
	if err != nil {
		t.Error(err)
	}

	_, 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())
	}
}

474
func (tp *provider) TestRemove(t *testing.T) {
475 476
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
477
	api, err := tp.makeAPI(ctx)
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 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
	if err != nil {
		t.Error(err)
	}

	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())
	}
}