Skip to content

Commit

Permalink
Fixed *.Decode method when not enough metrics (issue ##26)
Browse files Browse the repository at this point in the history
  • Loading branch information
spiegel-im-spiegel committed Jan 31, 2023
1 parent cb90764 commit fe2cbef
Show file tree
Hide file tree
Showing 12 changed files with 181 additions and 532 deletions.
42 changes: 11 additions & 31 deletions cvsserr/errors.go
Original file line number Diff line number Diff line change
@@ -1,39 +1,19 @@
package cvsserr

import "fmt"
import "errors"

// Num is error number for CVSS
type Num int

const (
ErrNullPointer Num = iota + 1
ErrUndefinedMetric
ErrInvalidVector
ErrNotSupportVer
ErrNotSupportMetric
ErrInvalidTemplate
ErrSameMetric
ErrInvalidValue
var (
ErrNullPointer = errors.New("Null reference instance")
ErrUndefinedMetric = errors.New("undefined metric")
ErrInvalidVector = errors.New("invalid vector")
ErrNotSupportVer = errors.New("not support version")
ErrNotSupportMetric = errors.New("not support metric")
ErrInvalidTemplate = errors.New("invalid templete string")
ErrSameMetric = errors.New("exist same metric")
ErrInvalidValue = errors.New("invalid value of metric")
ErrNoMetrics = errors.New("no metrics")
)

var errMessage = map[Num]string{
ErrNullPointer: "Null reference instance",
ErrUndefinedMetric: "undefined metric",
ErrInvalidVector: "invalid vector",
ErrNotSupportVer: "not support version",
ErrNotSupportMetric: "not support metric",
ErrInvalidTemplate: "invalid templete string",
ErrSameMetric: "exist same metric",
ErrInvalidValue: "invalid value of metric",
}

func (n Num) Error() string {
if s, ok := errMessage[n]; ok {
return s
}
return fmt.Sprintf("unknown error (%d)", int(n))
}

/* Copyright 2018-2023 Spiegel
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
46 changes: 0 additions & 46 deletions cvsserr/errors_test.go

This file was deleted.

9 changes: 3 additions & 6 deletions v2/metric/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ func (m *Base) Decode(vector string) (*Base, error) {
m = NewBase()
}
values := strings.Split(vector, "/")
if len(values) < 6 { // Temporal and Environmental metrics are optional
return nil, errs.Wrap(cvsserr.ErrInvalidVector, errs.WithContext("vector", vector))
}
// parse metrics
var lastErr error
for _, value := range values {
Expand Down Expand Up @@ -117,11 +114,11 @@ func (m *Base) decodeOne(str string) error {
// GetError returns error instance if unknown metric
func (m *Base) GetError() error {
if m == nil {
return errs.Wrap(cvsserr.ErrUndefinedMetric)
return errs.Wrap(cvsserr.ErrNoMetrics)
}
switch true {
case !m.AV.IsUnknown(), !m.AC.IsUnknown(), !m.Au.IsUnknown(), !m.C.IsUnknown(), !m.I.IsUnknown(), !m.A.IsUnknown():
return errs.Wrap(cvsserr.ErrUndefinedMetric)
return errs.Wrap(cvsserr.ErrNoMetrics)
default:
return nil
}
Expand Down Expand Up @@ -174,7 +171,7 @@ func (m *Base) Severity() Severity {
return severity(m.Score())
}

/* Copyright 2018-2023 Spiegel
/* Copyright 2023 Spiegel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
17 changes: 7 additions & 10 deletions v2/metric/environmental.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ type Environmental struct {
func NewEnvironmental() *Environmental {
return &Environmental{
Temporal: NewTemporal(),
CDP: CollateralDamagePotentialNotDefined,
TD: TargetDistributionNotDefined,
CR: ConfidentialityRequirementNotDefined,
IR: IntegrityRequirementNotDefined,
AR: AvailabilityRequirementNotDefined,
CDP: CollateralDamagePotentialInvalid,
TD: TargetDistributionInvalid,
CR: ConfidentialityRequirementInvalid,
IR: IntegrityRequirementInvalid,
AR: AvailabilityRequirementInvalid,
names: map[string]bool{},
}
}
Expand All @@ -47,9 +47,6 @@ func (m *Environmental) Decode(vector string) (*Environmental, error) {
m = NewEnvironmental()
}
values := strings.Split(vector, "/")
if len(values) < 6 { // Temporal and Environmental metrics are optional
return nil, errs.Wrap(cvsserr.ErrInvalidVector, errs.WithContext("vector", vector))
}
// parse metrics
var lastErr error
for _, value := range values {
Expand Down Expand Up @@ -118,14 +115,14 @@ func (m *Environmental) decodeOne(str string) error {
// GetError returns error instance if undefined metric
func (m *Environmental) GetError() error {
if m == nil {
return errs.Wrap(cvsserr.ErrUndefinedMetric)
return errs.Wrap(cvsserr.ErrNoMetrics)
}
if err := m.Temporal.GetError(); err != nil {
return errs.Wrap(err)
}
switch true {
case !m.CDP.IsValid(), !m.TD.IsValid(), !m.CR.IsValid(), !m.IR.IsValid(), !m.AR.IsValid():
return errs.Wrap(cvsserr.ErrUndefinedMetric)
return errs.Wrap(cvsserr.ErrNoMetrics)
default:
return nil
}
Expand Down
Loading

0 comments on commit fe2cbef

Please # to comment.