Skip to content

Commit 4bce3f4

Browse files
committed
nearly all test coverage
Signed-off-by: hfuss <[email protected]>
1 parent 6e6d0ff commit 4bce3f4

File tree

2 files changed

+90
-4
lines changed

2 files changed

+90
-4
lines changed

internal/dataexchange/ffdx/ffdx.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -519,12 +519,10 @@ func extractSoonestExpiryFromCertBundle(certBundle string) (time.Time, error) {
519519
if block == nil {
520520
break
521521
}
522-
if block.Type != "CERTIFICATE" {
523-
continue
524-
}
522+
525523
cert, err := x509.ParseCertificate(block.Bytes)
526524
if err != nil {
527-
return time.Time{}, fmt.Errorf("failed to parse certificate: %v", err)
525+
return time.Time{}, fmt.Errorf("failed to parse non-certificate within bundle: %v", err)
528526
}
529527
if leafCert == nil || cert.NotAfter.Before(leafCert.NotAfter) {
530528
leafCert = cert

internal/dataexchange/ffdx/ffdx_test.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"bytes"
2121
"context"
2222
"encoding/json"
23+
"errors"
2324
"fmt"
2425
"io/ioutil"
2526
"net/http"
@@ -913,6 +914,9 @@ func TestWebsocketWithEmptyNodesInit(t *testing.T) {
913914
ctx, cancel := context.WithCancel(context.Background())
914915
err := h.Init(ctx, cancel, utConfig, nil)
915916
assert.NoError(t, err)
917+
dxc := &dataexchangemocks.Callbacks{}
918+
h.callbacks = callbacks{handlers: map[string]dataexchange.Callbacks{"ns1": dxc}}
919+
dxc.On("DXConnect", h).Return(nil)
916920

917921
err = h.Start()
918922
assert.NoError(t, err)
@@ -962,6 +966,90 @@ func TestDeleteBlobFail(t *testing.T) {
962966
assert.Regexp(t, "FF10229", err)
963967
}
964968

969+
type mockDXCallbacks struct {
970+
connectCalls int
971+
}
972+
973+
func (m *mockDXCallbacks) DXConnect(plugin dataexchange.Plugin) error {
974+
m.connectCalls++
975+
return errors.New("connect callback failed")
976+
}
977+
978+
func (m *mockDXCallbacks) DXEvent(plugin dataexchange.Plugin, event dataexchange.DXEvent) error {
979+
panic("implement me")
980+
}
981+
982+
func TestWebsocketDXConnectFails(t *testing.T) {
983+
mockedClient := &http.Client{}
984+
httpmock.ActivateNonDefault(mockedClient)
985+
defer httpmock.DeactivateAndReset()
986+
987+
_, _, wsURL, cancel := wsclient.NewTestWSServer(nil)
988+
defer cancel()
989+
990+
u, _ := url.Parse(wsURL)
991+
u.Scheme = "http"
992+
httpURL := u.String()
993+
h := &FFDX{}
994+
995+
coreconfig.Reset()
996+
h.InitConfig(utConfig)
997+
utConfig.Set(ffresty.HTTPConfigURL, httpURL)
998+
utConfig.Set(ffresty.HTTPCustomClient, mockedClient)
999+
utConfig.Set(DataExchangeInitEnabled, true)
1000+
1001+
httpmock.RegisterResponder("POST", fmt.Sprintf("%s/api/v1/init", httpURL),
1002+
func(req *http.Request) (*http.Response, error) {
1003+
var reqNodes []fftypes.JSONObject
1004+
1005+
// we want to make sure when theres are no peer nodes, an empty list is being
1006+
// passed as the req, not "null"
1007+
err := json.NewDecoder(req.Body).Decode(&reqNodes)
1008+
assert.NoError(t, err)
1009+
assert.Empty(t, reqNodes)
1010+
assert.NotNil(t, reqNodes)
1011+
1012+
return httpmock.NewJsonResponse(200, fftypes.JSONObject{
1013+
"status": "ready",
1014+
})
1015+
})
1016+
1017+
h.InitConfig(utConfig)
1018+
ctx, cancel := context.WithCancel(context.Background())
1019+
err := h.Init(ctx, cancel, utConfig, nil)
1020+
assert.NoError(t, err)
1021+
dxc := &mockDXCallbacks{
1022+
connectCalls: 0,
1023+
}
1024+
h.callbacks = callbacks{handlers: map[string]dataexchange.Callbacks{"ns1": dxc}}
1025+
1026+
err = h.Start()
1027+
assert.NoError(t, err)
1028+
1029+
assert.Equal(t, 1, httpmock.GetTotalCallCount())
1030+
assert.True(t, h.initialized)
1031+
assert.Equal(t, 1, dxc.connectCalls)
1032+
}
1033+
1034+
func TestExtractSoonestExpiryFromCertBundleEmpty(t *testing.T) {
1035+
_, err := extractSoonestExpiryFromCertBundle("")
1036+
assert.ErrorContains(t, err, "no valid certificate found")
1037+
1038+
}
1039+
1040+
func TestExtractSoonestExpiryFromCertBundleBadBundle(t *testing.T) {
1041+
nonCertPEMBundle := `
1042+
-----BEGIN NON-CERTIFICATE-----
1043+
MIIDXTCCAkWgAwIBAgIJALa6+u2k5u2kMA0GCSqGSIb3DQEBCwUAMEUxCzAJBgNV
1044+
BAYTAkFVMRMwEQYDVQQIDApxdWVlbnNsYW5kMREwDwYDVQQHDAhCcm9va2ZpZWxk
1045+
-----END NON-CERTIFICATE-----
1046+
`
1047+
1048+
_, err := extractSoonestExpiryFromCertBundle(nonCertPEMBundle)
1049+
assert.ErrorContains(t, err, "failed to parse non-certificate within bundle")
1050+
1051+
}
1052+
9651053
//
9661054
//matchingProfile := fftypes.JSONAnyPtr(fmt.Sprintf(`{"cert": "%s" }`, strings.ReplaceAll(testCertBundle, "\n", `\n`))).JSONObject()
9671055
//

0 commit comments

Comments
 (0)