From e0eb67bac805c924f72ce7f9fd03f985a7da1e07 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Tue, 18 Aug 2020 19:06:35 +0300 Subject: [PATCH] v2/signature: Add test for BalanceResponse Signed-off-by: Leonard Lyubich --- v2/signature/sign_test.go | 72 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 v2/signature/sign_test.go diff --git a/v2/signature/sign_test.go b/v2/signature/sign_test.go new file mode 100644 index 00000000..a1bfcab2 --- /dev/null +++ b/v2/signature/sign_test.go @@ -0,0 +1,72 @@ +package signature + +import ( + "testing" + + "github.com/nspcc-dev/neofs-api-go/v2/accounting" + "github.com/nspcc-dev/neofs-api-go/v2/service" + crypto "github.com/nspcc-dev/neofs-crypto" + "github.com/stretchr/testify/require" +) + +func TestBalanceResponse(t *testing.T) { + dec := new(accounting.Decimal) + dec.SetValue(100) + + body := new(accounting.BalanceResponseBody) + body.SetBalance(dec) + + meta := new(service.ResponseMetaHeader) + meta.SetTTL(1) + + req := new(accounting.BalanceResponse) + req.SetBody(body) + req.SetMetaHeader(meta) + + // verify unsigned request + require.Error(t, VerifyServiceMessage(req)) + + key, err := crypto.LoadPrivateKey("Kwk6k2eC3L3QuPvD8aiaNyoSXgQ2YL1bwS5CP1oKoA9waeAze97s") + require.NoError(t, err) + + // sign request + require.NoError(t, SignServiceMessage(key, req)) + + // verification must pass + require.NoError(t, VerifyServiceMessage(req)) + + // add level to meta header matryoshka + meta = new(service.ResponseMetaHeader) + meta.SetOrigin(req.GetMetaHeader()) + req.SetMetaHeader(meta) + + // sign request + require.NoError(t, SignServiceMessage(key, req)) + + // verification must pass + require.NoError(t, VerifyServiceMessage(req)) + + // corrupt body + dec.SetValue(dec.GetValue() + 1) + + // verification must fail + require.Error(t, VerifyServiceMessage(req)) + + // restore body + dec.SetValue(dec.GetValue() - 1) + + // corrupt meta header + meta.SetTTL(meta.GetTTL() + 1) + + // verification must fail + require.Error(t, VerifyServiceMessage(req)) + + // restore meta header + meta.SetTTL(meta.GetTTL() - 1) + + // corrupt origin verification header + req.GetVerificationHeader().SetOrigin(nil) + + // verification must fail + require.Error(t, VerifyServiceMessage(req)) +}