|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using Android.App; |
| 5 | +using Android.Provider; |
| 6 | +using Android.Util; |
| 7 | +using Common; |
| 8 | +using Seeker; |
| 9 | +using Seeker.Helpers; |
| 10 | +using NUnit.Framework; |
| 11 | + |
| 12 | +namespace InstrumentationTests |
| 13 | +{ |
| 14 | + // Checks SimpleHelpers.ToValidFileName against what the real storage provider stores. |
| 15 | + [TestFixture] |
| 16 | + public class ProviderFileNameParityTests |
| 17 | + { |
| 18 | + const string Tag = "SeekerTests"; |
| 19 | + |
| 20 | + private Android.Net.Uri _scratchDir; |
| 21 | + |
| 22 | + private static IEnumerable<(string label, string name)> Cases() |
| 23 | + { |
| 24 | + foreach (char c in "\"*/:<>?\\|") |
| 25 | + { |
| 26 | + yield return ("char " + c, "a" + c + "b"); |
| 27 | + } |
| 28 | + yield return ("tab", "a\tb"); |
| 29 | + yield return ("DEL", "a\u007fb"); |
| 30 | + yield return (":3", ":3"); |
| 31 | + yield return ("only invalid", ":"); |
| 32 | + yield return ("full test", "fulltest|\\?*<\":>+[]/'~!@#$%^&*()_+/\\.,"); |
| 33 | + yield return (".", "."); |
| 34 | + yield return ("..", ".."); |
| 35 | + yield return ("...", "..."); |
| 36 | + yield return ("empty", ""); |
| 37 | + yield return ("ampersand quote", "Rock & Roll's"); |
| 38 | + yield return ("japanese", "東京事変 - 教育"); |
| 39 | + yield return ("ascii 300 bytes", new string('a', 150) + new string('b', 150)); |
| 40 | + yield return ("cjk 300 bytes", new string('東', 50) + new string('京', 50)); |
| 41 | + yield return ("ascii 254 + 3-byte char", new string('a', 254) + "東"); |
| 42 | + yield return ("ascii 255", new string('a', 255)); |
| 43 | + yield return ("ascii 256", new string('a', 256)); |
| 44 | + // trimming deletes single UTF-16 chars from the middle, so these split surrogate pairs |
| 45 | + yield return ("emoji 300 bytes", string.Concat(Enumerable.Repeat("😀", 75))); |
| 46 | + yield return ("ascii + emoji middle", new string('a', 140) + string.Concat(Enumerable.Repeat("😀", 5)) + new string('b', 140)); |
| 47 | + yield return ("ascii + emoji middle odd", new string('a', 141) + string.Concat(Enumerable.Repeat("😀", 5)) + new string('b', 140)); |
| 48 | + yield return ("mixed emoji cjk", string.Concat(Enumerable.Repeat("東😀", 50))); |
| 49 | + // AOSP and a .NET byte count disagree here (a lone surrogate is 1 byte in Java, 3 in .NET) |
| 50 | + yield return ("emoji count divergence", new string('a', 101) + string.Concat(Enumerable.Repeat("😀", 9)) + new string('b', 119)); |
| 51 | + } |
| 52 | + |
| 53 | + private static IEnumerable<string> MediaFileNames() |
| 54 | + { |
| 55 | + foreach (string ext in new[] { "mp3", "flac", "ogg", "oga", "opus", "m4a", "aac", "ape", "wav", "aiff", "aif", "wma", "wv", "alac", "mp4", "mka", "dsf", "weirdext" }) |
| 56 | + { |
| 57 | + yield return "01 Track." + ext; |
| 58 | + } |
| 59 | + yield return "cover.jpg"; |
| 60 | + yield return "folder.jpeg"; |
| 61 | + yield return "cover.png"; |
| 62 | + yield return "album.cue"; |
| 63 | + yield return "rip.log"; |
| 64 | + yield return "info.nfo"; |
| 65 | + yield return "playlist.m3u"; |
| 66 | + yield return "readme.txt"; |
| 67 | + yield return "01 Track.FLAC"; |
| 68 | + yield return "01 Track.Mp3"; |
| 69 | + yield return "a.b.flac"; |
| 70 | + yield return "noextension"; |
| 71 | + yield return "trailing."; |
| 72 | + } |
| 73 | + |
| 74 | + [SetUp] |
| 75 | + public void SetUp() |
| 76 | + { |
| 77 | + var resolver = Application.Context.ContentResolver; |
| 78 | + var tree = resolver.PersistedUriPermissions.FirstOrDefault(p => p.IsWritePermission)?.Uri; |
| 79 | + if (tree == null) |
| 80 | + { |
| 81 | + throw new InvalidOperationException("no persisted tree grant: launch the Seeker Tests app once and pick a folder"); |
| 82 | + } |
| 83 | + var root = DocumentsContract.BuildDocumentUriUsingTree(tree, DocumentsContract.GetTreeDocumentId(tree)); |
| 84 | + _scratchDir = DocumentsContract.CreateDocument(resolver, root, DocumentsContract.Document.MimeTypeDir, "parity_" + Guid.NewGuid().ToString("N")); |
| 85 | + Assert.That(_scratchDir, Is.Not.Null, "could not create scratch dir under " + tree); |
| 86 | + } |
| 87 | + |
| 88 | + [TearDown] |
| 89 | + public void TearDown() |
| 90 | + { |
| 91 | + if (_scratchDir != null) |
| 92 | + { |
| 93 | + DocumentsContract.DeleteDocument(Application.Context.ContentResolver, _scratchDir); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + [Test] |
| 98 | + public void ProviderStoresToValidFileName() |
| 99 | + { |
| 100 | + Log.Info(Tag, "provider: " + _scratchDir.Authority + " api " + (int)Android.OS.Build.VERSION.SdkInt); |
| 101 | + var mismatches = new List<string>(); |
| 102 | + foreach (var (label, name) in Cases()) |
| 103 | + { |
| 104 | + CheckOne(label + " (dir)", name, DocumentsContract.Document.MimeTypeDir, name, mismatches, providerMayFail: true); |
| 105 | + CheckOne(label + " (file)", name + ".mp3", "audio/mpeg", name + ".mp3", mismatches, providerMayFail: true); |
| 106 | + } |
| 107 | + Assert.That(mismatches, Is.Empty, string.Join("\n", mismatches)); |
| 108 | + } |
| 109 | + |
| 110 | + [Test] |
| 111 | + public void SanitizedNamesRoundTrip() |
| 112 | + { |
| 113 | + Log.Info(Tag, "provider: " + _scratchDir.Authority + " api " + (int)Android.OS.Build.VERSION.SdkInt); |
| 114 | + var mismatches = new List<string>(); |
| 115 | + foreach (var (label, name) in Cases()) |
| 116 | + { |
| 117 | + string dirName = SimpleHelpers.ToValidFileName(name); |
| 118 | + string fileName = SimpleHelpers.ToValidFileName(name + ".mp3"); |
| 119 | + CheckOne(label + " (dir, sanitized)", dirName, DocumentsContract.Document.MimeTypeDir, dirName, mismatches, providerMayFail: false); |
| 120 | + CheckOne(label + " (file, sanitized)", fileName, "audio/mpeg", fileName, mismatches, providerMayFail: false); |
| 121 | + } |
| 122 | + Assert.That(mismatches, Is.Empty, string.Join("\n", mismatches)); |
| 123 | + } |
| 124 | + |
| 125 | + // CommonHelpers.CreateMediaFile now passes the full name with MediaMimeTypes.GetMimeTypeForCreate. |
| 126 | + [Test] |
| 127 | + public void MediaFilesKeepTheirName() |
| 128 | + { |
| 129 | + Log.Info(Tag, "provider: " + _scratchDir.Authority + " api " + (int)Android.OS.Build.VERSION.SdkInt); |
| 130 | + var mismatches = new List<string>(); |
| 131 | + foreach (string name in MediaFileNames()) |
| 132 | + { |
| 133 | + string mime = MediaMimeTypes.GetMimeTypeForCreate(name); |
| 134 | + CheckOne(name + " [" + (mime ?? "null") + "]", name, mime, name, mismatches, providerMayFail: false); |
| 135 | + |
| 136 | + // PREVIOUS BEHAVIOR: since we stripped the extension, Android would take the liberty or renaming |
| 137 | + // track.ogg -> track.oga |
| 138 | + // track.opus -> track.oga |
| 139 | + // noexttrack -> noexttrack.mp3 |
| 140 | + string legacyMime = MediaMimeTypes.GetMimeTypeFromFilename(name); |
| 141 | + bool legacyFullName = legacyMime == null || legacyMime == MediaMimeTypes.M4A_MIME || legacyMime == MediaMimeTypes.APE_MIME; |
| 142 | + string legacyRequested = legacyFullName ? name : System.IO.Path.GetFileNameWithoutExtension(name); |
| 143 | + Log.Info(Tag, " legacy would store '" + CreateAndReadName(legacyRequested, legacyMime) + "'"); |
| 144 | + } |
| 145 | + Assert.That(mismatches, Is.Empty, string.Join("\n", mismatches)); |
| 146 | + } |
| 147 | + |
| 148 | + private string CreateAndReadName(string requested, string mimeType) |
| 149 | + { |
| 150 | + var resolver = Application.Context.ContentResolver; |
| 151 | + Android.Net.Uri created = null; |
| 152 | + try |
| 153 | + { |
| 154 | + created = DocumentsContract.CreateDocument(resolver, _scratchDir, mimeType, requested); |
| 155 | + return created == null ? "<null>" : QueryDisplayName(created); |
| 156 | + } |
| 157 | + catch (Exception e) |
| 158 | + { |
| 159 | + Log.Warn(Tag, " create threw for '" + requested + "': " + e); |
| 160 | + return "<threw" + e.GetType().Name + ": " + e.Message + ">"; |
| 161 | + } |
| 162 | + finally |
| 163 | + { |
| 164 | + if (created != null) |
| 165 | + { |
| 166 | + DocumentsContract.DeleteDocument(resolver, created); |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + private void CheckOne(string label, string requested, string mimeType, string original, List<string> mismatches, bool providerMayFail) |
| 172 | + { |
| 173 | + string expected = SimpleHelpers.ToValidFileName(original); |
| 174 | + string actual = CreateAndReadName(requested, mimeType); |
| 175 | + if (providerMayFail && actual.StartsWith("<threw")) |
| 176 | + { |
| 177 | + // android cannot create this name itself (i.e. its trim left half a surrogate pair), nothing to match |
| 178 | + Log.Info(Tag, " skip " + label + ": provider cannot create it, ours '" + Escape(expected) + "'"); |
| 179 | + return; |
| 180 | + } |
| 181 | + |
| 182 | + bool ok = actual == expected; |
| 183 | + Log.Info(Tag, (ok ? " same " : " DIFF ") + label + ": expected '" + Escape(expected) + "' actual '" + Escape(actual) + "'"); |
| 184 | + if (!ok) |
| 185 | + { |
| 186 | + mismatches.Add(label + ": expected '" + Escape(expected) + "' actual '" + Escape(actual) + "'"); |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + private static string QueryDisplayName(Android.Net.Uri uri) |
| 191 | + { |
| 192 | + using var cursor = Application.Context.ContentResolver.Query(uri, new[] { DocumentsContract.Document.ColumnDisplayName }, null, null, null); |
| 193 | + if (cursor == null || !cursor.MoveToFirst()) |
| 194 | + { |
| 195 | + return "<no row>"; |
| 196 | + } |
| 197 | + return cursor.GetString(0); |
| 198 | + } |
| 199 | + |
| 200 | + private static string Escape(string s) |
| 201 | + { |
| 202 | + if (s.Length > 40) |
| 203 | + { |
| 204 | + int bytes = System.Text.Encoding.UTF8.GetByteCount(s); |
| 205 | + s = s.Substring(0, 18) + "~" + s.Substring(s.Length - 18) + " [" + bytes + "B]"; |
| 206 | + } |
| 207 | + return string.Concat(s.Select(c => c < 0x20 || c == 0x7f ? "\\x" + ((int)c).ToString("x2") : c.ToString())); |
| 208 | + } |
| 209 | + } |
| 210 | +} |
0 commit comments