github.com/cs3org/reva/v2@v2.27.7/internal/http/services/owncloud/ocdav/net/net_test.go (about)

     1  // Copyright 2018-2022 CERN
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  // In applying this license, CERN does not waive the privileges and immunities
    16  // granted to it by virtue of its status as an Intergovernmental Organization
    17  // or submit itself to any jurisdiction.
    18  
    19  package net_test
    20  
    21  import (
    22  	"time"
    23  
    24  	"github.com/cs3org/reva/v2/internal/http/services/owncloud/ocdav/net"
    25  
    26  	. "github.com/onsi/ginkgo/v2"
    27  	. "github.com/onsi/gomega"
    28  	. "github.com/onsi/gomega/gmeasure"
    29  )
    30  
    31  var _ = Describe("Net", func() {
    32  	DescribeTable("TestParseDepth",
    33  		func(v string, expectSuccess bool, expectedValue net.Depth) {
    34  			parsed, err := net.ParseDepth(v)
    35  			Expect(err == nil).To(Equal(expectSuccess))
    36  			Expect(parsed).To(Equal(expectedValue))
    37  		},
    38  		Entry("default", "", true, net.DepthOne),
    39  		Entry("0", "0", true, net.DepthZero),
    40  		Entry("1", "1", true, net.DepthOne),
    41  		Entry("infinity", "infinity", true, net.DepthInfinity),
    42  		Entry("invalid", "invalid", false, net.Depth("")))
    43  
    44  	Describe("ParseDepth", func() {
    45  		It("is reasonably fast", func() {
    46  			experiment := NewExperiment("Parsing depth headers")
    47  			AddReportEntry(experiment.Name, experiment)
    48  
    49  			inputs := []string{"", "0", "1", "infinity", "INFINITY"}
    50  			size := len(inputs)
    51  			experiment.Sample(func(i int) {
    52  				experiment.MeasureDuration("parsing", func() {
    53  					_, _ = net.ParseDepth(inputs[i%size])
    54  				})
    55  			}, SamplingConfig{Duration: time.Second})
    56  
    57  			encodingStats := experiment.GetStats("parsing")
    58  			medianDuration := encodingStats.DurationFor(StatMedian)
    59  
    60  			Expect(medianDuration).To(BeNumerically("<", 3*time.Millisecond))
    61  		})
    62  	})
    63  
    64  	Describe("EncodePath", func() {
    65  		It("encodes paths", func() {
    66  			Expect(net.EncodePath("foo")).To(Equal("foo"))
    67  			Expect(net.EncodePath("/some/path/Folder %^*(#1)")).To(Equal("/some/path/Folder%20%25%5E%2A%28%231%29"))
    68  		})
    69  
    70  		/*
    71  			The encodePath method as it is implemented currently is terribly inefficient.
    72  			As soon as there are a few special characters which need to be escaped the allocation count rises and the time spent too.
    73  			Adding more special characters increases the allocations and the time spent can rise up to a few milliseconds.
    74  			Granted this is not a lot on it's own but when a user has tens or hundreds of paths which need to be escaped and contain a few special characters
    75  			then this method alone will cost a huge amount of time.
    76  		*/
    77  		It("is reasonably fast", func() {
    78  			experiment := NewExperiment("Encoding paths")
    79  			AddReportEntry(experiment.Name, experiment)
    80  
    81  			experiment.Sample(func(idx int) {
    82  				experiment.MeasureDuration("encoding", func() {
    83  					_ = net.EncodePath("/some/path/Folder %^*(#1)")
    84  				})
    85  			}, SamplingConfig{Duration: time.Second})
    86  
    87  			encodingStats := experiment.GetStats("encoding")
    88  			medianDuration := encodingStats.DurationFor(StatMedian)
    89  
    90  			Expect(medianDuration).To(BeNumerically("<", 10*time.Millisecond))
    91  		})
    92  	})
    93  
    94  	DescribeTable("TestParseOverwrite",
    95  		func(v string, expectSuccess bool, expectedValue bool) {
    96  			parsed, err := net.ParseOverwrite(v)
    97  			Expect(err == nil).To(Equal(expectSuccess))
    98  			Expect(parsed).To(Equal(expectedValue))
    99  		},
   100  		Entry("default", "", true, true),
   101  		Entry("T", "T", true, true),
   102  		Entry("F", "F", true, false),
   103  		Entry("invalid", "invalid", false, false))
   104  
   105  	DescribeTable("TestParseDestination",
   106  		func(baseURI, v string, expectSuccess bool, expectedValue string) {
   107  			parsed, err := net.ParseDestination(baseURI, v)
   108  			Expect(err == nil).To(Equal(expectSuccess))
   109  			Expect(parsed).To(Equal(expectedValue))
   110  		},
   111  		Entry("invalid1", "", "", false, ""),
   112  		Entry("invalid2", "baseURI", "", false, ""),
   113  		Entry("invalid3", "", "/dest/path", false, ""),
   114  		Entry("invalid4", "/foo", "/dest/path", false, ""),
   115  		Entry("valid", "/foo", "https://example.com/foo/dest/path", true, "/dest/path"))
   116  })