代码之家  ›  专栏  ›  技术社区  ›  Kurt Peek

“无效内存地址或零指针取消引用”运行抓取单元测试?

go
  •  0
  • Kurt Peek  · 技术社区  · 5 年前

    我对运行包的单元测试感兴趣 https://github.com/cavaliercoder/grab GO111MODULE 环境通常 on ,我已将包下载到 GOPATH

    env GO111MODULE=off go get -u -d github.com/cavaliercoder/grab
    

    在恢复中 grab go mod init 从而产生以下结果 go.mod :

    module github.com/cavaliercoder/grab
    
    go 1.13
    

    如果我想逃跑 go test

    > go test
    panic: runtime error: invalid memory address or nil pointer dereference [recovered]
        panic: runtime error: invalid memory address or nil pointer dereference
    [signal SIGSEGV: segmentation violation code=0x1 addr=0x10 pc=0x12cc70d]
    
    goroutine 1556 [running]:
    testing.tRunner.func1(0xc00010ea00)
        /usr/local/Cellar/go/1.13/libexec/src/testing/testing.go:874 +0x3a3
    panic(0x132ba20, 0x1629ed0)
        /usr/local/Cellar/go/1.13/libexec/src/runtime/panic.go:679 +0x1b2
    github.com/cavaliercoder/grab.guessFilename(0xc00052aed0, 0xc0000aa008, 0x13951b5, 0x3, 0x139c440)
        /Users/kurt/go/src/github.com/cavaliercoder/grab/util.go:51 +0x2d
    github.com/cavaliercoder/grab.TestURLFilenames.func2(0xc00010ea00)
        /Users/kurt/go/src/github.com/cavaliercoder/grab/util_test.go:54 +0x123
    testing.tRunner(0xc00010ea00, 0x13afdf8)
        /usr/local/Cellar/go/1.13/libexec/src/testing/testing.go:909 +0xc9
    created by testing.(*T).Run
        /usr/local/Cellar/go/1.13/libexec/src/testing/testing.go:960 +0x350
    exit status 2
    FAIL    github.com/cavaliercoder/grab   2.531s
    

    util_test.go ,但找不到任何问题:

    package grab
    
    import (
        "fmt"
        "net/http"
        "net/url"
        "testing"
    )
    
    func TestURLFilenames(t *testing.T) {
        t.Run("Valid", func(t *testing.T) {
            expect := "filename"
            testCases := []string{
                "http://test.com/filename",
                "http://test.com/path/filename",
                "http://test.com/deep/path/filename",
                "http://test.com/filename?with=args",
                "http://test.com/filename#with-fragment",
                "http://test.com/filename?with=args&and#with-fragment",
            }
    
            for _, tc := range testCases {
                req, _ := http.NewRequest("GET", tc, nil)
                resp := &http.Response{
                    Request: req,
                }
                actual, err := guessFilename(resp)
                if err != nil {
                    t.Errorf("%v", err)
                }
    
                if actual != expect {
                    t.Errorf("expected '%v', got '%v'", expect, actual)
                }
            }
        })
    
        t.Run("Invalid", func(t *testing.T) {
            testCases := []string{
                "http://test.com",
                "http://test.com/",
                "http://test.com/filename/",
                "http://test.com/filename/?with=args",
                "http://test.com/filename/#with-fragment",
                "http://test.com/filename\x00",
            }
    
            for _, tc := range testCases {
                req, _ := http.NewRequest("GET", tc, nil)
                resp := &http.Response{
                    Request: req,
                }
    
                _, err := guessFilename(resp)
                if err != ErrNoFilename {
                    t.Errorf("expected '%v', got '%v'", ErrNoFilename, err)
                }
            }
        })
    }
    

    req 是一个空指针,但是由于它是用 http.NewRequest() 我不明白怎么会这样?

    1 回复  |  直到 5 年前
        1
  •  2
  •   p1gd0g    5 年前

                req, err1 := http.NewRequest("GET", tc, nil)
                if err1 != nil {
                    log.Println(err1.Error())
                }
    

    它将打印:

    解析 http://test.com/filename :net/url:url中的控制字符无效

    "http://test.com/filename\x00" 是不允许的。

    注释这一行,然后它就起作用了:

    [p1gd0g@p1gd0g-pc grab]$ go test
    PASS
    ok      github.com/cavaliercoder/grab   2.586s