Commit f5bfe5c2 authored by Mikio Hara's avatar Mikio Hara

dns/dnsmessage: tweak the error value on nil resource body

Change-Id: I17444d1cc3ce03b6947aaae18d4cecdf9c36193f
Reviewed-on: https://go-review.googlesource.com/47110Reviewed-by: 's avatarIan Gudger <igudger@google.com>
Reviewed-by: 's avatarMatt Layher <mdlayher@gmail.com>
Run-TryBot: Matt Layher <mdlayher@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent 1f922427
...@@ -80,6 +80,7 @@ var ( ...@@ -80,6 +80,7 @@ var (
errReserved = errors.New("segment prefix is reserved") errReserved = errors.New("segment prefix is reserved")
errTooManyPtr = errors.New("too many pointers (>10)") errTooManyPtr = errors.New("too many pointers (>10)")
errInvalidPtr = errors.New("invalid pointer") errInvalidPtr = errors.New("invalid pointer")
errNilResouceBody = errors.New("nil resource body")
errResourceLen = errors.New("insufficient data for resource body length") errResourceLen = errors.New("insufficient data for resource body length")
errSegTooLong = errors.New("segment length too long") errSegTooLong = errors.New("segment length too long")
errZeroSegLen = errors.New("zero length segment") errZeroSegLen = errors.New("zero length segment")
...@@ -281,7 +282,7 @@ type ResourceBody interface { ...@@ -281,7 +282,7 @@ type ResourceBody interface {
func (r *Resource) pack(msg []byte, compression map[string]int) ([]byte, error) { func (r *Resource) pack(msg []byte, compression map[string]int) ([]byte, error) {
if r.Body == nil { if r.Body == nil {
return msg, &nestedError{"Resource", errors.New("nil resource body")} return msg, errNilResouceBody
} }
oldMsg := msg oldMsg := msg
r.Header.Type = r.Body.realType() r.Header.Type = r.Body.realType()
......
...@@ -535,40 +535,57 @@ func TestBuilder(t *testing.T) { ...@@ -535,40 +535,57 @@ func TestBuilder(t *testing.T) {
} }
func TestResourcePack(t *testing.T) { func TestResourcePack(t *testing.T) {
for _, m := range []Message{ for _, tt := range []struct {
m Message
err error
}{
{ {
Questions: []Question{ Message{
{ Questions: []Question{
Name: mustNewName("."), {
Type: TypeAAAA, Name: mustNewName("."),
Class: ClassINET, Type: TypeAAAA,
Class: ClassINET,
},
}, },
Answers: []Resource{{ResourceHeader{}, nil}},
}, },
Answers: []Resource{{ResourceHeader{}, nil}}, &nestedError{"packing Answer", errNilResouceBody},
}, },
{ {
Questions: []Question{ Message{
{ Questions: []Question{
Name: mustNewName("."), {
Type: TypeAAAA, Name: mustNewName("."),
Class: ClassINET, Type: TypeAAAA,
Class: ClassINET,
},
},
Authorities: []Resource{{ResourceHeader{}, (*NSResource)(nil)}},
},
&nestedError{"packing Authority",
&nestedError{"ResourceHeader",
&nestedError{"Name", errNonCanonicalName},
}, },
}, },
Authorities: []Resource{{ResourceHeader{}, (*NSResource)(nil)}},
}, },
{ {
Questions: []Question{ Message{
{ Questions: []Question{
Name: mustNewName("."), {
Type: TypeA, Name: mustNewName("."),
Class: ClassINET, Type: TypeA,
Class: ClassINET,
},
}, },
Additionals: []Resource{{ResourceHeader{}, nil}},
}, },
Additionals: []Resource{{ResourceHeader{}, nil}}, &nestedError{"packing Additional", errNilResouceBody},
}, },
} { } {
if _, err := m.Pack(); err == nil { _, err := tt.m.Pack()
t.Errorf("should fail: %v", m) if !reflect.DeepEqual(err, tt.err) {
t.Errorf("got %v for %v; want %v", err, tt.m, tt.err)
} }
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment