|
| 1 | +package offline |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "github.com/ipfs/go-ipfs/thirdparty/testutil" |
| 7 | + ds "gx/ipfs/QmRWDav6mzWseLWeYfVd5fvUKiVe9xNH29YfMF438fG364/go-datastore" |
| 8 | + "testing" |
| 9 | +) |
| 10 | + |
| 11 | +func TestOfflineRouterStorage(t *testing.T) { |
| 12 | + ctx := context.Background() |
| 13 | + |
| 14 | + nds := ds.NewMapDatastore() |
| 15 | + privkey, _, _ := testutil.RandTestKeyPair(128) |
| 16 | + offline := NewOfflineRouter(nds, privkey) |
| 17 | + |
| 18 | + err := offline.PutValue(ctx, "key", []byte("testing 1 2 3")) |
| 19 | + if err != nil { |
| 20 | + t.Fatal(err) |
| 21 | + } |
| 22 | + |
| 23 | + val, err := offline.GetValue(ctx, "key") |
| 24 | + if !bytes.Equal([]byte("testing 1 2 3"), val) { |
| 25 | + t.Fatal("OfflineRouter does not properly store") |
| 26 | + } |
| 27 | + |
| 28 | + val, err = offline.GetValue(ctx, "notHere") |
| 29 | + if err == nil { |
| 30 | + t.Fatal("Router should throw errors for unfound records") |
| 31 | + } |
| 32 | + |
| 33 | + recVal, err := offline.GetValues(ctx, "key", 0) |
| 34 | + if err != nil { |
| 35 | + t.Fatal(err) |
| 36 | + } |
| 37 | + |
| 38 | + _, err = offline.GetValues(ctx, "notHere", 0) |
| 39 | + if err == nil { |
| 40 | + t.Fatal("Router should throw errors for unfound records") |
| 41 | + } |
| 42 | + |
| 43 | + local := recVal[0].Val |
| 44 | + if !bytes.Equal([]byte("testing 1 2 3"), local) { |
| 45 | + t.Fatal("OfflineRouter does not properly store") |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +func TestOfflineRouterLocal(t *testing.T) { |
| 50 | + ctx := context.Background() |
| 51 | + |
| 52 | + nds := ds.NewMapDatastore() |
| 53 | + privkey, _, _ := testutil.RandTestKeyPair(128) |
| 54 | + offline := NewOfflineRouter(nds, privkey) |
| 55 | + |
| 56 | + id, _ := testutil.RandPeerID() |
| 57 | + _, err := offline.FindPeer(ctx, id) |
| 58 | + if err != ErrOffline { |
| 59 | + t.Fatal("OfflineRouting should alert that its offline") |
| 60 | + } |
| 61 | + |
| 62 | + cid, _ := testutil.RandCidV0() |
| 63 | + pChan := offline.FindProvidersAsync(ctx, cid, 1) |
| 64 | + p, ok := <-pChan |
| 65 | + if ok { |
| 66 | + t.Fatalf("FindProvidersAsync did not return a closed channel. Instead we got %+v !", p) |
| 67 | + } |
| 68 | + |
| 69 | + cid, _ = testutil.RandCidV0() |
| 70 | + err = offline.Provide(ctx, cid) |
| 71 | + if err != ErrOffline { |
| 72 | + t.Fatal("OfflineRouting should alert that its offline") |
| 73 | + } |
| 74 | + |
| 75 | + err = offline.Bootstrap(ctx) |
| 76 | + if err != nil { |
| 77 | + t.Fatal("You shouldn't be able to bootstrap offline routing.") |
| 78 | + } |
| 79 | +} |
0 commit comments