|
| 1 | +package attributevalidator |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "testing" |
| 7 | + "unicode/utf8" |
| 8 | + |
| 9 | + neofscrypto "github.com/nspcc-dev/neofs-sdk-go/crypto" |
| 10 | + "github.com/nspcc-dev/neofs-sdk-go/crypto/test" |
| 11 | + "github.com/nspcc-dev/neofs-sdk-go/netmap" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | +) |
| 14 | + |
| 15 | +type testController struct { |
| 16 | + tb testing.TB |
| 17 | + |
| 18 | + nodeKey neofscrypto.PublicKey |
| 19 | + mAttr map[string]string |
| 20 | + |
| 21 | + mProcessedAttr map[string]struct{} |
| 22 | + |
| 23 | + retErr error |
| 24 | +} |
| 25 | + |
| 26 | +func newTestController(tb testing.TB, nodeKey neofscrypto.PublicKey, mAttr map[string]string) *testController { |
| 27 | + return &testController{ |
| 28 | + tb: tb, |
| 29 | + nodeKey: nodeKey, |
| 30 | + mAttr: mAttr, |
| 31 | + mProcessedAttr: make(map[string]struct{}), |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +func (x *testController) setRetErr(err error) { |
| 36 | + x.retErr = err |
| 37 | +} |
| 38 | + |
| 39 | +func (x *testController) CheckNodeAccessToAttribute(nodeKey neofscrypto.PublicKey, attrKey, attrValue string) error { |
| 40 | + require.Equal(x.tb, x.nodeKey, nodeKey, "node public key must be decoded from the original binary") |
| 41 | + require.NotEmpty(x.tb, attrKey, "empty attribute key must not be passed", attrKey) |
| 42 | + require.True(x.tb, utf8.ValidString(attrKey), "invalid UTF-8 attribute key must not be passed", attrKey) |
| 43 | + require.NotEmptyf(x.tb, attrValue, "empty attribute value must not be passed", attrValue) |
| 44 | + |
| 45 | + _, ok := x.mProcessedAttr[attrKey] |
| 46 | + require.False(x.tb, ok, "attribute key must be passed once", attrKey) |
| 47 | + |
| 48 | + x.mProcessedAttr[attrKey] = struct{}{} |
| 49 | + |
| 50 | + val, ok := x.mAttr[attrKey] |
| 51 | + require.True(x.tb, ok, "attribute key must be from the original attributes", attrKey) |
| 52 | + require.Equal(x.tb, val, attrValue, "attribute value must be from the original attributes", attrValue) |
| 53 | + |
| 54 | + return x.retErr |
| 55 | +} |
| 56 | + |
| 57 | +func newNodeInfo(bNodeKey []byte, mAttr map[string]string) netmap.NodeInfo { |
| 58 | + var res netmap.NodeInfo |
| 59 | + res.SetPublicKey(bNodeKey) |
| 60 | + |
| 61 | + for k, v := range mAttr { |
| 62 | + res.SetAttribute(k, v) |
| 63 | + } |
| 64 | + |
| 65 | + return res |
| 66 | +} |
| 67 | + |
| 68 | +type unprotectedNodeInfo struct { |
| 69 | + bNodeKey []byte |
| 70 | + attrs [][2]string |
| 71 | +} |
| 72 | + |
| 73 | +func (x unprotectedNodeInfo) PublicKey() []byte { |
| 74 | + return x.bNodeKey |
| 75 | +} |
| 76 | + |
| 77 | +func (x unprotectedNodeInfo) NumberOfAttributes() int { |
| 78 | + return len(x.attrs) |
| 79 | +} |
| 80 | + |
| 81 | +func (x unprotectedNodeInfo) IterateAttributes(f func(string, string)) { |
| 82 | + for _, a := range x.attrs { |
| 83 | + f(a[0], a[1]) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +func newUnprotectedNodeInfo(bNodeKey []byte, attrs [][2]string) nodeInfo { |
| 88 | + return unprotectedNodeInfo{ |
| 89 | + bNodeKey: bNodeKey, |
| 90 | + attrs: attrs, |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +func TestValidator_VerifyAndUpdate(t *testing.T) { |
| 95 | + nodeKey := test.RandomSigner(t).Public() |
| 96 | + bNodeKey := neofscrypto.PublicKeyBytes(nodeKey) |
| 97 | + |
| 98 | + t.Run("invalid node key", func(t *testing.T) { |
| 99 | + mAttr := map[string]string{ |
| 100 | + "any_key": "any_value", |
| 101 | + } |
| 102 | + |
| 103 | + node := newNodeInfo([]byte("definitely not a key"), mAttr) |
| 104 | + ctrl := newTestController(t, nodeKey, mAttr) |
| 105 | + v := New(ctrl) |
| 106 | + |
| 107 | + err := v.VerifyAndUpdate(&node) |
| 108 | + require.ErrorIs(t, err, errInvalidNodeBinaryKey) |
| 109 | + }) |
| 110 | + |
| 111 | + for _, tc := range []struct { |
| 112 | + desc string |
| 113 | + attrs [][2]string |
| 114 | + expectedErr error |
| 115 | + }{ |
| 116 | + { |
| 117 | + desc: "empty key", |
| 118 | + attrs: [][2]string{{"", "any_value"}}, |
| 119 | + expectedErr: errEmptyAttributeKey, |
| 120 | + }, |
| 121 | + { |
| 122 | + desc: "not a UTF-8 key", |
| 123 | + attrs: [][2]string{ |
| 124 | + {"\xed\xa0\x80", "any_value"}, // U+D800 UTF-16 high surrogate mentioned in RFC 3629 |
| 125 | + }, |
| 126 | + expectedErr: errNonUTF8AttributeKey, |
| 127 | + }, |
| 128 | + { |
| 129 | + desc: "empty value", |
| 130 | + attrs: [][2]string{{"any_key", ""}}, |
| 131 | + expectedErr: errEmptyAttributeValue, |
| 132 | + }, |
| 133 | + { |
| 134 | + desc: "duplicated attribute", |
| 135 | + attrs: [][2]string{ |
| 136 | + {"any_key", "val1"}, |
| 137 | + {"any_key", "val2"}, |
| 138 | + }, |
| 139 | + expectedErr: errDuplicatedAttributeKey, |
| 140 | + }, |
| 141 | + } { |
| 142 | + t.Run(fmt.Sprintf("invalid attributes (%s)", tc.desc), func(t *testing.T) { |
| 143 | + node := newUnprotectedNodeInfo(bNodeKey, tc.attrs) |
| 144 | + |
| 145 | + mAttr := make(map[string]string) |
| 146 | + for _, a := range tc.attrs { |
| 147 | + mAttr[a[0]] = a[1] |
| 148 | + } |
| 149 | + |
| 150 | + ctrl := newTestController(t, nodeKey, mAttr) |
| 151 | + v := New(ctrl) |
| 152 | + |
| 153 | + err := v.verifyAndUpdate(node) |
| 154 | + require.ErrorIs(t, err, tc.expectedErr, tc.desc) |
| 155 | + }) |
| 156 | + } |
| 157 | + |
| 158 | + t.Run("failed check", func(t *testing.T) { |
| 159 | + checkErr := func(expectedErr error) { |
| 160 | + mAttr := map[string]string{ |
| 161 | + "any_key1": "any_value2", |
| 162 | + "any_key2": "any_value2", |
| 163 | + } |
| 164 | + |
| 165 | + node := newNodeInfo(bNodeKey, mAttr) |
| 166 | + ctrl := newTestController(t, nodeKey, mAttr) |
| 167 | + v := New(ctrl) |
| 168 | + |
| 169 | + ctrl.setRetErr(expectedErr) |
| 170 | + err := v.VerifyAndUpdate(&node) |
| 171 | + require.ErrorIs(t, err, expectedErr) |
| 172 | + } |
| 173 | + |
| 174 | + t.Run("access denied", func(t *testing.T) { |
| 175 | + checkErr(ErrAccessDenied) |
| 176 | + }) |
| 177 | + |
| 178 | + t.Run("other reason", func(t *testing.T) { |
| 179 | + anyErr := errors.New("any error") |
| 180 | + checkErr(anyErr) |
| 181 | + }) |
| 182 | + }) |
| 183 | + |
| 184 | + mAttr := map[string]string{ |
| 185 | + "any_key1": "any_value1", |
| 186 | + "any_key2": "any_value2", |
| 187 | + } |
| 188 | + |
| 189 | + node := newNodeInfo(bNodeKey, mAttr) |
| 190 | + ctrl := newTestController(t, nodeKey, mAttr) |
| 191 | + v := New(ctrl) |
| 192 | + |
| 193 | + err := v.VerifyAndUpdate(&node) |
| 194 | + require.NoError(t, err) |
| 195 | + require.Len(t, ctrl.mProcessedAttr, len(mAttr)) |
| 196 | +} |
0 commit comments