forked from TrueCloudLab/frostfs-sdk-go
[#273] status: Support signature status
Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
This commit is contained in:
parent
6994eb0e55
commit
27fe9c19a7
3 changed files with 94 additions and 1 deletions
|
@ -123,3 +123,54 @@ func (x WrongMagicNumber) CorrectMagic() (magic uint64, ok int8) {
|
|||
|
||||
return
|
||||
}
|
||||
|
||||
// SignatureVerification describes failure status related to signature verification.
|
||||
// Instances provide Status and StatusV2 interfaces.
|
||||
type SignatureVerification struct {
|
||||
v2 status.Status
|
||||
}
|
||||
|
||||
func (x SignatureVerification) Error() string {
|
||||
return errMessageStatusV2(
|
||||
globalizeCodeV2(status.SignatureVerificationFail, status.GlobalizeCommonFail),
|
||||
x.v2.Message(),
|
||||
)
|
||||
}
|
||||
|
||||
// implements local interface defined in FromStatusV2 func.
|
||||
func (x *SignatureVerification) fromStatusV2(st *status.Status) {
|
||||
x.v2 = *st
|
||||
}
|
||||
|
||||
// ToStatusV2 implements StatusV2 interface method.
|
||||
// If the value was returned by FromStatusV2, returns the source message.
|
||||
// Otherwise, returns message with
|
||||
// * code: SIGNATURE_VERIFICATION_FAIL;
|
||||
// * string message: written message via SetMessage or
|
||||
// "signature verification failed" as a default message;
|
||||
// * details: empty.
|
||||
func (x SignatureVerification) ToStatusV2() *status.Status {
|
||||
x.v2.SetCode(globalizeCodeV2(status.SignatureVerificationFail, status.GlobalizeCommonFail))
|
||||
|
||||
if x.v2.Message() == "" {
|
||||
x.v2.SetMessage("signature verification failed")
|
||||
}
|
||||
|
||||
return &x.v2
|
||||
}
|
||||
|
||||
// SetMessage writes signature verification failure message.
|
||||
// Message should be used for debug purposes only.
|
||||
//
|
||||
// See also Message.
|
||||
func (x *SignatureVerification) SetMessage(v string) {
|
||||
x.v2.SetMessage(v)
|
||||
}
|
||||
|
||||
// Message returns status message. Zero status returns empty message.
|
||||
// Message should be used for debug purposes only.
|
||||
//
|
||||
// See also SetMessage.
|
||||
func (x SignatureVerification) Message() string {
|
||||
return x.v2.Message()
|
||||
}
|
||||
|
|
|
@ -50,3 +50,42 @@ func TestWrongMagicNumber_CorrectMagic(t *testing.T) {
|
|||
_, ok = st.CorrectMagic()
|
||||
require.EqualValues(t, -1, ok)
|
||||
}
|
||||
|
||||
func TestSignatureVerification(t *testing.T) {
|
||||
t.Run("default", func(t *testing.T) {
|
||||
var st apistatus.SignatureVerification
|
||||
|
||||
require.Empty(t, st.Message())
|
||||
})
|
||||
|
||||
t.Run("custom message", func(t *testing.T) {
|
||||
var st apistatus.SignatureVerification
|
||||
msg := "some message"
|
||||
|
||||
st.SetMessage(msg)
|
||||
|
||||
stV2 := st.ToStatusV2()
|
||||
|
||||
require.Equal(t, msg, st.Message())
|
||||
require.Equal(t, msg, stV2.Message())
|
||||
})
|
||||
|
||||
t.Run("empty to V2", func(t *testing.T) {
|
||||
var st apistatus.SignatureVerification
|
||||
|
||||
stV2 := st.ToStatusV2()
|
||||
|
||||
require.Equal(t, "signature verification failed", stV2.Message())
|
||||
})
|
||||
|
||||
t.Run("non-empty to V2", func(t *testing.T) {
|
||||
var st apistatus.SignatureVerification
|
||||
msg := "some other msg"
|
||||
|
||||
st.SetMessage(msg)
|
||||
|
||||
stV2 := st.ToStatusV2()
|
||||
|
||||
require.Equal(t, msg, stV2.Message())
|
||||
})
|
||||
}
|
||||
|
|
|
@ -31,7 +31,8 @@ type StatusV2 interface {
|
|||
// * status.OK: *SuccessDefaultV2 (this also includes nil argument).
|
||||
//
|
||||
// Common failures:
|
||||
// * status.Internal: *ServerInternal.
|
||||
// * status.Internal: *ServerInternal;
|
||||
// * status.SignatureVerificationFail: *SignatureVerification.
|
||||
//
|
||||
// Object failures:
|
||||
// * object.StatusLocked: *ObjectLocked;
|
||||
|
@ -55,6 +56,8 @@ func FromStatusV2(st *status.Status) Status {
|
|||
decoder = new(ServerInternal)
|
||||
case status.WrongMagicNumber:
|
||||
decoder = new(WrongMagicNumber)
|
||||
case status.SignatureVerificationFail:
|
||||
decoder = new(SignatureVerification)
|
||||
}
|
||||
case object.LocalizeFailStatus(&code):
|
||||
switch code {
|
||||
|
|
Loading…
Reference in a new issue