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));
}
}
}
}

@ -58,6 +58,8 @@ namespace ZeroLevel.Services.FileSystem
private static string _invalid_filename_characters = new string(Path.GetInvalidFileNameChars());
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)
{
if (name.Length >= 3)
{
switch (name[0])
{
@ -71,7 +73,7 @@ namespace ZeroLevel.Services.FileSystem
{
case 'x':
case 'X':
return name[3] == '.'; // AUX.
return name.Length == 3 || name[3] == '.'; // AUX.
}
break;
}
@ -86,10 +88,10 @@ namespace ZeroLevel.Services.FileSystem
{
case 'n':
case 'N':
return name[3] == '.'; // CON.
return name.Length == 3 || name[3] == '.'; // CON.
case 'm':
case 'M':
return char.IsDigit(name[3]) && name[4] == '.'; // COM0 - COM9
return name.Length >= 4 && char.IsDigit(name[3]) && (name.Length == 4 || name[4] == '.'); // COM0 - COM9
}
break;
}
@ -104,7 +106,7 @@ namespace ZeroLevel.Services.FileSystem
{
case 't':
case 'T':
return char.IsDigit(name[3]) && name[4] == '.'; // LPT0 - LPT9
return name.Length >= 4 && char.IsDigit(name[3]) && (name.Length == 4 || name[4] == '.'); // LPT0 - LPT9
}
break;
}
@ -119,7 +121,7 @@ namespace ZeroLevel.Services.FileSystem
{
case 'n':
case 'N':
return name[3] == '.'; // PRN.
return name.Length == 3 || name[3] == '.'; // PRN.
}
break;
}
@ -134,12 +136,13 @@ namespace ZeroLevel.Services.FileSystem
{
case 'l':
case 'L':
return name[3] == '.'; // NUL.
return name.Length == 3 || name[3] == '.'; // NUL.
}
break;
}
break;
}
}
return false;
}

Loading…
Cancel
Save

Powered by TurnKey Linux.