kopia lustrzana https://github.com/ryukoposting/Signal-Android
Add test for double encoded html in link preview tags.
rodzic
f091502949
commit
bf28dfee66
|
@ -1,7 +1,6 @@
|
||||||
package org.thoughtcrime.securesms.linkpreview;
|
package org.thoughtcrime.securesms.linkpreview;
|
||||||
|
|
||||||
import android.annotation.SuppressLint;
|
import android.annotation.SuppressLint;
|
||||||
import android.text.Html;
|
|
||||||
import android.text.SpannableString;
|
import android.text.SpannableString;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
import android.text.style.URLSpan;
|
import android.text.style.URLSpan;
|
||||||
|
@ -10,6 +9,7 @@ import android.text.util.Linkify;
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
import androidx.annotation.VisibleForTesting;
|
import androidx.annotation.VisibleForTesting;
|
||||||
|
import androidx.core.text.HtmlCompat;
|
||||||
import androidx.core.text.util.LinkifyCompat;
|
import androidx.core.text.util.LinkifyCompat;
|
||||||
|
|
||||||
import com.annimon.stream.Collectors;
|
import com.annimon.stream.Collectors;
|
||||||
|
@ -100,11 +100,6 @@ public final class LinkPreviewUtil {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static @NonNull OpenGraph parseOpenGraphFields(@Nullable String html) {
|
public static @NonNull OpenGraph parseOpenGraphFields(@Nullable String html) {
|
||||||
return parseOpenGraphFields(html, text -> Html.fromHtml(text).toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
@VisibleForTesting
|
|
||||||
static @NonNull OpenGraph parseOpenGraphFields(@Nullable String html, @NonNull HtmlDecoder htmlDecoder) {
|
|
||||||
if (html == null) {
|
if (html == null) {
|
||||||
return new OpenGraph(Collections.emptyMap(), null, null);
|
return new OpenGraph(Collections.emptyMap(), null, null);
|
||||||
}
|
}
|
||||||
|
@ -119,7 +114,7 @@ public final class LinkPreviewUtil {
|
||||||
if (property != null) {
|
if (property != null) {
|
||||||
Matcher contentMatcher = OPEN_GRAPH_CONTENT_PATTERN.matcher(tag);
|
Matcher contentMatcher = OPEN_GRAPH_CONTENT_PATTERN.matcher(tag);
|
||||||
if (contentMatcher.find() && contentMatcher.groupCount() > 0) {
|
if (contentMatcher.find() && contentMatcher.groupCount() > 0) {
|
||||||
String content = htmlDecoder.fromEncoded(contentMatcher.group(1));
|
String content = fromDoubleEncoded(contentMatcher.group(1));
|
||||||
openGraphTags.put(property.toLowerCase(), content);
|
openGraphTags.put(property.toLowerCase(), content);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -134,7 +129,7 @@ public final class LinkPreviewUtil {
|
||||||
if (property != null) {
|
if (property != null) {
|
||||||
Matcher contentMatcher = OPEN_GRAPH_CONTENT_PATTERN.matcher(tag);
|
Matcher contentMatcher = OPEN_GRAPH_CONTENT_PATTERN.matcher(tag);
|
||||||
if (contentMatcher.find() && contentMatcher.groupCount() > 0) {
|
if (contentMatcher.find() && contentMatcher.groupCount() > 0) {
|
||||||
String content = htmlDecoder.fromEncoded(contentMatcher.group(1));
|
String content = fromDoubleEncoded(contentMatcher.group(1));
|
||||||
openGraphTags.put(property.toLowerCase(), content);
|
openGraphTags.put(property.toLowerCase(), content);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -145,7 +140,7 @@ public final class LinkPreviewUtil {
|
||||||
|
|
||||||
Matcher titleMatcher = TITLE_PATTERN.matcher(html);
|
Matcher titleMatcher = TITLE_PATTERN.matcher(html);
|
||||||
if (titleMatcher.find() && titleMatcher.groupCount() > 0) {
|
if (titleMatcher.find() && titleMatcher.groupCount() > 0) {
|
||||||
htmlTitle = htmlDecoder.fromEncoded(titleMatcher.group(1));
|
htmlTitle = fromDoubleEncoded(titleMatcher.group(1));
|
||||||
}
|
}
|
||||||
|
|
||||||
Matcher faviconMatcher = FAVICON_PATTERN.matcher(html);
|
Matcher faviconMatcher = FAVICON_PATTERN.matcher(html);
|
||||||
|
@ -169,6 +164,9 @@ public final class LinkPreviewUtil {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static @NonNull String fromDoubleEncoded(@NonNull String html) {
|
||||||
|
return HtmlCompat.fromHtml(HtmlCompat.fromHtml(html, 0).toString(), 0).toString();
|
||||||
|
}
|
||||||
|
|
||||||
public static final class OpenGraph {
|
public static final class OpenGraph {
|
||||||
|
|
||||||
|
@ -216,10 +214,6 @@ public final class LinkPreviewUtil {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface HtmlDecoder {
|
|
||||||
@NonNull String fromEncoded(@NonNull String html);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class Links {
|
public static class Links {
|
||||||
static final Links EMPTY = new Links(Collections.emptyList());
|
static final Links EMPTY = new Links(Collections.emptyList());
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,12 @@
|
||||||
package org.thoughtcrime.securesms.linkpreview;
|
package org.thoughtcrime.securesms.linkpreview;
|
||||||
|
|
||||||
|
import android.app.Application;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.junit.runners.Parameterized;
|
import org.junit.runners.Parameterized;
|
||||||
|
import org.robolectric.ParameterizedRobolectricTestRunner;
|
||||||
|
import org.robolectric.annotation.Config;
|
||||||
import org.whispersystems.libsignal.util.guava.Optional;
|
import org.whispersystems.libsignal.util.guava.Optional;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
@ -10,7 +14,8 @@ import java.util.Collection;
|
||||||
|
|
||||||
import static junit.framework.TestCase.assertEquals;
|
import static junit.framework.TestCase.assertEquals;
|
||||||
|
|
||||||
@RunWith(Parameterized.class)
|
@RunWith(ParameterizedRobolectricTestRunner.class)
|
||||||
|
@Config(manifest = Config.NONE, application = Application.class)
|
||||||
public class LinkPreviewUtilTest_parseOpenGraphFields {
|
public class LinkPreviewUtilTest_parseOpenGraphFields {
|
||||||
|
|
||||||
private final String html;
|
private final String html;
|
||||||
|
@ -19,7 +24,7 @@ public class LinkPreviewUtilTest_parseOpenGraphFields {
|
||||||
private final long date;
|
private final long date;
|
||||||
private final String imageUrl;
|
private final String imageUrl;
|
||||||
|
|
||||||
@Parameterized.Parameters
|
@ParameterizedRobolectricTestRunner.Parameters
|
||||||
public static Collection<Object[]> data() {
|
public static Collection<Object[]> data() {
|
||||||
return Arrays.asList(new Object[][]{
|
return Arrays.asList(new Object[][]{
|
||||||
// Normal
|
// Normal
|
||||||
|
@ -150,6 +155,16 @@ public class LinkPreviewUtilTest_parseOpenGraphFields {
|
||||||
694051200000L,
|
694051200000L,
|
||||||
null},
|
null},
|
||||||
|
|
||||||
|
// Double encoded HTML
|
||||||
|
{ "<meta content=\"Daily Bugle\" property=\"og:title\">\n" +
|
||||||
|
"<meta content=\"https://images.com/my-image.jpg\" property=\"og:image\">" +
|
||||||
|
"<meta content=\"A newspaper&#39;s\" property=\"og:description\">" +
|
||||||
|
"<meta content=\"1991-12-30T00:00:00+00:00\" property=\"og:published_time\">",
|
||||||
|
"Daily Bugle",
|
||||||
|
"A newspaper's",
|
||||||
|
694051200000L,
|
||||||
|
"https://images.com/my-image.jpg"},
|
||||||
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -163,7 +178,7 @@ public class LinkPreviewUtilTest_parseOpenGraphFields {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void parseOpenGraphFields() {
|
public void parseOpenGraphFields() {
|
||||||
LinkPreviewUtil.OpenGraph openGraph = LinkPreviewUtil.parseOpenGraphFields(html, html -> html);
|
LinkPreviewUtil.OpenGraph openGraph = LinkPreviewUtil.parseOpenGraphFields(html);
|
||||||
assertEquals(Optional.fromNullable(title), openGraph.getTitle());
|
assertEquals(Optional.fromNullable(title), openGraph.getTitle());
|
||||||
assertEquals(Optional.fromNullable(description), openGraph.getDescription());
|
assertEquals(Optional.fromNullable(description), openGraph.getDescription());
|
||||||
assertEquals(date, openGraph.getDate());
|
assertEquals(date, openGraph.getDate());
|
||||||
|
|
Ładowanie…
Reference in New Issue