| 1 | |
| 2 | |
| 3 | |
| 4 | package hostagent |
| 5 | |
| 6 | import ( |
| 7 | "context" |
| 8 | "testing" |
| 9 | |
| 10 | "google.golang.org/grpc" |
| 11 | "google.golang.org/grpc/test/bufconn" |
| 12 | "google.golang.org/protobuf/types/known/emptypb" |
| 13 | "gotest.tools/v3/assert" |
| 14 | |
| 15 | "github.com/lima-vm/lima/v2/pkg/guestagent/api" |
| 16 | guestagentclient "github.com/lima-vm/lima/v2/pkg/guestagent/api/client" |
| 17 | ) |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | type invalidIPGuestService struct { |
| 24 | api.UnimplementedGuestServiceServer |
| 25 | } |
| 26 | |
| 27 | func (*invalidIPGuestService) GetInfo(context.Context, *emptypb.Empty) (*api.Info, error) { |
| 28 | return &api.Info{}, nil |
| 29 | } |
| 30 | |
| 31 | func (*invalidIPGuestService) GetEvents(_ *emptypb.Empty, stream api.GuestService_GetEventsServer) error { |
| 32 | return stream.Send(&api.Event{ |
| 33 | AddedLocalPorts: []*api.IPPort{{Ip: "1.2.3.4\x1b[2Kspoofed\x07", Port: 8080, Protocol: "tcp"}}, |
| 34 | }) |
| 35 | } |
| 36 | |
| 37 | func TestProcessGuestAgentEventsRejectsInvalidIP(t *testing.T) { |
| 38 | listener := bufconn.Listen(1024 * 1024) |
| 39 | server := grpc.NewServer(grpc.Creds(guestagentclient.NewCredentials())) |
| 40 | api.RegisterGuestServiceServer(server, &invalidIPGuestService{}) |
| 41 | go func() { |
| 42 | _ = server.Serve(listener) |
| 43 | }() |
| 44 | t.Cleanup(func() { |
| 45 | server.Stop() |
| 46 | _ = listener.Close() |
| 47 | }) |
| 48 | |
| 49 | client, err := guestagentclient.NewGuestAgentClient(listener.DialContext) |
| 50 | assert.NilError(t, err) |
| 51 | t.Cleanup(func() { _ = client.Close() }) |
| 52 | |
| 53 | a := &HostAgent{guestAgentAliveCh: make(chan struct{})} |
| 54 | err = a.processGuestAgentEvents(t.Context(), client) |
| 55 | assert.ErrorContains(t, err, "invalid IP") |
| 56 | } |