Example #1
0
        public virtual void TestOverflow() // memory hole
        {
            BaseDirectoryWrapper dir = NewFSDirectory(CreateTempDir("testOverflow"));

            if (dir is MockDirectoryWrapper)
            {
                ((MockDirectoryWrapper)dir).Throttling = MockDirectoryWrapper.Throttling_e.NEVER;
            }
            int blockBits = TestUtil.NextInt(Random(), 14, 28);
            int blockSize = 1 << blockBits;
            var arr       = new byte[TestUtil.NextInt(Random(), blockSize / 2, blockSize * 2)];

            for (int i = 0; i < arr.Length; ++i)
            {
                arr[i] = (byte)(sbyte)i;
            }
            long numBytes = (1L << 31) + TestUtil.NextInt(Random(), 1, blockSize * 3);
            var  p        = new PagedBytes(blockBits);
            var  @out     = dir.CreateOutput("foo", IOContext.DEFAULT);

            for (long i = 0; i < numBytes;)
            {
                Assert.AreEqual(i, @out.FilePointer);
                int len = (int)Math.Min(arr.Length, numBytes - i);
                @out.WriteBytes(arr, len);
                i += len;
            }
            Assert.AreEqual(numBytes, @out.FilePointer);
            @out.Dispose();
            IndexInput @in = dir.OpenInput("foo", IOContext.DEFAULT);

            p.Copy(@in, numBytes);
            PagedBytes.Reader reader = p.Freeze(Random().NextBoolean());

            foreach (long offset in new long[] { 0L, int.MaxValue, numBytes - 1, TestUtil.NextLong(Random(), 1, numBytes - 2) })
            {
                BytesRef b = new BytesRef();
                reader.FillSlice(b, offset, 1);
                Assert.AreEqual(arr[(int)(offset % arr.Length)], b.Bytes[b.Offset]);
            }
            @in.Dispose();
            dir.Dispose();
        }