ObexReqTests.cs :  » Business-Application » 32feet.NET » InTheHand » Net » Tests » Bluetooth » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Business Application » 32feet.NET 
32feet.NET » InTheHand » Net » Tests » Bluetooth » ObexReqTests.cs
using System;
using NUnit.Framework;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Diagnostics;
#if FX1_1
using NullableIntSystem.Object;
#else
using NullableIntSystem.NullableSystem.Int32;
#endif


namespace InTheHand.Net.Tests.ObexRequest{
    [TestFixture]
    public class UriFormats
    {
        ObexWebRequest CreateObexWebRequestOfClassThatThrows(Uri requestUri)
        {
            ObexWebRequest req = new ObexWebRequest(requestUri);
            return req;
        }


        [Test]
        [ExpectedException(typeof(UriFormatException))]
        public void BadUriScheme()
        {
            Uri uri = new Uri("xxxxxxxxx://11223344/foo.txt");
            ObexWebRequest req = CreateObexWebRequestOfClassThatThrows(uri);
        }

        //Depends on non-existence of chosen arbitrary address, and could be slow, so don't run.
        //[Test]
        //public void BadUriIrdaNoSuchPeer()
        //{
        //    Uri uri = new Uri("obex://00000000/foo.txt");
        //    ObexWebRequest req = CreateObexWebRequestOfClassThatThrows(uri);
        //    req.GetResponse();
        //}

        [Test]
        public void BadUriBadHostFormat()
        {
            Uri uri = new Uri("obex://0000000000000000000000000/foo.txt");
            ObexWebRequest req = CreateObexWebRequestOfClassThatThrows(uri);
            try {
                req.GetResponse();
                Assert.Fail("Should have thrown!");
            } catch (WebException we) {
                Exception innerException = we.InnerException;
                Assert.IsNotNull(innerException);
                SocketException sex = innerException as SocketException;
                Assert.IsNotNull(sex);
                const int WSAEADDRNOTAVAIL = 10049; // (SocketError enum not in CF)
                int err = sex.ErrorCode;
                Assert.AreEqual(WSAEADDRNOTAVAIL, err);
            }
        }

    }//class


    class NewWholeProcessTests_Data
    {
        //--------
        public static readonly byte[] SimplePut1_Data ={ (byte)'a', (byte)'b', (byte)'c', (byte)'d' };
        public static readonly byte[] SimplePut1_Responses ={
            // Connect response
            0xA0, 0,7, 0x10, 0x00, 0x08, 0x00, // MTU=0x800=2048
            // Subsequent response(s)
            0x90, 0,3,
            0xA0, 0,3,
            //
            0xA0, 0,3,
        };
        public static readonly byte[] SimplePut1_ExpectedRequests ={
            //Connect
            0x80, 0x00, 0x07, 0x10, 0x00, 0x20, 0x00,
            //Put
            0x02, 0x00, 0x1D,
                0x01, 0x00, 0x15, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x2E,
                    0x00, 0x74, 0x00, 0x78, 0x00, 0x74, 0x00, 0x00,
                0xC3, 0x00, 0x00, 0x00, 0x04, 
            //Put
            0x82, 0x00, 0x0A,
                0x49, 0x00, 0x07, 0x61, 0x62, 0x63, 0x64,
            //Disconnect
            0x81, 0x00, 0x03
        };
        public static readonly byte[] SimplePut1_TooLongContentLength_ExpectedRequests ={
            //Connect
            0x80, 0x00, 0x07, 0x10, 0x00, 0x20, 0x00,
            //Put
            0x02, 0x00, 0x1D,
                0x01, 0x00, 0x15, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x2E,
                    0x00, 0x74, 0x00, 0x78, 0x00, 0x74, 0x00, 0x00,
                0xC3, 0x00, 0x00, 0x90, 0x04, 
            //Put
            0x82, 0x00, 0x0A,
                0x49, 0x00, 0x07, 0x61, 0x62, 0x63, 0x64,
            //Disconnect
            0x81, 0x00, 0x03
        };
        public static readonly byte[] SimplePut1_TooShortContentLength_ExpectedRequests ={
            //Connect
            0x80, 0x00, 0x07, 0x10, 0x00, 0x20, 0x00,
            //Put
            0x02, 0x00, 0x1D,
                0x01, 0x00, 0x15, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x2E,
                    0x00, 0x74, 0x00, 0x78, 0x00, 0x74, 0x00, 0x00,
                0xC3, 0x00, 0x00, 0x00, 0x01, 
            //Put
            0x82, 0x00, 0x0A,
                0x49, 0x00, 0x07, 0x61, 0x62, 0x63, 0x64,
            //Disconnect
            0x81, 0x00, 0x03
        };
        public static readonly byte[] SimplePut1_NoContentLength_ExpectedRequests ={
            //Connect
            0x80, 0x00, 0x07, 0x10, 0x00, 0x20, 0x00,
            //Put
            0x02, 0x00, 0x18,
                0x01, 0x00, 0x15, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x2E,
                    0x00, 0x74, 0x00, 0x78, 0x00, 0x74, 0x00, 0x00,
                //0xC3, 0x00, 0x00, 0x00, 0x01, 
            //Put
            0x82, 0x00, 0x0A,
                0x49, 0x00, 0x07, 0x61, 0x62, 0x63, 0x64,
            //Disconnect
            0x81, 0x00, 0x03
        };

