|
| 1 | +use sipmsg::*; |
| 2 | + |
| 3 | +#[test] |
| 4 | +fn parse_request() { |
| 5 | + let invite_msg_buf = "INVITE sip:vivekg@chair-dnrc.example.com;unknownparam SIP/2.0\r\n\ |
| 6 | + TO :\r\n \ |
| 7 | + sip:vivekg@chair-dnrc.example.com ; tag = 1918181833n\r\n\ |
| 8 | + from : \"J Rosenberg \\\"\" <sip:jdrosen@example.com>\r\n \ |
| 9 | + ;\r\n \ |
| 10 | + tag = 98asjd8\r\n\ |
| 11 | + MaX-fOrWaRdS: 0068\r\n\ |
| 12 | + Call-ID: wsinv.ndaksdj@192.0.2.1\r\n\ |
| 13 | + Content-Length : 150\r\n\ |
| 14 | + cseq: 0009\r\n \ |
| 15 | + INVITE\r\n\ |
| 16 | + Via : SIP / 2.0\r\n \ |
| 17 | + /UDP\r\n \ |
| 18 | + 192.0.2.2;branch=390skdjuw\r\n\ |
| 19 | + s :\r\n\ |
| 20 | + NewFangledHeader: newfangled value\r\n \ |
| 21 | + continued newfangled value\r\n\ |
| 22 | + UnknownHeaderWithUnusualValue: ;;,,;;,;\r\n\ |
| 23 | + Content-Type: application/sdp\r\n\ |
| 24 | + Route: \r\n \ |
| 25 | + <sip:services.example.com;lr;unknownwith=value;unknown-no-value>\r\n\ |
| 26 | + v: SIP / 2.0 / TCP spindle.example.com ;\r\n \ |
| 27 | + branch = z9hG4bK9ikj8 ,\r\n \ |
| 28 | + SIP / 2.0 / UDP 192.168.255.111 ; branch=\r\n \ |
| 29 | + z9hG4bK30239\r\n\ |
| 30 | + m:\"Quoted string \\\"\\\"\" <sip:jdrosen@example.com> ; newparam =\r\n \ |
| 31 | + newvalue ;\r\n \ |
| 32 | + secondparam ; q = 0.33\r\n\ |
| 33 | + \r\n\ |
| 34 | + v=0\ |
| 35 | + o=mhandley 29739 7272939 IN IP4 192.0.2.3\ |
| 36 | + s=-\ |
| 37 | + c=IN IP4 192.0.2.4\ |
| 38 | + t=0 0\ |
| 39 | + m=audio 49217 RTP/AVP 0 12\ |
| 40 | + m=video 3227 RTP/AVP 31\ |
| 41 | + a=rtpmap:31 LPC" |
| 42 | + .as_bytes(); |
| 43 | + |
| 44 | + let res = SipRequest::parse(invite_msg_buf); |
| 45 | + let (_, parsed_req) = res.unwrap(); |
| 46 | + let request_line = &parsed_req.rl; |
| 47 | + let headers = &parsed_req.headers; |
| 48 | + assert_eq!(request_line.method, SipMethod::INVITE); |
| 49 | + assert_eq!(request_line.uri.scheme, SipRequestUriScheme::SIP); |
| 50 | + assert_eq!(request_line.uri.user_info().unwrap().value, "vivekg"); |
| 51 | + assert_eq!(request_line.uri.hostport.host, "chair-dnrc.example.com"); |
| 52 | + assert_eq!(request_line.sip_version, SipVersion(2, 0)); |
| 53 | + assert_eq!( |
| 54 | + request_line.uri.params().unwrap().get(&"unknownparam"), |
| 55 | + Some(&None) |
| 56 | + ); |
| 57 | + |
| 58 | + let to_hdr = headers.get_rfc_s(SipRFCHeader::To).unwrap(); |
| 59 | + assert_eq!( |
| 60 | + to_hdr.params().unwrap().get(&"tag"), |
| 61 | + Some(&Some("1918181833n")) |
| 62 | + ); |
| 63 | + assert_eq!(to_hdr.value.vstr, "sip:vivekg@chair-dnrc.example.com"); |
| 64 | + |
| 65 | + let from_hdr = headers.get_rfc_s(SipRFCHeader::From).unwrap(); |
| 66 | + assert_eq!( |
| 67 | + from_hdr.value.vstr, |
| 68 | + "\"J Rosenberg \\\"\" <sip:jdrosen@example.com>" |
| 69 | + ); |
| 70 | + assert_eq!( |
| 71 | + from_hdr.value.tags().unwrap()[&SipHeaderTagType::DisplayName], |
| 72 | + b"J Rosenberg \\\"" |
| 73 | + ); |
| 74 | + assert_eq!( |
| 75 | + from_hdr.value.sip_uri().unwrap().user_info().unwrap().value, |
| 76 | + "jdrosen" |
| 77 | + ); |
| 78 | + assert_eq!( |
| 79 | + from_hdr.value.sip_uri().unwrap().hostport.host, |
| 80 | + "example.com" |
| 81 | + ); |
| 82 | + assert_eq!( |
| 83 | + from_hdr.params().unwrap().get(&"tag"), |
| 84 | + Some(&Some("98asjd8")) |
| 85 | + ); |
| 86 | + |
| 87 | + let max_forwards = parsed_req |
| 88 | + .headers |
| 89 | + .get_rfc_s(SipRFCHeader::MaxForwards) |
| 90 | + .unwrap(); |
| 91 | + assert_eq!(max_forwards.value.vstr, "0068"); |
| 92 | + assert_eq!(max_forwards.params(), None); |
| 93 | + assert_eq!(max_forwards.value.vtype, SipHeaderValueType::Digit); |
| 94 | + |
| 95 | + let call_id = headers.get_rfc_s(SipRFCHeader::CallID).unwrap(); |
| 96 | + assert_eq!(call_id.value.vstr, "wsinv.ndaksdj@192.0.2.1"); |
| 97 | + assert_eq!( |
| 98 | + call_id.value.tags().unwrap()[&SipHeaderTagType::ID], |
| 99 | + b"wsinv.ndaksdj" |
| 100 | + ); |
| 101 | + assert_eq!( |
| 102 | + call_id.value.tags().unwrap()[&SipHeaderTagType::Host], |
| 103 | + b"192.0.2.1" |
| 104 | + ); |
| 105 | + |
| 106 | + let content_length = &parsed_req |
| 107 | + .headers |
| 108 | + .get_rfc_s(SipRFCHeader::ContentLength) |
| 109 | + .unwrap(); |
| 110 | + assert_eq!(content_length.value.vstr, "150"); |
| 111 | + |
| 112 | + let cseq_header = &headers.get_rfc_s(SipRFCHeader::CSeq).unwrap(); |
| 113 | + assert_eq!(cseq_header.value.vstr, "0009\r\n INVITE"); |
| 114 | + assert_eq!(cseq_header.params(), None); |
| 115 | + assert_eq!( |
| 116 | + cseq_header.value.tags().unwrap()[&SipHeaderTagType::Number], |
| 117 | + b"0009" |
| 118 | + ); |
| 119 | + assert_eq!( |
| 120 | + cseq_header.value.tags().unwrap()[&SipHeaderTagType::Method], |
| 121 | + b"INVITE" |
| 122 | + ); |
| 123 | + |
| 124 | + let via_hdrs = headers.get_rfc(SipRFCHeader::Via).unwrap(); |
| 125 | + let first_via = &via_hdrs[0]; |
| 126 | + assert_eq!(first_via.value.vstr, "SIP / 2.0\r\n /UDP\r\n 192.0.2.2"); |
| 127 | + assert_eq!( |
| 128 | + first_via.params().unwrap().get(&"branch"), |
| 129 | + Some(&Some("390skdjuw")) |
| 130 | + ); |
| 131 | + |
| 132 | + assert_eq!( |
| 133 | + first_via.value.tags().unwrap()[&SipHeaderTagType::ProtocolName], |
| 134 | + b"SIP" |
| 135 | + ); |
| 136 | + assert_eq!( |
| 137 | + first_via.value.tags().unwrap()[&SipHeaderTagType::ProtocolVersion], |
| 138 | + b"2.0" |
| 139 | + ); |
| 140 | + assert_eq!( |
| 141 | + first_via.value.tags().unwrap()[&SipHeaderTagType::ProtocolTransport], |
| 142 | + b"UDP" |
| 143 | + ); |
| 144 | + assert_eq!( |
| 145 | + first_via.value.tags().unwrap()[&SipHeaderTagType::Host], |
| 146 | + b"192.0.2.2" |
| 147 | + ); |
| 148 | + |
| 149 | + let seond_via = &via_hdrs[1]; |
| 150 | + assert_eq!( |
| 151 | + seond_via.value.tags().unwrap()[&SipHeaderTagType::ProtocolTransport], |
| 152 | + b"TCP" |
| 153 | + ); |
| 154 | + assert_eq!( |
| 155 | + seond_via.value.vstr, |
| 156 | + "SIP / 2.0 / TCP spindle.example.com" |
| 157 | + ); |
| 158 | + assert_eq!( |
| 159 | + seond_via.value.tags().unwrap()[&SipHeaderTagType::Host], |
| 160 | + b"spindle.example.com" |
| 161 | + ); |
| 162 | + assert_eq!( |
| 163 | + seond_via.params().unwrap().get(&"branch"), |
| 164 | + Some(&Some("z9hG4bK9ikj8")) |
| 165 | + ); |
| 166 | + |
| 167 | + let subject_hdr = &headers.get_rfc_s(SipRFCHeader::Subject).unwrap(); |
| 168 | + assert_eq!(subject_hdr.value.vtype, SipHeaderValueType::EmptyValue); |
| 169 | + |
| 170 | + let new_fangled_header = &headers.get_ext_s("newfangledheader").unwrap(); |
| 171 | + assert_eq!( |
| 172 | + new_fangled_header.value.vstr, |
| 173 | + "newfangled value\r\n continued newfangled value" |
| 174 | + ); |
| 175 | + |
| 176 | + let unknown_header_with_unusual_value = |
| 177 | + &headers.get_ext_s("unknownHeaderwithunusualValue").unwrap(); |
| 178 | + assert_eq!(unknown_header_with_unusual_value.value.vstr, ";;,,;;,;"); |
| 179 | + |
| 180 | + let content_type = &headers.get_rfc_s(SipRFCHeader::ContentType).unwrap(); |
| 181 | + assert_eq!(content_type.value.vstr, "application/sdp"); |
| 182 | + |
| 183 | + let route_header = &headers.get_rfc_s(SipRFCHeader::Route).unwrap(); |
| 184 | + assert_eq!( |
| 185 | + route_header.value.vstr, |
| 186 | + "<sip:services.example.com;lr;unknownwith=value;unknown-no-value>" |
| 187 | + ); |
| 188 | + let route_uri = &route_header.value.sip_uri().unwrap(); |
| 189 | + let route_uri_params = &route_uri.params().unwrap(); |
| 190 | + assert_eq!(route_uri.scheme, sipuri::RequestUriScheme::SIP); |
| 191 | + assert_eq!(route_uri.hostport.host, "services.example.com"); |
| 192 | + assert_eq!(route_uri_params.get(&"lr"), Some(&None)); |
| 193 | + assert_eq!(route_uri_params.get(&"unknownwith"), Some(&Some("value"))); |
| 194 | + assert_eq!(route_uri_params.get(&"unknown-no-value"), Some(&None)); |
| 195 | + assert_eq!(route_uri_params.get(&"missing_param"), None); |
| 196 | + |
| 197 | + let contact = &headers.get_rfc_s(SipRFCHeader::Contact).unwrap(); |
| 198 | + assert_eq!( |
| 199 | + contact.value.vstr, |
| 200 | + "\"Quoted string \\\"\\\"\" <sip:jdrosen@example.com>" |
| 201 | + ); |
| 202 | + assert_eq!( |
| 203 | + contact.value.tags().unwrap()[&SipHeaderTagType::DisplayName], |
| 204 | + b"Quoted string \\\"\\\"" |
| 205 | + ); |
| 206 | + let contact_params = contact.params().unwrap(); |
| 207 | + assert_eq!(contact_params.get(&"newparam"), Some(&Some("newvalue"))); |
| 208 | + assert_eq!(contact_params.get(&"secondparam"), Some(&None)); |
| 209 | + assert_eq!(contact_params.get(&"q"), Some(&Some("0.33"))); |
| 210 | + |
| 211 | + let contact_uri = &contact.value.sip_uri().unwrap(); |
| 212 | + assert_eq!(contact_uri.scheme, sipuri::RequestUriScheme::SIP); |
| 213 | + assert_eq!(contact_uri.user_info().unwrap().value, "jdrosen"); |
| 214 | + assert_eq!(contact_uri.hostport.host, "example.com"); |
| 215 | + assert_eq!(contact_uri.params(), None); |
| 216 | + /*********************************************************/ |
| 217 | + assert_eq!( |
| 218 | + parsed_req.body.unwrap(), |
| 219 | + "v=0\ |
| 220 | + o=mhandley 29739 7272939 IN IP4 192.0.2.3\ |
| 221 | + s=-\ |
| 222 | + c=IN IP4 192.0.2.4\ |
| 223 | + t=0 0\ |
| 224 | + m=audio 49217 RTP/AVP 0 12\ |
| 225 | + m=video 3227 RTP/AVP 31\ |
| 226 | + a=rtpmap:31 LPC" |
| 227 | + .as_bytes() |
| 228 | + ); |
| 229 | +} |
0 commit comments