From 81f4ef5e09b9e4ca9d0feddb0c251afe8836da71 Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Thu, 11 Jun 2020 13:02:11 +0100 Subject: [PATCH] Start testing stuff --- serverkeyapi/serverkeyapi_test.go | 104 ++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 serverkeyapi/serverkeyapi_test.go diff --git a/serverkeyapi/serverkeyapi_test.go b/serverkeyapi/serverkeyapi_test.go new file mode 100644 index 000000000..c53986280 --- /dev/null +++ b/serverkeyapi/serverkeyapi_test.go @@ -0,0 +1,104 @@ +package serverkeyapi + +import ( + "bytes" + "context" + "crypto/ed25519" + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + "testing" + "time" + + "github.com/matrix-org/dendrite/federationapi/routing" + "github.com/matrix-org/dendrite/internal/caching" + "github.com/matrix-org/dendrite/internal/config" + "github.com/matrix-org/dendrite/serverkeyapi/api" + "github.com/matrix-org/gomatrixserverlib" +) + +type server struct { + name gomatrixserverlib.ServerName + config *config.Dendrite + fedclient *gomatrixserverlib.FederationClient + cache *caching.Caches + api api.ServerKeyInternalAPI +} + +var serverA = &server{name: "a.com"} +var serverB = &server{name: "b.com"} +var serverC = &server{name: "c.com"} + +var servers = map[string]*server{ + "a.com": serverA, + "b.com": serverB, + "c.com": serverC, +} + +func TestMain(m *testing.M) { + caches, err := caching.NewInMemoryLRUCache() + if err != nil { + panic("can't create cache: " + err.Error()) + } + + for _, s := range []*server{serverA, serverB, serverC} { + _, testPriv, err := ed25519.GenerateKey(nil) + if err != nil { + panic("can't generate identity key: " + err.Error()) + } + + s.config = &config.Dendrite{} + s.config.SetDefaults() + s.config.Matrix.ServerName = gomatrixserverlib.ServerName(s.name) + s.config.Matrix.PrivateKey = testPriv + s.config.Matrix.KeyID = "ed25519:test" + s.config.Database.ServerKey = config.DataSource("file::memory:") + + transport := &http.Transport{} + transport.RegisterProtocol("matrix", &MockRoundTripper{}) + + s.fedclient = gomatrixserverlib.NewFederationClientWithTransport( + s.config.Matrix.ServerName, "ed25519:test", testPriv, transport, + ) + + s.cache = caches + s.api = NewInternalAPI(s.config, s.fedclient, s.cache) + } +} + +type MockRoundTripper struct{} + +func (m *MockRoundTripper) RoundTrip(req *http.Request) (res *http.Response, err error) { + serv, ok := servers[req.Host] + if !ok { + return nil, fmt.Errorf("server not known: %s", req.Host) + } + + keys := routing.LocalKeys(serv.config).JSON + body, err := json.MarshalIndent(keys, "", " ") + if err != nil { + return nil, err + } + res = &http.Response{ + StatusCode: 200, + Body: ioutil.NopCloser(bytes.NewReader(body)), + } + return +} + +func TestServerKeyAPIDirect(t *testing.T) { + res, err := serverA.api.FetchKeys( + context.Background(), + map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.Timestamp{ + { + ServerName: serverA.name, + KeyID: "ed25519:test", + }: gomatrixserverlib.AsTimestamp(time.Now()), + }, + ) + if err != nil { + t.Fatalf("serverKeyAPI.FetchKeys: %s", err) + } + t.Log(res) +}