|
| 1 | +package coreapi_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "io" |
| 6 | + "math/rand" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + ipath "github.com/ipfs/go-ipfs/path" |
| 11 | + |
| 12 | + coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface" |
| 13 | +) |
| 14 | + |
| 15 | +var rnd = rand.New(rand.NewSource(0x62796532303137)) |
| 16 | + |
| 17 | +func addTestObject(ctx context.Context, api coreiface.CoreAPI) (coreiface.Path, error) { |
| 18 | + return api.Unixfs().Add(ctx, &io.LimitedReader{R: rnd, N: 4092}) |
| 19 | +} |
| 20 | + |
| 21 | +func TestBasicPublishResolve(t *testing.T) { |
| 22 | + ctx := context.Background() |
| 23 | + n, api, err := makeAPIIdent(ctx, true) |
| 24 | + if err != nil { |
| 25 | + t.Fatal(err) |
| 26 | + return |
| 27 | + } |
| 28 | + |
| 29 | + p, err := addTestObject(ctx, api) |
| 30 | + if err != nil { |
| 31 | + t.Fatal(err) |
| 32 | + return |
| 33 | + } |
| 34 | + |
| 35 | + e, err := api.Name().Publish(ctx, p) |
| 36 | + if err != nil { |
| 37 | + t.Fatal(err) |
| 38 | + return |
| 39 | + } |
| 40 | + |
| 41 | + if e.Name() != n.Identity.Pretty() { |
| 42 | + t.Errorf("expected e.Name to equal '%s', got '%s'", n.Identity.Pretty(), e.Name()) |
| 43 | + } |
| 44 | + |
| 45 | + if e.Value().String() != p.String() { |
| 46 | + t.Errorf("expected paths to match, '%s'!='%s'", e.Value().String(), p.String()) |
| 47 | + } |
| 48 | + |
| 49 | + resPath, err := api.Name().Resolve(ctx, e.Name()) |
| 50 | + if err != nil { |
| 51 | + t.Fatal(err) |
| 52 | + return |
| 53 | + } |
| 54 | + |
| 55 | + if resPath.String() != p.String() { |
| 56 | + t.Errorf("expected paths to match, '%s'!='%s'", resPath.String(), p.String()) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func TestBasicPublishResolveKey(t *testing.T) { |
| 61 | + ctx := context.Background() |
| 62 | + _, api, err := makeAPIIdent(ctx, true) |
| 63 | + if err != nil { |
| 64 | + t.Fatal(err) |
| 65 | + return |
| 66 | + } |
| 67 | + |
| 68 | + k, err := api.Key().Generate(ctx, "foo") |
| 69 | + if err != nil { |
| 70 | + t.Fatal(err) |
| 71 | + return |
| 72 | + } |
| 73 | + |
| 74 | + p, err := addTestObject(ctx, api) |
| 75 | + if err != nil { |
| 76 | + t.Fatal(err) |
| 77 | + return |
| 78 | + } |
| 79 | + |
| 80 | + e, err := api.Name().Publish(ctx, p, api.Name().WithKey(k.Name())) |
| 81 | + if err != nil { |
| 82 | + t.Fatal(err) |
| 83 | + return |
| 84 | + } |
| 85 | + |
| 86 | + if ipath.Join([]string{"/ipns", e.Name()}) != k.Path().String() { |
| 87 | + t.Errorf("expected e.Name to equal '%s', got '%s'", e.Name(), k.Path().String()) |
| 88 | + } |
| 89 | + |
| 90 | + if e.Value().String() != p.String() { |
| 91 | + t.Errorf("expected paths to match, '%s'!='%s'", e.Value().String(), p.String()) |
| 92 | + } |
| 93 | + |
| 94 | + resPath, err := api.Name().Resolve(ctx, e.Name()) |
| 95 | + if err != nil { |
| 96 | + t.Fatal(err) |
| 97 | + return |
| 98 | + } |
| 99 | + |
| 100 | + if resPath.String() != p.String() { |
| 101 | + t.Errorf("expected paths to match, '%s'!='%s'", resPath.String(), p.String()) |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +func TestBasicPublishResolveTimeout(t *testing.T) { |
| 106 | + t.Skip("ValidTime doesn't appear to work at this time resolution") |
| 107 | + |
| 108 | + ctx := context.Background() |
| 109 | + n, api, err := makeAPIIdent(ctx, true) |
| 110 | + if err != nil { |
| 111 | + t.Fatal(err) |
| 112 | + return |
| 113 | + } |
| 114 | + |
| 115 | + p, err := addTestObject(ctx, api) |
| 116 | + if err != nil { |
| 117 | + t.Fatal(err) |
| 118 | + return |
| 119 | + } |
| 120 | + |
| 121 | + e, err := api.Name().Publish(ctx, p, api.Name().WithValidTime(time.Millisecond*100)) |
| 122 | + if err != nil { |
| 123 | + t.Fatal(err) |
| 124 | + return |
| 125 | + } |
| 126 | + |
| 127 | + if e.Name() != n.Identity.Pretty() { |
| 128 | + t.Errorf("expected e.Name to equal '%s', got '%s'", n.Identity.Pretty(), e.Name()) |
| 129 | + } |
| 130 | + |
| 131 | + if e.Value().String() != p.String() { |
| 132 | + t.Errorf("expected paths to match, '%s'!='%s'", e.Value().String(), p.String()) |
| 133 | + } |
| 134 | + |
| 135 | + time.Sleep(time.Second) |
| 136 | + |
| 137 | + _, err = api.Name().Resolve(ctx, e.Name()) |
| 138 | + if err == nil { |
| 139 | + t.Fatal("Expected an error") |
| 140 | + return |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +//TODO: When swarm api is created, add multinode tests |
0 commit comments