        //--
        public static readonly byte[] SimplePut3EmptyEob_ExpectedRequests ={
            //Connect
            0x80, 0x00, 0x07, 0x10, 0x00, 0x20, 0x00,
            //Put
            0x02, 0x00, 0x18,
                0x01, 0x00, 0x15, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x2E,
                    0x00, 0x74, 0x00, 0x78, 0x00, 0x74, 0x00, 0x00,
                //0xC3, 0x00, 0x00, 0x00, 0x01, 
            //Put + Body
            0x02, 0x00, 0x0A,
                0x48, 0x00, 0x07, 0x61, 0x62, 0x63, 0x64,
            //Put-Final + EoB
            0x82, 0x00, 0x06,
                0x49, 0x00, 0x03,
            //Disconnect
            0x81, 0x00, 0x03
        };
        public static readonly byte[] SimplePut3_Responses ={
            // Connect response
            0xA0, 0,7, 0x10, 0x00, 0x20, 0x00, // MTU=0x800=2048
            // Subsequent response(s)
            0x90, 0,3,
            0x90, 0,3,
            0xA0, 0,3,
            //
            0xA0, 0,3,
        };

        public static readonly byte[] SimplePut3_ConnectionTruncatedBeforeFinal_ExpectedRequests ={
            //Connect
            0x80, 0x00, 0x07, 0x10, 0x00, 0x20, 0x00,
            //Put
            0x02, 0x00, 0x18,
                0x01, 0x00, 0x15, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x2E,
                    0x00, 0x74, 0x00, 0x78, 0x00, 0x74, 0x00, 0x00,
                //0xC3, 0x00, 0x00, 0x00, 0x01, 
            //Put + Body
            0x02, 0x00, 0x0A,
                0x48, 0x00, 0x07, 0x61, 0x62, 0x63, 0x64,
            ////Put-Final + EoB
            //0x82, 0x00, 0x06,
            //    0x49, 0x00, 0x03,
            ////Disconnect
            //0x81, 0x00, 0x03
        };
        public static readonly byte[] SimplePut3_ConnectionTruncatedBeforeFinal_TruncatedInPacketInHeader_ExpectedRequests ={
            //Connect
            0x80, 0x00, 0x07, 0x10, 0x00, 0x20, 0x00,
            //Put
            0x02, 0x00, 0x18,
                0x01, 0x00, 0x15, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x2E,
                    0x00, 0x74, 0x00, 0x78, 0x00, 0x74, 0x00, 0x00,
                //0xC3, 0x00, 0x00, 0x00, 0x01, 
            //Put + Body
            0x02, 0x00, //!!!! 0x0A,
            //    0x48, 0x00, 0x07, 0x61, 0x62, 0x63, 0x64,
            ////Put-Final + EoB
            //0x82, 0x00, 0x06,
            //    0x49, 0x00, 0x03,
            ////Disconnect
            //0x81, 0x00, 0x03
        };
        public static readonly byte[] SimplePut3_ConnectionTruncatedBeforeFinal_TruncatedInPacketAfterHeader_ExpectedRequests ={
            //Connect
            0x80, 0x00, 0x07, 0x10, 0x00, 0x20, 0x00,
            //Put
            0x02, 0x00, 0x18,
                0x01, 0x00, 0x15, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x2E,
                    0x00, 0x74, 0x00, 0x78, 0x00, 0x74, 0x00, 0x00,
                //0xC3, 0x00, 0x00, 0x00, 0x01, 
            //Put + Body
            0x02, 0x00, 0x0A,
                0x48, 0x00, 0x07, 0x61, 0x62, //!!!!0x63, 0x64,
            ////Put-Final + EoB
            //0x82, 0x00, 0x06,
            //    0x49, 0x00, 0x03,
            ////Disconnect
            //0x81, 0x00, 0x03
        };
        public static readonly byte[] SimplePut3_Truncated_Responses ={
            // Connect response
            0xA0, 0,7, 0x10, 0x00, 0x20, 0x00, // MTU=0x800=2048
            // Subsequent response(s)
            0x90, 0,3,
            0x90, 0,3,
            //0xA0, 0,3,
            ////
            //0xA0, 0,3,
        };

