navidrome/core/storage/storage_test.go
Deluan Quintão 0da2352907
fix: improve URL path handling in local storage for special characters (#4378)
* refactor: improve URL path handling in local storage system

Enhanced the local storage implementation to properly handle URL-decoded paths
and fix issues with file paths containing special characters. Added decodedPath
field to localStorage struct to separate URL parsing concerns from file system
operations.

Key changes:
- Added decodedPath field to localStorage struct for proper URL decoding
- Modified newLocalStorage to use decoded path instead of modifying original URL
- Fixed Windows path handling to work with decoded paths
- Improved URL escaping in storage.For() to handle special characters
- Added comprehensive test suite covering URL decoding, symlink resolution,
  Windows paths, and edge cases
- Refactored test extractor to use mockTestExtractor for better isolation

This ensures that file paths with spaces, hash symbols, and other special
characters are handled correctly throughout the storage system.

Signed-off-by: Deluan <deluan@navidrome.org>

* fix(tests): fix test file permissions and add missing tests.Init call

* refactor(tests): remove redundant test

Signed-off-by: Deluan <deluan@navidrome.org>

* fix: URL building for Windows and remove redundant variable

Signed-off-by: Deluan <deluan@navidrome.org>

* refactor: simplify URL path escaping in local storage

Signed-off-by: Deluan <deluan@navidrome.org>

---------

Signed-off-by: Deluan <deluan@navidrome.org>
2025-07-23 20:46:47 -04:00

94 lines
2.9 KiB
Go

package storage
import (
"net/url"
"os"
"path/filepath"
"testing"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
func TestApp(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Storage Test Suite")
}
var _ = Describe("Storage", func() {
When("schema is not registered", func() {
BeforeEach(func() {
registry = map[string]constructor{}
})
It("should return error", func() {
_, err := For("file:///tmp")
Expect(err).To(HaveOccurred())
})
})
When("schema is registered", func() {
BeforeEach(func() {
registry = map[string]constructor{}
Register("file", func(url url.URL) Storage { return &fakeLocalStorage{u: url} })
Register("s3", func(url url.URL) Storage { return &fakeS3Storage{u: url} })
})
It("should return correct implementation", func() {
s, err := For("file:///tmp")
Expect(err).ToNot(HaveOccurred())
Expect(s).To(BeAssignableToTypeOf(&fakeLocalStorage{}))
Expect(s.(*fakeLocalStorage).u.Scheme).To(Equal("file"))
Expect(s.(*fakeLocalStorage).u.Path).To(Equal("/tmp"))
s, err = For("s3:///bucket")
Expect(err).ToNot(HaveOccurred())
Expect(s).To(BeAssignableToTypeOf(&fakeS3Storage{}))
Expect(s.(*fakeS3Storage).u.Scheme).To(Equal("s3"))
Expect(s.(*fakeS3Storage).u.Path).To(Equal("/bucket"))
})
It("should return a file implementation when schema is not specified", func() {
s, err := For("/tmp")
Expect(err).ToNot(HaveOccurred())
Expect(s).To(BeAssignableToTypeOf(&fakeLocalStorage{}))
Expect(s.(*fakeLocalStorage).u.Scheme).To(Equal("file"))
Expect(s.(*fakeLocalStorage).u.Path).To(Equal("/tmp"))
})
It("should return a file implementation for a relative folder", func() {
s, err := For("tmp")
Expect(err).ToNot(HaveOccurred())
cwd, _ := os.Getwd()
Expect(s).To(BeAssignableToTypeOf(&fakeLocalStorage{}))
Expect(s.(*fakeLocalStorage).u.Scheme).To(Equal("file"))
Expect(s.(*fakeLocalStorage).u.Path).To(Equal(filepath.Join(cwd, "tmp")))
})
It("should return error if schema is unregistered", func() {
_, err := For("webdav:///tmp")
Expect(err).To(HaveOccurred())
})
DescribeTable("should handle paths with special characters correctly",
func(inputPath string) {
s, err := For(inputPath)
Expect(err).ToNot(HaveOccurred())
Expect(s).To(BeAssignableToTypeOf(&fakeLocalStorage{}))
Expect(s.(*fakeLocalStorage).u.Scheme).To(Equal("file"))
// The path should be exactly the same as the input - after URL parsing it gets decoded back
Expect(s.(*fakeLocalStorage).u.Path).To(Equal(inputPath))
},
Entry("hash symbols", "/tmp/test#folder/file.mp3"),
Entry("spaces", "/tmp/test folder/file with spaces.mp3"),
Entry("question marks", "/tmp/test?query/file.mp3"),
Entry("ampersands", "/tmp/test&amp/file.mp3"),
Entry("multiple special chars", "/tmp/Song #1 & More?.mp3"),
)
})
})
type fakeLocalStorage struct {
Storage
u url.URL
}
type fakeS3Storage struct {
Storage
u url.URL
}