1

I want to create a file with null character in its name on mac. filename like - abc\0ds.txt I tried creating a file with name 'abc\0ahd.txt' but \0 in the name is not really unicode null character. It is some other character. I want to create a file with null character in its name because I want to reproduce a scenario for integration tests of my application which uses fileItemStream.getName() (org.apache.commons.fileupload.FileUploadBase) to get filename and then it should throw InvalidFileNameException exception (org.apache.commons.fileupload.InvalidFileNameException)

We get such scenarios in production and get logs like "org.apache.commons.fileupload.InvalidFileNameException: Invalid file name: uploadertest.asp\0.png" but I can't reproduce this by uploading a file from my system or integrations tests but it is reproducible from unit tests when I send byte stream like this.

byte[] formData = getFormData(boundary, defaultFormParams, invalidFileNameWithNullCharacter);

MockHttpServletRequestBuilder request = getRequest(boundary, formData);
Olaf Kock
  • 46,930
  • 8
  • 59
  • 90
Himanshu Sharma
  • 101
  • 1
  • 4
  • In short - I guess you want to produce a file in macOS with null char in its name so you can perform your integration testing. – Nishant Dwivedi May 08 '20 at 12:22
  • Yes, I have tried this but it's not working https://superuser.com/questions/293260/how-can-i-write-the-null-character-on-a-macbook-pro – Himanshu Sharma May 08 '20 at 12:25
  • 1
    I don't quite understand. Just because some upload claims that it provides a file with a `\0` in the file name does **not** mean that the client has such a file anywhere on disk. They might be *lying* to you. And in a similar vain, I don't understand how writing such a file would help you with your tests. Basically UNIX type systems disallow that character in file names since it's the end-of-string indicator and I'd be surprised if Mac OS doesn't do the same thing. – Joachim Sauer May 08 '20 at 12:27
  • 3
    @JoachimSauer you are right one cannot create a file with a name having \0 in mac also. So for integration testing, I suggest reusing formData and create the file with name \0 as suggested in the above question. – Nishant Dwivedi May 08 '20 at 13:07
  • @Robert I guess he asking to rename or create a new file with \0 in its name and not to edit the file. – Nishant Dwivedi May 08 '20 at 13:16

1 Answers1

0

So I got the answer. It is "NO, I can never create a file with a null character in its name" If anyone will try to create a file with null character in name then UNIX will not allow it and throw below exception if tried using java code

Exception in thread "main" java.nio.file.InvalidPathException: Nul character not allowed: /Users/hhims/Documents/TestCode/a.txt
    at sun.nio.fs.UnixPath.checkNotNul(UnixPath.java:93)
    at sun.nio.fs.UnixPath.normalizeAndCheck(UnixPath.java:83)
    at sun.nio.fs.UnixPath.<init>(UnixPath.java:71)
    at sun.nio.fs.UnixFileSystem.getPath(UnixFileSystem.java:281)
    at java.io.File.toPath(File.java:2234)
    at sun.nio.fs.UnixUriUtils.fromUri(UnixUriUtils.java:61)
    at sun.nio.fs.UnixFileSystemProvider.getPath(UnixFileSystemProvider.java:98)
    at java.nio.file.Paths.get(Paths.java:138)
    at Main.testExists(Main.java:35)
    at Main.main(Main.java:24)

For solving my problem I created a byte stream, set name and boundary of file and made a http request. Didn't save file on disk as it is not possible.

Himanshu Sharma
  • 101
  • 1
  • 4