Skip to content

Commit

Permalink
Skip OpenType field whose reference value does not match any field (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
ShouheiNishi authored Jun 14, 2023
1 parent a2b9363 commit c737358
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 24 deletions.
43 changes: 25 additions & 18 deletions aper.go
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ func getReferenceFieldValue(v reflect.Value) (value int64, err error) {
return
}

func (pd *perBitData) parseOpenType(v reflect.Value, params fieldParameters) error {
func (pd *perBitData) parseOpenType(skip bool, v reflect.Value, params fieldParameters) error {
pdOpenType := &perBitData{[]byte(""), 0, 0}
repeat := false
for {
Expand Down Expand Up @@ -670,10 +670,15 @@ func (pd *perBitData) parseOpenType(v reflect.Value, params fieldParameters) err
break
}
}
perTrace(2, fmt.Sprintf("Decoding OpenType %s with (len = %d byte)", v.Type().String(), len(pdOpenType.bytes)))
err := parseField(v, pdOpenType, params)
perTrace(2, fmt.Sprintf("Decoded OpenType %s", v.Type().String()))
return err
if skip {
perTrace(2, fmt.Sprintf("Skip OpenType (len = %d byte)", len(pdOpenType.bytes)))
return nil
} else {
perTrace(2, fmt.Sprintf("Decoding OpenType %s with (len = %d byte)", v.Type().String(), len(pdOpenType.bytes)))
err := parseField(v, pdOpenType, params)
perTrace(2, fmt.Sprintf("Decoded OpenType %s", v.Type().String()))
return err
}
}

// parseField is the main parsing function. Given a byte slice and an offset
Expand Down Expand Up @@ -802,13 +807,15 @@ func parseField(v reflect.Value, pd *perBitData, params fieldParameters) error {
}
}
if present == 0 {
return fmt.Errorf("OpenType reference value does not match any field")
val.Field(0).SetInt(0)
perTrace(2, "OpenType reference value does not match any field")
return pd.parseOpenType(true, reflect.Value{}, fieldParameters{})
} else if present >= structType.NumField() {
return fmt.Errorf("OpenType Present is bigger than number of struct field")
} else {
val.Field(0).SetInt(int64(present))
perTrace(2, fmt.Sprintf("Decoded Present index of OpenType is %d ", present))
return pd.parseOpenType(val.Field(present), structParams[present])
return pd.parseOpenType(false, val.Field(present), structParams[present])
}
} else {
if presentTmp, err := pd.getChoiceIndex(valueExtensible, params.valueUpperBound); err != nil {
Expand Down Expand Up @@ -915,17 +922,17 @@ func parseField(v reflect.Value, pd *perBitData, params fieldParameters) error {
//
// The following tags on struct fields have special meaning to Unmarshal:
//
// optional OPTIONAL tag in SEQUENCE
// sizeExt specifies that size is extensible
// valueExt specifies that value is extensible
// sizeLB set the minimum value of size constraint
// sizeUB set the maximum value of value constraint
// valueLB set the minimum value of size constraint
// valueUB set the maximum value of value constraint
// default sets the default value
// openType specifies the open Type
// referenceFieldName the string of the reference field for this type (only if openType used)
// referenceFieldValue the corresponding value of the reference field for this type (only if openType used)
// optional OPTIONAL tag in SEQUENCE
// sizeExt specifies that size is extensible
// valueExt specifies that value is extensible
// sizeLB set the minimum value of size constraint
// sizeUB set the maximum value of value constraint
// valueLB set the minimum value of size constraint
// valueUB set the maximum value of value constraint
// default sets the default value
// openType specifies the open Type
// referenceFieldName the string of the reference field for this type (only if openType used)
// referenceFieldValue the corresponding value of the reference field for this type (only if openType used)
//
// Other ASN.1 types are not supported; if it encounters them,
// Unmarshal returns a parse error.
Expand Down
17 changes: 12 additions & 5 deletions aper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"strings"
"testing"

"github.com/stretchr/testify/assert"

"github.com/free5gc/aper/logger"
)

Expand Down Expand Up @@ -446,7 +448,7 @@ var intTest2Data = []intTest2{
{3},
}

// 2 <= bmax-bmin <= 255
// 2 <= bmax-bmin <= 255
type intTest3 struct {
Value int64 `aper:"valueLB:1,valueUB:110"`
}
Expand All @@ -458,7 +460,7 @@ var intTest3Data = []intTest3{
{110},
}

// bmax-bmin == 256
// bmax-bmin == 256
type intTest4 struct {
Value int64 `aper:"valueLB:0,valueUB:255"`
}
Expand All @@ -467,7 +469,7 @@ var intTest4Data = []intTest4{
{140},
}

// 257 <= bmax-bmin <= 65536
// 257 <= bmax-bmin <= 65536
type intTest5 struct {
Value int64 `aper:"valueLB:0,valueUB:65535"`
}
Expand All @@ -476,7 +478,7 @@ var intTest5Data = []intTest5{
{140},
}

// 65537 <= bmax-bmin
// 65537 <= bmax-bmin
type intTest6 struct {
Value int64 `aper:"valueLB:0,valueUB:4294967295"`
}
Expand All @@ -488,7 +490,7 @@ var intTest6Data = []intTest6{
{65536},
}

// value extensed
// value extensed
type intTest7 struct {
Value int64 `aper:"valueExt,valueLB:0,valueUB:45"`
}
Expand Down Expand Up @@ -934,6 +936,11 @@ func TestOpenType(t *testing.T) {
perTestTrace(1, "[FAIL]\n")
t.Errorf("TEST %d is FAILED", i+1)
}

var x openTypeTest1
err := Unmarshal([]byte{0x11, 0x08, 0x06, 0x88, 0xFE, 0x06, 0xEC, 0x00, 0x05, 0xD8}, &x)
assert.NoError(t, err)
assert.Equal(t, 0, x.Value.Present)
}

// BOOLEAN TEST
Expand Down
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ go 1.17
require (
github.com/antonfisher/nested-logrus-formatter v1.3.1
github.com/sirupsen/logrus v1.8.1
github.com/stretchr/testify v1.2.2
)

require golang.org/x/sys v0.1.0 // indirect
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/sys v0.1.0 // indirect
)

0 comments on commit c737358

Please sign in to comment.