private void Init()
        {
            inputStream  = new CvsStream(new MemoryStream());
            outputStream = new CvsStream(new MemoryStream());

            this.config = SharpCvsLibConfig.GetInstance();
            try {
                if (config.Log.DebugLog.Enabled)
                {
                    requestLog  = new RequestLog();
                    responseLog = new ResponseLog();

                    this.InputStream.RequestMessage.MessageEvent +=
                        new EncodedMessage.MessageHandler(requestLog.Log);
                    this.OutputStream.ResponseMessage.MessageEvent +=
                        new EncodedMessage.MessageHandler(responseLog.Log);
                }
            } catch (Exception e) {
                LOGGER.Error(e);
            }

            if (null == config)
            {
                config = new SharpCvsLibConfig();
            }
            LOGGER.Debug("Config=[" + config.ToString() + "]");

            if (this.repository == null)
            {
                this.repository = DeriveWorkingDirectory();
            }
        }
        public void ReceiveTextFileTest()
        {
            int linefeedChars = 1;    // Input is *nix style so only 1 linefeed char

            // Create a CvsStream based on a MemoryStream for ReceiveTextFile to receive the file from
            MemoryStream     memoryStream     = new MemoryStream();
            GZipOutputStream gzipOutputStream = new GZipOutputStream(memoryStream);
            CvsStream        cvsStream        = new CvsStream(gzipOutputStream);

            UncompressedFileHandlerTest.CreateTextStream(cvsStream);
            gzipOutputStream.Finish();    // This is essential to finish off the zipping
            cvsStream.BaseStream = memoryStream;
            cvsStream.Position   = 0;

            // Create a temporary file to receive the file to
            testFileName = Path.GetTempFileName();

            // Call the function under test
            CompressedFileHandler fileHandler = new CompressedFileHandler();

            fileHandler.ReceiveTextFile(cvsStream, testFileName,
                                        UncompressedFileHandlerTest.GetTextLen(UncompressedFileHandlerTest.TEXT_BLOCKS, linefeedChars));

            // check the received file
            UncompressedFileHandlerTest.CheckTextFile(testFileName);
        }
Beispiel #3
0
        /// <summary>
        ///     Checks that the given stream contains the content
        ///     we expect to find in it.
        /// </summary>
        static public void CheckTextStream(CvsStream cvsStream, bool isCompressed)
        {
            int    linefeedChars = 1; // this stream is intended for cvs
            int    len;
            String numStr;
            String msg;

            // Rewind the memory stream so we can check what was written to it
            cvsStream.Position = 0;

            if (isCompressed)
            {
                // First char should be a 'z'
                Assert.AreEqual(cvsStream.ReadByte(), 'z');
            }

            // Read the first line which should be the line length
            numStr = cvsStream.ReadLine();
            len    = Int32.Parse(numStr);
            msg    = String.Format("Expected length of {0} but got length {1}",
                                   GetTextLen(TEXT_BLOCKS, linefeedChars), len);
            Assert.AreEqual(GetTextLen(TEXT_BLOCKS, linefeedChars), len);

            // Check what was written to the memory stream matches the file we generated
            for (int n = 0; n < GetTextLen(TEXT_BLOCKS, linefeedChars); n++)
            {
                byte actual = (byte)cvsStream.ReadByte();
                byte wanted = GenerateTextByte(n, linefeedChars);
                msg = String.Format("n:{0} actual:{1} wanted:{2}", n, actual, wanted);
                Assert.AreEqual(wanted, actual, msg);
            }
        }