        //--
        public static readonly byte[] SimplePut1_ConnectionId_Responses ={
            // Connect response
            0xA0, 0,12, 0x10, 0x00, 0x08, 0x00, // MTU=0x800=2048
                /*ConnectionId*/0xCB, 0x00,0x00,0x16,0x0A,
            // Subsequent response(s)
            0x90, 0,3,
            0xA0, 0,3,
            //
            0xA0, 0,3,
        };
        public static readonly byte[] SimplePut1_ConnectionIdInAllResponses_Responses ={
            // Connect response
            0xA0, 0,12, 0x10, 0x00, 0x08, 0x00, // MTU=0x800=2048
                /*ConnectionId*/0xCB, 0x00,0x00,0x16,0x0A,
            // Subsequent response(s)
            0x90, 0,8, /*ConnectionId*/0xCB, 0x00,0x00,0x16,0x0A,
            0xA0, 0,8, /*ConnectionId*/0xCB, 0x00,0x00,0x16,0x0A,
            //
            0xA0, 0,8, /*ConnectionId*/0xCB, 0x00,0x00,0x16,0x0A,
        };

        public static readonly byte[] SimplePut1_ConnectionId_ExpectedRequests ={
            //Connect
            0x80, 0x00, 0x07, 0x10, 0x00, 0x20, 0x00,
            //Put
            0x02, 0x00, 0x22, 
                /*ConnectionId*/0xCB, 0x00,0x00,0x16,0x0A,
                0x01, 0x00, 0x15, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x2E,
                    0x00, 0x74, 0x00, 0x78, 0x00, 0x74, 0x00, 0x00, 0xC3, 0x00, 0x00, 0x00, 0x04, 
            //Put
            0x82, 0x00, 0x0A, 0x49, 0x00, 0x07, 0x61, 0x62, 0x63, 0x64,
            //Disconnect
            0x81, 0x00, 0x08,
                /*ConnectionId*/0xCB, 0x00,0x00,0x16,0x0A,
        };

        //--
        public static readonly byte[] SimplePut1SimpleNonAsciiFilename_ExpectedRequests ={
            //Connect
            0x80, 0x00, 0x07, 0x10, 0x00, 0x20, 0x00,
            //Put
            0x02, 0x00, 0x23, 
                0x01, 0x00, 0x1b, 
                    // "aaa%a.txt"
                    0x00, 0x61, 0x00, 0xE1, 0x00, 0x61, 0x20, 0x21, 0x00, 0x61, 0x00, 0x25, 0x00, 0x61, 
                    0x00, 0x2E,
                    0x00, 0x74, 0x00, 0x78, 0x00, 0x74, 0x00, 0x00, 
                0xC3, 0x00, 0x00, 0x00, 0x04, 
            //Put
            0x82, 0x00, 0x0A, 0x49, 0x00, 0x07, 0x61, 0x62, 0x63, 0x64,
            //Disconnect
            0x81, 0x00, 0x03
        };

        public static readonly byte[] SimplePut1UnicodeWithSurrogatePair_ExpectedRequests ={
            //Connect
            0x80, 0x00, 0x07, 0x10, 0x00, 0x20, 0x00,
            //Put
            0x02, 0, 33, 
                0x01, 0, 25, 
                    0x00, 0x61, 0x00, 0x61, 0xD8, 0x08, 0xDC, 0x04, 0x00, 0x61, 0x00, 0x61, 
                    0x00, 0x2E, 0x00, 0x74, 0x00, 0x78, 0x00, 0x74, 0x00, 0x00, 
                0xC3, 0x00, 0x00, 0x00, 0x04, 
            //Put
            0x82, 0x00, 0x0A, 0x49, 0x00, 0x07, 0x61, 0x62, 0x63, 0x64,
            //Disconnect
            0x81, 0x00, 0x03
        };

        public static readonly byte[] SimplePut1UnicodeChinese_ExpectedRequests ={
            //Connect
            0x80, 0x00, 0x07, 0x10, 0x00, 0x20, 0x00,
            //Put
            0x02, 0x00, 0x1D, 
                0x01, 0x00, 0x15, 
                    0x7A, 0x7A, 0x76, 0x7D, 0x65, 0x87, 0x4E, 0xF6, 
                    0x00, 0x2E, 0x00, 0x64, 0x00, 0x6F, 0x00, 0x63, 0x00, 0x00, 
                0xC3, 0x00, 0x00, 0x00, 0x04, 
            //Put
            0x82, 0x00, 0x0A, 0x49, 0x00, 0x07, 0x61, 0x62, 0x63, 0x64,
            //Disconnect
            0x81, 0x00, 0x03
        };



        public static readonly byte[] SimplePut1_PeerMtuEndsNonZero_Responses ={
            // Connect response
            0xA0, 0,7, 0x10, 0x00, 0x07, 0xFF, // MTU=0x7FF=2047
            // Subsequent response(s)
            0x90, 0,3,
            0xA0, 0,3,
            //
            0xA0, 0,3,
        };

        public static readonly byte[] SimplePut2HugePeerMruAndBigFile_Data = new byte[70000];
        public static readonly byte[] SimplePut2HugePeerMruAndBigFile_Responses ={
            // Connect response
            0xA0, 0,7, 0x10, 0x00, 0xFF, 0xFF, // MTU=0xFFFF=65535
            // Subsequent response(s)
            0x90, 0,3,
            0x90, 0,3,
            0xA0, 0,3,
            //
            0xA0, 0,3,
        };

