Ejemplo n.º 1
0
 public void CanRead_ReturnsTrue()
 {
     using (var stream = new TextReaderStream(new StringReader(string.Empty)))
     {
         Assert.IsTrue(stream.CanRead);
     }
 }
Ejemplo n.º 2
0
 public void Constructor_NullTextEncoding()
 {
     using (var stream = new TextReaderStream(new StringReader(string.Empty), encoding: null))
     {
         Assert.AreEqual(Encoding.UTF8, stream.Encoding);
     }
 }
Ejemplo n.º 3
0
 public void Write_ThrowsNotSupportedException()
 {
     using (var stream = new TextReaderStream(new StringReader(string.Empty)))
     {
         Assert.Throws <NotSupportedException>(() => stream.Write(new byte[1], 0, 1));
     }
 }
Ejemplo n.º 4
0
 public void Seek_ThrowsNotSupportedException()
 {
     using (var stream = new TextReaderStream(new StringReader(string.Empty)))
     {
         Assert.Throws <NotSupportedException>(() => stream.Seek(0, SeekOrigin.Begin));
     }
 }
Ejemplo n.º 5
0
 public void CanSeek_ReturnsFalse()
 {
     using (var stream = new TextReaderStream(new StringReader(string.Empty)))
     {
         Assert.IsFalse(stream.CanSeek);
     }
 }
Ejemplo n.º 6
0
		public void Index_StartState_ReturnsNegOne()
		{
			using (var scanner = new TextReaderStream(TextReader.Null))
			{
				Assert.Equal(-1, scanner.Index);
			}
		}
Ejemplo n.º 7
0
		public void Line_StartState_ReturnsNegOne()
		{
			using (var scanner = new TextReaderStream(TextReader.Null))
			{
				Assert.Equal(0, scanner.Line);
			}
		}
Ejemplo n.º 8
0
 public void PositionSet_ThrowsNotSupportedException()
 {
     using (var stream = new TextReaderStream(new StringReader(string.Empty)))
     {
         Assert.Throws <NotSupportedException>(() => { stream.Position = 0; });
     }
 }
Ejemplo n.º 9
0
		public void Pop_NullReader_ReturnsNullChar()
		{
			using (var scanner = new TextReaderStream(TextReader.Null))
			{
				Assert.Equal('\0', scanner.Pop());
			}
		}
Ejemplo n.º 10
0
 public void SetLength_ThrowsNotSupportedException()
 {
     using (var stream = new TextReaderStream(new StringReader(string.Empty)))
     {
         Assert.Throws <NotSupportedException>(() => { stream.SetLength(0); });
     }
 }
Ejemplo n.º 11
0
 public void LengthnGet_ThrowsNotSupportedException()
 {
     using (var stream = new TextReaderStream(new StringReader(string.Empty)))
     {
         Assert.Throws <NotSupportedException>(() => { var temp = stream.Length; });
     }
 }
Ejemplo n.º 12
0
        public void ReadBytes_FillBufferWithPartialCharacter()
        {
            // Reading a multi-byte character into a buffer that is too small requires us to buffer
            // the result from the text reader, since we're only returning part of a character.
            // So this test is to make sure we're doing that.

            // This character should be encoded into three bytes for UTF8.
            const string inputString = "☃";

            Assert.AreEqual(3, Encoding.UTF8.GetBytes(inputString).Length);

            using (var stream = new TextReaderStream(new StringReader(inputString)))
            {
                var buffer = new byte[3];

                int readCount = stream.Read(buffer, 0, 2);
                Assert.AreEqual(2, readCount);

                int secondReadCount = stream.Read(buffer, 2, 999);
                Assert.AreEqual(1, secondReadCount);

                var streamText = Encoding.UTF8.GetString(buffer);
                Assert.AreEqual(inputString, streamText);
            }
        }
