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

     1  // Copyright 2018-2021 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 ocdav
    20  
    21  // copy of https://github.com/golang/net/blob/master/webdav/if_test.go
    22  
    23  import (
    24  	"reflect"
    25  	"strings"
    26  	"testing"
    27  )
    28  
    29  func TestParseIfHeader(t *testing.T) {
    30  	// The "section x.y.z" test cases come from section x.y.z of the spec at
    31  	// http://www.webdav.org/specs/rfc4918.html
    32  	testCases := []struct {
    33  		desc  string
    34  		input string
    35  		want  ifHeader
    36  	}{{
    37  		"bad: empty",
    38  		``,
    39  		ifHeader{},
    40  	}, {
    41  		"bad: no parens",
    42  		`foobar`,
    43  		ifHeader{},
    44  	}, {
    45  		"bad: empty list #1",
    46  		`()`,
    47  		ifHeader{},
    48  	}, {
    49  		"bad: empty list #2",
    50  		`(a) (b c) () (d)`,
    51  		ifHeader{},
    52  	}, {
    53  		"bad: no list after resource #1",
    54  		`<foo>`,
    55  		ifHeader{},
    56  	}, {
    57  		"bad: no list after resource #2",
    58  		`<foo> <bar> (a)`,
    59  		ifHeader{},
    60  	}, {
    61  		"bad: no list after resource #3",
    62  		`<foo> (a) (b) <bar>`,
    63  		ifHeader{},
    64  	}, {
    65  		"bad: no-tag-list followed by tagged-list",
    66  		`(a) (b) <foo> (c)`,
    67  		ifHeader{},
    68  	}, {
    69  		"bad: unfinished list",
    70  		`(a`,
    71  		ifHeader{},
    72  	}, {
    73  		"bad: unfinished ETag",
    74  		`([b`,
    75  		ifHeader{},
    76  	}, {
    77  		"bad: unfinished Notted list",
    78  		`(Not a`,
    79  		ifHeader{},
    80  	}, {
    81  		"bad: double Not",
    82  		`(Not Not a)`,
    83  		ifHeader{},
    84  	}, {
    85  		"good: one list with a Token",
    86  		`(a)`,
    87  		ifHeader{
    88  			lists: []ifList{{
    89  				conditions: []Condition{{
    90  					Token: `a`,
    91  				}},
    92  			}},
    93  		},
    94  	}, {
    95  		"good: one list with an ETag",
    96  		`([a])`,
    97  		ifHeader{
    98  			lists: []ifList{{
    99  				conditions: []Condition{{
   100  					ETag: `a`,
   101  				}},
   102  			}},
   103  		},
   104  	}, {
   105  		"good: one list with three Nots",
   106  		`(Not a Not b Not [d])`,
   107  		ifHeader{
   108  			lists: []ifList{{
   109  				conditions: []Condition{{
   110  					Not:   true,
   111  					Token: `a`,
   112  				}, {
   113  					Not:   true,
   114  					Token: `b`,
   115  				}, {
   116  					Not:  true,
   117  					ETag: `d`,
   118  				}},
   119  			}},
   120  		},
   121  	}, {
   122  		"good: two lists",
   123  		`(a) (b)`,
   124  		ifHeader{
   125  			lists: []ifList{{
   126  				conditions: []Condition{{
   127  					Token: `a`,
   128  				}},
   129  			}, {
   130  				conditions: []Condition{{
   131  					Token: `b`,
   132  				}},
   133  			}},
   134  		},
   135  	}, {
   136  		"good: two Notted lists",
   137  		`(Not a) (Not b)`,
   138  		ifHeader{
   139  			lists: []ifList{{
   140  				conditions: []Condition{{
   141  					Not:   true,
   142  					Token: `a`,
   143  				}},
   144  			}, {
   145  				conditions: []Condition{{
   146  					Not:   true,
   147  					Token: `b`,
   148  				}},
   149  			}},
   150  		},
   151  	}, {
   152  		"section 7.5.1",
   153  		`<http://www.example.com/users/f/fielding/index.html> 
   154  			(<urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6>)`,
   155  		ifHeader{
   156  			lists: []ifList{{
   157  				resourceTag: `http://www.example.com/users/f/fielding/index.html`,
   158  				conditions: []Condition{{
   159  					Token: `urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6`,
   160  				}},
   161  			}},
   162  		},
   163  	}, {
   164  		"section 7.5.2 #1",
   165  		`(<urn:uuid:150852e2-3847-42d5-8cbe-0f4f296f26cf>)`,
   166  		ifHeader{
   167  			lists: []ifList{{
   168  				conditions: []Condition{{
   169  					Token: `urn:uuid:150852e2-3847-42d5-8cbe-0f4f296f26cf`,
   170  				}},
   171  			}},
   172  		},
   173  	}, {
   174  		"section 7.5.2 #2",
   175  		`<http://example.com/locked/>
   176  			(<urn:uuid:150852e2-3847-42d5-8cbe-0f4f296f26cf>)`,
   177  		ifHeader{
   178  			lists: []ifList{{
   179  				resourceTag: `http://example.com/locked/`,
   180  				conditions: []Condition{{
   181  					Token: `urn:uuid:150852e2-3847-42d5-8cbe-0f4f296f26cf`,
   182  				}},
   183  			}},
   184  		},
   185  	}, {
   186  		"section 7.5.2 #3",
   187  		`<http://example.com/locked/member>
   188  			(<urn:uuid:150852e2-3847-42d5-8cbe-0f4f296f26cf>)`,
   189  		ifHeader{
   190  			lists: []ifList{{
   191  				resourceTag: `http://example.com/locked/member`,
   192  				conditions: []Condition{{
   193  					Token: `urn:uuid:150852e2-3847-42d5-8cbe-0f4f296f26cf`,
   194  				}},
   195  			}},
   196  		},
   197  	}, {
   198  		"section 9.9.6",
   199  		`(<urn:uuid:fe184f2e-6eec-41d0-c765-01adc56e6bb4>) 
   200  			(<urn:uuid:e454f3f3-acdc-452a-56c7-00a5c91e4b77>)`,
   201  		ifHeader{
   202  			lists: []ifList{{
   203  				conditions: []Condition{{
   204  					Token: `urn:uuid:fe184f2e-6eec-41d0-c765-01adc56e6bb4`,
   205  				}},
   206  			}, {
   207  				conditions: []Condition{{
   208  					Token: `urn:uuid:e454f3f3-acdc-452a-56c7-00a5c91e4b77`,
   209  				}},
   210  			}},
   211  		},
   212  	}, {
   213  		"section 9.10.8",
   214  		`(<urn:uuid:e71d4fae-5dec-22d6-fea5-00a0c91e6be4>)`,
   215  		ifHeader{
   216  			lists: []ifList{{
   217  				conditions: []Condition{{
   218  					Token: `urn:uuid:e71d4fae-5dec-22d6-fea5-00a0c91e6be4`,
   219  				}},
   220  			}},
   221  		},
   222  	}, {
   223  		"section 10.4.6",
   224  		`(<urn:uuid:181d4fae-7d8c-11d0-a765-00a0c91e6bf2> 
   225  			["I am an ETag"])
   226  			(["I am another ETag"])`,
   227  		ifHeader{
   228  			lists: []ifList{{
   229  				conditions: []Condition{{
   230  					Token: `urn:uuid:181d4fae-7d8c-11d0-a765-00a0c91e6bf2`,
   231  				}, {
   232  					ETag: `"I am an ETag"`,
   233  				}},
   234  			}, {
   235  				conditions: []Condition{{
   236  					ETag: `"I am another ETag"`,
   237  				}},
   238  			}},
   239  		},
   240  	}, {
   241  		"section 10.4.7",
   242  		`(Not <urn:uuid:181d4fae-7d8c-11d0-a765-00a0c91e6bf2> 
   243  			<urn:uuid:58f202ac-22cf-11d1-b12d-002035b29092>)`,
   244  		ifHeader{
   245  			lists: []ifList{{
   246  				conditions: []Condition{{
   247  					Not:   true,
   248  					Token: `urn:uuid:181d4fae-7d8c-11d0-a765-00a0c91e6bf2`,
   249  				}, {
   250  					Token: `urn:uuid:58f202ac-22cf-11d1-b12d-002035b29092`,
   251  				}},
   252  			}},
   253  		},
   254  	}, {
   255  		"section 10.4.8",
   256  		`(<urn:uuid:181d4fae-7d8c-11d0-a765-00a0c91e6bf2>) 
   257  			(Not <DAV:no-lock>)`,
   258  		ifHeader{
   259  			lists: []ifList{{
   260  				conditions: []Condition{{
   261  					Token: `urn:uuid:181d4fae-7d8c-11d0-a765-00a0c91e6bf2`,
   262  				}},
   263  			}, {
   264  				conditions: []Condition{{
   265  					Not:   true,
   266  					Token: `DAV:no-lock`,
   267  				}},
   268  			}},
   269  		},
   270  	}, {
   271  		"section 10.4.9",
   272  		`</resource1> 
   273  			(<urn:uuid:181d4fae-7d8c-11d0-a765-00a0c91e6bf2> 
   274  			[W/"A weak ETag"]) (["strong ETag"])`,
   275  		ifHeader{
   276  			lists: []ifList{{
   277  				resourceTag: `/resource1`,
   278  				conditions: []Condition{{
   279  					Token: `urn:uuid:181d4fae-7d8c-11d0-a765-00a0c91e6bf2`,
   280  				}, {
   281  					ETag: `W/"A weak ETag"`,
   282  				}},
   283  			}, {
   284  				resourceTag: `/resource1`,
   285  				conditions: []Condition{{
   286  					ETag: `"strong ETag"`,
   287  				}},
   288  			}},
   289  		},
   290  	}, {
   291  		"section 10.4.10",
   292  		`<http://www.example.com/specs/> 
   293  			(<urn:uuid:181d4fae-7d8c-11d0-a765-00a0c91e6bf2>)`,
   294  		ifHeader{
   295  			lists: []ifList{{
   296  				resourceTag: `http://www.example.com/specs/`,
   297  				conditions: []Condition{{
   298  					Token: `urn:uuid:181d4fae-7d8c-11d0-a765-00a0c91e6bf2`,
   299  				}},
   300  			}},
   301  		},
   302  	}, {
   303  		"section 10.4.11 #1",
   304  		`</specs/rfc2518.doc> (["4217"])`,
   305  		ifHeader{
   306  			lists: []ifList{{
   307  				resourceTag: `/specs/rfc2518.doc`,
   308  				conditions: []Condition{{
   309  					ETag: `"4217"`,
   310  				}},
   311  			}},
   312  		},
   313  	}, {
   314  		"section 10.4.11 #2",
   315  		`</specs/rfc2518.doc> (Not ["4217"])`,
   316  		ifHeader{
   317  			lists: []ifList{{
   318  				resourceTag: `/specs/rfc2518.doc`,
   319  				conditions: []Condition{{
   320  					Not:  true,
   321  					ETag: `"4217"`,
   322  				}},
   323  			}},
   324  		},
   325  	}}
   326  
   327  	for _, tc := range testCases {
   328  		got, ok := parseIfHeader(strings.ReplaceAll(tc.input, "\n", ""))
   329  		if gotEmpty := reflect.DeepEqual(got, ifHeader{}); gotEmpty == ok {
   330  			t.Errorf("%s: should be different: empty header == %t, ok == %t", tc.desc, gotEmpty, ok)
   331  			continue
   332  		}
   333  		if !reflect.DeepEqual(got, tc.want) {
   334  			t.Errorf("%s:\ngot  %v\nwant %v", tc.desc, got, tc.want)
   335  			continue
   336  		}
   337  	}
   338  }