Beispiel #1
0
        public virtual void TestStandardFormat_SmallObject()
        {
            int type = Constants.OBJ_BLOB;

            byte[]       data = GetRng().NextBytes(300);
            byte[]       gz   = CompressStandardFormat(type, data);
            ObjectId     id   = ObjectId.ZeroId;
            ObjectLoader ol   = UnpackedObject.Open(new ByteArrayInputStream(gz), Path(id), id,
                                                    wc);

            NUnit.Framework.Assert.IsNotNull(ol, "created loader");
            NUnit.Framework.Assert.AreEqual(type, ol.GetType());
            NUnit.Framework.Assert.AreEqual(data.Length, ol.GetSize());
            NUnit.Framework.Assert.IsFalse(ol.IsLarge(), "is not large");
            NUnit.Framework.Assert.IsTrue(Arrays.Equals(data, ol.GetCachedBytes()), "same content"
                                          );
            ObjectStream @in = ol.OpenStream();

            NUnit.Framework.Assert.IsNotNull(@in, "have stream");
            NUnit.Framework.Assert.AreEqual(type, @in.GetType());
            NUnit.Framework.Assert.AreEqual(data.Length, @in.GetSize());
            byte[] data2 = new byte[data.Length];
            IOUtil.ReadFully(@in, data2, 0, data.Length);
            NUnit.Framework.Assert.IsTrue(Arrays.Equals(data2, data), "same content");
            NUnit.Framework.Assert.AreEqual(-1, @in.Read(), "stream at EOF");
            @in.Close();
        }
Beispiel #2
0
        public virtual void TestPackFormat_DeltaNotAllowed()
        {
            ObjectId id = ObjectId.ZeroId;

            byte[] data = GetRng().NextBytes(300);
            try
            {
                byte[] gz = CompressPackFormat(Constants.OBJ_OFS_DELTA, data);
                UnpackedObject.Open(new ByteArrayInputStream(gz), Path(id), id, wc);
                NUnit.Framework.Assert.Fail("Did not throw CorruptObjectException");
            }
            catch (CorruptObjectException coe)
            {
                NUnit.Framework.Assert.AreEqual(MessageFormat.Format(JGitText.Get().objectIsCorrupt
                                                                     , id.Name, JGitText.Get().corruptObjectInvalidType), coe.Message);
            }
            try
            {
                byte[] gz = CompressPackFormat(Constants.OBJ_REF_DELTA, data);
                UnpackedObject.Open(new ByteArrayInputStream(gz), Path(id), id, wc);
                NUnit.Framework.Assert.Fail("Did not throw CorruptObjectException");
            }
            catch (CorruptObjectException coe)
            {
                NUnit.Framework.Assert.AreEqual(MessageFormat.Format(JGitText.Get().objectIsCorrupt
                                                                     , id.Name, JGitText.Get().corruptObjectInvalidType), coe.Message);
            }
            try
            {
                byte[] gz = CompressPackFormat(Constants.OBJ_TYPE_5, data);
                UnpackedObject.Open(new ByteArrayInputStream(gz), Path(id), id, wc);
                NUnit.Framework.Assert.Fail("Did not throw CorruptObjectException");
            }
            catch (CorruptObjectException coe)
            {
                NUnit.Framework.Assert.AreEqual(MessageFormat.Format(JGitText.Get().objectIsCorrupt
                                                                     , id.Name, JGitText.Get().corruptObjectInvalidType), coe.Message);
            }
            try
            {
                byte[] gz = CompressPackFormat(Constants.OBJ_EXT, data);
                UnpackedObject.Open(new ByteArrayInputStream(gz), Path(id), id, wc);
                NUnit.Framework.Assert.Fail("Did not throw CorruptObjectException");
            }
            catch (CorruptObjectException coe)
            {
                NUnit.Framework.Assert.AreEqual(MessageFormat.Format(JGitText.Get().objectIsCorrupt
                                                                     , id.Name, JGitText.Get().corruptObjectInvalidType), coe.Message);
            }
        }
