From 552e552e88dc9f1385e556cfed11f465e35b3c88 Mon Sep 17 00:00:00 2001 From: Art O Cathain Date: Tue, 11 Nov 2014 18:07:00 +0000 Subject: [PATCH] Add unit tests for FileHelper.sanitizeFilename() --- src/com/fsck/k9/helper/FileHelper.java | 3 +- .../com/fsck/k9/helper/FileHelperTest.java | 32 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 tests/src/com/fsck/k9/helper/FileHelperTest.java diff --git a/src/com/fsck/k9/helper/FileHelper.java b/src/com/fsck/k9/helper/FileHelper.java index c33ed4cb9..e4f98c322 100644 --- a/src/com/fsck/k9/helper/FileHelper.java +++ b/src/com/fsck/k9/helper/FileHelper.java @@ -12,6 +12,7 @@ import com.fsck.k9.K9; public class FileHelper { + /** * Regular expression that represents characters we won't allow in file names. * @@ -27,7 +28,7 @@ public class FileHelper { * * @see #sanitizeFilename(String) */ - private static final String INVALID_CHARACTERS = "[^\\w !#$%&'()\\-@\\^`{}~.,]+"; + private static final String INVALID_CHARACTERS = "[^\\w !#$%&'()\\-@\\^`{}~.,]"; /** * Invalid characters in a file name are replaced by this character. diff --git a/tests/src/com/fsck/k9/helper/FileHelperTest.java b/tests/src/com/fsck/k9/helper/FileHelperTest.java new file mode 100644 index 000000000..1e8260256 --- /dev/null +++ b/tests/src/com/fsck/k9/helper/FileHelperTest.java @@ -0,0 +1,32 @@ +package com.fsck.k9.helper; + +import junit.framework.TestCase; + +import java.lang.String; + +public class FileHelperTest extends TestCase { + + public void testSanitize1() { + checkSanitization(".._bla_", "../bla_"); + } + + public void testSanitize2() { + checkSanitization("_etc_bla", "/etc/bla"); + } + + public void testSanitize3() { + checkSanitization("_пPп", "+пPп"); + } + + public void testSanitize4() { + checkSanitization(".東京_!", ".東京?!"); + } + + public void testSanitize5() { + checkSanitization("Plan 9", "Plan 9"); + } + + private void checkSanitization(String expected, String actual) { + assertEquals(expected, FileHelper.sanitizeFilename(actual)); + } +}