        //--------
        public static readonly byte[] SimpleGet1_ConnectionId_Responses ={
            // Connect response
            0xA0, 0,12, 0x10, 0x00, 0x08, 0x00, // MTU=0x800=2048
                /*ConnectionId*/ 0xCB, 0, 0, 22, 10,
            // Subsequent response(s)
            0x90, 0,10, /*Body*/0x48, 0,7, 0x61, 0x62, 0x63, 0x64,
            0xA0, 0, 6, /*EoB */0x49, 0,3,
            //Disconnect
            0xA0, 0,3,
        };
        public static readonly byte[] SimpleGet1_NoConnectionId_Responses ={
            // Connect response
            0xA0, 0,7, 0x10, 0x00, 0x08, 0x00, // MTU=0x800=2048
            // Subsequent response(s)
            0x90, 0,10, /*Body*/0x48, 0,7, 0x61, 0x62, 0x63, 0x64,
            0xA0, 0, 6, /*EoB */0x49, 0,3,
            //Disconnect
            0xA0, 0,3,
        };
        public static readonly byte[] SimpleGet1_ConnectionIdInAllResponse_Responses ={
            // Connect response
            0xA0, 0,12, 0x10, 0x00, 0x08, 0x00, // MTU=0x800=2048
                /*ConnectionId*/ 0xCB, 0, 0, 22, 10,
            // Subsequent response(s)
            0x90, 0,15, /*ConnectionId*/ 0xCB, 0, 0, 22, 10, /*Body*/0x48, 0,7, 0x61, 0x62, 0x63, 0x64,
            0xA0, 0,11, /*ConnectionId*/ 0xCB, 0, 0, 22, 10, /*EoB */0x49, 0,3,
            //Disconnect
            0xA0, 0,8,  /*ConnectionId*/ 0xCB, 0, 0, 22, 10,
        };

        public static readonly byte[] SimpleGet1_NoConnectionId_ExpectedRequests ={
            //Connect
            0x80, 0x00, 0x07, 0x10, 0x00, 0x20, 0x00,
            //Get
            0x83, 0x00, 0x18,
                0x01, 0x00, 0x15, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x2E,
                0x00, 0x74, 0x00, 0x78, 0x00, 0x74, 0x00, 0x00,
            //Get
            0x83, 0,3,
            //Disconnect
            0x81, 0x00, 0x03,
        };

        public static readonly byte[] SimpleGet1_ExpectedRequests ={
            //Connect
            0x80, 0x00, 0x07, 0x10, 0x00, 0x20, 0x00,
            //Get
            0x83, 0x00, 0x1D, /*ConnectionId*/ 0xCB, 0, 0, 22, 10,
                0x01, 0x00, 0x15, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x2E,
                0x00, 0x74, 0x00, 0x78, 0x00, 0x74, 0x00, 0x00,
            //Get
            0x83, 0,3,
            //Disconnect
            0x81, 0x00, 0x08, /*ConnectionId*/ 0xCB, 0, 0, 22, 10,
        };


        //public static readonly byte[] SimpleTwoPuts_Data ={ (byte)'a', (byte)'b', (byte)'c', (byte)'d' };
        public static readonly byte[] TwoPuts_ExpectedRequests ={
            //Connect
            0x80, 0x00, 0x07, 0x10, 0x00, 0x20, 0x00,
            //Put
            0x02, 0x00, 0x1D,
                0x01, 0x00, 0x15, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x2E,
                    0x00, 0x74, 0x00, 0x78, 0x00, 0x74, 0x00, 0x00,
                0xC3, 0x00, 0x00, 0x00, 0x04, 
            //Put
            0x82, 0x00, 0x0A,
                0x49, 0x00, 0x07, 0x61, 0x62, 0x63, 0x64,
            //Put
            0x02, 0x00, 0x1D,
                0x01, 0x00, 0x15, 0x00, 0x6d, 0x00, 0x6d, 0x00, 0x6d, 0x00, 0x6d, 0x00, 0x2E,
                    0x00, 0x74, 0x00, 0x78, 0x00, 0x74, 0x00, 0x00,
                0xC3, 0x00, 0x00, 0x00, 0x04, 
            //Put
            0x82, 0x00, 0x0A,
                0x49, 0x00, 0x07, 0x6d, 0x6e, 0x6f, 0x70,
            //Disconnect
            0x81, 0x00, 0x03
        };

    }//class


    public abstract class NewWholeProcessTestsInfrastructure
    {
        public virtual Uri CreateObexUriForStreamTest(String filename)
        {
            return new Uri("obex:" + filename);
        }

        //----
        public ObexWebRequest CreateObexWebRequestWithGivenConnectionStream(Stream peer, String filename)
        {
            return new ObexWebRequest(CreateObexUriForStreamTest(filename), peer);
        }

