|
| 1 | +package csapi_tests |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/tidwall/gjson" |
| 8 | + |
| 9 | + "github.com/matrix-org/complement/internal/b" |
| 10 | + "github.com/matrix-org/complement/internal/client" |
| 11 | + "github.com/matrix-org/complement/internal/match" |
| 12 | + "github.com/matrix-org/complement/internal/must" |
| 13 | +) |
| 14 | + |
| 15 | +type backupKey struct { |
| 16 | + isVerified bool |
| 17 | + firstMessageIndex float64 |
| 18 | + forwardedCount float64 |
| 19 | +} |
| 20 | + |
| 21 | +// This test checks that the rules for replacing room keys are implemented correctly. |
| 22 | +// Specifically: |
| 23 | +// |
| 24 | +// if the keys have different values for is_verified, then it will keep the key that has is_verified set to true; |
| 25 | +// if they have the same values for is_verified, then it will keep the key with a lower first_message_index; |
| 26 | +// and finally, is is_verified and first_message_index are equal, then it will keep the key with a lower forwarded_count. |
| 27 | +func TestE2EKeyBackupReplaceRoomKeyRules(t *testing.T) { |
| 28 | + deployment := Deploy(t, b.BlueprintAlice) |
| 29 | + defer deployment.Destroy(t) |
| 30 | + userID := "@alice:hs1" |
| 31 | + roomID := "!foo:hs1" |
| 32 | + alice := deployment.Client(t, "hs1", userID) |
| 33 | + |
| 34 | + // make a new key backup |
| 35 | + res := alice.MustDoFunc(t, "POST", []string{"_matrix", "client", "r0", "room_keys", "version"}, client.WithJSONBody(t, map[string]interface{}{ |
| 36 | + "algorithm": "m.megolm_backup.v1", |
| 37 | + "auth_data": map[string]interface{}{ |
| 38 | + "foo": "bar", |
| 39 | + }, |
| 40 | + })) |
| 41 | + defer res.Body.Close() |
| 42 | + body := must.ParseJSON(t, res.Body) |
| 43 | + backupVersion := gjson.GetBytes(body, "version").Str |
| 44 | + if backupVersion == "" { |
| 45 | + t.Fatalf("failed to get 'version' key from response: %s", string(body)) |
| 46 | + } |
| 47 | + |
| 48 | + testCases := []struct { |
| 49 | + sessionID string // change this for each test case to namespace tests correctly |
| 50 | + input backupKey // the key to compare against |
| 51 | + keysThatDontReplace []backupKey // the keys which won't update the input key |
| 52 | + }{ |
| 53 | + { |
| 54 | + sessionID: "a", |
| 55 | + input: backupKey{ |
| 56 | + isVerified: false, |
| 57 | + firstMessageIndex: 10, |
| 58 | + forwardedCount: 5, |
| 59 | + }, |
| 60 | + keysThatDontReplace: []backupKey{ |
| 61 | + { |
| 62 | + isVerified: false, |
| 63 | + firstMessageIndex: 11, // higher first message index |
| 64 | + forwardedCount: 5, |
| 65 | + }, |
| 66 | + { |
| 67 | + isVerified: false, |
| 68 | + firstMessageIndex: 10, |
| 69 | + forwardedCount: 6, // higher forwarded count |
| 70 | + }, |
| 71 | + { |
| 72 | + isVerified: false, |
| 73 | + firstMessageIndex: 11, // higher first message index |
| 74 | + forwardedCount: 6, // higher forwarded count |
| 75 | + }, |
| 76 | + }, |
| 77 | + }, |
| 78 | + { |
| 79 | + sessionID: "b", |
| 80 | + input: backupKey{ |
| 81 | + isVerified: true, |
| 82 | + firstMessageIndex: 10, |
| 83 | + forwardedCount: 5, |
| 84 | + }, |
| 85 | + keysThatDontReplace: []backupKey{ |
| 86 | + { |
| 87 | + isVerified: false, // is verified is false |
| 88 | + firstMessageIndex: 11, // higher first message index |
| 89 | + forwardedCount: 5, |
| 90 | + }, |
| 91 | + { |
| 92 | + isVerified: false, // is verified is false |
| 93 | + firstMessageIndex: 10, |
| 94 | + forwardedCount: 6, // higher forwarded count |
| 95 | + }, |
| 96 | + { |
| 97 | + isVerified: false, // is verified is false |
| 98 | + firstMessageIndex: 11, // higher first message index |
| 99 | + forwardedCount: 6, // higher forwarded count |
| 100 | + }, |
| 101 | + { |
| 102 | + isVerified: true, |
| 103 | + firstMessageIndex: 11, // higher first message index |
| 104 | + forwardedCount: 5, |
| 105 | + }, |
| 106 | + { |
| 107 | + isVerified: true, |
| 108 | + firstMessageIndex: 10, |
| 109 | + forwardedCount: 6, // higher forwarded count |
| 110 | + }, |
| 111 | + { |
| 112 | + isVerified: true, |
| 113 | + firstMessageIndex: 11, // higher first message index |
| 114 | + forwardedCount: 6, // higher forwarded count |
| 115 | + }, |
| 116 | + }, |
| 117 | + }, |
| 118 | + } |
| 119 | + |
| 120 | + t.Run("parallel", func(t *testing.T) { |
| 121 | + for i := range testCases { |
| 122 | + tc := testCases[i] |
| 123 | + t.Run(fmt.Sprintf("%+v", tc.input), func(t *testing.T) { |
| 124 | + t.Parallel() |
| 125 | + // insert the key that will be tested against |
| 126 | + alice.MustDoFunc(t, "PUT", []string{"_matrix", "client", "r0", "room_keys", "keys", roomID, tc.sessionID}, |
| 127 | + client.WithQueries(map[string][]string{"version": {backupVersion}}), client.WithJSONBody(t, map[string]interface{}{ |
| 128 | + "first_message_index": tc.input.firstMessageIndex, |
| 129 | + "forwarded_count": tc.input.forwardedCount, |
| 130 | + "is_verified": tc.input.isVerified, |
| 131 | + "session_data": map[string]interface{}{"a": "b"}, |
| 132 | + }), |
| 133 | + ) |
| 134 | + // now check that each key in keysThatDontReplace do not replace this key |
| 135 | + for _, testKey := range tc.keysThatDontReplace { |
| 136 | + alice.MustDoFunc(t, "PUT", []string{"_matrix", "client", "r0", "room_keys", "keys", roomID, tc.sessionID}, |
| 137 | + client.WithQueries(map[string][]string{"version": {backupVersion}}), client.WithJSONBody(t, map[string]interface{}{ |
| 138 | + "first_message_index": testKey.firstMessageIndex, |
| 139 | + "forwarded_count": testKey.forwardedCount, |
| 140 | + "is_verified": testKey.isVerified, |
| 141 | + "session_data": map[string]interface{}{"a": "b"}, |
| 142 | + }), |
| 143 | + ) |
| 144 | + checkResp := alice.MustDoFunc(t, "GET", []string{"_matrix", "client", "r0", "room_keys", "keys", roomID, tc.sessionID}, |
| 145 | + client.WithQueries(map[string][]string{"version": {backupVersion}}), |
| 146 | + ) |
| 147 | + must.MatchResponse(t, checkResp, match.HTTPResponse{ |
| 148 | + StatusCode: 200, |
| 149 | + JSON: []match.JSON{ |
| 150 | + match.JSONKeyEqual("first_message_index", tc.input.firstMessageIndex), |
| 151 | + match.JSONKeyEqual("forwarded_count", tc.input.forwardedCount), |
| 152 | + match.JSONKeyEqual("is_verified", tc.input.isVerified), |
| 153 | + }, |
| 154 | + }) |
| 155 | + } |
| 156 | + }) |
| 157 | + } |
| 158 | + }) |
| 159 | +} |
0 commit comments