Skip to content

Commit

Permalink
Test path manipulation methods sanitise correctly
Browse files Browse the repository at this point in the history
There's some nuance here with pattern vs non-pattern characters, so
useful to test for that.
  • Loading branch information
SquidDev committed Aug 18, 2024
1 parent e57b6fe commit 80c7a54
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ private FileSystem getFileSystem() {
* }</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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public synchronized BasicFileAttributes getAttributes(String path) throws FileSy
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);

Expand All @@ -165,10 +165,8 @@ public synchronized String[] list(String path) throws FileSystemException {
}

// 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 {
Expand Down
24 changes: 24 additions & 0 deletions projects/core/src/test/resources/test-rom/spec/apis/fs_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand Down

0 comments on commit 80c7a54

Please sign in to comment.