package bootprotocol_test import ( "fmt" "testing" "git.faercol.me/faercol/http-boot-server/bootserver/bootprotocol" "github.com/google/uuid" "github.com/stretchr/testify/require" ) func TestRequestMessage(t *testing.T) { t.Parallel() t.Run("Message OK", func(t *testing.T) { id := uuid.New() msgStr := fmt.Sprintf("%s id=%s", bootprotocol.ActionRequest.String(), id.String()) parsedMsg, err := bootprotocol.MessageFromBytes([]byte(msgStr)) require.NoError(t, err, "failed to generate message from bytes") require.Equal(t, id, parsedMsg.ID()) require.Equal(t, bootprotocol.ActionRequest, parsedMsg.Action()) parsedMsgBytes, err := parsedMsg.MarshalBinary() require.NoError(t, err, "failed to generate bytes from parsed message") require.Equal(t, msgStr, string(parsedMsgBytes)) }) t.Run("Err no parameters", func(t *testing.T) { msgStr := bootprotocol.ActionRequest.String() _, err := bootprotocol.MessageFromBytes([]byte(msgStr)) require.ErrorIs(t, err, bootprotocol.ErrInvalidFormat) }) t.Run("Err invalid parameter format", func(t *testing.T) { id := uuid.New() msgStr := fmt.Sprintf("%s id:%s", bootprotocol.ActionRequest.String(), id.String()) _, err := bootprotocol.MessageFromBytes([]byte(msgStr)) require.ErrorIs(t, err, bootprotocol.ErrInvalidParam) }) t.Run("Err invalid id wrong key", func(t *testing.T) { id := uuid.New() msgStr := fmt.Sprintf("%s uuid=%s", bootprotocol.ActionRequest.String(), id.String()) _, err := bootprotocol.MessageFromBytes([]byte(msgStr)) require.ErrorIs(t, err, bootprotocol.ErrMissingParam) }) t.Run("Err invalid id wrong format", func(t *testing.T) { msgStr := fmt.Sprintf("%s id=toto", bootprotocol.ActionRequest.String()) _, err := bootprotocol.MessageFromBytes([]byte(msgStr)) require.ErrorIs(t, err, bootprotocol.ErrInvalidParam) }) } func TestAcceptMessage(t *testing.T) { t.Parallel() t.Run("Message OK", func(t *testing.T) { id := uuid.New() msgStr := fmt.Sprintf("%s id=%s,efi_app=toto.efi", bootprotocol.ActionAccept.String(), id.String()) parsedMsg, err := bootprotocol.MessageFromBytes([]byte(msgStr)) require.NoError(t, err, "failed to generate message from bytes") require.Equal(t, id, parsedMsg.ID()) require.Equal(t, bootprotocol.ActionAccept, parsedMsg.Action()) parsedMsgBytes, err := parsedMsg.MarshalBinary() require.NoError(t, err, "failed to generate bytes from parsed message") require.Equal(t, msgStr, string(parsedMsgBytes)) }) t.Run("Err no parameters", func(t *testing.T) { msgStr := bootprotocol.ActionAccept.String() _, err := bootprotocol.MessageFromBytes([]byte(msgStr)) require.ErrorIs(t, err, bootprotocol.ErrInvalidFormat) }) t.Run("Err invalid parameter format", func(t *testing.T) { id := uuid.New() msgStr := fmt.Sprintf("%s id:%s", bootprotocol.ActionAccept.String(), id.String()) _, err := bootprotocol.MessageFromBytes([]byte(msgStr)) require.ErrorIs(t, err, bootprotocol.ErrInvalidParam) }) t.Run("Err invalid id wrong key", func(t *testing.T) { id := uuid.New() msgStr := fmt.Sprintf("%s uuid=%s,efi_app=toto.efi", bootprotocol.ActionAccept.String(), id.String()) _, err := bootprotocol.MessageFromBytes([]byte(msgStr)) require.ErrorIs(t, err, bootprotocol.ErrMissingParam) }) t.Run("Err invalid id wrong format", func(t *testing.T) { msgStr := fmt.Sprintf("%s id=toto,efi_app=toto.efi", bootprotocol.ActionAccept.String()) _, err := bootprotocol.MessageFromBytes([]byte(msgStr)) require.ErrorIs(t, err, bootprotocol.ErrInvalidParam) }) t.Run("Err invalid efi_app wrong key", func(t *testing.T) { id := uuid.New() msgStr := fmt.Sprintf("%s id=%s,uefi_app=toto.efi", bootprotocol.ActionAccept.String(), id.String()) _, err := bootprotocol.MessageFromBytes([]byte(msgStr)) require.ErrorIs(t, err, bootprotocol.ErrMissingParam) }) } func TestDenyMessage(t *testing.T) { t.Parallel() t.Run("Message OK", func(t *testing.T) { id := uuid.New() msgStr := fmt.Sprintf("%s id=%s,reason=lol", bootprotocol.ActionDeny.String(), id.String()) parsedMsg, err := bootprotocol.MessageFromBytes([]byte(msgStr)) require.NoError(t, err, "failed to generate message from bytes") require.Equal(t, id, parsedMsg.ID()) require.Equal(t, bootprotocol.ActionDeny, parsedMsg.Action()) parsedMsgBytes, err := parsedMsg.MarshalBinary() require.NoError(t, err, "failed to generate bytes from parsed message") require.Equal(t, msgStr, string(parsedMsgBytes)) }) t.Run("Err no parameters", func(t *testing.T) { msgStr := bootprotocol.ActionDeny.String() _, err := bootprotocol.MessageFromBytes([]byte(msgStr)) require.ErrorIs(t, err, bootprotocol.ErrInvalidFormat) }) t.Run("Err invalid parameter format", func(t *testing.T) { id := uuid.New() msgStr := fmt.Sprintf("%s id:%s", bootprotocol.ActionDeny.String(), id.String()) _, err := bootprotocol.MessageFromBytes([]byte(msgStr)) require.ErrorIs(t, err, bootprotocol.ErrInvalidParam) }) t.Run("Err invalid id wrong key", func(t *testing.T) { id := uuid.New() msgStr := fmt.Sprintf("%s uuid=%s,reason=lol", bootprotocol.ActionDeny.String(), id.String()) _, err := bootprotocol.MessageFromBytes([]byte(msgStr)) require.ErrorIs(t, err, bootprotocol.ErrMissingParam) }) t.Run("Err invalid id wrong format", func(t *testing.T) { msgStr := fmt.Sprintf("%s id=toto,reason=lol", bootprotocol.ActionDeny.String()) _, err := bootprotocol.MessageFromBytes([]byte(msgStr)) require.ErrorIs(t, err, bootprotocol.ErrInvalidParam) }) t.Run("Err invalid reason wrong key", func(t *testing.T) { id := uuid.New() msgStr := fmt.Sprintf("%s id=%s,no_reason=lol", bootprotocol.ActionDeny.String(), id.String()) _, err := bootprotocol.MessageFromBytes([]byte(msgStr)) require.ErrorIs(t, err, bootprotocol.ErrMissingParam) }) } func TestUnknownAction(t *testing.T) { t.Parallel() msgStr := "PLOP param1=toto" _, err := bootprotocol.MessageFromBytes([]byte(msgStr)) require.ErrorIs(t, err, bootprotocol.ErrUnknownAction) require.Equal(t, bootprotocol.ActionUnknown.String(), "unknown") } func TestAccept(t *testing.T) { t.Parallel() id := uuid.New() efiApp := "toto.efi" msg := bootprotocol.Accept(id, efiApp) require.Equal(t, id, msg.ID()) require.Equal(t, bootprotocol.ActionAccept, msg.Action()) msgBytes, err := msg.MarshalBinary() require.NoError(t, err) require.Contains(t, string(msgBytes), efiApp) } func TestDeny(t *testing.T) { t.Parallel() id := uuid.New() reason := "lolnope" msg := bootprotocol.Deny(id, reason) require.Equal(t, id, msg.ID()) require.Equal(t, bootprotocol.ActionDeny, msg.Action()) msgBytes, err := msg.MarshalBinary() require.NoError(t, err) require.Contains(t, string(msgBytes), reason) }