Repository: vmware/govmomi
Pull request #2719 (2022-01-27 03:30:12)

vcsim: Fix CreateFolder to encode folder name
Closes: #2717
Signed-off-by: syuparn <s.hello.spagetti@gmail.com>

## Description

Fix vcsim `CreateFolder()` to encode special characters in requested folder name

Closes: #2717 

## Type of change

Please mark options that are relevant:

- [x] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to
  not work as expected)
- [ ] This change requires a documentation update
- [ ] Build related change

## How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide
instructions so we can reproduce. If applicable, please also list any relevant
details for your test configuration.

- add unit tests to check special characters are escaped

- [ ] Test Description 1
- [ ] Test Description 2

## Checklist:

- [x] My code follows the CONTRIBUTION [guidelines](./../CONTRIBUTING.md) of
  this project
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [x] I have added tests that prove my fix is effective or that my feature works
- [x] New and existing unit tests pass locally with my changes
- [ ] Any dependent changes have been merged


Patch:
diff --git a/simulator/folder.go b/simulator/folder.go
index a329112f..7048a688 100644
--- a/simulator/folder.go
+++ b/simulator/folder.go
@@ -20,6 +20,7 @@ import (
 	"errors"
 	"fmt"
 	"math/rand"
+	"net/url"
 	"path"
 	"strings"
 
@@ -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,
 			})
 
@@ -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)
@@ -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


Test patch:
diff --git a/simulator/folder_test.go b/simulator/folder_test.go
index a84c7c64..9fb3e0b5 100644
--- a/simulator/folder_test.go
+++ b/simulator/folder_test.go
@@ -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"}