Beispiel #4
0
        /// <summary>
        ///     Checks that the given stream contains the content
        ///     we expect to find in it.
        /// </summary>
        static public void CheckBinaryStream(CvsStream cvsStream, bool isCompressed)
        {
            int    len;
            String numStr;
            String msg;

            // Rewind the memory stream so we can check what was written to it
            cvsStream.Position = 0;

            if (isCompressed)
            {
                // First char should be a 'z'
                Assert.AreEqual(cvsStream.ReadByte(), 'z');
            }

            // Read the first line which should be the line length
            numStr = cvsStream.ReadLine();
            len    = Int32.Parse(numStr);
            msg    = String.Format("Expected length of {0} but got length {1}",
                                   GetBinaryLen(BINARY_BLOCKS), len);
            Assert.AreEqual(GetBinaryLen(BINARY_BLOCKS), len);

            // Check what was written to the memory stream matches the file we generated
            for (int n = 0; n < GetBinaryLen(BINARY_BLOCKS); n++)
            {
                byte actual = (byte)cvsStream.ReadByte();
                byte wanted = GenerateBinaryByte(n);
                msg = String.Format("n:{0} actual:0x{1:X2} wanted:0x{2:X2}", n, actual, wanted);
                Assert.AreEqual(wanted, actual, msg);
            }
        }
        /// <summary>
        /// Send a text file to the cvs server.
        /// </summary>
        /// <param name="outStream"></param>
        /// <param name="fileName"></param>
        public virtual void SendTextFile(CvsStream outStream, string fileName)
        {
            // convert operating system linefeeds (\r\n) to UNIX style
            // linefeeds (\n)
            string     tmpFileName = Path.GetTempFileName();
            FileStream tmpFile     = File.Create(tmpFileName);

            StreamReader fs = File.OpenText(fileName);

            LOGGER.Debug(fileName);
            while (true)
            {
                string line = fs.ReadLine();
//                if (line == null || line.Length == 0 || String.Empty == line) {
                if (line == null)
                {
                    break;
                }

                byte[] buf = new byte[line.Length];
                SharpCvsLibConfig.Encoding.GetBytes(line.ToCharArray(), 0, line.Length, buf, 0);
                tmpFile.Write(buf, 0, buf.Length);
                tmpFile.WriteByte((byte)'\n');
            }
            tmpFile.Close();
            fs.Close();

            // send converted file like a binary file
            SendBinaryFile(outStream, tmpFileName);

            // delete temp file
            File.Delete(tmpFileName);
        }
        /// <summary>
        /// Receive a text file from the cvs server.
        /// </summary>
        /// <param name="inputStream"></param>
        /// <param name="fileName"></param>
        /// <param name="length"></param>
        public override void ReceiveTextFile(CvsStream inputStream, string fileName, int length)
        {
            Stream oldStream = inputStream.BaseStream;

            inputStream.BaseStream = new GZipInputStream(inputStream.BaseStream);
            base.ReceiveTextFile(inputStream, fileName, length);
            inputStream.BaseStream = oldStream;
        }
Beispiel #7
0
 /// <summary>
 ///     Fills the stream with data representing a binary file.
 /// </summary>
 static public void CreateBinaryStream(CvsStream cvsStream)
 {
     // Put a binary file onto the stream
     for (int n = 0; n < GetBinaryLen(BINARY_BLOCKS); n++)
     {
         cvsStream.WriteByte(GenerateBinaryByte(n));
     }
 }
Beispiel #8
0
        /// <summary>
        ///     Fills the stream with data representing a text file.
        /// </summary>
        static public void CreateTextStream(CvsStream cvsStream)
        {
            int linefeedChars = 1;    // we are emulating a file coming from cvs

            // Put a text file onto the stream
            for (int n = 0; n < GetTextLen(TEXT_BLOCKS, linefeedChars); n++)
            {
                cvsStream.WriteByte(GenerateTextByte(n, linefeedChars));
            }
        }
        /// <summary>
        /// Send a binary file to the cvs server.
        /// </summary>
        /// <param name="outStream">Writable stream to the cvs server.</param>
        /// <param name="fileName">The name of the file to stream across.</param>
        public virtual void SendBinaryFile(CvsStream outStream, string fileName)
        {
            FileStream fs = File.OpenRead(fileName);

            byte[] data = new byte[fs.Length];
            fs.Read(data, 0, data.Length);
            fs.Close();

            outStream.SendString(data.Length.ToString() + "\n");
            outStream.Write(data);
        }
        /// <summary>
        /// Receive a binary file from the cvs server.
        /// </summary>
        /// <param name="inputStream"></param>
        /// <param name="fileName"></param>
        /// <param name="length"></param>
        public virtual void ReceiveBinaryFile(CvsStream inputStream,
                                              String fileName, int length)
        {
            byte[] buffer = new byte[length];

            inputStream.ReadBlock(buffer, length);

            FileStream fs = System.IO.File.Create(fileName);

            fs.Write(buffer, 0, length);
            fs.Close();
        }
        /// <summary>
        /// Send a binary file to the cvs server.
        /// </summary>
        /// <param name="outStream"></param>
        /// <param name="fileName"></param>
        public override void SendBinaryFile(CvsStream outStream, string fileName)
        {
            FileStream fs = File.OpenRead(fileName);

            byte[] data = new byte[fs.Length];
            fs.Read(data, 0, data.Length);
            fs.Close();

            Stream oldStream = outStream.BaseStream;

            outStream.BaseStream = new GZipOutputStream(outStream.BaseStream);

            outStream.SendString("z" + data.Length.ToString() + "\n");
            outStream.Write(data);
            outStream.BaseStream = oldStream;
        }