        //----
        protected virtual MemoryStream InternalDoTest(ObexMethod method, String filename, 
            byte[] data, Byte[] CannedResponses, byte[] outData, out int outDataLen, bool restrictReadsToSingleBytes,
            NullableInt truncateWrites)
        {
            if (method != ObexMethod.Put && method != ObexMethod.Get)
                throw new ArgumentOutOfRangeException("method");
            //
            MemoryStream ourRequests;
            if (truncateWrites != null) {
                byte[] fixedLenBuffer = new byte[(int)truncateWrites];
                ourRequests = new MemoryStream(fixedLenBuffer);
            } else {
                ourRequests = new MemoryStream();
            }
            //
            Stream peerResponses = new MemoryStream(CannedResponses, false);
            if (restrictReadsToSingleBytes) {
                peerResponses = new ByteByByteReadStream(peerResponses);
            }
            //
            TwoWayStream peer = new TwoWayStream(peerResponses, ourRequests);
            //--
            ObexWebRequest req = CreateObexWebRequestWithGivenConnectionStream(peer, filename);
            //
            if (data != null) {
                Stream put = req.GetRequestStream();
                put.Write(data, 0, data.Length);
                req.ContentLength = data.Length;
            }
            //
            if (method == ObexMethod.Get) {
                req.Method = "GET";
            }
            ObexWebResponse rsp = (ObexWebResponse)req.GetResponse();
            //
            if (outData != null) {
                Stream get = rsp.GetResponseStream();
                outDataLen = Stream_CopyTo(get, outData);
            } else {
                outDataLen = -1;
            }
            //
            return ourRequests;
        }

        internal void DoTestPut(String filename, byte[] data, Byte[] CannedResponses, Byte[] ExpectedRequests, bool restrictReadsToSingleBytes)
        {
            DoTestPut(filename, data, CannedResponses, ExpectedRequests, restrictReadsToSingleBytes, null);
        }

        internal void DoTestPut(String filename, byte[] data, Byte[] CannedResponses, Byte[] ExpectedRequests, bool restrictReadsToSingleBytes, NullableInt truncateWrites)
        {
            int dummy;
            Debug.Assert(truncateWrites == null || (int)truncateWrites < ExpectedRequests.Length,
                "Bad truncateWrites value; is greater than normal result!");
            MemoryStream ourRequests = InternalDoTest(ObexMethod.Put, filename, data, CannedResponses, null, out dummy, restrictReadsToSingleBytes, truncateWrites);
            //
            //TODO Assert.AreEqual.... for InTheHand.Net.Tests.ObexRequest.NewWholeProcessTests.DoTest
            Assert.AreEqual(ExpectedRequests, ourRequests.ToArray(), "Requests");
        }

        internal void DoTestPut(String filename, byte[] data, Byte[] CannedResponses, bool restrictReadsToSingleBytes)
        {
            int dummy;
            /*MemoryStream ourRequests =*/
            InternalDoTest(ObexMethod.Put, filename, data, CannedResponses, null, out dummy, restrictReadsToSingleBytes, null);
        }

        internal void DoTestGet(String filename, byte[] expectedData, Byte[] CannedResponses, Byte[] ExpectedRequests, bool restrictReadsToSingleBytes)
        {
            DoTestGet(filename, expectedData, CannedResponses, ExpectedRequests, restrictReadsToSingleBytes, null);
        }

        internal void DoTestGet(String filename, byte[] expectedData, Byte[] CannedResponses, Byte[] ExpectedRequests, bool restrictReadsToSingleBytes, NullableInt truncateWrites)
        {
            byte[] getData0 = new byte[1000];
            int getDataLen;
            Debug.Assert(truncateWrites == null || (int)truncateWrites < ExpectedRequests.Length,
                "Bad truncateWrites value; is greater than normal result!");
            MemoryStream ourRequests = InternalDoTest(ObexMethod.Get, filename, null, CannedResponses, getData0, out getDataLen, restrictReadsToSingleBytes, truncateWrites);
            //
            //TODO Assert.AreEqual.... for InTheHand.Net.Tests.ObexRequest.NewWholeProcessTests.DoTest
            Assert.AreEqual(ExpectedRequests, ourRequests.ToArray(), "Requests");
            byte[] getData = new byte[getDataLen];
            Array.Copy(getData0, 0, getData, 0, getData.Length);
            Assert.AreEqual(expectedData, getData, "GET data");
        }

        private static int Stream_CopyTo(Stream get, byte[] outData)
        {
            int len = 0;
            while (true) {
                int readLen = get.Read(outData, len, outData.Length - len);
                if (readLen == 0)
                    break;
                len += readLen;
            }
            return len;
        }

        protected static byte[] Truncate(byte[] src, int length)
        {
            byte[] dst = new byte[length];
            Array.Copy(src, 0, dst, 0, dst.Length);
            return dst;
        }

    }//class


