diff --git a/handlers/sshd_config/ast/listener.go b/handlers/sshd_config/ast/listener.go index c574f96..d7d51db 100644 --- a/handlers/sshd_config/ast/listener.go +++ b/handlers/sshd_config/ast/listener.go @@ -11,15 +11,15 @@ func createListener( context *sshListenerContext, ) sshParserListener { return sshParserListener{ - Config: config, - Errors: make([]common.LSPError, 0), + Config: config, + Errors: make([]common.LSPError, 0), sshContext: context, } } type sshListenerContext struct { line uint32 - currentOption *SSHOption + currentOption *SSHOption currentMatchBlock *SSHMatchBlock isKeyAMatchBlock bool } @@ -94,14 +94,14 @@ func (s *sshParserListener) ExitValue(ctx *parser.ValueContext) { if s.sshContext.isKeyAMatchBlock { location := common.CharacterRangeFromCtx(ctx.BaseParserRuleContext) location.ChangeBothLines(s.sshContext.line) - + rawEntry, _ := s.Config.Options.Get(location.Start.Line) entry := rawEntry.(*SSHOption) // Overwrite the current match block matchBlock := &SSHMatchBlock{ LocationRange: location, - MatchEntry: entry, + MatchEntry: entry, } s.Config.Options.Put( location.Start.Line, @@ -114,4 +114,3 @@ func (s *sshParserListener) ExitValue(ctx *parser.ValueContext) { s.sshContext.currentOption = nil } - diff --git a/handlers/sshd_config/ast/parser.go b/handlers/sshd_config/ast/parser.go index 9143211..fc510ee 100644 --- a/handlers/sshd_config/ast/parser.go +++ b/handlers/sshd_config/ast/parser.go @@ -60,7 +60,7 @@ func (c *SSHConfig) parseStatement( stream := antlr.NewInputStream(input) lexerErrorListener := createErrorListener(context.line) - lexer := parser.NewConfigLexer(&stream) + lexer := parser.NewConfigLexer(stream) lexer.RemoveErrorListeners() lexer.AddErrorListener(&lexerErrorListener) @@ -83,4 +83,3 @@ func (c *SSHConfig) parseStatement( return errors } - diff --git a/handlers/sshd_config/ast/parser_test.go b/handlers/sshd_config/ast/parser_test.go new file mode 100644 index 0000000..3b3626e --- /dev/null +++ b/handlers/sshd_config/ast/parser_test.go @@ -0,0 +1,60 @@ +package ast + +import ( + "config-lsp/utils" + "testing" +) + +func TestSimpleParserExample( + t *testing.T, +) { + input := utils.Dedent(` +PermitRootLogin no +PasswordAuthentication yes +`) + p := NewSSHConfig() + errors := p.Parse(input) + + if len(errors) != 0 { + t.Fatalf("Expected no errors, got %v", errors) + } + + if !(p.Options.Size() == 2 && + len(utils.KeysOfMap(p.CommentLines)) == 0) { + t.Errorf("Expected 2 options and no comment lines, but got: %v, %v", p.Options, p.CommentLines) + } + + rawFirstEntry, _ := p.Options.Get(uint32(0)) + firstEntry := rawFirstEntry.(*SSHOption) + + if !(firstEntry.Value == "PermitRootLogin no" && + firstEntry.LocationRange.Start.Line == 0 && + firstEntry.LocationRange.End.Line == 0 && + firstEntry.LocationRange.Start.Character == 0 && + firstEntry.LocationRange.End.Character == 18 && + firstEntry.Key.Value == "PermitRootLogin" && + firstEntry.Key.LocationRange.Start.Character == 0 && + firstEntry.Key.LocationRange.End.Character == 15 && + firstEntry.OptionValue.Value == "no" && + firstEntry.OptionValue.LocationRange.Start.Character == 16 && + firstEntry.OptionValue.LocationRange.End.Character == 18) { + t.Errorf("Expected first entry to be PermitRootLogin no, but got: %v", firstEntry) + } + + rawSecondEntry, _ := p.Options.Get(uint32(1)) + secondEntry := rawSecondEntry.(*SSHOption) + + if !(secondEntry.Value == "PasswordAuthentication yes" && + secondEntry.LocationRange.Start.Line == 1 && + secondEntry.LocationRange.End.Line == 1 && + secondEntry.LocationRange.Start.Character == 0 && + secondEntry.LocationRange.End.Character == 26 && + secondEntry.Key.Value == "PasswordAuthentication" && + secondEntry.Key.LocationRange.Start.Character == 0 && + secondEntry.Key.LocationRange.End.Character == 22 && + secondEntry.OptionValue.Value == "yes" && + secondEntry.OptionValue.LocationRange.Start.Character == 23 && + secondEntry.OptionValue.LocationRange.End.Character == 26) { + t.Errorf("Expected second entry to be PasswordAuthentication yes, but got: %v", secondEntry) + } +}