Beispiel #12
0
        public void SendBinaryFileTest()
        {
            // Create a temporary text file as the file to send
            testFileName = CreateTestBinaryFile();

            // Create a CvsStream based on a MemoryStream for SendTextFile to send the file to
            MemoryStream memoryStream = new MemoryStream();
            CvsStream    cvsStream    = new CvsStream(memoryStream);

            // Call the function under test
            UncompressedFileHandler fileHandler = new UncompressedFileHandler();

            fileHandler.SendBinaryFile(cvsStream, testFileName);

            // check what SendBinaryFile put in the stream
            CheckBinaryStream(cvsStream, false);
        }
        public void SendTextFileTest()
        {
            // Create a temporary text file as the file to send
            testFileName = UncompressedFileHandlerTest.CreateTestTextFile();

            // Create a CvsStream based on a MemoryStream for SendTextFile to send the file to
            MemoryStream memoryStream = new MemoryStream();
            CvsStream    cvsStream    = new CvsStream(memoryStream);

            // Call the function under test
            CompressedFileHandler fileHandler = new CompressedFileHandler();

            fileHandler.SendTextFile(cvsStream, testFileName);

            // check what SendTextFile put in the stream
            cvsStream.BaseStream = new GZipInputStream(cvsStream.BaseStream);
            UncompressedFileHandlerTest.CheckTextStream(cvsStream, true);
        }
        /// <summary>
        /// Receive a text file from the cvs server.
        /// </summary>
        /// <param name="inputStream">Input stream from the cvs server.</param>
        /// <param name="fileName">The name of the file to be created.</param>
        /// <param name="length">The number of bytes the file contains.</param>
        public virtual void ReceiveTextFile(CvsStream inputStream,
                                            string fileName, int length)
        {
            byte[] buffer = new byte[length];

            inputStream.ReadBlock(buffer, length);

            // Take care to preserve none printable or other culture token
            // encodings
            using (MemoryStream ms = new MemoryStream(buffer, 0, length)) {
                StreamReader sr = new StreamReader(ms, Encoding.Default);
                StreamWriter sw = new StreamWriter(fileName, false, Encoding.Default);
                while (sr.Peek() >= 0)
                {
                    sw.WriteLine(sr.ReadLine());
                }
                sw.Close();
                sr.Close();
            }
        }