    [TestFixture]
    public class NewWholeProcessTests_ByteAtATime : NewWholeProcessTests
    {
        protected override MemoryStream InternalDoTest(ObexMethod method, string filename, byte[] data, byte[] CannedResponses, byte[] outData, out int outDataLen, bool restrictReadsToSingleBytes, NullableInt truncateWrites)
        {
            return base.InternalDoTest(method, filename, data, CannedResponses, outData, out outDataLen, true, truncateWrites);
        }
    }

    [TestFixture]
    public class NewWholeProcessTests : NewWholeProcessTestsInfrastructure
    {

        [Test]
        public void SimplePut1()
        {
            DoTestPut("aaaa.txt", NewWholeProcessTests_Data.SimplePut1_Data, NewWholeProcessTests_Data.SimplePut1_Responses, NewWholeProcessTests_Data.SimplePut1_ExpectedRequests, false);
        }

        [Test]
        public void SimplePut1_ConnectionId()
        {
            DoTestPut("aaaa.txt", NewWholeProcessTests_Data.SimplePut1_Data, NewWholeProcessTests_Data.SimplePut1_ConnectionId_Responses, NewWholeProcessTests_Data.SimplePut1_ConnectionId_ExpectedRequests, false);
            DoTestPut("aaaa.txt", NewWholeProcessTests_Data.SimplePut1_Data, NewWholeProcessTests_Data.SimplePut1_ConnectionIdInAllResponses_Responses, NewWholeProcessTests_Data.SimplePut1_ConnectionId_ExpectedRequests, false);
        }

        [Test]
        public void SimplePut1_SendsConnectionIdInEachResponse()
        {
            DoTestPut("aaaa.txt", NewWholeProcessTests_Data.SimplePut1_Data, NewWholeProcessTests_Data.SimplePut1_ConnectionIdInAllResponses_Responses, NewWholeProcessTests_Data.SimplePut1_ConnectionId_ExpectedRequests, false);
        }

        [Test]
        public void SimplePut1_PeerMtuEndsNonZero()
        {
            DoTestPut("aaaa.txt", NewWholeProcessTests_Data.SimplePut1_Data, NewWholeProcessTests_Data.SimplePut1_PeerMtuEndsNonZero_Responses, NewWholeProcessTests_Data.SimplePut1_ExpectedRequests, false);
        }

        [Test]
        public void SimplePut2HugePeerMruAndBigFile()
        {
            DoTestPut("aaaa.txt", NewWholeProcessTests_Data.SimplePut2HugePeerMruAndBigFile_Data, NewWholeProcessTests_Data.SimplePut2HugePeerMruAndBigFile_Responses, /*SimplePut1_ExpectedRequests,*/ false);
        }

        //---------
        [Test]
        public void SimplePut1_EarlyDisconnect_TruncateRcv()
        {
            int totalLen = NewWholeProcessTests_Data.SimplePut1_Responses.Length;
            for (int rcvTruncate = 0; rcvTruncate < totalLen; ++rcvTruncate) {
                try {
                    DoTestPut("aaaa.txt", NewWholeProcessTests_Data.SimplePut1_Data,
                        Truncate(NewWholeProcessTests_Data.SimplePut1_Responses, rcvTruncate),
                        NewWholeProcessTests_Data.SimplePut1_ExpectedRequests, false);
                    Assert.Fail("should have thrown");
                } catch (AssertionException) {
                    throw;
                } catch (Exception) {
                    //Console.WriteLine("sht: " + ex.GetType());
                }
            }
        }

        [Test]
        public void SimplePut1_EarlyDisconnect_WhileWriting()
        {
            int totalLen = NewWholeProcessTests_Data.SimplePut1_ExpectedRequests.Length;
            for (int sndTruncate = 0; sndTruncate < totalLen; ++sndTruncate) {
                try {
                    DoTestPut("aaaa.txt", NewWholeProcessTests_Data.SimplePut1_Data,
                        NewWholeProcessTests_Data.SimplePut1_Responses,
                        NewWholeProcessTests_Data.SimplePut1_ExpectedRequests, false, sndTruncate);
                    Assert.Fail("should have thrown");
                } catch (AssertionException) {
                    throw;
                } catch (Exception) {
                    //Console.WriteLine("sht: " + ex.GetType());
                }
            }
        }

        //--------------------------------------------------------------
        [Test]
        public void SimpleGet1()
        {
            DoTestGet("aaaa.txt", NewWholeProcessTests_Data.SimplePut1_Data, NewWholeProcessTests_Data.SimpleGet1_ConnectionId_Responses, NewWholeProcessTests_Data.SimpleGet1_ExpectedRequests, false);
        }

        [Test]
        public void SimpleGet1_ConnectionIdInAllResponse()
        {
            DoTestGet("aaaa.txt", NewWholeProcessTests_Data.SimplePut1_Data, NewWholeProcessTests_Data.SimpleGet1_ConnectionIdInAllResponse_Responses, NewWholeProcessTests_Data.SimpleGet1_ExpectedRequests, false);
        }

