| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package config |
| 4 | |
| 5 | import ( |
| 6 | "fmt" |
| 7 | "testing" |
| 8 | |
| 9 | "github.com/stretchr/testify/assert" |
| 10 | ) |
| 11 | |
| 12 | func TestParse(t *testing.T) { |
| 13 | tests := map[string]struct { |
| 14 | path string |
| 15 | wantCfg UnboundConfig |
| 16 | wantErr bool |
| 17 | }{ |
| 18 | "valid include": { |
| 19 | path: "testdata/valid_include.conf", |
| 20 | wantCfg: UnboundConfig{ |
| 21 | cumulative: "yes", |
| 22 | enable: "yes", |
| 23 | iface: "10.0.0.1", |
| 24 | port: "8955", |
| 25 | useCert: "yes", |
| 26 | keyFile: "/etc/unbound/unbound_control_2.key", |
| 27 | certFile: "/etc/unbound/unbound_control_2.pem", |
| 28 | }, |
| 29 | }, |
| 30 | "valid include-toplevel": { |
| 31 | path: "testdata/valid_include_toplevel.conf", |
| 32 | wantCfg: UnboundConfig{ |
| 33 | cumulative: "yes", |
| 34 | enable: "yes", |
| 35 | iface: "10.0.0.1", |
| 36 | port: "8955", |
| 37 | useCert: "yes", |
| 38 | keyFile: "/etc/unbound/unbound_control_2.key", |
| 39 | certFile: "/etc/unbound/unbound_control_2.pem", |
| 40 | }, |
| 41 | }, |
| 42 | "valid glob include": { |
| 43 | path: "testdata/valid_glob.conf", |
| 44 | wantCfg: UnboundConfig{ |
| 45 | cumulative: "yes", |
| 46 | enable: "yes", |
| 47 | iface: "10.0.0.1", |
| 48 | port: "8955", |
| 49 | useCert: "yes", |
| 50 | keyFile: "/etc/unbound/unbound_control_2.key", |
| 51 | certFile: "/etc/unbound/unbound_control_2.pem", |
| 52 | }, |
| 53 | }, |
| 54 | "non existent glob include": { |
| 55 | path: "testdata/non_existent_glob_include.conf", |
| 56 | wantCfg: UnboundConfig{ |
| 57 | cumulative: "yes", |
| 58 | enable: "yes", |
| 59 | iface: "10.0.0.1", |
| 60 | port: "8953", |
| 61 | useCert: "yes", |
| 62 | keyFile: "/etc/unbound/unbound_control.key", |
| 63 | certFile: "/etc/unbound/unbound_control.pem", |
| 64 | }, |
| 65 | }, |
| 66 | "infinite recursion include": { |
| 67 | path: "testdata/infinite_rec.conf", |
| 68 | wantErr: true, |
| 69 | }, |
| 70 | "non existent include": { |
| 71 | path: "testdata/non_existent_include.conf", |
| 72 | wantErr: true, |
| 73 | }, |
| 74 | "non existent path": { |
| 75 | path: "testdata/non_existent_path.conf", |
| 76 | wantErr: true, |
| 77 | }, |
| 78 | } |
| 79 | |
| 80 | for name, test := range tests { |
| 81 | name = fmt.Sprintf("%s (%s)", name, test.path) |
| 82 | t.Run(name, func(t *testing.T) { |
| 83 | cfg, err := Parse(test.path) |
| 84 | |
| 85 | if test.wantErr { |
| 86 | assert.Error(t, err) |
| 87 | } else { |
| 88 | assert.NoError(t, err) |
| 89 | assert.Equal(t, test.wantCfg, *cfg) |
| 90 | } |
| 91 | }) |
| 92 | } |
| 93 | } |