mirror of
https://github.com/signalapp/Signal-Server.git
synced 2026-11-30 05:56:50 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3bd58bf25e | ||
|
|
ba05e577ae | ||
|
|
4206f6af45 | ||
|
|
0c5da1cc47 |
No files matched your search
@@ -9,7 +9,7 @@
|
||||
|
||||
<groupId>org.whispersystems.textsecure</groupId>
|
||||
<artifactId>TextSecureServer</artifactId>
|
||||
<version>0.10</version>
|
||||
<version>0.12</version>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
|
||||
@@ -174,6 +174,7 @@ public class KeysController {
|
||||
|
||||
if (device.isPresent() && device.get().isActive()) {
|
||||
preKey.setRegistrationId(device.get().getRegistrationId());
|
||||
preKey.setIdentityKey(destination.getIdentityKey());
|
||||
filteredKeys.add(preKey);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ package org.whispersystems.textsecuregcm.entities;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
import javax.xml.bind.annotation.XmlTransient;
|
||||
@@ -58,8 +59,19 @@ public class PreKey {
|
||||
public PreKey() {}
|
||||
|
||||
public PreKey(long id, String number, long deviceId, long keyId,
|
||||
String publicKey, String identityKey,
|
||||
boolean lastResort)
|
||||
String publicKey, boolean lastResort)
|
||||
{
|
||||
this.id = id;
|
||||
this.number = number;
|
||||
this.deviceId = deviceId;
|
||||
this.keyId = keyId;
|
||||
this.publicKey = publicKey;
|
||||
this.lastResort = lastResort;
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public PreKey(long id, String number, long deviceId, long keyId,
|
||||
String publicKey, String identityKey, boolean lastResort)
|
||||
{
|
||||
this.id = id;
|
||||
this.number = number;
|
||||
|
||||
@@ -50,12 +50,12 @@ public abstract class Keys {
|
||||
@SqlUpdate("DELETE FROM keys WHERE id = :id")
|
||||
abstract void removeKey(@Bind("id") long id);
|
||||
|
||||
@SqlBatch("INSERT INTO keys (number, device_id, key_id, public_key, identity_key, last_resort) VALUES " +
|
||||
"(:number, :device_id, :key_id, :public_key, :identity_key, :last_resort)")
|
||||
@SqlBatch("INSERT INTO keys (number, device_id, key_id, public_key, last_resort) VALUES " +
|
||||
"(:number, :device_id, :key_id, :public_key, :last_resort)")
|
||||
abstract void append(@PreKeyBinder List<PreKey> preKeys);
|
||||
|
||||
@SqlUpdate("INSERT INTO keys (number, device_id, key_id, public_key, identity_key, last_resort) VALUES " +
|
||||
"(:number, :device_id, :key_id, :public_key, :identity_key, :last_resort)")
|
||||
@SqlUpdate("INSERT INTO keys (number, device_id, key_id, public_key, last_resort) VALUES " +
|
||||
"(:number, :device_id, :key_id, :public_key, :last_resort)")
|
||||
abstract void append(@PreKeyBinder PreKey preKey);
|
||||
|
||||
@SqlQuery("SELECT * FROM keys WHERE number = :number AND device_id = :device_id ORDER BY key_id ASC FOR UPDATE")
|
||||
@@ -129,7 +129,6 @@ public abstract class Keys {
|
||||
sql.bind("device_id", preKey.getDeviceId());
|
||||
sql.bind("key_id", preKey.getKeyId());
|
||||
sql.bind("public_key", preKey.getPublicKey());
|
||||
sql.bind("identity_key", preKey.getIdentityKey());
|
||||
sql.bind("last_resort", preKey.isLastResort() ? 1 : 0);
|
||||
}
|
||||
};
|
||||
@@ -145,7 +144,6 @@ public abstract class Keys {
|
||||
{
|
||||
return new PreKey(resultSet.getLong("id"), resultSet.getString("number"), resultSet.getLong("device_id"),
|
||||
resultSet.getLong("key_id"), resultSet.getString("public_key"),
|
||||
resultSet.getString("identity_key"),
|
||||
resultSet.getInt("last_resort") == 1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -142,4 +142,29 @@
|
||||
</createIndex>
|
||||
|
||||
</changeSet>
|
||||
|
||||
<changeSet id="3" author="moxie">
|
||||
<sql>CREATE OR REPLACE FUNCTION "custom_json_object_set_key"(
|
||||
"json" json,
|
||||
"key_to_set" TEXT,
|
||||
"value_to_set" anyelement
|
||||
)
|
||||
RETURNS json
|
||||
LANGUAGE sql
|
||||
IMMUTABLE
|
||||
STRICT
|
||||
AS $function$
|
||||
SELECT COALESCE(
|
||||
(SELECT ('{' || string_agg(to_json("key") || ':' || "value", ',') || '}')
|
||||
FROM (SELECT *
|
||||
FROM json_each("json")
|
||||
WHERE "key" <> "key_to_set"
|
||||
UNION ALL
|
||||
SELECT "key_to_set", to_json("value_to_set")) AS "fields"),
|
||||
'{}'
|
||||
)::json
|
||||
$function$;</sql>
|
||||
<sql>UPDATE accounts SET data = custom_json_object_set_key(data, 'identityKey', k.identity_key) FROM keys k WHERE (data->>'identityKey')::text is null AND k.number = data->>'number' AND k.last_resort = 1;</sql>
|
||||
<sql>UPDATE accounts SET data = custom_json_object_set_key(data, 'identityKey', k.identity_key) FROM keys k WHERE (data->>'identityKey')::text is null AND k.number = data->>'number';</sql>
|
||||
</changeSet>
|
||||
</databaseChangeLog>
|
||||
+30
-10
@@ -5,6 +5,8 @@ import com.sun.jersey.api.client.ClientResponse;
|
||||
import com.yammer.dropwizard.testing.ResourceTest;
|
||||
import org.junit.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
import org.mockito.stubbing.Answer;
|
||||
import org.whispersystems.textsecuregcm.controllers.KeysController;
|
||||
import org.whispersystems.textsecuregcm.entities.PreKey;
|
||||
import org.whispersystems.textsecuregcm.entities.PreKeyList;
|
||||
@@ -34,7 +36,7 @@ public class KeyControllerTest extends ResourceTest {
|
||||
private final int SAMPLE_REGISTRATION_ID2 = 1002;
|
||||
|
||||
private final PreKey SAMPLE_KEY = new PreKey(1, EXISTS_NUMBER, Device.MASTER_ID, 1234, "test1", "test2", false);
|
||||
private final PreKey SAMPLE_KEY2 = new PreKey(2, EXISTS_NUMBER, 2, 5667, "test3", "test4", false );
|
||||
private final PreKey SAMPLE_KEY2 = new PreKey(2, EXISTS_NUMBER, 2, 5667, "test3", "test4,", false );
|
||||
private final PreKey SAMPLE_KEY3 = new PreKey(3, EXISTS_NUMBER, 3, 334, "test5", "test6", false );
|
||||
private final Keys keys = mock(Keys.class );
|
||||
private final AccountsManager accounts = mock(AccountsManager.class);
|
||||
@@ -63,21 +65,34 @@ public class KeyControllerTest extends ResourceTest {
|
||||
when(existsAccount.getDevice(2L)).thenReturn(Optional.of(sampleDevice2));
|
||||
when(existsAccount.getDevice(3L)).thenReturn(Optional.of(sampleDevice3));
|
||||
when(existsAccount.isActive()).thenReturn(true);
|
||||
when(existsAccount.getIdentityKey()).thenReturn("existsidentitykey");
|
||||
|
||||
when(accounts.get(EXISTS_NUMBER)).thenReturn(Optional.of(existsAccount));
|
||||
when(accounts.get(NOT_EXISTS_NUMBER)).thenReturn(Optional.<Account>absent());
|
||||
|
||||
when(rateLimiters.getPreKeysLimiter()).thenReturn(rateLimiter);
|
||||
|
||||
when(keys.get(eq(EXISTS_NUMBER), eq(1L))).thenReturn(Optional.of(new UnstructuredPreKeyList(SAMPLE_KEY)));
|
||||
when(keys.get(eq(EXISTS_NUMBER), eq(1L))).thenAnswer(new Answer<Optional<UnstructuredPreKeyList>>() {
|
||||
@Override
|
||||
public Optional<UnstructuredPreKeyList> answer(InvocationOnMock invocationOnMock) throws Throwable {
|
||||
return Optional.of(new UnstructuredPreKeyList(cloneKey(SAMPLE_KEY)));
|
||||
}
|
||||
});
|
||||
|
||||
when(keys.get(eq(NOT_EXISTS_NUMBER), eq(1L))).thenReturn(Optional.<UnstructuredPreKeyList>absent());
|
||||
|
||||
List<PreKey> allKeys = new LinkedList<>();
|
||||
allKeys.add(SAMPLE_KEY);
|
||||
allKeys.add(SAMPLE_KEY2);
|
||||
allKeys.add(SAMPLE_KEY3);
|
||||
when(keys.get(EXISTS_NUMBER)).thenAnswer(new Answer<Optional<UnstructuredPreKeyList>>() {
|
||||
@Override
|
||||
public Optional<UnstructuredPreKeyList> answer(InvocationOnMock invocationOnMock) throws Throwable {
|
||||
List<PreKey> allKeys = new LinkedList<>();
|
||||
allKeys.add(cloneKey(SAMPLE_KEY));
|
||||
allKeys.add(cloneKey(SAMPLE_KEY2));
|
||||
allKeys.add(cloneKey(SAMPLE_KEY3));
|
||||
|
||||
return Optional.of(new UnstructuredPreKeyList(allKeys));
|
||||
}
|
||||
});
|
||||
|
||||
when(keys.get(EXISTS_NUMBER)).thenReturn(Optional.of(new UnstructuredPreKeyList(allKeys)));
|
||||
when(keys.getCount(eq(AuthHelper.VALID_NUMBER), eq(1L))).thenReturn(5);
|
||||
|
||||
when(AuthHelper.VALID_ACCOUNT.getIdentityKey()).thenReturn(null);
|
||||
@@ -105,7 +120,7 @@ public class KeyControllerTest extends ResourceTest {
|
||||
|
||||
assertThat(result.getKeyId()).isEqualTo(SAMPLE_KEY.getKeyId());
|
||||
assertThat(result.getPublicKey()).isEqualTo(SAMPLE_KEY.getPublicKey());
|
||||
assertThat(result.getIdentityKey()).isEqualTo(SAMPLE_KEY.getIdentityKey());
|
||||
assertThat(result.getIdentityKey()).isEqualTo(existsAccount.getIdentityKey());
|
||||
|
||||
assertThat(result.getId() == 0);
|
||||
assertThat(result.getNumber() == null);
|
||||
@@ -126,7 +141,7 @@ public class KeyControllerTest extends ResourceTest {
|
||||
|
||||
assertThat(result.getKeyId()).isEqualTo(SAMPLE_KEY.getKeyId());
|
||||
assertThat(result.getPublicKey()).isEqualTo(SAMPLE_KEY.getPublicKey());
|
||||
assertThat(result.getIdentityKey()).isEqualTo(SAMPLE_KEY.getIdentityKey());
|
||||
assertThat(result.getIdentityKey()).isEqualTo(existsAccount.getIdentityKey());
|
||||
assertThat(result.getRegistrationId()).isEqualTo(SAMPLE_REGISTRATION_ID);
|
||||
|
||||
assertThat(result.getId() == 0);
|
||||
@@ -135,7 +150,7 @@ public class KeyControllerTest extends ResourceTest {
|
||||
result = results.getKeys().get(1);
|
||||
assertThat(result.getKeyId()).isEqualTo(SAMPLE_KEY2.getKeyId());
|
||||
assertThat(result.getPublicKey()).isEqualTo(SAMPLE_KEY2.getPublicKey());
|
||||
assertThat(result.getIdentityKey()).isEqualTo(SAMPLE_KEY2.getIdentityKey());
|
||||
assertThat(result.getIdentityKey()).isEqualTo(existsAccount.getIdentityKey());
|
||||
assertThat(result.getRegistrationId()).isEqualTo(SAMPLE_REGISTRATION_ID2);
|
||||
|
||||
assertThat(result.getId() == 0);
|
||||
@@ -209,4 +224,9 @@ public class KeyControllerTest extends ResourceTest {
|
||||
verify(accounts).update(AuthHelper.VALID_ACCOUNT);
|
||||
}
|
||||
|
||||
private PreKey cloneKey(PreKey source) {
|
||||
return new PreKey(source.getId(), source.getNumber(), source.getDeviceId(), source.getKeyId(),
|
||||
source.getPublicKey(), source.getIdentityKey(), source.isLastResort());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user