public void SaveToFileAndReadBack() { byte[] bytes = RandomData.Build(1024, 512); // todo:应该使用反射,取得枚举的最大可能值。 Schema.Context context = (Schema.Context)RandomData.Random.Next((int)Schema.Context.Skeletons, (int)Schema.Context.AnimationClip); const string filePath = TestData.testData_path + "TestFile.doub"; if (System.IO.File.Exists(filePath)) { System.IO.File.Delete(filePath); } ByteBuffer bb = new ByteBuffer(bytes); FileSaver.Save(bb, context, filePath); Schema.Context out_context = Context.Unknown; ByteBuffer bbOut = FileUnserializer.LoadFromFile(filePath, out out_context); Assert.AreEqual(context, out_context); Assert.AreEqual(bytes.Length, bbOut.Length - bbOut.Position); for (int i = 0; i < bytes.Length; i++) { Assert.AreEqual(bytes [i], bbOut.Data [bbOut.Position + i]); } if (System.IO.File.Exists(filePath)) { System.IO.File.Delete(filePath); } }
public void BadZip() { MemoryStream ms = new MemoryStream(); ZipFile zip = new ZipFile(); byte[] bytes = RandomData.Build(2048, 512); zip.AddEntry("OneFileInBadZip.data", bytes); zip.Save(ms); zip.Dispose(); bytes = new byte[ms.Length]; Array.Copy(ms.GetBuffer(), bytes, ms.Length); for (int i = 0; i < 10; i++) { bytes [bytes.Length - 1 - i] = 0xff; } FileStream writeStream = new FileStream(bassPacketPath, FileMode.CreateNew, FileAccess.Write, FileShare.ReadWrite | FileShare.Delete); writeStream.Write(bytes, 0, bytes.Length); writeStream.Dispose(); Assert.Throws <PacketException> (new TestDelegate(() => { DownloaderFactory.Instance.Initialize(DownloadMode.Packet, packetName); })); }
public void BundleLoadError() { // 随机产生bytes数组 // 写入文件作为测试数据 byte[] bytes = RandomData.Build(2048, 512); // 写入文件 const string targetPath = "ShaderErrorBundle.assetbundle"; bool writed = RandomData.WriteToFile(bytes, fullPath + targetPath); Assert.IsTrue(writed); bool bRunned = false; ShaderManager.Instance.LoadAssetBundle( (result, error) => { Assert.IsTrue(result == ShaderLoadResult.BundleLoadError); Assert.IsFalse(string.IsNullOrEmpty(error)); bRunned = true; }, "ShaderErrorBundle.assetbundle"); Assert.IsTrue(bRunned); System.IO.File.Delete(fullPath + targetPath); ShaderManager.Instance.DisposeBundle(); }
public void DataValid() { // 随机产生bytes数组 // 写入文件作为测试数据 byte[] bytes = RandomData.Build(2048, 512); // 写入文件 const string targetPath = "FileDownloader.Dat"; bool writed = RandomData.WriteToFile(bytes, TestData.testResource_path + targetPath); Assert.IsTrue(writed); FileDownloader fd = downloader as FileDownloader; Assert.IsNotNull(fd); bool runned = false; IEnumerator enumerator = fd.ResourceTask(targetPath, (results, error) => { Assert.IsNotNull(results); Assert.AreEqual(bytes.Length, results.Length); Assert.IsTrue(string.IsNullOrEmpty(error)); for (int i = 0; i < bytes.Length; i++) { Assert.AreEqual(bytes [i], results [i]); } // 删除文件 System.IO.File.Delete(TestData.testResource_path + targetPath); runned = true; }); bool completed = enumerator.RunCoroutineWithoutYields(int.MaxValue); Assert.IsTrue(completed); Assert.IsTrue(runned); }
public void NoVersionNumber() { ZipFile zip = new ZipFile(); byte[] bytes = RandomData.Build(2048, 512); zip.AddEntry("OneFileInBadZip.data", bytes); zip.Save(bassPacketPath); zip.Dispose(); Assert.Throws <PacketException> (new TestDelegate(() => { DownloaderFactory.Instance.Initialize(DownloadMode.Packet, packetName); })); }
public void NotZip() { // 创建文件 FileStream writeStream = new FileStream(bassPacketPath, FileMode.CreateNew, FileAccess.Write, FileShare.ReadWrite | FileShare.Delete); byte[] bytes = RandomData.Build(2048, 512); writeStream.Write(bytes, 0, bytes.Length); writeStream.Dispose(); // 写入文件 bool writed = RandomData.WriteToFile(bytes, bassPacketPath); Assert.IsTrue(writed); Assert.Throws <PacketException> (new TestDelegate(() => { DownloaderFactory.Instance.Initialize(DownloadMode.Packet, packetName); })); }
void PreparePacket(Dictionary <string, byte[]> dict, string packetPath, int version, int startIndex) { ZipFile zip = new ZipFile(); /// 随机文件数 int files = RandomData.Random.Next(2, 5); for (int i = 0; i < files; i++) { /// 每个随机文件内容 string fileName = string.Format(filePrefix, i + 1 + startIndex); byte[] bytes = RandomData.Build(2048, 512); dict.Add(fileName, bytes); zip.AddEntry(fileName, bytes); } zip.Comment = version.ToString(); zip.Save(packetPath); zip.Dispose(); }
public void BadEntry() { const string fileName = "OneFileInBadZip.Data"; MemoryStream ms = new MemoryStream(); ZipFile zip = new ZipFile(); byte[] bytes = RandomData.Build(2048, 512); zip.AddEntry(fileName, bytes); zip.Save(ms); zip.Dispose(); bytes = new byte[ms.Length]; Array.Copy(ms.GetBuffer(), bytes, ms.Length); // make entry bad for (int i = 30; i < 51; i++) { bytes [i] = 0xff; } FileStream writeStream = new FileStream(bassPacketPath, FileMode.CreateNew, FileAccess.Write, FileShare.ReadWrite | FileShare.Delete); writeStream.Write(bytes, 0, bytes.Length); writeStream.Dispose(); Assert.DoesNotThrow(new TestDelegate(() => { DownloaderFactory.Instance.Initialize(DownloadMode.Packet, packetName); })); Assert.Throws <PacketException> (new TestDelegate(() => { IDownloader fd = DownloaderFactory.Instance.Downloader; Assert.IsNotNull(fd); IEnumerator enumerator = fd.ResourceTask(fileName, null); bool completed = enumerator.RunCoroutineWithoutYields(int.MaxValue); Assert.IsTrue(completed); })); }