        [Test]
        public void SimpleGet1_NoConnectionId()
        {
            DoTestGet("aaaa.txt", NewWholeProcessTests_Data.SimplePut1_Data, NewWholeProcessTests_Data.SimpleGet1_NoConnectionId_Responses, NewWholeProcessTests_Data.SimpleGet1_NoConnectionId_ExpectedRequests, false);
        }

        //---------
        [Test]
        public void SimpleGet1_EarlyDisconnect_TruncateRcv()
        {
            int totalLen = NewWholeProcessTests_Data.SimpleGet1_ConnectionId_Responses.Length;
            for (int rcvTruncate = 0; rcvTruncate < totalLen; ++rcvTruncate) {
                try {
                    DoTestGet("aaaa.txt", NewWholeProcessTests_Data.SimplePut1_Data,
                        Truncate(NewWholeProcessTests_Data.SimpleGet1_ConnectionId_Responses, rcvTruncate),
                        NewWholeProcessTests_Data.SimpleGet1_ExpectedRequests, false);
                    Assert.Fail("should have thrown");
                } catch (AssertionException) {
                    throw;
                } catch (Exception) {
                    //Console.WriteLine("sht: " + ex.GetType());
                }
            }
        }

        [Test]
        public void SimpleGet1_EarlyDisconnect_WhileWriting()
        {
            int totalLen = NewWholeProcessTests_Data.SimpleGet1_ExpectedRequests.Length;
            for (int sndTruncate = 0; sndTruncate < totalLen; ++sndTruncate) {
                try {
                    DoTestGet("aaaa.txt", NewWholeProcessTests_Data.SimplePut1_Data,
                        NewWholeProcessTests_Data.SimpleGet1_ConnectionId_Responses,
                        NewWholeProcessTests_Data.SimpleGet1_ExpectedRequests, false, sndTruncate);
                    Assert.Fail("should have thrown");
                } catch (AssertionException) {
                    throw;
                } catch (Exception) {
                    //Console.WriteLine("sht: " + ex.GetType());
                }
            }
        }

    }//class

    [TestFixture]
    public class NewWholeProcessTestsUriPathEncoding : NewWholeProcessTestsInfrastructure
    {
        [Test]
        public void SimplePut1()
        {
            DoTestPut("aaaa.txt", NewWholeProcessTests_Data.SimplePut1_Data, NewWholeProcessTests_Data.SimplePut1_Responses, NewWholeProcessTests_Data.SimplePut1_ExpectedRequests, false);
        }

        [Test]
        public void SimplePut1WithSlash()
        {
            DoTestPut("/aaaa.txt", NewWholeProcessTests_Data.SimplePut1_Data, NewWholeProcessTests_Data.SimplePut1_Responses, NewWholeProcessTests_Data.SimplePut1_ExpectedRequests, false);
        }

        [Test]
        public void SimplePut1SimpleNonAsciiUnicodeFilename()
        {
            DoTestPut("aaa%a.txt", NewWholeProcessTests_Data.SimplePut1_Data, NewWholeProcessTests_Data.SimplePut1_Responses, NewWholeProcessTests_Data.SimplePut1SimpleNonAsciiFilename_ExpectedRequests, false);
        }

        [Test]
        public void SimplePut1UnicodeWithSurrogatePair()
        {
            DoTestPut("aa\U00012004aa.txt", NewWholeProcessTests_Data.SimplePut1_Data, NewWholeProcessTests_Data.SimplePut1_Responses, NewWholeProcessTests_Data.SimplePut1UnicodeWithSurrogatePair_ExpectedRequests, false);
        }

        [Test]
        public void SimplePut1UnicodeChinese()
        {
            DoTestPut(".doc", NewWholeProcessTests_Data.SimplePut1_Data, NewWholeProcessTests_Data.SimplePut1_Responses, NewWholeProcessTests_Data.SimplePut1UnicodeChinese_ExpectedRequests, false);
        }
    }

    [TestFixture]
    public class NewWholeProcessTestsUriPathEncoding_UserDontEscape : NewWholeProcessTestsUriPathEncoding
    {
        public override Uri CreateObexUriForStreamTest(string filename)
        {
#if FX1_1
/*
#endif
#pragma warning disable 618
#if FX1_1
*/
#endif
            return new Uri("obex:" + filename, true);
#if FX1_1
/*
#endif
#pragma warning restore 618
#if FX1_1
*/
#endif
        }
    }


    public class ByteByByteReadStream : ForV1CloseDisposeLikeV2Stream
    {
        Stream m_readStrm;

        protected override void Dispose(bool disposing)
        {
            try {
                m_readStrm.Close();
            } finally {
                base.Dispose(disposing);
            }
        }


        public ByteByByteReadStream(Stream readStrm)
        {
            m_readStrm = readStrm;
        }



        public override bool CanRead { get { return m_readStrm.CanRead; } }

        public override bool CanWrite { get { throw new Exception("The method or operation is not implemented."); /*or false*/} }

