qur / withmock

Automatic Go package mock generation tool
Other
71 stars 9 forks source link

Can't seem to mock syscall.Mmap #37

Closed worr closed 10 years ago

worr commented 10 years ago
package secstring

import (
    "errors"
    "syscall" // mock
    "testing"
    "code.google.com/p/gomock/gomock"
)

func TestNewBadMmap(t *testing.T) {
    ctrl := gomock.NewController(t)
    defer ctrl.Finish()

    b := []byte("test")
    syscall.MOCK().SetController(ctrl)
    syscall.EXPECT().Mmap(0, 0, 16, 3, 4098).Returns(nil, errors.New("error"))
    if _, err := NewSecString(b); err == nil {
        t.Error("NewBadMmap: Expected non-nil error. Got nil")
    }
}

CUT:

func NewSecString(str []byte) (*SecString, error) {
    ret := &SecString{Length: len(str)}
    var err error

    if padding := ret.Length % aes.BlockSize; padding != 0 {
        ret.Padding = aes.BlockSize - padding
    }

    ret.String, err = syscall.Mmap(0, 0, ret.Length+ret.Padding, syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_ANON|syscall.MAP_PRIVATE)
    if err != nil {
        return nil, err
    }

...

Error:

 [ worr on worr-mn1 ] ( secstring ) % withmock go test                                                                                                                                                                                    [2]
--- FAIL: TestNewBadMmap (0.00 seconds)
    controller.go:109: no matching expected call: *syscall._packageMock.Mmap([0 0 16 3 4098])
    controller.go:154: missing call(s) to *syscall._packageMock.Mmap(is equal to 0, is equal to 0, is equal to 16, is equal to 3, is equal to 4098)
    controller.go:161: aborting test due to missing call(s)
FAIL
exit status 1
FAIL    github.com/worr/secstring   0.015s
qur commented 10 years ago

I would try "syscall.EXPECT().Mmap(0, int64(0), 16, 3, 4098).Returns(nil, errors.New("error"))", the types need to match in addition to the values (and gomock tends to not tell you the types in the output).

worr commented 10 years ago

My bad. I had earlier tried it after converting all of the types to match the syscall.Mmap signature, but it didn't work and gave the same error.

When only doing the int64 conversion, like in your example, worked. Sorry for the erroneous bug.