mirror of
https://github.com/wavetermdev/waveterm
synced 2026-05-14 20:38:28 +00:00
This adds the ability to stream `tar` archives over channels between `wsh` instances. The main use cases for this are remote copy and move operations. It also completes the `wavefs` implementation of the FileShare interface to allow copy/move interoperability between wavefiles and other storage types. The tar streaming functionality has been broken out into the new `tarcopy` package for easy reuse. New `fileshare` functions are added for `CopyInternal`, which allows copying files internal to a filesystem to bypass the expensive interop layer, and `MoveInternal`, which does the same for moving a file within a filesystem. Copying between remotes is now handled by `CopyRemote`, which accepts the source `FileShareClient` as a parameter. `wsh` connections use the same implementation for `CopyInternal` and `CopyRemote` as they need to request the channel on the remote destination, since we don't offer a way to pass channels as a parameter to a remote call. This also adds a recursive `-r` flag to `wsh file rm` to allow for deleting a directory and all its contents. S3 support will be addressed in a future PR. --------- Co-authored-by: sawka <mike@commandline.dev> Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
401 lines
11 KiB
Go
401 lines
11 KiB
Go
package connparse_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/wavetermdev/waveterm/pkg/remote/connparse"
|
|
)
|
|
|
|
func TestParseURI_WSHWithScheme(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
// Test with localhost
|
|
cstr := "wsh://user@localhost:8080/path/to/file"
|
|
c, err := connparse.ParseURI(cstr)
|
|
if err != nil {
|
|
t.Fatalf("failed to parse URI: %v", err)
|
|
}
|
|
expected := "/path/to/file"
|
|
if c.Path != expected {
|
|
t.Fatalf("expected path to be %q, got %q", expected, c.Path)
|
|
}
|
|
expected = "user@localhost:8080"
|
|
if c.Host != expected {
|
|
t.Fatalf("expected host to be %q, got %q", expected, c.Host)
|
|
}
|
|
expected = "user@localhost:8080/path/to/file"
|
|
pathWithHost := c.GetPathWithHost()
|
|
if pathWithHost != expected {
|
|
t.Fatalf("expected path with host to be %q, got %q", expected, pathWithHost)
|
|
}
|
|
expected = "wsh"
|
|
if c.Scheme != expected {
|
|
t.Fatalf("expected scheme to be %q, got %q", expected, c.Scheme)
|
|
}
|
|
if len(c.GetSchemeParts()) != 1 {
|
|
t.Fatalf("expected scheme parts to be 1, got %d", len(c.GetSchemeParts()))
|
|
}
|
|
|
|
// Test with an IP address
|
|
cstr = "wsh://user@192.168.0.1:22/path/to/file"
|
|
c, err = connparse.ParseURI(cstr)
|
|
if err != nil {
|
|
t.Fatalf("failed to parse URI: %v", err)
|
|
}
|
|
expected = "/path/to/file"
|
|
if c.Path != expected {
|
|
t.Fatalf("expected path to be %q, got %q", expected, c.Path)
|
|
}
|
|
expected = "user@192.168.0.1:22"
|
|
if c.Host != expected {
|
|
t.Fatalf("expected host to be %q, got %q", expected, c.Host)
|
|
}
|
|
expected = "user@192.168.0.1:22/path/to/file"
|
|
pathWithHost = c.GetPathWithHost()
|
|
if pathWithHost != expected {
|
|
t.Fatalf("expected path with host to be %q, got %q", expected, pathWithHost)
|
|
}
|
|
expected = "wsh"
|
|
if c.GetType() != expected {
|
|
t.Fatalf("expected conn type to be %q, got %q", expected, c.Scheme)
|
|
}
|
|
if len(c.GetSchemeParts()) != 1 {
|
|
t.Fatalf("expected scheme parts to be 1, got %d", len(c.GetSchemeParts()))
|
|
}
|
|
got := c.GetFullURI()
|
|
if got != cstr {
|
|
t.Fatalf("expected full URI to be %q, got %q", cstr, got)
|
|
}
|
|
}
|
|
|
|
func TestParseURI_WSHRemoteShorthand(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
// Test with a simple remote path
|
|
cstr := "//conn/path/to/file"
|
|
c, err := connparse.ParseURI(cstr)
|
|
if err != nil {
|
|
t.Fatalf("failed to parse URI: %v", err)
|
|
}
|
|
expected := "/path/to/file"
|
|
if c.Path != expected {
|
|
t.Fatalf("expected path to be %q, got %q", expected, c.Path)
|
|
}
|
|
if c.Host != "conn" {
|
|
t.Fatalf("expected host to be empty, got %q", c.Host)
|
|
}
|
|
expected = "wsh"
|
|
if c.Scheme != expected {
|
|
t.Fatalf("expected scheme to be %q, got %q", expected, c.Scheme)
|
|
}
|
|
expected = "wsh://conn/path/to/file"
|
|
if c.GetFullURI() != expected {
|
|
t.Fatalf("expected full URI to be %q, got %q", expected, c.GetFullURI())
|
|
}
|
|
|
|
// Test with a complex remote path
|
|
cstr = "//user@localhost:8080/path/to/file"
|
|
c, err = connparse.ParseURI(cstr)
|
|
if err != nil {
|
|
t.Fatalf("failed to parse URI: %v", err)
|
|
}
|
|
expected = "/path/to/file"
|
|
if c.Path != expected {
|
|
t.Fatalf("expected path to be %q, got %q", expected, c.Path)
|
|
}
|
|
expected = "user@localhost:8080"
|
|
if c.Host != expected {
|
|
t.Fatalf("expected host to be %q, got %q", expected, c.Host)
|
|
}
|
|
expected = "wsh"
|
|
if c.Scheme != expected {
|
|
t.Fatalf("expected scheme to be %q, got %q", expected, c.Scheme)
|
|
}
|
|
expected = "wsh://user@localhost:8080/path/to/file"
|
|
if c.GetFullURI() != expected {
|
|
t.Fatalf("expected full URI to be %q, got %q", expected, c.GetFullURI())
|
|
}
|
|
|
|
// Test with an IP address
|
|
cstr = "//user@192.168.0.1:8080/path/to/file"
|
|
c, err = connparse.ParseURI(cstr)
|
|
if err != nil {
|
|
t.Fatalf("failed to parse URI: %v", err)
|
|
}
|
|
expected = "/path/to/file"
|
|
if c.Path != expected {
|
|
t.Fatalf("expected path to be %q, got %q", expected, c.Path)
|
|
}
|
|
expected = "user@192.168.0.1:8080"
|
|
if c.Host != expected {
|
|
t.Fatalf("expected host to be %q, got %q", expected, c.Host)
|
|
}
|
|
expected = "wsh"
|
|
if c.Scheme != expected {
|
|
t.Fatalf("expected scheme to be %q, got %q", expected, c.Scheme)
|
|
}
|
|
expected = "wsh://user@192.168.0.1:8080/path/to/file"
|
|
if c.GetFullURI() != expected {
|
|
t.Fatalf("expected full URI to be %q, got %q", expected, c.GetFullURI())
|
|
}
|
|
}
|
|
|
|
func TestParseURI_WSHCurrentPathShorthand(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
// Test with a relative path to home
|
|
cstr := "~/path/to/file"
|
|
c, err := connparse.ParseURI(cstr)
|
|
if err != nil {
|
|
t.Fatalf("failed to parse URI: %v", err)
|
|
}
|
|
expected := "~/path/to/file"
|
|
if c.Path != expected {
|
|
t.Fatalf("expected path to be %q, got %q", expected, c.Path)
|
|
}
|
|
expected = "current"
|
|
if c.Host != expected {
|
|
t.Fatalf("expected host to be %q, got %q", expected, c.Host)
|
|
}
|
|
expected = "wsh"
|
|
if c.Scheme != expected {
|
|
t.Fatalf("expected scheme to be %q, got %q", expected, c.Scheme)
|
|
}
|
|
expected = "wsh://current/~/path/to/file"
|
|
if c.GetFullURI() != expected {
|
|
t.Fatalf("expected full URI to be %q, got %q", expected, c.GetFullURI())
|
|
}
|
|
|
|
// Test with a absolute path
|
|
cstr = "/path/to/file"
|
|
c, err = connparse.ParseURI(cstr)
|
|
if err != nil {
|
|
t.Fatalf("expected nil, got %v", err)
|
|
}
|
|
expected = "/path/to/file"
|
|
if c.Path != expected {
|
|
t.Fatalf("expected path to be %q, got %q", expected, c.Path)
|
|
}
|
|
expected = "current"
|
|
if c.Host != expected {
|
|
t.Fatalf("expected host to be %q, got %q", expected, c.Host)
|
|
}
|
|
expected = "wsh"
|
|
if c.Scheme != expected {
|
|
t.Fatalf("expected scheme to be %q, got %q", expected, c.Scheme)
|
|
}
|
|
expected = "wsh://current/path/to/file"
|
|
if c.GetFullURI() != expected {
|
|
t.Fatalf("expected full URI to be %q, got %q", expected, c.GetFullURI())
|
|
}
|
|
}
|
|
|
|
func TestParseURI_WSHCurrentPath(t *testing.T) {
|
|
cstr := "./Documents/path/to/file"
|
|
c, err := connparse.ParseURI(cstr)
|
|
if err != nil {
|
|
t.Fatalf("failed to parse URI: %v", err)
|
|
}
|
|
expected := "./Documents/path/to/file"
|
|
if c.Path != expected {
|
|
t.Fatalf("expected path to be %q, got %q", expected, c.Path)
|
|
}
|
|
expected = "current"
|
|
if c.Host != expected {
|
|
t.Fatalf("expected host to be %q, got %q", expected, c.Host)
|
|
}
|
|
expected = "wsh"
|
|
if c.Scheme != expected {
|
|
t.Fatalf("expected scheme to be %q, got %q", expected, c.Scheme)
|
|
}
|
|
expected = "wsh://current/./Documents/path/to/file"
|
|
if c.GetFullURI() != expected {
|
|
t.Fatalf("expected full URI to be %q, got %q", expected, c.GetFullURI())
|
|
}
|
|
}
|
|
|
|
func TestParseURI_WSHCurrentPathWindows(t *testing.T) {
|
|
cstr := ".\\Documents\\path\\to\\file"
|
|
c, err := connparse.ParseURI(cstr)
|
|
if err != nil {
|
|
t.Fatalf("failed to parse URI: %v", err)
|
|
}
|
|
expected := ".\\Documents\\path\\to\\file"
|
|
if c.Path != expected {
|
|
t.Fatalf("expected path to be %q, got %q", expected, c.Path)
|
|
}
|
|
expected = "current"
|
|
if c.Host != expected {
|
|
t.Fatalf("expected host to be %q, got %q", expected, c.Host)
|
|
}
|
|
expected = "wsh"
|
|
if c.Scheme != expected {
|
|
t.Fatalf("expected scheme to be %q, got %q", expected, c.Scheme)
|
|
}
|
|
expected = "wsh://current/.\\Documents\\path\\to\\file"
|
|
if c.GetFullURI() != expected {
|
|
t.Fatalf("expected full URI to be %q, got %q", expected, c.GetFullURI())
|
|
}
|
|
}
|
|
|
|
func TestParseURI_WSHLocalShorthand(t *testing.T) {
|
|
t.Parallel()
|
|
cstr := "/~/path/to/file"
|
|
c, err := connparse.ParseURI(cstr)
|
|
if err != nil {
|
|
t.Fatalf("failed to parse URI: %v", err)
|
|
}
|
|
expected := "~/path/to/file"
|
|
if c.Path != expected {
|
|
t.Fatalf("expected path to be %q, got %q", expected, c.Path)
|
|
}
|
|
if c.Host != "local" {
|
|
t.Fatalf("expected host to be empty, got %q", c.Host)
|
|
}
|
|
expected = "wsh"
|
|
if c.Scheme != expected {
|
|
t.Fatalf("expected scheme to be %q, got %q", expected, c.Scheme)
|
|
}
|
|
|
|
cstr = "wsh:///~/path/to/file"
|
|
c, err = connparse.ParseURI(cstr)
|
|
if err != nil {
|
|
t.Fatalf("failed to parse URI: %v", err)
|
|
}
|
|
expected = "~/path/to/file"
|
|
if c.Path != expected {
|
|
t.Fatalf("expected path to be %q, got %q", expected, c.Path)
|
|
}
|
|
if c.Host != "local" {
|
|
t.Fatalf("expected host to be empty, got %q", c.Host)
|
|
}
|
|
expected = "wsh"
|
|
if c.Scheme != expected {
|
|
t.Fatalf("expected scheme to be %q, got %q", expected, c.Scheme)
|
|
}
|
|
expected = "wsh://local/~/path/to/file"
|
|
if c.GetFullURI() != expected {
|
|
t.Fatalf("expected full URI to be %q, got %q", expected, c.GetFullURI())
|
|
}
|
|
}
|
|
|
|
func TestParseURI_WSHWSL(t *testing.T) {
|
|
t.Parallel()
|
|
cstr := "wsh://wsl://Ubuntu/path/to/file"
|
|
|
|
testUri := func() {
|
|
c, err := connparse.ParseURI(cstr)
|
|
if err != nil {
|
|
t.Fatalf("failed to parse URI: %v", err)
|
|
}
|
|
expected := "/path/to/file"
|
|
if c.Path != expected {
|
|
t.Fatalf("expected path to be %q, got %q", expected, c.Path)
|
|
}
|
|
expected = "wsl://Ubuntu"
|
|
if c.Host != expected {
|
|
t.Fatalf("expected host to be %q, got %q", expected, c.Host)
|
|
}
|
|
expected = "wsh"
|
|
if c.Scheme != expected {
|
|
t.Fatalf("expected scheme to be %q, got %q", expected, c.Scheme)
|
|
}
|
|
expected = "wsh://wsl://Ubuntu/path/to/file"
|
|
if expected != c.GetFullURI() {
|
|
t.Fatalf("expected full URI to be %q, got %q", expected, c.GetFullURI())
|
|
}
|
|
}
|
|
t.Log("Testing with scheme")
|
|
testUri()
|
|
|
|
t.Log("Testing without scheme")
|
|
cstr = "//wsl://Ubuntu/path/to/file"
|
|
testUri()
|
|
}
|
|
|
|
func TestParseUri_LocalWindowsAbsPath(t *testing.T) {
|
|
t.Parallel()
|
|
cstr := "wsh://local/C:\\path\\to\\file"
|
|
|
|
testAbsPath := func() {
|
|
c, err := connparse.ParseURI(cstr)
|
|
if err != nil {
|
|
t.Fatalf("failed to parse URI: %v", err)
|
|
}
|
|
expected := "C:\\path\\to\\file"
|
|
if c.Path != expected {
|
|
t.Fatalf("expected path to be %q, got %q", expected, c.Path)
|
|
}
|
|
expected = "local"
|
|
if c.Host != expected {
|
|
t.Fatalf("expected host to be %q, got %q", expected, c.Host)
|
|
}
|
|
expected = "wsh"
|
|
if c.Scheme != expected {
|
|
t.Fatalf("expected scheme to be %q, got %q", expected, c.Scheme)
|
|
}
|
|
expected = "wsh://local/C:\\path\\to\\file"
|
|
if c.GetFullURI() != expected {
|
|
t.Fatalf("expected full URI to be %q, got %q", expected, c.GetFullURI())
|
|
}
|
|
}
|
|
|
|
t.Log("Testing with scheme")
|
|
testAbsPath()
|
|
t.Log("Testing without scheme")
|
|
cstr = "//local/C:\\path\\to\\file"
|
|
testAbsPath()
|
|
}
|
|
|
|
func TestParseURI_LocalWindowsRelativeShorthand(t *testing.T) {
|
|
cstr := "/~\\path\\to\\file"
|
|
c, err := connparse.ParseURI(cstr)
|
|
if err != nil {
|
|
t.Fatalf("failed to parse URI: %v", err)
|
|
}
|
|
expected := "~\\path\\to\\file"
|
|
if c.Path != expected {
|
|
t.Fatalf("expected path to be %q, got %q", expected, c.Path)
|
|
}
|
|
expected = "local"
|
|
if c.Host != expected {
|
|
t.Fatalf("expected host to be %q, got %q", expected, c.Host)
|
|
}
|
|
expected = "wsh"
|
|
if c.Scheme != expected {
|
|
t.Fatalf("expected scheme to be %q, got %q", expected, c.Scheme)
|
|
}
|
|
expected = "wsh://local/~\\path\\to\\file"
|
|
if c.GetFullURI() != expected {
|
|
t.Fatalf("expected full URI to be %q, got %q", expected, c.GetFullURI())
|
|
}
|
|
}
|
|
|
|
func TestParseURI_BasicS3(t *testing.T) {
|
|
t.Parallel()
|
|
cstr := "profile:s3://bucket/path/to/file"
|
|
c, err := connparse.ParseURI(cstr)
|
|
if err != nil {
|
|
t.Fatalf("failed to parse URI: %v", err)
|
|
}
|
|
expected := "path/to/file"
|
|
if c.Path != expected {
|
|
t.Fatalf("expected path to be %q, got %q", expected, c.Path)
|
|
}
|
|
expected = "bucket"
|
|
if c.Host != expected {
|
|
t.Fatalf("expected host to be %q, got %q", expected, c.Host)
|
|
}
|
|
expected = "bucket/path/to/file"
|
|
pathWithHost := c.GetPathWithHost()
|
|
if pathWithHost != expected {
|
|
t.Fatalf("expected path with host to be %q, got %q", expected, pathWithHost)
|
|
}
|
|
expected = "s3"
|
|
if c.GetType() != expected {
|
|
t.Fatalf("expected conn type to be %q, got %q", expected, c.GetType())
|
|
}
|
|
if len(c.GetSchemeParts()) != 2 {
|
|
t.Fatalf("expected scheme parts to be 2, got %d", len(c.GetSchemeParts()))
|
|
}
|
|
}
|