        public override bool CanSeek
        {
            get { throw new Exception("The method or operation is not implemented."); }
        }

        public override void Flush()
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public override long Length
        {
            get { throw new Exception("The method or operation is not implemented."); }
        }

        public override long Position
        {
            get { throw new Exception("The method or operation is not implemented."); }
            set { throw new Exception("The method or operation is not implemented."); }
        }

        public override int Read(byte[] buffer, int offset, int count)
        {
            Debug.Assert(count > 0, "count: " + count);
            if (count == 0)
                throw new ApplicationException("!!ByteByByteReadStream.Read, count: " + count);
            int newCount = Math.Min(count, 1);
            return m_readStrm.Read(buffer, offset, newCount);
        }

        public override long Seek(long offset, SeekOrigin origin)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public override void SetLength(long value)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public override void Write(byte[] buffer, int offset, int count)
        {
            throw new Exception("The method or operation is not implemented.");
        }
    }//class


    public class ExceptionAtReadEosStream : ForV1CloseDisposeLikeV2Stream
    {
        Stream m_child;

        protected override void Dispose(bool disposing)
        {
            try {
                m_child.Close();
            } finally {
                base.Dispose(disposing);
            }
        }


        public ExceptionAtReadEosStream(Stream readStrm)
        {
            m_child = readStrm;
        }



        public override bool CanRead { get { return m_child.CanRead; } }

        public override bool CanWrite { get { return m_child.CanWrite; } }

        public override bool CanSeek
        {
            get { throw new Exception("The method or operation is not implemented."); }
        }

        public override void Flush()
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public override long Length
        {
            get { throw new Exception("The method or operation is not implemented."); }
        }

        public override long Position
        {
            get { throw new Exception("The method or operation is not implemented."); }
            set { throw new Exception("The method or operation is not implemented."); }
        }

        public override int Read(byte[] buffer, int offset, int count)
        {
            Debug.Assert(count > 0, "count: " + count);
            if (count == 0)
                throw new ApplicationException("!!ExceptionAtEosReadStream.Read, count: " + count);
            int readLen = m_child.Read(buffer, offset, count);
            if (readLen == 0 && count != 0)
                throw new IOException("ConnectionClosed");
            return readLen;
        }

        public override long Seek(long offset, SeekOrigin origin)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public override void SetLength(long value)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public override void Write(byte[] buffer, int offset, int count)
        {
            m_child.Write(buffer, offset, count);
        }
    }//class


    public class TwoWayStream : ForV1CloseDisposeLikeV2Stream
    {
        Stream m_readStrm;
        Stream m_writeStrm;

        protected override void Dispose(bool disposing)
        {
            try {
                m_readStrm.Close();
            } finally {
                try {
                    m_writeStrm.Close();
                } finally {
                    base.Dispose(disposing);
                }
            }
        }


        public TwoWayStream(Stream readStrm, Stream writeStrm)
        {
            m_readStrm = readStrm;
            m_writeStrm = writeStrm;
        }



        public override bool CanRead { get { return m_readStrm.CanRead; } }

        public override bool CanWrite { get { return m_writeStrm.CanWrite; } }

        public override bool CanSeek
        {
            get { throw new Exception("The method or operation is not implemented."); }
        }

        public override void Flush()
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public override long Length
        {
            get { throw new Exception("The method or operation is not implemented."); }
        }

        public override long Position
        {
            get
            {
                throw new Exception("The method or operation is not implemented.");
            }
            set
            {
                throw new Exception("The method or operation is not implemented.");
            }
        }

        public override int Read(byte[] buffer, int offset, int count)
        {
            int x = m_readStrm.Read(buffer, offset, count);
            return x;
        }

        public override long Seek(long offset, SeekOrigin origin)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public override void SetLength(long value)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public override void Write(byte[] buffer, int offset, int count)
        {
            m_writeStrm.Write(buffer, offset, count);
        }
    }//class


    /// <exclude/>
    /// <summary>
    /// An internal abstract <see cref="T:System.IO.Stream"/> used to implement some of the 
    /// Close/Dispose functionality from FXv2 when compiling the library for FXv1.1.
    /// </summary>
    public abstract class ForV1CloseDisposeLikeV2Stream : Stream
    {
#if FX1_1
        /* The sequence of related calls supplied by Stream in V2 is a follows.
         * There is no such sequence in V1 so we supply it in the 
         * ForV1CloseDisposeLikeV2Stream class.
         * 
         * public override void Close()
         *   In base (Stream) calls Dispose(true);
         * public void Dispose()
         *   In base (Stream) calls Close();
         * protected override void Dispose(bool disposing)
         *   In base (AbortableStream) calls CloseCore(true);
         *   and then base.Dispose(disposing);
         */

        public /*virtual*/override void Close()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }

        public void Dispose()
        {
            this.Close();
        }

        protected virtual void Dispose(bool disposing)
        { }
#endif
    }//class

}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.