Skip to content

Commit

Permalink
Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe committed Apr 11, 2024
1 parent f5a1022 commit ca33f80
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 15 deletions.
12 changes: 7 additions & 5 deletions impl/internal/dht/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,19 @@ func startGetTraversal(
if sha1.Sum(bv) == target {
select {
case vChan <- FullGetResult{
V: rv,
Sig: r.Sig,
V: rv,
Sig: r.Sig,
Mutable: false,
}:
case <-ctx.Done():
}
} else if sha1.Sum(append(r.K[:], salt...)) == target && bep44.Verify(r.K[:], salt, *r.Seq, bv, r.Sig[:]) {
select {
case vChan <- FullGetResult{
Seq: *r.Seq,
V: rv,
Sig: r.Sig,
Seq: *r.Seq,
V: rv,
Sig: r.Sig,
Mutable: true,
}:
case <-ctx.Done():
}
Expand Down
5 changes: 4 additions & 1 deletion impl/internal/did/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"net/http"
"net/url"
"time"

"github.com/TBD54566975/ssi-sdk/did"
"github.com/anacrolix/dht/v2/bep44"
Expand All @@ -24,9 +25,11 @@ func NewGatewayClient(gatewayURL string) (*GatewayClient, error) {
if _, err := url.Parse(gatewayURL); err != nil {
return nil, err
}
client := http.DefaultClient
client.Timeout = time.Second * 10
return &GatewayClient{
gatewayURL: gatewayURL,
client: http.DefaultClient,
client: client,
}, nil
}

Expand Down
11 changes: 11 additions & 0 deletions impl/internal/did/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,17 @@ func TestClient(t *testing.T) {
t.Logf("time to put and get: %s", since)
}

func TestGet(t *testing.T) {
client, err := NewGatewayClient("http://localhost:8305")

require.NoError(t, err)
require.NotNil(t, client)

doc, _, _, err := client.GetDIDDocument("did:dht:uqaj3fcr9db6jg6o9pjs53iuftyj45r46aubogfaceqjbo6pp9sy")
require.NoError(t, err)
require.NotNil(t, doc)
}

func TestClientInvalidGateway(t *testing.T) {
g, err := NewGatewayClient("\n")
assert.Error(t, err)
Expand Down
3 changes: 1 addition & 2 deletions impl/pkg/dht/dht.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ func NewDHT(bootstrapPeers []string) (*DHT, error) {
logrus.WithField("bootstrap_peers", len(bootstrapPeers)).Info("initializing DHT")

c := dht.NewDefaultServerConfig()
// change default expire to 24 hours
c.Exp = time.Hour * 24
c.NoSecurity = false
conn, err := net.ListenPacket("udp", "0.0.0.0:6881")
Expand Down Expand Up @@ -134,7 +133,7 @@ func (d *DHT) GetFull(ctx context.Context, key string) (*dhtint.FullGetResult, e

z32Decoded, err := util.Z32Decode(key)
if err != nil {
return nil, errutil.LoggingCtxErrorMsgf(ctx, err, "failed to decode key [%s]", key)
return nil, errors.Wrapf(err, "failed to decode key [%s]", key)
}
res, t, err := dhtint.Get(ctx, infohash.HashBytes(z32Decoded), d.Server, nil, nil)
if err != nil {
Expand Down
9 changes: 5 additions & 4 deletions impl/pkg/dht/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
)

func init() {
logrus.SetFormatter(&logrus.JSONFormatter{})
log.Default.Handlers = []log.Handler{logrusHandler{}}
}

Expand All @@ -19,12 +20,12 @@ func (logrusHandler) Handle(record log.Record) {

switch record.Level {
case log.Debug:
entry.Debug(msg)
entry.Debugf("%s\n", msg)
case log.Info:
entry.Info(msg)
entry.Infof("%s\n", msg)
case log.Warning, log.Error:
entry.Warn(msg)
entry.Warnf("%s\n", msg)
default:
entry.Debug(msg)
entry.Debugf("%s\n", msg)
}
}
6 changes: 3 additions & 3 deletions impl/pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ func NewServer(cfg *config.Config, shutdown chan os.Signal, d *dht.DHT) (*Server
Server: &http.Server{
Addr: fmt.Sprintf("%s:%d", cfg.ServerConfig.APIHost, cfg.ServerConfig.APIPort),
Handler: handler,
ReadTimeout: time.Second * 15,
ReadHeaderTimeout: time.Second * 15,
WriteTimeout: time.Second * 15,
ReadTimeout: time.Second * 10,
ReadHeaderTimeout: time.Second * 10,
WriteTimeout: time.Second * 10,
},
cfg: cfg,
svc: pkarrService,
Expand Down

0 comments on commit ca33f80

Please sign in to comment.