Exemple #1
0
        public unsafe void DecoderIsUsedCorrectly()
        {
            byte *ptr = null;

            byte[] buffer = null;
            int    bytesRead;
            bool   prefixed = false;

            var decoder = new TestMetadataStringDecoder(
                Encoding.UTF8,
                (bytes, byteCount) =>
            {
                Assert.True(ptr != null);
                Assert.True(prefixed != (ptr == bytes));
                Assert.Equal(prefixed ? "PrefixTest".Length : "Test".Length, byteCount);
                string s = Encoding.UTF8.GetString(bytes, byteCount);
                Assert.Equal(s, prefixed ? "PrefixTest" : "Test");
                return("Intercepted");
            }
                );

            fixed(byte *fixedPtr = (buffer = Encoding.UTF8.GetBytes("Test")))
            {
                ptr = fixedPtr;
                Assert.Equal("Intercepted", new MemoryBlock(ptr, buffer.Length).PeekUtf8NullTerminated(0, null, decoder, out bytesRead));
                Assert.Equal(buffer.Length, bytesRead);

                prefixed = true;
                Assert.Equal("Intercepted", new MemoryBlock(ptr, buffer.Length).PeekUtf8NullTerminated(0, Encoding.UTF8.GetBytes("Prefix"), decoder, out bytesRead));
                Assert.Equal(buffer.Length, bytesRead);
            }

            // decoder will fail to intercept because we don't bother calling it for empty strings.
            Assert.Same(string.Empty, new MemoryBlock(null, 0).PeekUtf8NullTerminated(0, null, decoder, out bytesRead));
            Assert.Equal(0, bytesRead);

            Assert.Same(string.Empty, new MemoryBlock(null, 0).PeekUtf8NullTerminated(0, new byte[0], decoder, out bytesRead));
            Assert.Equal(0, bytesRead);
        }
Exemple #2
0
        private unsafe void TestGetMetadataReader(MetadataReaderProvider provider)
        {
            var decoder = new TestMetadataStringDecoder(Encoding.UTF8, (a, b) => "str");

            var reader1 = provider.GetMetadataReader(MetadataReaderOptions.None, decoder);

            Assert.Equal("str", reader1.MetadataVersion);
            Assert.Same(reader1.UTF8Decoder, decoder);
            Assert.Equal(reader1.Options, MetadataReaderOptions.None);

            var reader2 = provider.GetMetadataReader(MetadataReaderOptions.None, decoder);

            Assert.Same(reader1, reader2);

            var reader3 = provider.GetMetadataReader(MetadataReaderOptions.None);

            Assert.NotSame(reader2, reader3);
            Assert.Equal("v9.9.9.9", reader3.MetadataVersion);
            Assert.Same(reader3.UTF8Decoder, MetadataStringDecoder.DefaultUTF8);
            Assert.Equal(reader3.Options, MetadataReaderOptions.None);

            var reader4 = provider.GetMetadataReader(MetadataReaderOptions.None);

            Assert.Same(reader3, reader4);

            var reader5 = provider.GetMetadataReader(MetadataReaderOptions.ApplyWindowsRuntimeProjections);

            Assert.NotSame(reader4, reader5);
            Assert.Equal("v9.9.9.9", reader5.MetadataVersion);
            Assert.Same(reader5.UTF8Decoder, MetadataStringDecoder.DefaultUTF8);
            Assert.Equal(reader5.Options, MetadataReaderOptions.ApplyWindowsRuntimeProjections);

            provider.Dispose();
            Assert.Throws <ObjectDisposedException>(() => provider.GetMetadataReader(MetadataReaderOptions.ApplyWindowsRuntimeProjections));
            Assert.Throws <ObjectDisposedException>(() => provider.GetMetadataReader());
        }
        private unsafe void TestGetMetadataReader(MetadataReaderProvider provider)
        {
            var decoder = new TestMetadataStringDecoder(Encoding.UTF8, (a, b) => "str");

            var reader1 = provider.GetMetadataReader(MetadataReaderOptions.None, decoder);
            Assert.Equal("str", reader1.MetadataVersion);
            Assert.Same(reader1.Utf8Decoder, decoder);
            Assert.Equal(reader1.Options, MetadataReaderOptions.None);

            var reader2 = provider.GetMetadataReader(MetadataReaderOptions.None, decoder);
            Assert.Same(reader1, reader2);

            var reader3 = provider.GetMetadataReader(MetadataReaderOptions.None);
            Assert.NotSame(reader2, reader3);
            Assert.Equal("v9.9.9.9", reader3.MetadataVersion);
            Assert.Same(reader3.Utf8Decoder, MetadataStringDecoder.DefaultUTF8);
            Assert.Equal(reader3.Options, MetadataReaderOptions.None);

            var reader4 = provider.GetMetadataReader(MetadataReaderOptions.None);
            Assert.Same(reader3, reader4);

            var reader5 = provider.GetMetadataReader(MetadataReaderOptions.ApplyWindowsRuntimeProjections);
            Assert.NotSame(reader4, reader5);
            Assert.Equal("v9.9.9.9", reader5.MetadataVersion);
            Assert.Same(reader5.Utf8Decoder, MetadataStringDecoder.DefaultUTF8);
            Assert.Equal(reader5.Options, MetadataReaderOptions.ApplyWindowsRuntimeProjections);

            provider.Dispose();
            Assert.Throws<ObjectDisposedException>(() => provider.GetMetadataReader(MetadataReaderOptions.ApplyWindowsRuntimeProjections));
            Assert.Throws<ObjectDisposedException>(() => provider.GetMetadataReader());
        }
        public unsafe void DecoderIsUsedCorrectly()
        {
            byte* ptr = null;
            byte[] buffer = null;
            int bytesRead;
            bool prefixed = false;

            var decoder = new TestMetadataStringDecoder(
                Encoding.UTF8,
                (bytes, byteCount) =>
                {
                    Assert.True(ptr != null);
                    Assert.True(prefixed != (ptr == bytes));
                    Assert.Equal(prefixed ? "PrefixTest".Length : "Test".Length, byteCount);
                    string s = Encoding.UTF8.GetString(bytes, byteCount);
                    Assert.Equal(s, prefixed ? "PrefixTest" : "Test");
                    return "Intercepted";
                }
             );

            fixed (byte* fixedPtr = (buffer = Encoding.UTF8.GetBytes("Test")))
            {
                ptr = fixedPtr;
                Assert.Equal("Intercepted", new MemoryBlock(ptr, buffer.Length).PeekUtf8NullTerminated(0, null, decoder, out bytesRead));
                Assert.Equal(buffer.Length, bytesRead);

                prefixed = true;
                Assert.Equal("Intercepted", new MemoryBlock(ptr, buffer.Length).PeekUtf8NullTerminated(0, Encoding.UTF8.GetBytes("Prefix"), decoder, out bytesRead));
                Assert.Equal(buffer.Length, bytesRead);
            }

            // decoder will fail to intercept because we don't bother calling it for empty strings.
            Assert.Same(string.Empty, new MemoryBlock(null, 0).PeekUtf8NullTerminated(0, null, decoder, out bytesRead));
            Assert.Equal(0, bytesRead);

            Assert.Same(string.Empty, new MemoryBlock(null, 0).PeekUtf8NullTerminated(0, new byte[0], decoder, out bytesRead));
            Assert.Equal(0, bytesRead);
        }