Beispiel #3
0
 /// <exception cref="System.IO.IOException"></exception>
 public override void Close()
 {
     try
     {
         if (this.remaining <= 0)
         {
             UnpackedObject.CheckValidEndOfStream(this.@in, this.inf, id, new byte[64]);
         }
     }
     finally
     {
         InflaterCache.Release(this.inf);
         base.Close();
     }
 }
Beispiel #4
0
        public virtual void TestPackFormat_LargeObject()
        {
            int type = Constants.OBJ_BLOB;

            byte[]   data = GetRng().NextBytes(streamThreshold + 5);
            ObjectId id   = new ObjectInserter.Formatter().IdFor(type, data);

            Write(id, CompressPackFormat(type, data));
            ObjectLoader ol;

            {
                FileInputStream fs = new FileInputStream(Path(id));
                try
                {
                    ol = UnpackedObject.Open(fs, Path(id), id, wc);
                }
                finally
                {
                    fs.Close();
                }
            }
            NUnit.Framework.Assert.IsNotNull(ol, "created loader");
            NUnit.Framework.Assert.AreEqual(type, ol.GetType());
            NUnit.Framework.Assert.AreEqual(data.Length, ol.GetSize());
            NUnit.Framework.Assert.IsTrue(ol.IsLarge(), "is large");
            try
            {
                ol.GetCachedBytes();
                NUnit.Framework.Assert.Fail("Should have thrown LargeObjectException");
            }
            catch (LargeObjectException tooBig)
            {
                NUnit.Framework.Assert.AreEqual(MessageFormat.Format(JGitText.Get().largeObjectException
                                                                     , id.Name), tooBig.Message);
            }
            ObjectStream @in = ol.OpenStream();

            NUnit.Framework.Assert.IsNotNull(@in, "have stream");
            NUnit.Framework.Assert.AreEqual(type, @in.GetType());
            NUnit.Framework.Assert.AreEqual(data.Length, @in.GetSize());
            byte[] data2 = new byte[data.Length];
            IOUtil.ReadFully(@in, data2, 0, data.Length);
            NUnit.Framework.Assert.IsTrue(Arrays.Equals(data2, data), "same content");
            NUnit.Framework.Assert.AreEqual(-1, @in.Read(), "stream at EOF");
            @in.Close();
        }
Beispiel #5
0
        public virtual void TestStandardFormat_GarbageAfterSize()
        {
            ObjectId id = ObjectId.ZeroId;

            byte[] data = GetRng().NextBytes(300);
            try
            {
                byte[] gz = CompressStandardFormat("blob", "1foo", data);
                UnpackedObject.Open(new ByteArrayInputStream(gz), Path(id), id, wc);
                NUnit.Framework.Assert.Fail("Did not throw CorruptObjectException");
            }
            catch (CorruptObjectException coe)
            {
                NUnit.Framework.Assert.AreEqual(MessageFormat.Format(JGitText.Get().objectIsCorrupt
                                                                     , id.Name, JGitText.Get().corruptObjectGarbageAfterSize), coe.Message);
            }
        }
Beispiel #6
0
        public virtual void TestStandardFormat_NoHeader()
        {
            ObjectId id = ObjectId.ZeroId;

            byte[] data = new byte[] {  };
            try
            {
                byte[] gz = CompressStandardFormat(string.Empty, string.Empty, data);
                UnpackedObject.Open(new ByteArrayInputStream(gz), Path(id), id, wc);
                NUnit.Framework.Assert.Fail("Did not throw CorruptObjectException");
            }
            catch (CorruptObjectException coe)
            {
                NUnit.Framework.Assert.AreEqual(MessageFormat.Format(JGitText.Get().objectIsCorrupt
                                                                     , id.Name, JGitText.Get().corruptObjectNoHeader), coe.Message);
            }
        }
