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 bool StartWithInvalidWindowsPrefix(string name)
{
switch (name[0])
if (name.Length >= 3)
{
case 'a':
case 'A':
switch (name[1])
{
case 'u':
case 'U':
switch (name[2])
{
case 'x':
case 'X':
return name[3] == '.'; // AUX.
}
break;
}
break;
case 'c':
case 'C':
switch (name[1])
{
case 'o':
case 'O':
switch (name[2])
{
case 'n':
case 'N':
return name[3] == '.'; // CON.
case 'm':
case 'M':
return char.IsDigit(name[3]) && name[4] == '.'; // COM0 - COM9
}
break;
}
break;
case 'l':
case 'L':
switch (name[1])
{
case 'p':
case 'P':
switch (name[2])
{
case 't':
case 'T':
return char.IsDigit(name[3]) && name[4] == '.'; // LPT0 - LPT9
}
break;
}
break;
case 'p':
case 'P':
switch (name[1])
{
case 'r':
case 'R':
switch (name[2])
{
case 'n':
case 'N':
return name[3] == '.'; // PRN.
}
break;
}
break;
case 'n':
case 'N':
switch (name[1])
{
case 'u':
case 'U':
switch (name[2])
{
case 'l':
case 'L':
return name[3] == '.'; // NUL.
}
break;
}
break;
switch (name[0])
{
case 'a':
case 'A':
switch (name[1])
{
case 'u':
case 'U':
switch (name[2])
{
case 'x':
case 'X':
return name.Length == 3 || name[3] == '.'; // AUX.
}
break;
}
break;
case 'c':
case 'C':
switch (name[1])
{
case 'o':
case 'O':
switch (name[2])
{
case 'n':
case 'N':
return name.Length == 3 || name[3] == '.'; // CON.
case 'm':
case 'M':
return name.Length >= 4 && char.IsDigit(name[3]) && (name.Length == 4 || name[4] == '.'); // COM0 - COM9
}
break;
}
break;
case 'l':
case 'L':
switch (name[1])
{
case 'p':
case 'P':
switch (name[2])
{
case 't':
case 'T':
return name.Length >= 4 && char.IsDigit(name[3]) && (name.Length == 4 || name[4] == '.'); // LPT0 - LPT9
}
break;
}
break;
case 'p':
case 'P':
switch (name[1])
{
case 'r':
case 'R':
switch (name[2])
{
case 'n':
case 'N':
return name.Length == 3 || name[3] == '.'; // PRN.
}
break;
}
break;
case 'n':
case 'N':
switch (name[1])
{
case 'u':
case 'U':
switch (name[2])
{
case 'l':
case 'L':
return name.Length == 3 || name[3] == '.'; // NUL.
}
break;
}
break;
}
}
return false;
}

Loading…
Cancel
Save

Powered by TurnKey Linux.