Ejemplo n.º 13
0
        public void Dispose_ThenReadThrowsObjectDisposedException()
        {
            var stream = new TextReaderStream(new StringReader(string.Empty));

            stream.Dispose();
            Assert.Throws <ObjectDisposedException>(() => stream.Read(new byte[10], 0, 10));
        }
Ejemplo n.º 14
0
        /// <summary>
        /// returns possible languages of text contained in <paramref name="input"/> or empty sequence if too uncertain.
        /// </summary>
        /// <param name="input"></param>
        /// <param name="encoding">encoding of text contained in <paramref name="input"/> or null if encoding is unknown beforehand.
        /// <para> When encoding is not null, for performance and quality reasons
        /// please make sure that <see cref="LanguageIdentifier"/> is created with
        /// languageModelsDirectory parameter of constructor pointing to models
        /// built from UTF8 encoded files (models from folder "Wikipedia-Experimental-UTF8Only")</para></param>
        /// <param name="settings">null for default settings</param>
        /// <returns></returns>
        public IEnumerable <Tuple <LanguageInfo, double> > ClassifyBytes(Stream input, Encoding encoding = null, LanguageIdentifierSettings settings = null)
        {
            if (encoding != null && encoding != Encoding.UTF8)
            {
                // we can afford to not dispose TextReaderStream wrapper as it doesn't contain unmanaged resources
                // we do not own base stream passed so we cannot close it
                input = new TextReaderStream(new StreamReader(input, encoding), Encoding.UTF8); // decodes stream into UTF8 from any other encoding
                // todo: restrict to searching among UTF8 language models only
            }
            if (settings == null)
            {
                settings = new LanguageIdentifierSettings();
            }

            IEnumerable <UInt64> tokens =
                new ByteToUInt64NGramExtractor(settings.MaxNgramLength, settings.OnlyReadFirstNLines)
                .GetFeatures(input);
            var langaugeModel = LanguageModelCreator.CreateLangaugeModel(
                tokens, settings.OccuranceNumberThreshold, _maximumSizeOfDistribution);

            List <Tuple <LanguageInfo, double> > result = _classifier.Classify(langaugeModel).ToList();
            double leastDistance = result.First().Item2;
            List <Tuple <LanguageInfo, double> > acceptableResults =
                result.Where(t => t.Item2 <= leastDistance * settings.WorstAcceptableThreshold).ToList();

            if (acceptableResults.Count == 0 || acceptableResults.Count > settings.TooManyLanguagesThreshold)
            {
                return(Enumerable.Empty <Tuple <LanguageInfo, double> >());
            }
            return(acceptableResults);
        }
Ejemplo n.º 15
0
 public void Flush_DoesNothing()
 {
     using (var stream = new TextReaderStream(new StringReader(string.Empty)))
     {
         // Just to make sure this doesn't throw an exception or anything like that.
         stream.Flush();
     }
 }
Ejemplo n.º 16
0
        public void ReadByte_UnicodeMix()
        {
            const string inputString = "☀a☁b☂☃c☄d☇☈☉";

            using (var stream = new TextReaderStream(new StringReader(inputString)))
            {
                var streamBytes = ReadUsingReadByte(stream).ToArray();
                var streamText  = Encoding.UTF8.GetString(streamBytes);
                Assert.AreEqual(inputString, streamText);
            }
        }
Ejemplo n.º 17
0
        public void Dispose_AlsoDisposesTextReader()
        {
            var readerMock = new Mock <TextReader>();

            readerMock.Protected().Setup("Dispose", true).Verifiable();

            var stream = new TextReaderStream(readerMock.Object);

            stream.Dispose();

            readerMock.Verify();
        }
Ejemplo n.º 18
0
        public void Read_UnicodeMix(int bufferSize)
        {
            const string inputString = "☀a☁b☂☃c☄d☇☈☉";

            Assert.AreEqual(28, Encoding.UTF8.GetBytes(inputString).Length);

            using (var stream = new TextReaderStream(new StringReader(inputString)))
            {
                var streamBytes = ReadUsingBufferSize(stream, bufferSize).ToArray();
                var streamText  = Encoding.UTF8.GetString(streamBytes);
                Assert.AreEqual(inputString, streamText);
            }
        }
