[#412] ir/locode: Disallow explicit setting of LOCODE-derived attributes

Make `Validator.VerifyAndUpdate` method to return an error if at least one
of LOCODE-derived attributes is set explicitly. Thus, IR will not confirm
the candidates for the network map who independently set these attributes.

Cover `Validator.VerifyAndUpdate` method with unit tests.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2021-06-09 13:47:12 +03:00 committed by Alex Vanin
parent 75d6702d2e
commit 68e85e4b33
2 changed files with 203 additions and 2 deletions

View file

@ -13,8 +13,10 @@ var errMissingRequiredAttr = errors.New("missing required attribute in DB record
// VerifyAndUpdate validates UN-LOCODE attribute of n
// and adds a group of related attributes.
//
// If n does not contain UN-LOCODE attribute, nil is returned
// without any actions. Otherwise, if UN-LOCODE value does not
// If n contains at least one of the LOCODE-derived attributes,
// an error returns.
//
// If n contains UN-LOCODE attribute and its value does not
// match the UN/LOCODE format, an error returns.
//
// New attributes are formed from the record of DB instance (Prm).
@ -30,6 +32,16 @@ var errMissingRequiredAttr = errors.New("missing required attribute in DB record
func (v *Validator) VerifyAndUpdate(n *netmap.NodeInfo) error {
mAttr := uniqueAttributes(n.Attributes())
// check if derived attributes are presented
for attrKey := range v.mAttr {
if _, ok := mAttr[attrKey]; ok {
return fmt.Errorf("attribute derived from %s is presented: %s",
netmap.AttrUNLOCODE,
attrKey,
)
}
}
attrLocode, ok := mAttr[netmap.AttrUNLOCODE]
if !ok {
return nil