2023-01-23 17:55:12 +00:00
|
|
|
// Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package routing_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
|
|
|
"github.com/matrix-org/dendrite/relayapi/internal"
|
|
|
|
"github.com/matrix-org/dendrite/relayapi/routing"
|
|
|
|
"github.com/matrix-org/dendrite/relayapi/storage/shared"
|
|
|
|
"github.com/matrix-org/dendrite/test"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2023-04-06 09:55:01 +01:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/fclient"
|
2023-04-19 15:50:33 +01:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/spec"
|
2023-01-23 17:55:12 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func createQuery(
|
2023-04-19 15:50:33 +01:00
|
|
|
userID spec.UserID,
|
2023-04-06 09:55:01 +01:00
|
|
|
prevEntry fclient.RelayEntry,
|
2023-04-19 15:50:33 +01:00
|
|
|
) fclient.FederationRequest {
|
2023-01-23 17:55:12 +00:00
|
|
|
var federationPathPrefixV1 = "/_matrix/federation/v1"
|
2023-04-28 20:31:21 +01:00
|
|
|
path := federationPathPrefixV1 + "/relay_txn/" + userID.String()
|
2023-04-19 15:50:33 +01:00
|
|
|
request := fclient.NewFederationRequest("GET", userID.Domain(), "relay", path)
|
2023-01-23 17:55:12 +00:00
|
|
|
request.SetContent(prevEntry)
|
|
|
|
|
|
|
|
return request
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetEmptyDatabaseReturnsNothing(t *testing.T) {
|
|
|
|
testDB := test.NewInMemoryRelayDatabase()
|
|
|
|
db := shared.Database{
|
|
|
|
Writer: sqlutil.NewDummyWriter(),
|
|
|
|
RelayQueue: testDB,
|
|
|
|
RelayQueueJSON: testDB,
|
|
|
|
}
|
|
|
|
httpReq := &http.Request{}
|
2023-04-19 15:50:33 +01:00
|
|
|
userID, err := spec.NewUserID("@local:domain", false)
|
2023-01-23 17:55:12 +00:00
|
|
|
assert.NoError(t, err, "Invalid userID")
|
|
|
|
|
|
|
|
transaction := createTransaction()
|
|
|
|
|
|
|
|
_, err = db.StoreTransaction(context.Background(), transaction)
|
|
|
|
assert.NoError(t, err, "Failed to store transaction")
|
|
|
|
|
|
|
|
relayAPI := internal.NewRelayInternalAPI(
|
2023-01-29 19:26:16 +00:00
|
|
|
&db, nil, nil, nil, nil, false, "", true,
|
2023-01-23 17:55:12 +00:00
|
|
|
)
|
|
|
|
|
2023-04-06 09:55:01 +01:00
|
|
|
request := createQuery(*userID, fclient.RelayEntry{})
|
2023-01-23 17:55:12 +00:00
|
|
|
response := routing.GetTransactionFromRelay(httpReq, &request, relayAPI, *userID)
|
|
|
|
assert.Equal(t, http.StatusOK, response.Code)
|
|
|
|
|
2023-04-06 09:55:01 +01:00
|
|
|
jsonResponse := response.JSON.(fclient.RespGetRelayTransaction)
|
2023-01-23 17:55:12 +00:00
|
|
|
assert.Equal(t, false, jsonResponse.EntriesQueued)
|
|
|
|
assert.Equal(t, gomatrixserverlib.Transaction{}, jsonResponse.Transaction)
|
|
|
|
|
|
|
|
count, err := db.GetTransactionCount(context.Background(), *userID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Zero(t, count)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetInvalidPrevEntryFails(t *testing.T) {
|
|
|
|
testDB := test.NewInMemoryRelayDatabase()
|
|
|
|
db := shared.Database{
|
|
|
|
Writer: sqlutil.NewDummyWriter(),
|
|
|
|
RelayQueue: testDB,
|
|
|
|
RelayQueueJSON: testDB,
|
|
|
|
}
|
|
|
|
httpReq := &http.Request{}
|
2023-04-19 15:50:33 +01:00
|
|
|
userID, err := spec.NewUserID("@local:domain", false)
|
2023-01-23 17:55:12 +00:00
|
|
|
assert.NoError(t, err, "Invalid userID")
|
|
|
|
|
|
|
|
transaction := createTransaction()
|
|
|
|
|
|
|
|
_, err = db.StoreTransaction(context.Background(), transaction)
|
|
|
|
assert.NoError(t, err, "Failed to store transaction")
|
|
|
|
|
|
|
|
relayAPI := internal.NewRelayInternalAPI(
|
2023-01-29 19:26:16 +00:00
|
|
|
&db, nil, nil, nil, nil, false, "", true,
|
2023-01-23 17:55:12 +00:00
|
|
|
)
|
|
|
|
|
2023-04-06 09:55:01 +01:00
|
|
|
request := createQuery(*userID, fclient.RelayEntry{EntryID: -1})
|
2023-01-23 17:55:12 +00:00
|
|
|
response := routing.GetTransactionFromRelay(httpReq, &request, relayAPI, *userID)
|
|
|
|
assert.Equal(t, http.StatusInternalServerError, response.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetReturnsSavedTransaction(t *testing.T) {
|
|
|
|
testDB := test.NewInMemoryRelayDatabase()
|
|
|
|
db := shared.Database{
|
|
|
|
Writer: sqlutil.NewDummyWriter(),
|
|
|
|
RelayQueue: testDB,
|
|
|
|
RelayQueueJSON: testDB,
|
|
|
|
}
|
|
|
|
httpReq := &http.Request{}
|
2023-04-19 15:50:33 +01:00
|
|
|
userID, err := spec.NewUserID("@local:domain", false)
|
2023-01-23 17:55:12 +00:00
|
|
|
assert.NoError(t, err, "Invalid userID")
|
|
|
|
|
|
|
|
transaction := createTransaction()
|
|
|
|
receipt, err := db.StoreTransaction(context.Background(), transaction)
|
|
|
|
assert.NoError(t, err, "Failed to store transaction")
|
|
|
|
|
|
|
|
err = db.AssociateTransactionWithDestinations(
|
|
|
|
context.Background(),
|
2023-04-19 15:50:33 +01:00
|
|
|
map[spec.UserID]struct{}{
|
2023-01-23 17:55:12 +00:00
|
|
|
*userID: {},
|
|
|
|
},
|
|
|
|
transaction.TransactionID,
|
|
|
|
receipt)
|
|
|
|
assert.NoError(t, err, "Failed to associate transaction with user")
|
|
|
|
|
|
|
|
relayAPI := internal.NewRelayInternalAPI(
|
2023-01-29 19:26:16 +00:00
|
|
|
&db, nil, nil, nil, nil, false, "", true,
|
2023-01-23 17:55:12 +00:00
|
|
|
)
|
|
|
|
|
2023-04-06 09:55:01 +01:00
|
|
|
request := createQuery(*userID, fclient.RelayEntry{})
|
2023-01-23 17:55:12 +00:00
|
|
|
response := routing.GetTransactionFromRelay(httpReq, &request, relayAPI, *userID)
|
|
|
|
assert.Equal(t, http.StatusOK, response.Code)
|
|
|
|
|
2023-04-06 09:55:01 +01:00
|
|
|
jsonResponse := response.JSON.(fclient.RespGetRelayTransaction)
|
2023-01-23 17:55:12 +00:00
|
|
|
assert.True(t, jsonResponse.EntriesQueued)
|
|
|
|
assert.Equal(t, transaction, jsonResponse.Transaction)
|
|
|
|
|
|
|
|
// And once more to clear the queue
|
2023-04-06 09:55:01 +01:00
|
|
|
request = createQuery(*userID, fclient.RelayEntry{EntryID: jsonResponse.EntryID})
|
2023-01-23 17:55:12 +00:00
|
|
|
response = routing.GetTransactionFromRelay(httpReq, &request, relayAPI, *userID)
|
|
|
|
assert.Equal(t, http.StatusOK, response.Code)
|
|
|
|
|
2023-04-06 09:55:01 +01:00
|
|
|
jsonResponse = response.JSON.(fclient.RespGetRelayTransaction)
|
2023-01-23 17:55:12 +00:00
|
|
|
assert.False(t, jsonResponse.EntriesQueued)
|
|
|
|
assert.Equal(t, gomatrixserverlib.Transaction{}, jsonResponse.Transaction)
|
|
|
|
|
|
|
|
count, err := db.GetTransactionCount(context.Background(), *userID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Zero(t, count)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetReturnsMultipleSavedTransactions(t *testing.T) {
|
|
|
|
testDB := test.NewInMemoryRelayDatabase()
|
|
|
|
db := shared.Database{
|
|
|
|
Writer: sqlutil.NewDummyWriter(),
|
|
|
|
RelayQueue: testDB,
|
|
|
|
RelayQueueJSON: testDB,
|
|
|
|
}
|
|
|
|
httpReq := &http.Request{}
|
2023-04-19 15:50:33 +01:00
|
|
|
userID, err := spec.NewUserID("@local:domain", false)
|
2023-01-23 17:55:12 +00:00
|
|
|
assert.NoError(t, err, "Invalid userID")
|
|
|
|
|
|
|
|
transaction := createTransaction()
|
|
|
|
receipt, err := db.StoreTransaction(context.Background(), transaction)
|
|
|
|
assert.NoError(t, err, "Failed to store transaction")
|
|
|
|
|
|
|
|
err = db.AssociateTransactionWithDestinations(
|
|
|
|
context.Background(),
|
2023-04-19 15:50:33 +01:00
|
|
|
map[spec.UserID]struct{}{
|
2023-01-23 17:55:12 +00:00
|
|
|
*userID: {},
|
|
|
|
},
|
|
|
|
transaction.TransactionID,
|
|
|
|
receipt)
|
|
|
|
assert.NoError(t, err, "Failed to associate transaction with user")
|
|
|
|
|
|
|
|
transaction2 := createTransaction()
|
|
|
|
receipt2, err := db.StoreTransaction(context.Background(), transaction2)
|
|
|
|
assert.NoError(t, err, "Failed to store transaction")
|
|
|
|
|
|
|
|
err = db.AssociateTransactionWithDestinations(
|
|
|
|
context.Background(),
|
2023-04-19 15:50:33 +01:00
|
|
|
map[spec.UserID]struct{}{
|
2023-01-23 17:55:12 +00:00
|
|
|
*userID: {},
|
|
|
|
},
|
|
|
|
transaction2.TransactionID,
|
|
|
|
receipt2)
|
|
|
|
assert.NoError(t, err, "Failed to associate transaction with user")
|
|
|
|
|
|
|
|
relayAPI := internal.NewRelayInternalAPI(
|
2023-01-29 19:26:16 +00:00
|
|
|
&db, nil, nil, nil, nil, false, "", true,
|
2023-01-23 17:55:12 +00:00
|
|
|
)
|
|
|
|
|
2023-04-06 09:55:01 +01:00
|
|
|
request := createQuery(*userID, fclient.RelayEntry{})
|
2023-01-23 17:55:12 +00:00
|
|
|
response := routing.GetTransactionFromRelay(httpReq, &request, relayAPI, *userID)
|
|
|
|
assert.Equal(t, http.StatusOK, response.Code)
|
|
|
|
|
2023-04-06 09:55:01 +01:00
|
|
|
jsonResponse := response.JSON.(fclient.RespGetRelayTransaction)
|
2023-01-23 17:55:12 +00:00
|
|
|
assert.True(t, jsonResponse.EntriesQueued)
|
|
|
|
assert.Equal(t, transaction, jsonResponse.Transaction)
|
|
|
|
|
2023-04-06 09:55:01 +01:00
|
|
|
request = createQuery(*userID, fclient.RelayEntry{EntryID: jsonResponse.EntryID})
|
2023-01-23 17:55:12 +00:00
|
|
|
response = routing.GetTransactionFromRelay(httpReq, &request, relayAPI, *userID)
|
|
|
|
assert.Equal(t, http.StatusOK, response.Code)
|
|
|
|
|
2023-04-06 09:55:01 +01:00
|
|
|
jsonResponse = response.JSON.(fclient.RespGetRelayTransaction)
|
2023-01-23 17:55:12 +00:00
|
|
|
assert.True(t, jsonResponse.EntriesQueued)
|
|
|
|
assert.Equal(t, transaction2, jsonResponse.Transaction)
|
|
|
|
|
|
|
|
// And once more to clear the queue
|
2023-04-06 09:55:01 +01:00
|
|
|
request = createQuery(*userID, fclient.RelayEntry{EntryID: jsonResponse.EntryID})
|
2023-01-23 17:55:12 +00:00
|
|
|
response = routing.GetTransactionFromRelay(httpReq, &request, relayAPI, *userID)
|
|
|
|
assert.Equal(t, http.StatusOK, response.Code)
|
|
|
|
|
2023-04-06 09:55:01 +01:00
|
|
|
jsonResponse = response.JSON.(fclient.RespGetRelayTransaction)
|
2023-01-23 17:55:12 +00:00
|
|
|
assert.False(t, jsonResponse.EntriesQueued)
|
|
|
|
assert.Equal(t, gomatrixserverlib.Transaction{}, jsonResponse.Transaction)
|
|
|
|
|
|
|
|
count, err := db.GetTransactionCount(context.Background(), *userID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Zero(t, count)
|
|
|
|
}
|