Skip to content

Commit

Permalink
feat: run tests for both V1+V2 and V2 records
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Jul 27, 2023
1 parent edb8c96 commit 5a19956
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 101 deletions.
88 changes: 49 additions & 39 deletions routing/http/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,15 +467,15 @@ func makeName(t *testing.T) (crypto.PrivKey, ipns.Name) {
return sk, ipns.NameFromPeer(pid)
}

func makeIPNSRecord(t *testing.T, sk crypto.PrivKey) (*ipns.Record, []byte) {
func makeIPNSRecord(t *testing.T, sk crypto.PrivKey, opts ...ipns.Option) (*ipns.Record, []byte) {
cid, err := cid.Decode("bafkreifjjcie6lypi6ny7amxnfftagclbuxndqonfipmb64f2km2devei4")
require.NoError(t, err)

path := path.IpfsPath(cid)
eol := time.Now().Add(time.Hour * 48)
ttl := time.Second * 20

record, err := ipns.NewRecord(sk, ipfspath.FromString(path.String()), 1, eol, ttl)
record, err := ipns.NewRecord(sk, ipfspath.FromString(path.String()), 1, eol, ttl, opts...)
require.NoError(t, err)

rawRecord, err := ipns.MarshalRecord(record)
Expand All @@ -485,62 +485,72 @@ func makeIPNSRecord(t *testing.T, sk crypto.PrivKey) (*ipns.Record, []byte) {
}

func TestClient_IPNS(t *testing.T) {
t.Run("Find IPNS Record", func(t *testing.T) {
sk, name := makeName(t)
record, _ := makeIPNSRecord(t, sk)
t.Run("Find IPNS Record returns error if server errors", func(t *testing.T) {
_, name := makeName(t)

deps := makeTestDeps(t, nil, nil)
client := deps.client
router := deps.router

router.On("FindIPNSRecord", mock.Anything, name).Return(record, nil)
router.On("FindIPNSRecord", mock.Anything, name).Return(nil, errors.New("something wrong happened"))

receivedRecord, err := client.FindIPNSRecord(context.Background(), name)
require.NoError(t, err)
require.Equal(t, record, receivedRecord)
require.Error(t, err)
require.Nil(t, receivedRecord)
})

t.Run("Find IPNS Record returns error if server sends bad data", func(t *testing.T) {
sk, _ := makeName(t)
record, _ := makeIPNSRecord(t, sk)
_, name2 := makeName(t)
runWithRecordOptions := func(t *testing.T, opts ...ipns.Option) {
t.Run("Find IPNS Record", func(t *testing.T) {
sk, name := makeName(t)
record, _ := makeIPNSRecord(t, sk, opts...)

deps := makeTestDeps(t, nil, nil)
client := deps.client
router := deps.router
deps := makeTestDeps(t, nil, nil)
client := deps.client
router := deps.router

router.On("FindIPNSRecord", mock.Anything, name2).Return(record, nil)
router.On("FindIPNSRecord", mock.Anything, name).Return(record, nil)

receivedRecord, err := client.FindIPNSRecord(context.Background(), name2)
require.Error(t, err)
require.Nil(t, receivedRecord)
})
receivedRecord, err := client.FindIPNSRecord(context.Background(), name)
require.NoError(t, err)
require.Equal(t, record, receivedRecord)
})

t.Run("Find IPNS Record returns error if server errors", func(t *testing.T) {
_, name := makeName(t)
t.Run("Find IPNS Record returns error if server sends bad data", func(t *testing.T) {
sk, _ := makeName(t)
record, _ := makeIPNSRecord(t, sk, opts...)
_, name2 := makeName(t)

deps := makeTestDeps(t, nil, nil)
client := deps.client
router := deps.router
deps := makeTestDeps(t, nil, nil)
client := deps.client
router := deps.router

router.On("FindIPNSRecord", mock.Anything, name).Return(nil, errors.New("something wrong happened"))
router.On("FindIPNSRecord", mock.Anything, name2).Return(record, nil)

receivedRecord, err := client.FindIPNSRecord(context.Background(), name)
require.Error(t, err)
require.Nil(t, receivedRecord)
})
receivedRecord, err := client.FindIPNSRecord(context.Background(), name2)
require.Error(t, err)
require.Nil(t, receivedRecord)
})

t.Run("Provide IPNS Record", func(t *testing.T) {
sk, name := makeName(t)
record, _ := makeIPNSRecord(t, sk)
t.Run("Provide IPNS Record", func(t *testing.T) {
sk, name := makeName(t)
record, _ := makeIPNSRecord(t, sk, opts...)

deps := makeTestDeps(t, nil, nil)
client := deps.client
router := deps.router
deps := makeTestDeps(t, nil, nil)
client := deps.client
router := deps.router

router.On("ProvideIPNSRecord", mock.Anything, name, record).Return(nil)
router.On("ProvideIPNSRecord", mock.Anything, name, record).Return(nil)

err := client.ProvideIPNSRecord(context.Background(), name, record)
require.NoError(t, err)
})
}

t.Run("V1+V2 IPNS Records", func(t *testing.T) {
runWithRecordOptions(t, ipns.WithV1Compatibility(true))
})

err := client.ProvideIPNSRecord(context.Background(), name, record)
require.NoError(t, err)
t.Run("V2 IPNS Records", func(t *testing.T) {
runWithRecordOptions(t, ipns.WithV1Compatibility(false))
})
}
136 changes: 74 additions & 62 deletions routing/http/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,12 @@ func makeName(t *testing.T) (crypto.PrivKey, ipns.Name) {
return sk, ipns.NameFromPeer(pid)
}

func makeIPNSRecord(t *testing.T, cid cid.Cid, sk crypto.PrivKey) (*ipns.Record, []byte) {
func makeIPNSRecord(t *testing.T, cid cid.Cid, sk crypto.PrivKey, opts ...ipns.Option) (*ipns.Record, []byte) {
path := path.IpfsPath(cid)
eol := time.Now().Add(time.Hour * 48)
ttl := time.Second * 20

record, err := ipns.NewRecord(sk, ipfspath.FromString(path.String()), 1, eol, ttl)
record, err := ipns.NewRecord(sk, ipfspath.FromString(path.String()), 1, eol, ttl, opts...)
require.NoError(t, err)

rawRecord, err := ipns.MarshalRecord(record)
Expand All @@ -151,11 +151,6 @@ func TestIPNS(t *testing.T) {
cid1, err := cid.Decode("bafkreifjjcie6lypi6ny7amxnfftagclbuxndqonfipmb64f2km2devei4")
require.NoError(t, err)

sk, name1 := makeName(t)
record1, rawRecord1 := makeIPNSRecord(t, cid1, sk)

_, name2 := makeName(t)

makeRequest := func(t *testing.T, router *mockContentRouter, path string) *http.Response {
server := httptest.NewServer(Handler(router))
t.Cleanup(server.Close)
Expand All @@ -169,77 +164,94 @@ func TestIPNS(t *testing.T) {
return resp
}

t.Run("GET /routing/v1/ipns/{cid-peer-id} returns 200", func(t *testing.T) {
t.Parallel()
runWithRecordOptions := func(t *testing.T, opts ...ipns.Option) {
sk, name1 := makeName(t)
record1, rawRecord1 := makeIPNSRecord(t, cid1, sk)

rec, err := ipns.UnmarshalRecord(rawRecord1)
require.NoError(t, err)
_, name2 := makeName(t)

router := &mockContentRouter{}
router.On("FindIPNSRecord", mock.Anything, name1).Return(rec, nil)
t.Run("GET /routing/v1/ipns/{cid-peer-id} returns 200", func(t *testing.T) {
t.Parallel()

resp := makeRequest(t, router, "/routing/v1/ipns/"+name1.String())
require.Equal(t, 200, resp.StatusCode)
require.Equal(t, mediaTypeIPNSRecord, resp.Header.Get("Content-Type"))
require.NotEmpty(t, resp.Header.Get("Etag"))
require.Equal(t, "max-age=20", resp.Header.Get("Cache-Control"))
rec, err := ipns.UnmarshalRecord(rawRecord1)
require.NoError(t, err)

body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
require.Equal(t, body, rawRecord1)
})
router := &mockContentRouter{}
router.On("FindIPNSRecord", mock.Anything, name1).Return(rec, nil)

t.Run("GET /routing/v1/ipns/{non-peer-cid} returns 400", func(t *testing.T) {
t.Parallel()
router := &mockContentRouter{}
resp := makeRequest(t, router, "/routing/v1/ipns/"+cid1.String())
require.Equal(t, 400, resp.StatusCode)
})
resp := makeRequest(t, router, "/routing/v1/ipns/"+name1.String())
require.Equal(t, 200, resp.StatusCode)
require.Equal(t, mediaTypeIPNSRecord, resp.Header.Get("Content-Type"))
require.NotEmpty(t, resp.Header.Get("Etag"))
require.Equal(t, "max-age=20", resp.Header.Get("Cache-Control"))

t.Run("GET /routing/v1/ipns/{peer-id} returns 400", func(t *testing.T) {
t.Parallel()
router := &mockContentRouter{}
resp := makeRequest(t, router, "/routing/v1/ipns/"+name1.Peer().String())
require.Equal(t, 400, resp.StatusCode)
})
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
require.Equal(t, body, rawRecord1)
})

t.Run("PUT /routing/v1/ipns/{cid-peer-id} returns 200", func(t *testing.T) {
t.Parallel()
t.Run("GET /routing/v1/ipns/{non-peer-cid} returns 400", func(t *testing.T) {
t.Parallel()

router := &mockContentRouter{}
router.On("ProvideIPNSRecord", mock.Anything, name1, record1).Return(nil)
router := &mockContentRouter{}
resp := makeRequest(t, router, "/routing/v1/ipns/"+cid1.String())
require.Equal(t, 400, resp.StatusCode)
})

server := httptest.NewServer(Handler(router))
t.Cleanup(server.Close)
serverAddr := "http://" + server.Listener.Addr().String()
urlStr := serverAddr + "/routing/v1/ipns/" + name1.String()
t.Run("GET /routing/v1/ipns/{peer-id} returns 400", func(t *testing.T) {
t.Parallel()

req, err := http.NewRequest(http.MethodPut, urlStr, bytes.NewReader(rawRecord1))
require.NoError(t, err)
req.Header.Set("Content-Type", mediaTypeIPNSRecord)
router := &mockContentRouter{}
resp := makeRequest(t, router, "/routing/v1/ipns/"+name1.Peer().String())
require.Equal(t, 400, resp.StatusCode)
})

resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
require.Equal(t, 200, resp.StatusCode)
})
t.Run("PUT /routing/v1/ipns/{cid-peer-id} returns 200", func(t *testing.T) {
t.Parallel()

t.Run("PUT /routing/v1/ipns/{cid-peer-id} returns 400 for wrong record", func(t *testing.T) {
t.Parallel()
router := &mockContentRouter{}
router.On("ProvideIPNSRecord", mock.Anything, name1, record1).Return(nil)

router := &mockContentRouter{}
server := httptest.NewServer(Handler(router))
t.Cleanup(server.Close)
serverAddr := "http://" + server.Listener.Addr().String()
urlStr := serverAddr + "/routing/v1/ipns/" + name1.String()

server := httptest.NewServer(Handler(router))
t.Cleanup(server.Close)
serverAddr := "http://" + server.Listener.Addr().String()
urlStr := serverAddr + "/routing/v1/ipns/" + name2.String()
req, err := http.NewRequest(http.MethodPut, urlStr, bytes.NewReader(rawRecord1))
require.NoError(t, err)
req.Header.Set("Content-Type", mediaTypeIPNSRecord)

req, err := http.NewRequest(http.MethodPut, urlStr, bytes.NewReader(rawRecord1))
require.NoError(t, err)
req.Header.Set("Content-Type", mediaTypeIPNSRecord)
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
require.Equal(t, 200, resp.StatusCode)
})

resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
require.Equal(t, 400, resp.StatusCode)
t.Run("PUT /routing/v1/ipns/{cid-peer-id} returns 400 for wrong record", func(t *testing.T) {
t.Parallel()

router := &mockContentRouter{}

server := httptest.NewServer(Handler(router))
t.Cleanup(server.Close)
serverAddr := "http://" + server.Listener.Addr().String()
urlStr := serverAddr + "/routing/v1/ipns/" + name2.String()

req, err := http.NewRequest(http.MethodPut, urlStr, bytes.NewReader(rawRecord1))
require.NoError(t, err)
req.Header.Set("Content-Type", mediaTypeIPNSRecord)

resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
require.Equal(t, 400, resp.StatusCode)
})
}

t.Run("V1+V2 IPNS Records", func(t *testing.T) {
runWithRecordOptions(t, ipns.WithV1Compatibility(true))
})

t.Run("V2 IPNS Records", func(t *testing.T) {
runWithRecordOptions(t, ipns.WithV1Compatibility(false))
})
}

Expand Down

0 comments on commit 5a19956

Please sign in to comment.