|
| 1 | +/* |
| 2 | +Copyright 2019 HAProxy Technologies |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package sorter_test |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + parserErrors "github.com/haproxytech/config-parser/v4/errors" |
| 23 | + "github.com/haproxytech/config-parser/v4/sorter" |
| 24 | +) |
| 25 | + |
| 26 | +func TestSortWithFrom(t *testing.T) { |
| 27 | + tests := []struct { |
| 28 | + name string |
| 29 | + sections []sorter.Section |
| 30 | + result []sorter.Section |
| 31 | + ExpectedError error |
| 32 | + }{ |
| 33 | + {"empty", []sorter.Section{}, []sorter.Section{}, nil}, |
| 34 | + {"1", []sorter.Section{{"1", ""}}, []sorter.Section{{"1", ""}}, nil}, |
| 35 | + {"2", []sorter.Section{{"1", ""}, {"2", ""}}, []sorter.Section{{"1", ""}, {"2", ""}}, nil}, |
| 36 | + {"2a", []sorter.Section{{"1", ""}, {"2", "1"}}, []sorter.Section{{"1", ""}, {"2", "1"}}, nil}, |
| 37 | + {"2b", []sorter.Section{{"2", "1"}, {"1", ""}}, []sorter.Section{{"1", ""}, {"2", "1"}}, nil}, |
| 38 | + {"2 self depend", []sorter.Section{{"2", "2"}, {"1", "1"}}, []sorter.Section{{"2", "2"}, {"1", "1"}}, parserErrors.ErrCircularDependency}, |
| 39 | + {"with non existent sorter.Section", []sorter.Section{{"2", "3"}, {"1", ""}}, []sorter.Section{{"1", ""}, {"2", "3"}}, parserErrors.ErrFromDefaultsSectionMissing}, |
| 40 | + {"with non existent sorter.Section 2", []sorter.Section{{"0", "2"}, {"2", "3"}, {"1", ""}}, []sorter.Section{{"1", ""}, {"2", "3"}, {"0", "2"}}, parserErrors.ErrFromDefaultsSectionMissing}, |
| 41 | + {"3", []sorter.Section{{"0", ""}, {"2", "1"}, {"1", ""}}, []sorter.Section{{"0", ""}, {"1", ""}, {"2", "1"}}, nil}, |
| 42 | + {"4", []sorter.Section{{"0", ""}, {"3", "1"}, {"2", "1"}, {"1", ""}}, []sorter.Section{{"0", ""}, {"1", ""}, {"2", "1"}, {"3", "1"}}, nil}, |
| 43 | + {"4a", []sorter.Section{{"0", ""}, {"3", "0"}, {"2", "1"}, {"1", ""}}, []sorter.Section{{"0", ""}, {"1", ""}, {"2", "1"}, {"3", "0"}}, nil}, |
| 44 | + {"err 1", []sorter.Section{{"0", "1"}, {"1", "0"}}, []sorter.Section{{"0", "1"}, {"1", "0"}}, parserErrors.ErrCircularDependency}, |
| 45 | + {"err 2", []sorter.Section{{"0", "1"}, {"1", "2"}, {"2", "3"}, {"3", "0"}}, []sorter.Section{{"0", "1"}, {"1", "2"}, {"2", "3"}, {"3", "0"}}, parserErrors.ErrCircularDependency}, |
| 46 | + {"malicious", []sorter.Section{{"0", "3"}, {"1", "2"}, {"2", "3"}, {"3", "1"}}, []sorter.Section{{"0", "3"}, {"1", "2"}, {"2", "3"}, {"3", "1"}}, parserErrors.ErrCircularDependency}, |
| 47 | + {"ok but reorder", []sorter.Section{{"0", "3"}, {"1", ""}, {"2", ""}, {"3", ""}}, []sorter.Section{{"1", ""}, {"2", ""}, {"3", ""}, {"0", "3"}}, nil}, |
| 48 | + {"ok but reorder 2", []sorter.Section{{"0", "3"}, {"1", "2"}, {"2", ""}, {"3", ""}}, []sorter.Section{{"2", ""}, {"1", "2"}, {"3", ""}, {"0", "3"}}, nil}, |
| 49 | + {"ok but reorder 2", []sorter.Section{{"0", "3"}, {"1", "0"}, {"2", "1"}, {"3", ""}}, []sorter.Section{{"3", ""}, {"0", "3"}, {"1", "0"}, {"2", "1"}}, nil}, |
| 50 | + } |
| 51 | + for _, tt := range tests { |
| 52 | + t.Run(tt.name, func(t *testing.T) { |
| 53 | + err := sorter.Sort(tt.sections) |
| 54 | + if tt.ExpectedError == nil { |
| 55 | + if err != nil { |
| 56 | + t.Errorf("SortWithFrom() error = %v, expectedError %v", err, tt.ExpectedError) |
| 57 | + } |
| 58 | + } else { |
| 59 | + if err != tt.ExpectedError { //nolint:errorlint |
| 60 | + t.Errorf("SortWithFrom() error = %v, expectedError %v", err, tt.ExpectedError) |
| 61 | + } |
| 62 | + } |
| 63 | + if err == nil { |
| 64 | + if !Equal(tt.sections, tt.result) { |
| 65 | + t.Errorf("SortWithFrom() src = %v, result %v", tt.sections, tt.result) |
| 66 | + } |
| 67 | + } |
| 68 | + }) |
| 69 | + } |
| 70 | +} |
0 commit comments