1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-01-23 15:36:54 +00:00

Test path manipulation methods sanitise correctly

There's some nuance here with pattern vs non-pattern characters, so
useful to test for that.
This commit is contained in:
Jonathan Coates 2024-08-18 12:49:33 +01:00
parent e57b6fede2
commit 80c7a54ad4
No known key found for this signature in database
GPG Key ID: B9E431FF07C98D06
3 changed files with 28 additions and 6 deletions

View File

@ -101,7 +101,7 @@ public class FSAPI implements ILuaAPI {
* }</pre>
*/
@LuaFunction
public final String[] list(String path) throws LuaException {
public final List<String> list(String path) throws LuaException {
try (var ignored = environment.time(Metrics.FS_OPS)) {
return getFileSystem().list(path);
} catch (FileSystemException e) {

View File

@ -149,7 +149,7 @@ public class FileSystem {
return getMount(sanitizePath(path)).getAttributes(sanitizePath(path));
}
public synchronized String[] list(String path) throws FileSystemException {
public synchronized List<String> list(String path) throws FileSystemException {
path = sanitizePath(path);
var mount = getMount(path);
@ -165,10 +165,8 @@ public class FileSystem {
}
// Return list
var array = new String[list.size()];
list.toArray(array);
Arrays.sort(array);
return array;
list.sort(Comparator.naturalOrder());
return list;
}
public synchronized boolean exists(String path) throws FileSystemException {

View File

@ -158,6 +158,14 @@ describe("The fs library", function()
expect(fs.combine("", "a")):eq("a")
expect(fs.combine("a", "", "b", "c")):eq("a/b/c")
end)
it("preserves pattern characters", function()
expect(fs.combine("foo*?")):eq("foo*?")
end)
it("sanitises paths", function()
expect(fs.combine("foo\":<>|")):eq("foo")
end)
end)
describe("fs.getName", function()
@ -175,6 +183,14 @@ describe("The fs library", function()
it("returns '..' for parent directories", function()
expect(fs.getName("..")):eq("..")
end)
it("preserves pattern characters", function()
expect(fs.getName("foo*?")):eq("foo*?")
end)
it("sanitises paths", function()
expect(fs.getName("foo\":<>|")):eq("foo")
end)
end)
describe("fs.getDir", function()
@ -193,6 +209,14 @@ describe("The fs library", function()
expect(fs.getDir("..")):eq("../..")
expect(fs.getDir("../..")):eq("../../..")
end)
it("preserves pattern characters", function()
expect(fs.getDir("foo*?/x")):eq("foo*?")
end)
it("sanitises paths", function()
expect(fs.getDir("foo\":<>|/x")):eq("foo")
end)
end)
describe("fs.getSize", function()