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
)

Łukasz Magiera's avatar
Łukasz Magiera committed
12
func (tp *TestSuite) 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
}

Łukasz Magiera's avatar
Łukasz Magiera committed
38
func (tp *TestSuite) 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
	}
}

Łukasz Magiera's avatar
Łukasz Magiera committed
72
func (tp *TestSuite) 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())
		}
	}
}

Łukasz Magiera's avatar
Łukasz Magiera committed
100
func (tp *TestSuite) 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())
		}
	}
}

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

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

Łukasz Magiera's avatar
Łukasz Magiera committed
142
func (tp *TestSuite) TestGenerateSize(t *testing.T) {
143 144
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
145
	api, err := tp.makeAPI(ctx)
146
	if err != nil {
147
		t.Fatal(err)
148 149
	}

150
	k, err := api.Key().Generate(ctx, "foo", opt.Key.Size(2048))
151 152 153 154 155 156 157 158 159 160 161 162 163 164
	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())
	}
}

Łukasz Magiera's avatar
Łukasz Magiera committed
165
func (tp *TestSuite) 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
	if err != nil {
172
		t.Fatal(err)
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
	}

	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
191
func (tp *TestSuite) TestGenerateExisting(t *testing.T) {
192 193
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
194
	api, err := tp.makeAPI(ctx)
195
	if err != nil {
196
		t.Fatal(err)
197 198 199 200 201 202 203 204 205 206 207 208
	}

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

Łukasz Magiera's avatar
Łukasz Magiera committed
224
func (tp *TestSuite) TestList(t *testing.T) {
225 226
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
227
	api, err := tp.makeAPI(ctx)
228
	if err != nil {
229
		t.Fatal(err)
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
	}

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

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

	_, 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
299
func (tp *TestSuite) TestRenameToSelf(t *testing.T) {
300 301
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
302
	api, err := tp.makeAPI(ctx)
303
	if err != nil {
304
		t.Fatal(err)
305 306 307 308 309 310 311 312 313 314 315 316
	}

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

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

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

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

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

Łukasz Magiera's avatar
Łukasz Magiera committed
377
func (tp *TestSuite) TestRenameOverwrite(t *testing.T) {
378 379
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
380
	api, err := tp.makeAPI(ctx)
381
	if err != nil {
382
		t.Fatal(err)
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
	}

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

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

	_, 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
474
func (tp *TestSuite) TestRemove(t *testing.T) {
475 476
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
477
	api, err := tp.makeAPI(ctx)
478
	if err != nil {
479
		t.Fatal(err)
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
	}

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