Ejemplo n.º 19
0
        public void ReadAllBytes_SimpleString_AllAvailableEncodings()
        {
            const string inputString = "Hello world!";

            foreach (var encodingInfo in Encoding.GetEncodings())
            {
                var encoding = encodingInfo.GetEncoding();
                using (var stream = new TextReaderStream(new StringReader(inputString), encoding: encoding))
                {
                    Assert.AreEqual(encoding, stream.Encoding);

                    byte[] streamBytes;
                    using (var memoryStream = new MemoryStream())
                    {
                        stream.CopyTo(memoryStream);
                        streamBytes = memoryStream.ToArray();
                    }
                    var streamText = encoding.GetString(streamBytes);
                    Assert.AreEqual(inputString, streamText, encodingInfo.DisplayName);
                }
            }
        }
Ejemplo n.º 20
0
		public void IsCompleted_NullReader_ReturnsFalse()
		{
			using (var scanner = new TextReaderStream(TextReader.Null))
			{
				Assert.Equal(true, scanner.IsCompleted);
			}
		}
Ejemplo n.º 21
0
		public void Pop_LongSequence_ReturnsSameSequence()
		{
			const string input = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

			using (var scanner = new TextReaderStream(new StringReader(input)))
			{
				var buffer = new StringBuilder();
				while (!scanner.IsCompleted)
				{
					buffer.Append(scanner.Pop());
				}

				Assert.Equal(input, buffer.ToString());
			}
		}
Ejemplo n.º 22
0
		public void Pop_EscapedSequence_ReturnsSameSequence()
		{
			const string input = @"""\\\b\f\n\r\t\u0123\u4567\u89AB\uCDEF\uabcd\uef4A\""""";

			using (var scanner = new TextReaderStream(new StringReader(input)))
			{
				var buffer = new StringBuilder();
				while (!scanner.IsCompleted)
				{
					buffer.Append(scanner.Pop());
				}

				Assert.Equal(input, buffer.ToString());
			}
		}
Ejemplo n.º 23
0
		public void Pop_UnicodeSequence_ReturnsSameSequence()
		{
			const string input = "私が日本語を話すことはありません。";

			using (var scanner = new TextReaderStream(new StringReader(input)))
			{
				var buffer = new StringBuilder();
				while (!scanner.IsCompleted)
				{
					buffer.Append(scanner.Pop());
				}

				Assert.Equal(input, buffer.ToString());
			}
		}
Ejemplo n.º 24
0
		public void Peek_LongString_ReturnsSameAsPop()
		{
			const string input = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

			using (var scanner = new TextReaderStream(new StringReader(input)))
			{
				while (!scanner.IsCompleted)
				{
					char ch = scanner.Peek();
					Assert.Equal(scanner.Pop(), ch);
				}

				Assert.Equal(true, scanner.IsCompleted);
			}
		}
Ejemplo n.º 25
0
		public void Column_MultilineString_CountsCorrectNumberOfColumns()
		{
			const string input = @"Line one
Line two
Line three
Line Four";

			using (var scanner = new TextReaderStream(new StringReader(input)))
			{
				while (!scanner.IsCompleted)
				{
					scanner.Pop();
				}

				Assert.Equal(9, scanner.Column);
			}
		}
Ejemplo n.º 26
0
		public void Index_MultilineString_CountsCorrectNumberOfChars()
		{
			const string input = @"Line one
Line two
Line three
Line Four";

			using (var scanner = new TextReaderStream(new StringReader(input)))
			{
				long i;
				for (i=0; !scanner.IsCompleted; i++)
				{
					scanner.Pop();
					Assert.Equal(i, scanner.Index);
				}

				Assert.Equal(i-1, scanner.Index);
			}
		}
