Fix FSUtils.FileNameCorrection

Unit tests and fixes
pull/1/head
unknown 3 years ago
parent 7b90289582
commit 0060b766b5

@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using Xunit;
using ZeroLevel.Services.FileSystem;
namespace ZeroLevel.UnitTests
{
public class FSUtilsTest
{
[Fact]
public void FileNameCorrectionTest()
{
// Arrange
var validNames = new List<string> { "CON1", "a.cOn.a", "$PRN", "LPT10", "lpt10.txt", "COM11.ee" };
var invalidNames = new List<string> { "CON", "cOn", "PRN", "LPT4", "LPT4.", "LPT4.txt", "COM1.ee" };
var invalidRootNames = new List<string> { "$mft", "$mftmirr", "$logfile", "$volume", "$attrdef", "$bitmap", "$boot", "$badclus", "$secure", "$upcase", "$extend", "$quota", "$objid", "$reparse" };
// Act
// Assert
foreach (var validName in validNames)
{
Assert.Equal(validName, FSUtils.FileNameCorrection(validName));
}
foreach (var invalidName in invalidNames)
{
Assert.NotEqual(invalidName, FSUtils.FileNameCorrection(invalidName));
}
foreach (var invalidName in invalidRootNames)
{
Assert.NotEqual(invalidName, FSUtils.FileNameCorrection(invalidName, true));
}
}
}
}

@ -59,86 +59,89 @@ namespace ZeroLevel.Services.FileSystem
private static HashSet<string> _invalidRootFileNames = new HashSet<string> { "$mft", "$mftmirr", "$logfile", "$volume", "$attrdef", "$bitmap", "$boot", "$badclus", "$secure", "$upcase", "$extend", "$quota", "$objid", "$reparse" }; private static HashSet<string> _invalidRootFileNames = new HashSet<string> { "$mft", "$mftmirr", "$logfile", "$volume", "$attrdef", "$bitmap", "$boot", "$badclus", "$secure", "$upcase", "$extend", "$quota", "$objid", "$reparse" };
private static bool StartWithInvalidWindowsPrefix(string name) private static bool StartWithInvalidWindowsPrefix(string name)
{ {
switch (name[0]) if (name.Length >= 3)
{ {
case 'a': switch (name[0])
case 'A': {
switch (name[1]) case 'a':
{ case 'A':
case 'u': switch (name[1])
case 'U': {
switch (name[2]) case 'u':
{ case 'U':
case 'x': switch (name[2])
case 'X': {
return name[3] == '.'; // AUX. case 'x':
} case 'X':
break; return name.Length == 3 || name[3] == '.'; // AUX.
} }
break; break;
case 'c': }
case 'C': break;
switch (name[1]) case 'c':
{ case 'C':
case 'o': switch (name[1])
case 'O': {
switch (name[2]) case 'o':
{ case 'O':
case 'n': switch (name[2])
case 'N': {
return name[3] == '.'; // CON. case 'n':
case 'm': case 'N':
case 'M': return name.Length == 3 || name[3] == '.'; // CON.
return char.IsDigit(name[3]) && name[4] == '.'; // COM0 - COM9 case 'm':
} case 'M':
break; return name.Length >= 4 && char.IsDigit(name[3]) && (name.Length == 4 || name[4] == '.'); // COM0 - COM9
} }
break; break;
case 'l': }
case 'L': break;
switch (name[1]) case 'l':
{ case 'L':
case 'p': switch (name[1])
case 'P': {
switch (name[2]) case 'p':
{ case 'P':
case 't': switch (name[2])
case 'T': {
return char.IsDigit(name[3]) && name[4] == '.'; // LPT0 - LPT9 case 't':
} case 'T':
break; return name.Length >= 4 && char.IsDigit(name[3]) && (name.Length == 4 || name[4] == '.'); // LPT0 - LPT9
} }
break; break;
case 'p': }
case 'P': break;
switch (name[1]) case 'p':
{ case 'P':
case 'r': switch (name[1])
case 'R': {
switch (name[2]) case 'r':
{ case 'R':
case 'n': switch (name[2])
case 'N': {
return name[3] == '.'; // PRN. case 'n':
} case 'N':
break; return name.Length == 3 || name[3] == '.'; // PRN.
} }
break; break;
case 'n': }
case 'N': break;
switch (name[1]) case 'n':
{ case 'N':
case 'u': switch (name[1])
case 'U': {
switch (name[2]) case 'u':
{ case 'U':
case 'l': switch (name[2])
case 'L': {
return name[3] == '.'; // NUL. case 'l':
} case 'L':
break; return name.Length == 3 || name[3] == '.'; // NUL.
} }
break; break;
}
break;
}
} }
return false; return false;
} }

Loading…
Cancel
Save

Powered by TurnKey Linux.