diff --git a/app/src/main/java/org/thoughtcrime/securesms/util/SqlUtil.java b/app/src/main/java/org/thoughtcrime/securesms/util/SqlUtil.java index f2e280294c..71361d5b29 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/util/SqlUtil.java +++ b/app/src/main/java/org/thoughtcrime/securesms/util/SqlUtil.java @@ -18,6 +18,7 @@ import java.util.Arrays; import java.util.Collection; import java.util.LinkedList; import java.util.List; +import java.util.Locale; import java.util.Map; import java.util.Set; import java.util.stream.Collectors; @@ -122,8 +123,14 @@ public final class SqlUtil { for (Map.Entry entry : valueSet) { if (entry.getValue() != null) { - qualifier.append(entry.getKey()).append(" != ? OR ").append(entry.getKey()).append(" IS NULL"); - fullArgs.add(String.valueOf(entry.getValue())); + if (entry.getValue() instanceof byte[]) { + byte[] data = (byte[]) entry.getValue(); + qualifier.append("hex(").append(entry.getKey()).append(") != ? OR ").append(entry.getKey()).append(" IS NULL"); + fullArgs.add(Hex.toStringCondensed(data).toUpperCase(Locale.US)); + } else { + qualifier.append(entry.getKey()).append(" != ? OR ").append(entry.getKey()).append(" IS NULL"); + fullArgs.add(String.valueOf(entry.getValue())); + } } else { qualifier.append(entry.getKey()).append(" NOT NULL"); } diff --git a/app/src/test/java/org/thoughtcrime/securesms/util/SqlUtilTest.java b/app/src/test/java/org/thoughtcrime/securesms/util/SqlUtilTest.java index c6b4b07812..0ccddc4c90 100644 --- a/app/src/test/java/org/thoughtcrime/securesms/util/SqlUtilTest.java +++ b/app/src/test/java/org/thoughtcrime/securesms/util/SqlUtilTest.java @@ -9,6 +9,7 @@ import org.robolectric.RobolectricTestRunner; import org.robolectric.annotation.Config; import org.thoughtcrime.securesms.recipients.RecipientId; +import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -97,6 +98,22 @@ public final class SqlUtilTest { assertArrayEquals(new String[] { "1", "2", "3" }, updateQuery.getWhereArgs()); } + @Test + public void buildTrueUpdateQuery_blobComplex() { + String selection = "_id = ?"; + String[] args = new String[]{"1"}; + + ContentValues values = new ContentValues(); + values.put("a", hexToBytes("FF")); + values.put("b", 2); + values.putNull("c"); + + SqlUtil.Query updateQuery = SqlUtil.buildTrueUpdateQuery(selection, args, values); + + assertEquals("(_id = ?) AND (hex(a) != ? OR a IS NULL OR b != ? OR b IS NULL OR c NOT NULL)", updateQuery.getWhere()); + assertArrayEquals(new String[] { "1", "FF", "2" }, updateQuery.getWhereArgs()); + } + @Test public void buildCollectionQuery_single() { SqlUtil.Query updateQuery = SqlUtil.buildCollectionQuery("a", Arrays.asList(1)); @@ -255,4 +272,12 @@ public final class SqlUtilTest { assertEquals("INSERT INTO mytable (a, b) VALUES (?, ?)", output.get(1).getWhere()); assertArrayEquals(new String[] { "5", "6" }, output.get(1).getWhereArgs()); } + + private static byte[] hexToBytes(String hex) { + try { + return Hex.fromStringCondensed(hex); + } catch (IOException e) { + throw new AssertionError(e); + } + } }