Ejemplo n.º 27
0
        public override void Execute(string[] commandline)
        {
            if (commandline.Length < 2)
            {
                _console.Out.WriteLine("Error: [local_session_alias] not specified");
                return;
            }
            else if (commandline.Length < 3)
            {
                _console.Out.WriteLine("Error: [command] not specified");
                return;
            }

            ClientContext ctx = _console.GetValue <ClientContext> ("client_context", null);

            if (ctx == null)
            {
                _console.Out.WriteLine("No active connection was found");
                return;
            }

            string localAlias = commandline[1];

            IDictionary <string, TPMSession> tpmSessions = _console.GetValue <IDictionary <string, TPMSession> > ("tpm_sessions", null);

            if (tpmSessions == null || tpmSessions.ContainsKey(localAlias) == false)
            {
                _console.Out.WriteLine("Error: Specified local alias was not found");
                return;
            }

            if (tpmSessions[localAlias].Keystore == null)
            {
                _console.Out.WriteLine("Error: No keystore was opened");
                return;
            }

            string subCommand = commandline[2];
            IDictionary <string, string> arguments = _console.SplitArguments(commandline[3], 0);

            if (arguments.ContainsKey("name") == false)
            {
                _console.Out.WriteLine("Error: no key name was specified");
                return;
            }

//			if(arguments.ContainsKey("pcr") == false)
//			{
//				_console.Out.WriteLine("Error: no pcr values where specified");
//				return;
//			}

            if (arguments.ContainsKey("data_input") == false)
            {
                _console.Out.WriteLine("Error: no data input source specified");
                return;
            }

            DataInputMode dataInputMode;

            try
            {
                dataInputMode = (DataInputMode)Enum.Parse(typeof(DataInputMode), arguments["data_input"], true);
            }
            catch (Exception)
            {
                _console.Out.WriteLine("Error: Invalid data input source");
                return;
            }


            DataFormat inputDataFormat = DataFormat.Raw;

            if (arguments.ContainsKey("input_data_format"))
            {
                try
                {
                    inputDataFormat = (DataFormat)Enum.Parse(typeof(DataFormat), arguments["input_data_format"], true);
                }
                catch (Exception)
                {
                    _console.Out.WriteLine("Error: Invalid input data format");
                    return;
                }
            }


            if (dataInputMode == DataInputMode.File && arguments.ContainsKey("file") == false)
            {
                _console.Out.WriteLine("Error: data_input=file requires file argument!");
                return;
            }


            ClientKeyHandle keyHandle = tpmSessions[localAlias].KeyClient.GetKeyHandleByFriendlyName(arguments["name"]);


            Stream inputStream = null;

            if (dataInputMode == DataInputMode.Console)
            {
                inputStream = new TextReaderStream(_console.In);
            }
            else if (dataInputMode == DataInputMode.Embedded)
            {
                if (commandline.Length <= 3)
                {
                    _console.Out.WriteLine("Error: no embedded data");
                    return;
                }

                StringBuilder embeddedData = new StringBuilder();
                for (int i = 3; i < commandline.Length; i++)
                {
                    embeddedData.Append(commandline[i]);
                    if (i + 1 < commandline.Length)
                    {
                        embeddedData.Append(" ");
                    }
                }

                inputStream = new TextReaderStream(new StringReader(embeddedData.ToString()));
            }
            else if (dataInputMode == DataInputMode.File)
            {
                inputStream = new FileStream(arguments["file"], FileMode.Open, FileAccess.Read);
            }

            if (inputDataFormat == DataFormat.Hex)
            {
                inputStream = new HexFilterStream(inputStream);
            }



            ISigner signatureGenerator = null;


            if (subCommand == "verify")
            {
                signatureGenerator = keyHandle.CreateSigner();
                signatureGenerator.Init(false, null);
            }
            else if (subCommand == "generate")
            {
                signatureGenerator = keyHandle.CreateSigner();
                signatureGenerator.Init(true, null);
            }
            else if (subCommand == "generate_quote" || subCommand == "verify_quote")
            {
                if (arguments.ContainsKey("pcr") == false)
                {
                    _console.Out.WriteLine("Error: No pcrs specified!");
                    return;
                }

                TPMPCRSelection pcrSelection = tpmSessions[localAlias].CreateEmptyPCRSelection();

                foreach (string pcr in arguments["pcr"].Split('|'))
                {
                    int pcrValue = int.Parse(pcr);
                    pcrSelection.PcrSelection.SetBit(pcrValue - 1, true);
                }

                signatureGenerator = keyHandle.CreateQuoter(pcrSelection);
                signatureGenerator.Init(subCommand == "generate_quote", null);
            }

            byte[] buffer = new byte[1024];
            int    read   = 0;

            do
            {
                read = inputStream.Read(buffer, 0, buffer.Length);

                signatureGenerator.BlockUpdate(buffer, 0, read);
            }while(read > 0);

            _console.Out.WriteLine(ByteHelper.ByteArrayToHexString(signatureGenerator.GenerateSignature()));
            _console.Out.WriteLine();
            inputStream.Dispose();
        }
