github.com/pion/dtls/v2@v2.2.12/pkg/protocol/alert/alert_test.go (about) 1 // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly> 2 // SPDX-License-Identifier: MIT 3 4 package alert 5 6 import ( 7 "errors" 8 "reflect" 9 "testing" 10 ) 11 12 func TestAlert(t *testing.T) { 13 for _, test := range []struct { 14 Name string 15 Data []byte 16 Want *Alert 17 WantUnmarshalError error 18 }{ 19 { 20 Name: "Valid Alert", 21 Data: []byte{0x02, 0x0A}, 22 Want: &Alert{ 23 Level: Fatal, 24 Description: UnexpectedMessage, 25 }, 26 }, 27 { 28 Name: "Invalid alert length", 29 Data: []byte{0x00}, 30 Want: &Alert{}, 31 WantUnmarshalError: errBufferTooSmall, 32 }, 33 } { 34 a := &Alert{} 35 if err := a.Unmarshal(test.Data); !errors.Is(err, test.WantUnmarshalError) { 36 t.Errorf("Unexpected Error %v: exp: %v got: %v", test.Name, test.WantUnmarshalError, err) 37 } else if !reflect.DeepEqual(test.Want, a) { 38 t.Errorf("%q alert.unmarshal: got %v, want %v", test.Name, a, test.Want) 39 } 40 41 if test.WantUnmarshalError != nil { 42 return 43 } 44 45 data, marshalErr := a.Marshal() 46 if marshalErr != nil { 47 t.Errorf("Unexpected Error %v: got: %v", test.Name, marshalErr) 48 } else if !reflect.DeepEqual(test.Data, data) { 49 t.Errorf("%q alert.marshal: got % 02x, want % 02x", test.Name, data, test.Data) 50 } 51 } 52 }