From 5a19956a963e92fbc43b2959007347a290e7bc8e Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Thu, 27 Jul 2023 12:24:19 +0200 Subject: [PATCH] feat: run tests for both V1+V2 and V2 records --- routing/http/client/client_test.go | 88 ++++++++++--------- routing/http/server/server_test.go | 136 ++++++++++++++++------------- 2 files changed, 123 insertions(+), 101 deletions(-) diff --git a/routing/http/client/client_test.go b/routing/http/client/client_test.go index 47b52417c..c1690b3f2 100644 --- a/routing/http/client/client_test.go +++ b/routing/http/client/client_test.go @@ -467,7 +467,7 @@ 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) @@ -475,7 +475,7 @@ func makeIPNSRecord(t *testing.T, sk crypto.PrivKey) (*ipns.Record, []byte) { 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) @@ -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)) }) } diff --git a/routing/http/server/server_test.go b/routing/http/server/server_test.go index 2a43364e7..bec959cf8 100644 --- a/routing/http/server/server_test.go +++ b/routing/http/server/server_test.go @@ -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) @@ -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) @@ -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)) }) }