forked from spaghetty/sip_parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser_test.go
More file actions
99 lines (94 loc) · 4.44 KB
/
parser_test.go
File metadata and controls
99 lines (94 loc) · 4.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package sipparser
// Imports from the go standard library
import (
//"fmt"
"testing"
)
func TestHeader(t *testing.T) {
h := Header{"t", "v"}
if h.String() != "t: v" {
t.Errorf("Error with header.String() method. Unexpected res.")
}
}
// makes sure that the body is what is expected
func TestBody(t *testing.T) {
s := ParseMsg("fake\r\nheader\r\n\r\nbody ...\r\n\r\n")
if s.Body != "body ...\r\n\r\n" {
t.Errorf("[TestBody] Error getting the right body from the string.")
}
}
// actual msg testing
func TestParseMsg(t *testing.T) {
m := "SIP/2.0 200 OK\r\nVia: SIP/2.0/UDP 0.0.0.0:5060;branch=z9hG4bK24477ab511325213INV52e94be64e6687e3;received=0.0.0.0\r\nContact: <sip:[email protected]:6060>\r\nTo: <sip:[email protected];user=phone;noa=national>;tag=a94c095b773be1dd6e8d668a785a9c843f6f2cc0\r\nFrom: <sip:[email protected];user=phone;noa=national>;tag=52e94be6-co2998-INS002\r\nCall-ID: [email protected]\r\nCSeq: 299801 INVITE\r\nAccept: application/sdp, application/dtmf-relay, text/plain\r\nX-Nonsense-Hdr: nonsense\r\nAllow: PRACK, INVITE, BYE, REGISTER, ACK, OPTIONS, CANCEL, SUBSCRIBE, NOTIFY, INFO, REFER, UPDATE\r\nContent-Type: application/sdp\r\nServer: Dialogic-SIP/10.5.3.231 IMGDAL0001 0\r\nSupported: 100rel, path, replaces, timer, tdialog\r\nContent-Length: 239\r\n\r\nv=0\r\no=Dialogic_SDP 1452654 0 IN IP4 0.0.0.0\r\ns=Dialogic-SIP\r\nc=IN IP4 4.71.122.135\r\nt=0 0\r\nm=audio 11676 RTP/AVP 0 101\r\na=rtpmap:0 PCMU/8000\r\na=rtpmap:101 telephone-event/8000\r\na=fmtp:101 0-15\r\na=silenceSupp:off - - - -\r\na=ptime:20\r\n"
s := ParseMsg(m)
if s.Error != nil {
t.Errorf("[TestParseMsg] Error parsing msg. Recevied: " + s.Error.Error())
}
if len(s.Body) == 0 {
t.Errorf("[TestParseMsg] Error parsing msg. Body should have a length.")
}
if len(s.Headers) == 0 {
t.Errorf("[TestParseMsg] Error parsing msg. Does not appear to be any headers.")
}
if s.Via == nil || len(s.Via) == 0 {
t.Errorf("[TestParseMsg] Error parsing msg. Does not appear to be any vias parsed.")
//fmt.Println("msg:", s.Msg)
//fmt.Println("body:", s.Body)
//fmt.Println("via:", s.Via)
//fmt.Println("crlf:", s.crlf)
}
if s.ContentLength != "239" {
t.Errorf("[TestParseMsg] Error parsing msg. Content length should be 239. Received: " + s.ContentLength)
}
if len(s.Supported) != 5 {
t.Errorf("[TestParseMsg] Error parsing msg. s.Support should have length of 5.")
}
}
// testing the GetCallingParty functionality
func TestGetCallingParty(t *testing.T) {
rpid := "\"UNKNOWN\" <sip:[email protected]>;party=calling;screen=yes;privacy=off"
s := &SipMsg{RemotePartyIdVal: rpid}
err := s.GetCallingParty(CALLING_PARTY_RPID)
if err != nil {
t.Errorf("[TestGetCallingParty] Err with GetCallingParty. rcvd: " + err.Error())
}
if s.CallingParty == nil {
t.Errorf("[TestGetCallingParty] Err calling GetCallingParty. CallingParty field should not be nil.")
}
if s.CallingParty.Name != "UNKNOWN" {
t.Errorf("[TestGetCallingParty] Err calling GetCallingParty. Name should be \"UNKNOWN\".")
}
if s.CallingParty.Number != "8885551000" {
t.Errorf("[TestGetCallingParty] Err with GetCallingParty. Number should be \"8885551000\".")
}
paid := "<sip:[email protected]:5060;user=phone>"
s = &SipMsg{PAssertedIdVal: paid}
err = s.GetCallingParty(CALLING_PARTY_PAID)
if err != nil {
t.Errorf("[TestGetCallingParty] Err with GetCallingParty on paid. rcvd: " + err.Error())
}
if s.CallingParty == nil {
t.Errorf("[TestGetCallingParty] Err with GetCallingParty on paid. No CallingPartyInfo.")
}
if s.CallingParty.Name != "" {
t.Errorf("[TestGetCallingParty] Err with GetCallingParty on paid. Name should be \"\".")
}
if s.CallingParty.Number != "8884441000" {
t.Errorf("[TestGetCallingParty] Err with GetCallingParty on paid. Number should be \"8884441000\".")
}
s = &SipMsg{}
s.parseFrom("\"5556661000\" <sip:[email protected]>;tag=ZN21rHN5B7U0K")
err = s.GetCallingParty("")
if err != nil {
t.Errorf("[TestGetCallingParty] Err calling GetCallingParty on default. rcvd: " + err.Error())
}
if s.CallingParty == nil {
t.Errorf("[TestGetCallingParty] Err calling GetCallingParty on default. No CallingPartyInfo.")
}
if s.CallingParty.Name != "5556661000" {
t.Errorf("[TestGetCallingParty] Err calling GetCallingParty on default. Name should be \"5556661000\".")
}
if s.CallingParty.Number != "5556661000" {
t.Errorf("[TestGetCallingParty] Err calling GetCallingParty on default. Number should be \"5556661000\".")
}
}