/* * Try several ways to generate PartialTokenException. */ [Test] public void Test_Partial() { fail = false; AsynchElementStream es = new AsynchElementStream(); es.OnDocumentEnd += new ObjectHandler(jabOnEnd); es.Push(ENC.GetBytes("<stream>")); es.OnElement += new ProtocolHandler(jabOnElement); es.Push(ENC.GetBytes("<te")); are.WaitOne(100, true); Assert.IsTrue(! fail); es.Push(ENC.GetBytes("st/>")); es.Push(ENC.GetBytes("<test>")); es.Push(ENC.GetBytes("</")); es.Push(ENC.GetBytes("test>")); es.Push(ENC.GetBytes("<test>")); es.Push(ENC.GetBytes("16;est</test>")); es.Push(ENC.GetBytes("<test>")); es.Push(new byte[] {0xC5}); es.Push(new byte[] {0x81}); es.Push(ENC.GetBytes("</test>")); es.Push(ENC.GetBytes("<test f")); es.Push(ENC.GetBytes("oo='bar'/>")); es.Push(ENC.GetBytes("<test foo=")); es.Push(ENC.GetBytes("'bar'/>")); es.Push(ENC.GetBytes("<test foo='")); es.Push(ENC.GetBytes("bar'/>")); es.Push(new byte[] {} ); }
[Test] public void Test_Parse() { AsynchElementStream es = new AsynchElementStream(); es.AddFactory(new global::jabber.protocol.x.Factory()); es.OnElement += new ProtocolHandler(es_OnElement); es.Push(System.Text.Encoding.UTF8.GetBytes(tstring)); Assert.IsTrue(gotElement); }
public void Test_Large_Partial() { count = 0; AsynchElementStream es = new AsynchElementStream(); es.OnElement += new ProtocolHandler(es_OnElement); es.Push(ENC.GetBytes("<stream>")); string test = "<presence from='*****@*****.**' to='[email protected]/Test' type='error'><error code='404' type='cancel'><remote-server-not-found xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/></error></presence><presence from='*****@*****.**' to='[email protected]/Test' type='error'><error code='404' type='cancel'><remote-server-not-found xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/></error></presence><presence from='*****@*****.**' to='[email protected]/Test' type='error'><error code='404' type='cancel'><remote-server-not-found xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/></error></presence><presence from='*****@*****.**' to='[email protected]/Test' type='error'><error code='404' type='cancel'><remote-server-not-found xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/></error></presence><presence from='*****@*****.**' to='[email protected]/Test' type='error'><error code='404' type='cancel'><remote-server-not-found xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/></error></presence><presence from='*****@*****.**' to='[email protected]/Test' type='error'><error code='404' type='cancel'><remote-server-not-found xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/></error></presence><presence from='*****@*****.**' to='[email protected]/Test' type='error'><error code='404' type='cancel'><remote-server-not-found xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/></error></presence><presence from='*****@*****.**' to='[email protected]/Test' type='error'><error code='404' type='cancel'><remote-server-not-found xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/></error></presence><presence from='*****@*****.**' to='[email protected]/Test' type='error'><error code='404' type='cancel'><remote-server-not-found xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/></error></presence><presence from='[email protected]/Home' to='[email protected]/Test'><priority>1</priority><c xmlns='http://jabber.org/protocol/caps' node='http://pidgin.im/caps' ver='2.3.1' ext='moodn nickn tunen avatar'/><x xmlns='vcard-temp:x:update'><photo>d206b82e4c1478ab033dacc55eacb1cfda7706c8</photo></x></presence><presence from='*****@*****.**' to='[email protected]/Test' type='error'><error code='404' type='cancel'><remote-server-not-found xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/></error></presence><presence from='*****@*****.**' to='[email protected]/Test' type='error'><error code='404' type='cancel'><remote-server-not-found xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/></error></presence><presence from='*****@*****.**' to='[email protected]/Test' type='error'><error code='404' type='cancel'><remote-server-not-found xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/></error></presence><presence from='*****@*****.**' to='[email protected]/Test' type='error'><error code='404' type='cancel'><remote-server-not-found xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/></error></presence><presence from='*****@*****.**' to='[email protected]/Test' type='error'><error code='404' type='cancel'><remote-server-not-found xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/></error></presence><presence from='*****@*****.**' to='[email protected]/Test' type='error'><error code='404' type='cancel'><remote-server-not-found xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/></error></presence><presence from='*****@*****.**' to='[email protected]/Test' type='error'><error code='404' type='cancel'><remote-server-not-found xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/></error></presence><presence from='*****@*****.**' to='[email protected]/Test' type='error'><error code='404' type='cancel'><remote-server-not-found xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/></error></presence><presence from='*****@*****.**' to='[email protected]/Test' type='error'><error code='404' type='cancel'><remote-server-not-found xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/></error></presence><presence from='*****@*****.**' to='[email protected]/Test' type='error'><error code='404' type='cance"; es.Push(ENC.GetBytes(test)); test = "l'><remote-server-not-found xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/></error></presence>"; es.Push(ENC.GetBytes(test)); Assert.AreEqual(count, 20); }
public void Test_Large() { AsynchElementStream es = new AsynchElementStream(); // es.OnElement += new ProtocolHandler(jabOnElement); es.Push(ENC.GetBytes("<stream>")); byte[] buf = ENC.GetBytes("<test/>"); MemoryStream ms = new MemoryStream(); for (int i=0; i<1024; i++) { ms.Write(buf, 0, buf.Length); } es.Push(ms.ToArray()); }
/// <summary> /// Sets up the element stream. This is the place to add factories. /// </summary> public override void InitializeStream() { bool first = (m_elements == null); m_elements = new AsynchElementStream(); m_elements.OnDocumentStart += new ProtocolHandler(m_elements_OnDocumentStart); m_elements.OnDocumentEnd += new bedrock.ObjectHandler(m_elements_OnDocumentEnd); m_elements.OnElement += new ProtocolHandler(m_elements_OnElement); m_elements.OnError += new bedrock.ExceptionHandler(m_elements_OnError); m_listener.StreamInit(m_elements); Debug.Assert(this.Connected); if (first) m_sock.RequestRead(); }
/// <summary> /// Some XML parsing error occurred. Wrap it, and generate a little more context, so that we can try /// to figure out where the actual error happened. /// </summary> /// <param name="innerException"></param> /// <param name="stream"></param> /// <param name="buf"></param> /// <param name="offset"></param> /// <param name="length"></param> public XMLParseException(Exception innerException, AsynchElementStream stream, byte[] buf, int offset, int length) : base("Parsing exception", innerException) { XmlElement e = stream.m_elem; XmlElement last = null; System.Text.StringBuilder sb = new System.Text.StringBuilder(); while (e != null) { last = e; e = e.ParentNode as XmlElement; } if (last != null) { sb.Append("Outer element: "); sb.Append(last.OuterXml); sb.Append("\n"); } else { sb.Append("Root stanza\n"); } sb.Append("New text (note: it's normal to see what looks like extra close tags here): "); try { sb.Append(AsynchElementStream.utf.GetString(buf, offset, length)); } catch (Exception) { sb.Append("Error in UTF8 decode: "); sb.Append(Element.HexString(buf, offset, length)); } m_context = sb.ToString(); }
/// <summary> /// Some XML parsing error occurred. Wrap it, and generate a little more context, so that we can try /// to figure out where the actual error happened. /// </summary> /// <param name="innerException"></param> /// <param name="stream"></param> /// <param name="buf"></param> /// <param name="offset"></param> /// <param name="length"></param> public XMLParseException(Exception innerException, AsynchElementStream stream, byte[] buf, int offset, int length) : base("Parsing exception", innerException) { XmlElement e = stream.m_elem; XmlElement last = null; System.Text.StringBuilder sb = new System.Text.StringBuilder(); while (e != null) { last = e; e = e.ParentNode as XmlElement; } if (last != null) { sb.Append("Outer element: "); sb.Append(last.OuterXml); sb.Append("\n"); } else { sb.Append("Root stanza\n"); } sb.Append("New text (note: it's normal to see what looks like extra close tags here): "); try { sb.Append(AsynchElementStream.utf.GetString(buf, offset, length)); } catch (Exception) { sb.Append("Error in UTF8 decode: "); sb.Append(Element.HexString(buf, offset, length)); } m_context = sb.ToString(); }
/// <summary> /// Connects to the XMPP server. /// </summary> public override void Connect() { m_elements = null; int port = (int)m_listener[Options.PORT]; Debug.Assert(port > 0); //m_sslOn = m_ssl; ProxySocket proxy = null; ProxyType pt = (ProxyType)m_listener[Options.PROXY_TYPE]; switch (pt) { case ProxyType.Socks4: proxy = new Socks4Proxy(this); break; case ProxyType.Socks5: proxy = new Socks5Proxy(this); break; case ProxyType.HTTP: proxy = new ShttpProxy(this); break; /* case ProxyType.HTTP_Polling: XEP25Socket j25s = new XEP25Socket(this); if (m_ProxyHost != null) { System.Net.WebProxy wp = new System.Net.WebProxy(); wp.Address = new Uri("http://" + m_ProxyHost + ":" + m_ProxyPort); if (m_ProxyUsername != null) { wp.Credentials = new System.Net.NetworkCredential(m_ProxyUsername, m_ProxyPassword); } j25s.Proxy = wp; } j25s.URL = m_server; m_sock = j25s; break; */ case ProxyType.None: m_sock = new AsyncSocket(null, this, (bool)m_listener[Options.SSL], false); ((AsyncSocket)m_sock).LocalCertificate = m_listener[Options.LOCAL_CERTIFICATE] as System.Security.Cryptography.X509Certificates.X509Certificate2; ((AsyncSocket)m_sock).CertificateGui = (bool)m_listener[Options.CERTIFICATE_GUI]; break; default: throw new ArgumentException("no handler for proxy type: " + pt, "ProxyType"); } if (proxy != null) { proxy.Socket = new AsyncSocket(null, proxy, (bool)m_listener[Options.SSL], false); ((AsyncSocket)proxy.Socket).LocalCertificate = m_listener[Options.LOCAL_CERTIFICATE] as System.Security.Cryptography.X509Certificates.X509Certificate2; proxy.Host = m_listener[Options.PROXY_HOST] as string; proxy.Port = (int)m_listener[Options.PROXY_PORT]; proxy.Username = m_listener[Options.PROXY_USER] as string; proxy.Password = m_listener[Options.PROXY_PW] as string; m_sock = proxy; } string to = (string)m_listener[Options.TO]; Debug.Assert(to != null); string host = (string)m_listener[Options.NETWORK_HOST]; if ((host == null) || (host == "")) { #if __MonoCS__ host = to; #else try { Address.LookupSRV((string)m_listener[Options.SRV_PREFIX], to, ref host, ref port); } catch { Debug.WriteLine("WARNING: netlib.Dns.dll missing"); host = to; } #endif } Address addr = new Address(host, port); m_sock.Connect(addr, (string)m_listener[Options.SERVER_ID]); }
void ISocketEventListener.OnError(BaseSocket sock, Exception ex) { m_listener[Options.REMOTE_CERTIFICATE] = null; m_elements = null; m_listener.Errored(ex); }
void ISocketEventListener.OnClose(BaseSocket sock) { m_listener[Options.REMOTE_CERTIFICATE] = null; m_elements = null; m_listener.Closed(); }