| 1 |
package config |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"testing" |
| 6 |
|
| 7 |
. "gopkg.in/check.v1" |
| 8 |
) |
| 9 |
|
| 10 |
type DecoderSuite struct{} |
| 11 |
|
| 12 |
var _ = Suite(&DecoderSuite{}) |
| 13 |
|
| 14 |
func (s *DecoderSuite) TestDecode(c *C) { |
| 15 |
for idx, fixture := range fixtures { |
| 16 |
r := bytes.NewReader([]byte(fixture.Raw)) |
| 17 |
d := NewDecoder(r) |
| 18 |
cfg := &Config{} |
| 19 |
err := d.Decode(cfg) |
| 20 |
c.Assert(err, IsNil, Commentf("decoder error for fixture: %d", idx)) |
| 21 |
buf := bytes.NewBuffer(nil) |
| 22 |
e := NewEncoder(buf) |
| 23 |
_ = e.Encode(cfg) |
| 24 |
c.Assert(cfg, DeepEquals, fixture.Config, Commentf("bad result for fixture: %d, %s", idx, buf.String())) |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
func (s *DecoderSuite) TestDecodeFailsWithIdentBeforeSection(c *C) { |
| 29 |
t := ` |
| 30 |
key=value |
| 31 |
[section] |
| 32 |
key=value |
| 33 |
` |
| 34 |
decodeFails(c, t) |
| 35 |
} |
| 36 |
|
| 37 |
func (s *DecoderSuite) TestDecodeFailsWithEmptySectionName(c *C) { |
| 38 |
t := ` |
| 39 |
[] |
| 40 |
key=value |
| 41 |
` |
| 42 |
decodeFails(c, t) |
| 43 |
} |
| 44 |
|
| 45 |
func (s *DecoderSuite) TestDecodeFailsWithEmptySubsectionName(c *C) { |
| 46 |
t := ` |
| 47 |
[remote ""] |
| 48 |
key=value |
| 49 |
` |
| 50 |
decodeFails(c, t) |
| 51 |
} |
| 52 |
|
| 53 |
func (s *DecoderSuite) TestDecodeFailsWithBadSubsectionName(c *C) { |
| 54 |
t := ` |
| 55 |
[remote origin"] |
| 56 |
key=value |
| 57 |
` |
| 58 |
decodeFails(c, t) |
| 59 |
t = ` |
| 60 |
[remote "origin] |
| 61 |
key=value |
| 62 |
` |
| 63 |
decodeFails(c, t) |
| 64 |
} |
| 65 |
|
| 66 |
func (s *DecoderSuite) TestDecodeFailsWithTrailingGarbage(c *C) { |
| 67 |
t := ` |
| 68 |
[remote]garbage |
| 69 |
key=value |
| 70 |
` |
| 71 |
decodeFails(c, t) |
| 72 |
t = ` |
| 73 |
[remote "origin"]garbage |
| 74 |
key=value |
| 75 |
` |
| 76 |
decodeFails(c, t) |
| 77 |
} |
| 78 |
|
| 79 |
func (s *DecoderSuite) TestDecodeFailsWithGarbage(c *C) { |
| 80 |
decodeFails(c, "---") |
| 81 |
decodeFails(c, "????") |
| 82 |
decodeFails(c, "[sect\nkey=value") |
| 83 |
decodeFails(c, "sect]\nkey=value") |
| 84 |
decodeFails(c, `[section]key="value`) |
| 85 |
decodeFails(c, `[section]key=value"`) |
| 86 |
} |
| 87 |
|
| 88 |
func decodeFails(c *C, text string) { |
| 89 |
r := bytes.NewReader([]byte(text)) |
| 90 |
d := NewDecoder(r) |
| 91 |
cfg := &Config{} |
| 92 |
err := d.Decode(cfg) |
| 93 |
c.Assert(err, NotNil) |
| 94 |
} |
| 95 |
|
| 96 |
func FuzzDecoder(f *testing.F) { |
| 97 |
|
| 98 |
f.Fuzz(func(t *testing.T, input []byte) { |
| 99 |
|
| 100 |
d := NewDecoder(bytes.NewReader(input)) |
| 101 |
cfg := &Config{} |
| 102 |
d.Decode(cfg) |
| 103 |
}) |
| 104 |
} |