Ejemplo n.º 28
0
        public override void Execute(string[] commandline)
        {
            if (commandline.Length < 2)
            {
                _console.Out.WriteLine("Error: [local_session_alias] not specified");
                return;
            }
            else if (commandline.Length < 3)
            {
                _console.Out.WriteLine("Error: [command] not specified");
                return;
            }

            ClientContext ctx = _console.GetValue <ClientContext> ("client_context", null);

            if (ctx == null)
            {
                _console.Out.WriteLine("No active connection was found");
                return;
            }

            string localAlias = commandline[1];

            IDictionary <string, TPMSession> tpmSessions = _console.GetValue <IDictionary <string, TPMSession> > ("tpm_sessions", null);

            if (tpmSessions == null || tpmSessions.ContainsKey(localAlias) == false)
            {
                _console.Out.WriteLine("Error: Specified local alias was not found");
                return;
            }

            if (tpmSessions[localAlias].Keystore == null)
            {
                _console.Out.WriteLine("Error: No keystore was opened");
                return;
            }

            IDictionary <string, string> arguments = _console.SplitArguments(commandline[2], 0);

            if (arguments.ContainsKey("name") == false)
            {
                _console.Out.WriteLine("Error: no key name was specified");
                return;
            }

            if (arguments.ContainsKey("data_input") == false)
            {
                _console.Out.WriteLine("Error: no data input source specified");
                return;
            }

            TPMSessionSealCommand.DataInputMode dataInputMode;

            try
            {
                dataInputMode = (TPMSessionSealCommand.DataInputMode)Enum.Parse(typeof(TPMSessionSealCommand.DataInputMode), arguments["data_input"], true);
            }
            catch (Exception)
            {
                _console.Out.WriteLine("Error: Invalid data input source");
                return;
            }

            TPMSessionSealCommand.DataOutputMode dataOutputMode;

            try
            {
                dataOutputMode = (TPMSessionSealCommand.DataOutputMode)Enum.Parse(typeof(TPMSessionSealCommand.DataOutputMode), arguments["data_output"], true);
            }
            catch (Exception)
            {
                _console.Out.WriteLine("Error: Invalid data output destination");
                return;
            }

            TPMSessionSealCommand.DataFormat inputDataFormat = TPMSessionSealCommand.DataFormat.Raw;

            if (arguments.ContainsKey("input_data_format"))
            {
                try
                {
                    inputDataFormat = (TPMSessionSealCommand.DataFormat)Enum.Parse(typeof(TPMSessionSealCommand.DataFormat), arguments["input_data_format"], true);
                }
                catch (Exception)
                {
                    _console.Out.WriteLine("Error: Invalid input data format");
                    return;
                }
            }

            TPMSessionSealCommand.DataFormat outputDataFormat = TPMSessionSealCommand.DataFormat.Raw;

            if (arguments.ContainsKey("output_data_format"))
            {
                try
                {
                    outputDataFormat = (TPMSessionSealCommand.DataFormat)Enum.Parse(typeof(TPMSessionSealCommand.DataFormat), arguments["output_data_format"], true);
                }
                catch (Exception)
                {
                    _console.Out.WriteLine("Error: Invalid output data format");
                    return;
                }
            }


            if (dataInputMode == TPMSessionSealCommand.DataInputMode.File && arguments.ContainsKey("file") == false)
            {
                _console.Out.WriteLine("Error: data_input=file requires file argument!");
                return;
            }


            if (dataOutputMode == TPMSessionSealCommand.DataOutputMode.File && arguments.ContainsKey("output_file") == false)
            {
                _console.Out.WriteLine("Error: data_output=file requires output_file argument!");
                return;
            }

            ClientKeyHandle keyHandle = tpmSessions[localAlias].KeyClient.GetKeyHandleByFriendlyName(arguments["name"]);


            Stream inputStream = null;

            if (dataInputMode == TPMSessionSealCommand.DataInputMode.Console)
            {
                inputStream = new TextReaderStream(_console.In);
            }
            else if (dataInputMode == TPMSessionSealCommand.DataInputMode.Embedded)
            {
                if (commandline.Length <= 3)
                {
                    _console.Out.WriteLine("Error: no embedded data");
                    return;
                }

                StringBuilder embeddedData = new StringBuilder();
                for (int i = 3; i < commandline.Length; i++)
                {
                    embeddedData.Append(commandline[i]);
                    if (i + 1 < commandline.Length)
                    {
                        embeddedData.Append(" ");
                    }
                }

                inputStream = new TextReaderStream(new StringReader(embeddedData.ToString()));
            }
            else if (dataInputMode == TPMSessionSealCommand.DataInputMode.File)
            {
                inputStream = new FileStream(arguments["file"], FileMode.Open, FileAccess.Read);
            }

            if (inputDataFormat == TPMSessionSealCommand.DataFormat.Hex)
            {
                inputStream = new HexFilterStream(inputStream);
            }

            Stream outputStream = null;

            if (dataOutputMode == TPMSessionSealCommand.DataOutputMode.Console)
            {
                outputStream = new TextWriterStream(_console.Out);
            }
            else if (dataOutputMode == TPMSessionSealCommand.DataOutputMode.File)
            {
                outputStream = new FileStream(arguments["output_file"], FileMode.OpenOrCreate, FileAccess.Write);
            }

            if (outputDataFormat == TPMSessionSealCommand.DataFormat.Hex)
            {
                outputStream = new HexFilterStream(outputStream);
            }

            IAsymmetricBlockCipher bindCipher = keyHandle.CreateBindBlockCipher();

            bindCipher.Init(false, null);

            int read;

            byte[] buffer = new byte[bindCipher.GetInputBlockSize()];
            do
            {
                read = inputStream.Read(buffer, 0, buffer.Length);

                if (read > 0)
                {
                    byte[] encrypted = bindCipher.ProcessBlock(buffer, 0, read);
                    outputStream.Write(encrypted, 0, encrypted.Length);
                }
            }while(read > 0);

            _console.Out.WriteLine();
            outputStream.Dispose();
            inputStream.Dispose();
        }
Ejemplo n.º 29
0
		public void Pop_NullReader_ReturnsEmptySequence()
		{
			using (var scanner = new TextReaderStream(TextReader.Null))
			{
				var buffer = new StringBuilder();
				while (!scanner.IsCompleted)
				{
					buffer.Append(scanner.Pop());
				}

				Assert.Equal(String.Empty, buffer.ToString());
			}
		}