gitee.com/lonely0422/gometalinter.git@v3.0.1-0.20190307123442-32416ab75314+incompatible/regressiontests/safesql_test.go (about)

     1  package regressiontests
     2  
     3  import "testing"
     4  
     5  func TestSafesql(t *testing.T) {
     6  	t.Parallel()
     7  	source := `package test
     8  
     9  import (
    10  	"database/sql"
    11  	"log"
    12  	"strconv"
    13  )
    14  
    15  func main() {
    16  	getUser(42)
    17  }
    18  
    19  func getUser(userID int64) {
    20  	db, err := sql.Open("mysql", "user:password@tcp(127.0.0.1:3306)/hello")
    21  	if err != nil {
    22  		log.Fatal(err)
    23  	}
    24  	defer db.Close()
    25  
    26  	rows, err := db.Query("SELECT id, name FROM users WHERE id=" + strconv.FormatInt(userID, 10))
    27  	if err != nil {
    28  		log.Fatal(err)
    29  	}
    30  	defer rows.Close()
    31  }
    32  `
    33  	expected := Issues{
    34  		{Linter: "safesql", Severity: "warning", Path: "test.go", Line: 20, Col: 23, Message: `potentially unsafe SQL statement`},
    35  	}
    36  	ExpectIssues(t, "safesql", source, expected)
    37  }