Skip to content

Commit 15a946f

Browse files
authored
Merge pull request #432 from jesusprubio/main
ParseTransport and ParseMethod helpers (SIP)
2 parents 456ed19 + ec98768 commit 15a946f

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed

protocol/sip/helper.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,7 @@ func NewDefaultInviteBody(host, sessid, sessver string) string {
499499
if sessver == "" {
500500
sessver = random.RandDigits(6)
501501
}
502+
502503
return fmt.Sprintf(`v=0
503504
o=caller %s %s IN IP4 %s
504505
s=-
@@ -524,6 +525,7 @@ func NewDefaultPublishBody(id, status, entity string) string {
524525
if entity == "" {
525526
entity = fmt.Sprintf("sip:bob@%s:5060", random.RandIPv4())
526527
}
528+
527529
return fmt.Sprintf(`<?xml version="1.0" encoding="UTF-8"?>
528530
<presence xmlns="urn:ietf:params:xml:ns:pidf"
529531
entity="%s">
@@ -555,6 +557,7 @@ func NewDefaultNotifyBody(id, status, contact, ts string) string {
555557
if ts == "" {
556558
ts = time.Now().UTC().Format(time.RFC3339)
557559
}
560+
558561
return fmt.Sprintf(`<?xml version="1.0"?>
559562
<presence xmlns="urn:ietf:params:xml:ns:pidf">
560563
<tuple id="%s">
@@ -566,3 +569,53 @@ func NewDefaultNotifyBody(id, status, contact, ts string) string {
566569
</tuple>
567570
</presence>`, id, status, contact, ts)
568571
}
572+
573+
// Converts a string to 'TransportType'.
574+
func ParseTransport(transport string) TransportType {
575+
switch strings.ToLower(transport) {
576+
case "udp":
577+
return UDP
578+
case "tcp":
579+
return TCP
580+
case "tls":
581+
return TLS
582+
default:
583+
return UNKNOWN
584+
}
585+
}
586+
587+
// Converts a string to a 'sip.RequestMethod'.
588+
func ParseMethod(method string) sip.RequestMethod {
589+
switch strings.ToLower(method) {
590+
case "options":
591+
return sip.OPTIONS
592+
case "invite":
593+
return sip.INVITE
594+
case "register":
595+
return sip.REGISTER
596+
case "ack":
597+
return sip.ACK
598+
case "bye":
599+
return sip.BYE
600+
case "cancel":
601+
return sip.CANCEL
602+
case "subscribe":
603+
return sip.SUBSCRIBE
604+
case "notify":
605+
return sip.NOTIFY
606+
case "publish":
607+
return sip.PUBLISH
608+
case "refer":
609+
return sip.REFER
610+
case "info":
611+
return sip.INFO
612+
case "message":
613+
return sip.MESSAGE
614+
case "prack":
615+
return sip.PRACK
616+
case "update":
617+
return sip.UPDATE
618+
default:
619+
return ""
620+
}
621+
}

protocol/sip/helper_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,3 +809,58 @@ func TestNewDefaultNotifyBody(t *testing.T) {
809809
}
810810
})
811811
}
812+
813+
func TestParseTransport(t *testing.T) {
814+
tests := []struct {
815+
input string
816+
expected TransportType
817+
}{
818+
{"UDP", UDP},
819+
{"udp", UDP},
820+
{"TCP", TCP},
821+
{"TLS", TLS},
822+
{"invalid", UNKNOWN},
823+
{"", UNKNOWN},
824+
}
825+
for _, tt := range tests {
826+
t.Run(tt.input, func(t *testing.T) {
827+
result := ParseTransport(tt.input)
828+
if result != tt.expected {
829+
t.Fatalf("got %s, want %s", result, tt.expected)
830+
}
831+
})
832+
}
833+
}
834+
835+
func TestParseMethod(t *testing.T) {
836+
tests := []struct {
837+
input string
838+
expected sip.RequestMethod
839+
}{
840+
{"INVITE", sip.INVITE},
841+
{"invite", sip.INVITE},
842+
{"ACK", sip.ACK},
843+
{"BYE", sip.BYE},
844+
{"CANCEL", sip.CANCEL},
845+
{"REGISTER", sip.REGISTER},
846+
{"OPTIONS", sip.OPTIONS},
847+
{"SUBSCRIBE", sip.SUBSCRIBE},
848+
{"NOTIFY", sip.NOTIFY},
849+
{"REFER", sip.REFER},
850+
{"PUBLISH", sip.PUBLISH},
851+
{"MESSAGE", sip.MESSAGE},
852+
{"INFO", sip.INFO},
853+
{"PRACK", sip.PRACK},
854+
{"UPDATE", sip.UPDATE},
855+
{"invalid", ""},
856+
{"", ""},
857+
}
858+
for _, tt := range tests {
859+
t.Run(tt.input, func(t *testing.T) {
860+
result := ParseMethod(tt.input)
861+
if result != tt.expected {
862+
t.Fatalf("got %s, want %s", result, tt.expected)
863+
}
864+
})
865+
}
866+
}

random/random.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,5 +158,6 @@ func RandIPv6() net.IP {
158158
for i := range net.IPv6len {
159159
ip[i] = byte(RandIntRange(1, 256))
160160
}
161+
161162
return ip
162163
}

0 commit comments

Comments
 (0)