Beispiel #15
0
        public void ReceiveBinaryFileTest()
        {
            // Create a CvsStream based on a MemoryStream for ReceiveTextFile to receive the file from
            MemoryStream memoryStream = new MemoryStream();
            CvsStream    cvsStream    = new CvsStream(memoryStream);

            CreateBinaryStream(cvsStream);
            cvsStream.Position = 0;

            // Create a temporary file to receive the file to
            testFileName = Path.GetTempFileName();

            // Call the function under test
            UncompressedFileHandler fileHandler = new UncompressedFileHandler();

            fileHandler.ReceiveBinaryFile(cvsStream, testFileName, GetBinaryLen(BINARY_BLOCKS));

            // Now validate that the file we received is as expected
            CheckBinaryFile(testFileName);
        }
        /// <summary>
        /// Authentication for the repository.
        /// </summary>
        /// <param name="password"></param>
        public void Authentication(string password)
        {
            this.protocol =
                ProtocolFactory.Instance.GetProtocol(repository.CvsRoot.Protocol);

            this.protocol.Repository = this.Repository;
            this.protocol.Password   = password;

            protocol.Connect();
            this.inputStream  = protocol.InputStream;
            this.outputStream = protocol.OutputStream;


            // TODO: Move these into an abstract request class
            SubmitRequest(new ValidResponsesRequest());
            SubmitRequest(new ValidRequestsRequest());

            SubmitRequest(new UseUnchangedRequest());
            SubmitRequest(new RootRequest(repository.CvsRoot.CvsRepository));

            SubmitRequest(new GlobalOptionRequest(GlobalOptionRequest.Options.QUIET));
        }
Beispiel #17
0
        public void ReceiveTextFileTest()
        {
            int linefeedChars = 1;    // we emulate input from cvs so only 1 linefeed char

            // Create a CvsStream based on a MemoryStream for ReceiveTextFile to receive the file from
            MemoryStream memoryStream = new MemoryStream();
            CvsStream    cvsStream    = new CvsStream(memoryStream);

            // put a text file into the stream
            CreateTextStream(cvsStream);
            cvsStream.Position = 0;

            // Create a temporary file to receive the file to
            testFileName = Path.GetTempFileName();

            // Call the function under test
            UncompressedFileHandler fileHandler = new UncompressedFileHandler();

            fileHandler.ReceiveTextFile(cvsStream, testFileName, GetTextLen(TEXT_BLOCKS, linefeedChars));

            // check the received file
            CheckTextFile(testFileName);
        }
        public void ReceiveBinaryFileTest()
        {
            // Create a CvsStream based on a MemoryStream for ReceiveTextFile to receive the file from
            MemoryStream     memoryStream     = new MemoryStream();
            GZipOutputStream gzipOutputStream = new GZipOutputStream(memoryStream);
            CvsStream        cvsStream        = new CvsStream(gzipOutputStream);

            UncompressedFileHandlerTest.CreateBinaryStream(cvsStream);
            gzipOutputStream.Finish();    // This is essential to finish off the zipping
            cvsStream.BaseStream = memoryStream;
            cvsStream.Position   = 0;

            // Create a temporary file to receive the file to
            testFileName = Path.GetTempFileName();

            // Call the function under test
            CompressedFileHandler fileHandler = new CompressedFileHandler();

            fileHandler.ReceiveBinaryFile(cvsStream, testFileName,
                                          UncompressedFileHandlerTest.GetBinaryLen(UncompressedFileHandlerTest.BINARY_BLOCKS));

            // Now validate that the file we received is as expected
            UncompressedFileHandlerTest.CheckBinaryFile(testFileName);
        }
 /// <summary>
 /// Upload a text file to the cvs server.
 /// </summary>
 /// <param name="outStream"></param>
 /// <param name="fileName"></param>
 public override void SendTextFile(CvsStream outStream, string fileName)
 {
     base.SendTextFile(outStream, fileName); // because it depends ond SendBinaryFile this is OK
 }
 /// <summary>
 /// Set the internal copy of the input stream.
 /// </summary>
 /// <param name="stream"></param>
 protected void SetOutputStream(CvsStream stream)
 {
     this.outputStream = stream;
 }
 /// <summary>
 /// Set the cvs stream and then delegate processing to the parameterless process command.
 /// </summary>
 /// <param name="cvsStream"></param>
 /// <param name="services"></param>
 public void Process(CvsStream cvsStream, IResponseServices services)
 {
     this.stream   = cvsStream;
     this.services = services;
     this.Process();
 }
 /// <summary>
 /// Set the internal copy of the input stream.
 /// </summary>
 /// <param name="stream"></param>
 protected void SetInputStream(CvsStream stream)
 {
     this.inputStream = stream;
 }