Beispiel #7
0
        public virtual void TestStandardFormat_LargeObject_CorruptZLibStream()
        {
            int type = Constants.OBJ_BLOB;

            byte[]   data = GetRng().NextBytes(streamThreshold + 5);
            ObjectId id   = new ObjectInserter.Formatter().IdFor(type, data);

            byte[] gz = CompressStandardFormat(type, data);
            gz[gz.Length - 1] = 0;
            gz[gz.Length - 2] = 0;
            Write(id, gz);
            ObjectLoader ol;

            {
                FileInputStream fs = new FileInputStream(Path(id));
                try
                {
                    ol = UnpackedObject.Open(fs, Path(id), id, wc);
                }
                finally
                {
                    fs.Close();
                }
            }
            try
            {
                byte[]      tmp = new byte[data.Length];
                InputStream @in = ol.OpenStream();
                try
                {
                    IOUtil.ReadFully(@in, tmp, 0, tmp.Length);
                }
                finally
                {
                    @in.Close();
                }
                NUnit.Framework.Assert.Fail("Did not throw CorruptObjectException");
            }
            catch (CorruptObjectException coe)
            {
                NUnit.Framework.Assert.AreEqual(MessageFormat.Format(JGitText.Get().objectIsCorrupt
                                                                     , id.Name, JGitText.Get().corruptObjectBadStream), coe.Message);
            }
        }
Beispiel #8
0
        public virtual void TestStandardFormat_SmallObject_TrailingGarbage()
        {
            ObjectId id = ObjectId.ZeroId;

            byte[] data = GetRng().NextBytes(300);
            try
            {
                byte[] gz = CompressStandardFormat(Constants.OBJ_BLOB, data);
                byte[] tr = new byte[gz.Length + 1];
                System.Array.Copy(gz, 0, tr, 0, gz.Length);
                UnpackedObject.Open(new ByteArrayInputStream(tr), Path(id), id, wc);
                NUnit.Framework.Assert.Fail("Did not throw CorruptObjectException");
            }
            catch (CorruptObjectException coe)
            {
                NUnit.Framework.Assert.AreEqual(MessageFormat.Format(JGitText.Get().objectIsCorrupt
                                                                     , id.Name, JGitText.Get().corruptObjectBadStream), coe.Message);
            }
        }
Beispiel #9
0
 /// <exception cref="System.IO.IOException"></exception>
 internal override long GetObjectSize2(WindowCursor curs, string objectName, AnyObjectId
                                       objectId)
 {
     try
     {
         FilePath        path = FileFor(objectName);
         FileInputStream @in  = new FileInputStream(path);
         try
         {
             return(UnpackedObject.GetSize(@in, objectId, curs));
         }
         finally
         {
             @in.Close();
         }
     }
     catch (FileNotFoundException)
     {
         return(-1);
     }
 }
Beispiel #10
0
        public virtual void TestStandardFormat_SmallObject_CorruptZLibStream()
        {
            ObjectId id = ObjectId.ZeroId;

            byte[] data = GetRng().NextBytes(300);
            try
            {
                byte[] gz = CompressStandardFormat(Constants.OBJ_BLOB, data);
                for (int i = 5; i < gz.Length; i++)
                {
                    gz[i] = 0;
                }
                UnpackedObject.Open(new ByteArrayInputStream(gz), Path(id), id, wc);
                NUnit.Framework.Assert.Fail("Did not throw CorruptObjectException");
            }
            catch (CorruptObjectException coe)
            {
                NUnit.Framework.Assert.AreEqual(MessageFormat.Format(JGitText.Get().objectIsCorrupt
                                                                     , id.Name, JGitText.Get().corruptObjectBadStream), coe.Message);
            }
        }
Beispiel #11
0
 /// <exception cref="System.IO.IOException"></exception>
 internal override ObjectLoader OpenObject2(WindowCursor curs, string objectName,
                                            AnyObjectId objectId)
 {
     try
     {
         FilePath        path = FileFor(objectName);
         FileInputStream @in  = new FileInputStream(path);
         try
         {
             unpackedObjectCache.Add(objectId);
             return(UnpackedObject.Open(@in, path, objectId, curs));
         }
         finally
         {
             @in.Close();
         }
     }
     catch (FileNotFoundException)
     {
         unpackedObjectCache.Remove(objectId);
         return(null);
     }
 }