logging_test.go 1.94 KB
Newer Older
Steven Allen's avatar
Steven Allen committed
1 2 3 4 5 6 7 8
package dht

import (
	"testing"

	cid "github.com/ipfs/go-cid"
)

Adin Schmahmann's avatar
Adin Schmahmann committed
9
func TestLoggableRecordKey(t *testing.T) {
Steven Allen's avatar
Steven Allen committed
10 11 12 13 14
	c, err := cid.Decode("QmfUvYQhL2GinafMbPDYz7VFoZv4iiuLuR33aRsPurXGag")
	if err != nil {
		t.Fatal(err)
	}

Adin Schmahmann's avatar
Adin Schmahmann committed
15
	k, err := tryFormatLoggableRecordKey("/proto/" + string(c.Bytes()))
Steven Allen's avatar
Steven Allen committed
16
	if err != nil {
Adin Schmahmann's avatar
Adin Schmahmann committed
17
		t.Errorf("failed to format key: %s", err)
Steven Allen's avatar
Steven Allen committed
18
	}
19
	if k != "/proto/"+lowercaseB32Encode(c.Bytes()) {
Steven Allen's avatar
Steven Allen committed
20 21 22
		t.Error("expected path to be preserved as a loggable key")
	}

Adin Schmahmann's avatar
Adin Schmahmann committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
	for _, s := range []string{"/bla", "", "bla bla"} {
		if _, err := tryFormatLoggableRecordKey(s); err == nil {
			t.Errorf("expected to fail formatting: %s", s)
		}
	}

	for _, s := range []string{"/bla/asdf", "/a/b/c"} {
		if _, err := tryFormatLoggableRecordKey(s); err != nil {
			t.Errorf("expected to be formatable: %s", s)
		}
	}
}

func TestLoggableProviderKey(t *testing.T) {
	c0, err := cid.Decode("QmfUvYQhL2GinafMbPDYz7VFoZv4iiuLuR33aRsPurXGag")
Steven Allen's avatar
Steven Allen committed
38
	if err != nil {
Adin Schmahmann's avatar
Adin Schmahmann committed
39
		t.Fatal(err)
Steven Allen's avatar
Steven Allen committed
40
	}
Adin Schmahmann's avatar
Adin Schmahmann committed
41 42

	// Test logging CIDv0 provider
43 44
	b32MH := lowercaseB32Encode(c0.Hash())
	k, err := tryFormatLoggableProviderKey(c0.Bytes())
Adin Schmahmann's avatar
Adin Schmahmann committed
45 46 47
	if err != nil {
		t.Errorf("failed to format key: %s", err)
	}
48 49
	if k != b32MH {
		t.Error("expected cidv0 to be converted into base32 multihash")
Steven Allen's avatar
Steven Allen committed
50 51
	}

Adin Schmahmann's avatar
Adin Schmahmann committed
52 53
	// Test logging CIDv1 provider (from older DHT implementations)
	c1 := cid.NewCidV1(cid.DagProtobuf, c0.Hash())
54
	k, err = tryFormatLoggableProviderKey(c1.Hash())
Adin Schmahmann's avatar
Adin Schmahmann committed
55 56 57
	if err != nil {
		t.Errorf("failed to format key: %s", err)
	}
58 59
	if k != b32MH {
		t.Error("expected cidv1 to be converted into base32 multihash")
Steven Allen's avatar
Steven Allen committed
60
	}
Adin Schmahmann's avatar
Adin Schmahmann committed
61

Adin Schmahmann's avatar
Adin Schmahmann committed
62
	// Test logging multihash provider
63
	k, err = tryFormatLoggableProviderKey(c1.Hash())
Adin Schmahmann's avatar
Adin Schmahmann committed
64 65 66
	if err != nil {
		t.Errorf("failed to format key: %s", err)
	}
67 68
	if k != b32MH {
		t.Error("expected multihash to be displayed in base32")
Adin Schmahmann's avatar
Adin Schmahmann committed
69 70 71
	}

	for _, s := range []string{"/bla", "", "bla bla", "/bla/asdf", "/a/b/c"} {
72
		if _, err := tryFormatLoggableProviderKey([]byte(s)); err == nil {
Adin Schmahmann's avatar
Adin Schmahmann committed
73
			t.Errorf("expected to fail formatting: %s", s)
Adin Schmahmann's avatar
Adin Schmahmann committed
74 75
		}
	}
Steven Allen's avatar
Steven Allen committed
76
}