|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 The Matrix.org Foundation C.I.C. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.matrix.android.sdk.internal.util |
| 18 | + |
| 19 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 20 | +import org.junit.Assert.assertEquals |
| 21 | +import org.junit.Test |
| 22 | +import org.junit.runner.RunWith |
| 23 | +import org.matrix.android.sdk.InstrumentedTest |
| 24 | +import org.matrix.android.sdk.internal.session.DefaultFileService.Companion.DEFAULT_FILENAME |
| 25 | +import org.matrix.android.sdk.internal.util.file.safeFileName |
| 26 | + |
| 27 | +/** |
| 28 | + * These tests are run on an Android device because they need to use the static |
| 29 | + * MimeTypeMap#getSingleton() method, which was failing in the unit test directory. |
| 30 | + */ |
| 31 | +@RunWith(AndroidJUnit4::class) |
| 32 | +class FileUtilTest : InstrumentedTest { |
| 33 | + |
| 34 | + @Test |
| 35 | + fun shouldReturnOriginalFilenameWhenValidCharactersAreUsed() { |
| 36 | + val fileName = "validFileName.txt" |
| 37 | + val mimeType = "text/plain" |
| 38 | + val result = safeFileName(fileName, mimeType) |
| 39 | + assertEquals("validFileName.txt", result) |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + fun shouldReplaceInvalidCharactersWithUnderscores() { |
| 44 | + val fileName = "invalid/filename:with*chars?.txt" |
| 45 | + val mimeType = "text/plain" |
| 46 | + val result = safeFileName(fileName, mimeType) |
| 47 | + assertEquals("invalid/filename_with_chars_.txt", result) |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + fun shouldAllowCyrillicCharactersInTheFilename() { |
| 52 | + val fileName = "тестовыйФайл.txt" |
| 53 | + val mimeType = "text/plain" |
| 54 | + val result = safeFileName(fileName, mimeType) |
| 55 | + assertEquals("тестовыйФайл.txt", result) |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + fun shouldAllowHanCharactersInTheFilename() { |
| 60 | + val fileName = "测试文件.txt" |
| 61 | + val mimeType = "text/plain" |
| 62 | + val result = safeFileName(fileName, mimeType) |
| 63 | + assertEquals("测试文件.txt", result) |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + fun shouldReturnDefaultFilenameWhenInputIsNull() { |
| 68 | + val fileName = null |
| 69 | + val mimeType = "text/plain" |
| 70 | + val result = safeFileName(fileName, mimeType) |
| 71 | + assertEquals("$DEFAULT_FILENAME.txt", result) |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + fun shouldAddTheCorrectExtensionWhenMissing() { |
| 76 | + val fileName = "myDocument" |
| 77 | + val mimeType = "application/pdf" |
| 78 | + val result = safeFileName(fileName, mimeType) |
| 79 | + assertEquals("myDocument.pdf", result) |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + fun shouldReplaceInvalidCharactersAndAddTheCorrectExtension() { |
| 84 | + val fileName = "my*docu/ment" |
| 85 | + val mimeType = "application/pdf" |
| 86 | + val result = safeFileName(fileName, mimeType) |
| 87 | + assertEquals("my_docu/ment.pdf", result) |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + fun shouldNotModifyTheExtensionIfItMatchesTheMimeType() { |
| 92 | + val fileName = "report.pdf" |
| 93 | + val mimeType = "application/pdf" |
| 94 | + val result = safeFileName(fileName, mimeType) |
| 95 | + assertEquals("report.pdf", result) |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + fun shouldReplaceSpacesWithUnderscores() { |
| 100 | + val fileName = "my report.doc" |
| 101 | + val mimeType = "application/msword" |
| 102 | + val result = safeFileName(fileName, mimeType) |
| 103 | + assertEquals("my_report.doc", result) |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + fun shouldAppendExtensionIfFileNameHasNoneAndMimeTypeIsValid() { |
| 108 | + val fileName = "newfile" |
| 109 | + val mimeType = "image/jpeg" |
| 110 | + val result = safeFileName(fileName, mimeType) |
| 111 | + assertEquals("newfile.jpg", result) |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + fun shouldKeepHyphenatedNamesIntact() { |
| 116 | + val fileName = "my-file-name" |
| 117 | + val mimeType = "application/octet-stream" |
| 118 | + val result = safeFileName(fileName, mimeType) |
| 119 | + assertEquals("my-file-name.bin", result) |
| 120 | + } |
| 121 | +} |
0 commit comments