Skip to content

Commit

Permalink
vcsim: Fix CreateFolder to encode folder name
Browse files Browse the repository at this point in the history
Closes: #2717
Signed-off-by: syuparn <s.hello.spagetti@gmail.com>
  • Loading branch information
Syuparn committed Jan 27, 2022
1 parent 7c2cf2b commit 6542ccb
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
16 changes: 13 additions & 3 deletions simulator/folder.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"errors"
"fmt"
"math/rand"
"net/url"
"path"
"strings"

Expand Down Expand Up @@ -198,9 +199,11 @@ func (f *Folder) CreateFolder(ctx *Context, c *types.CreateFolder) soap.HasFault
r := &methods.CreateFolderBody{}

if folderHasChildType(&f.Folder, "Folder") {
if obj := ctx.Map.FindByName(c.Name, f.ChildEntity); obj != nil {
name := f.escapeSpecialCharacters(c.Name)

if obj := ctx.Map.FindByName(name, f.ChildEntity); obj != nil {
r.Fault_ = Fault("", &types.DuplicateName{
Name: c.Name,
Name: name,
Object: f.Self,
})

Expand All @@ -209,7 +212,7 @@ func (f *Folder) CreateFolder(ctx *Context, c *types.CreateFolder) soap.HasFault

folder := &Folder{}

folder.Name = c.Name
folder.Name = name
folder.ChildType = f.ChildType

folderPutChild(ctx, &f.Folder, folder)
Expand All @@ -224,6 +227,13 @@ func (f *Folder) CreateFolder(ctx *Context, c *types.CreateFolder) soap.HasFault
return r
}

func (f *Folder) escapeSpecialCharacters(name string) string {
name = strings.ReplaceAll(name, `%`, strings.ToLower(url.QueryEscape(`%`)))
name = strings.ReplaceAll(name, `/`, strings.ToLower(url.QueryEscape(`/`)))
name = strings.ReplaceAll(name, `\`, strings.ToLower(url.QueryEscape(`\`)))
return name
}

// StoragePod aka "Datastore Cluster"
type StoragePod struct {
mo.StoragePod
Expand Down
44 changes: 44 additions & 0 deletions simulator/folder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,50 @@ func TestFolderVC(t *testing.T) {
}
}

func TestFolderSpecialCharaters(t *testing.T) {
content := vpx.ServiceContent
s := New(NewServiceInstance(SpoofContext(), content, vpx.RootFolder))

ts := s.NewServer()
defer ts.Close()

ctx := context.Background()
c, err := govmomi.NewClient(ctx, ts.URL, true)
if err != nil {
t.Fatal(err)
}

f := object.NewRootFolder(c.Client)

tests := []struct {
name string
expected string
}{
{`/`, `%2f`},
{`\`, `%5c`},
{`%`, `%25`},
// multiple special characters
{`%%`, `%25%25`},
}

for _, test := range tests {
ff, err := f.CreateFolder(ctx, test.name)
if err != nil {
t.Fatal(err)
}

o := Map.Get(ff.Reference())
if o == nil {
t.Fatalf("failed to find %#v", ff)
}

e := o.(mo.Entity).Entity()
if e.Name != test.expected {
t.Errorf("expected %s, got %s", test.expected, e.Name)
}
}
}

func TestFolderFaults(t *testing.T) {
f := Folder{}
f.ChildType = []string{"VirtualMachine"}
Expand Down

0 comments on commit 6542ccb

Please sign in to comment.