public async Task BomScan(Encoding enc) { var data = Enumerable.Range(0, byte.MaxValue).Select(x => ANSI.GetChars(new byte[] { (byte)x })[0]).ToArray(); var encodedBytes = (enc.GetPreamble().AsBucket() + enc.GetBytes(data).AsBucket()).ToArray(); for (int i = 1; i < encodedBytes.Length - 1; i++) { var encSpan = encodedBytes.AsMemory(); var b = encSpan.Slice(0, i).ToArray().AsBucket() + encSpan.Slice(i).ToArray().AsBucket(); BucketBytes bb = await b.ConvertToUtf8(enc).ReadFullAsync(1024); Assert.AreEqual(Escape(new String(data)), Escape(bb.ToUTF8String()), $"Convert Iteration {i}"); if (enc is UnicodeEncoding u && !u.GetPreamble().Any()) { continue; // Unicode without preamble not detectable without scan via .Peek() } b = encSpan.Slice(0, i).ToArray().AsBucket() + encSpan.Slice(i).ToArray().AsBucket(); bb = await b.NormalizeToUtf8().ReadFullAsync(1024); Assert.AreEqual(Escape(new String(data)), Escape(bb.ToUTF8String()), $"Normalize Iteration {i}"); } }
public async Task ConvertBucketToUtf8(Encoding enc) { var data = Enumerable.Range(0, byte.MaxValue).Select(x => ANSI.GetChars(new byte[] { (byte)x })[0]).ToArray(); var encodedBytes = (enc.GetPreamble().AsBucket() + enc.GetBytes(data).AsBucket()).ToArray(); using var b = encodedBytes.AsBucket(); var bb = await b.ConvertToUtf8(enc).ReadFullAsync(1024); Assert.AreEqual(Escape(new String(data)), Escape(bb.ToUTF8String()), Escape(new String(encodedBytes.Select(x => (char)x).ToArray()))); }
public async Task EncodeScan() { var enc = new UTF8Encoding(false); var data = Enumerable.Range(120, 20).Select(x => ANSI.GetChars(new byte[] { (byte)x })[0]).ToArray(); var encodedBytes = (enc.GetPreamble().AsBucket() + enc.GetBytes(data).AsBucket()).ToArray(); for (int i = 1; i < encodedBytes.Length - 1; i++) { var encSpan = encodedBytes.AsMemory(); var b = encSpan.Slice(0, i).ToArray().AsBucket() + encSpan.Slice(i).ToArray().AsBucket(); BucketBytes bb = await b.ConvertToUtf8(enc).ReadFullAsync(1024); Assert.AreEqual(Escape(new String(data)), Escape(bb.ToUTF8String()), $"Convert Iteration {i}"); b = encSpan.Slice(0, i).ToArray().AsBucket() + encSpan.Slice(i).ToArray().AsBucket(); bb = await b.NormalizeToUtf8().ReadFullAsync(1024); Assert.AreEqual(Escape(new String(data)), Escape(bb.ToUTF8String()), $"Normalize Iteration {i}"); } }
public async Task NormalizeBucketToUtf8(Encoding enc) { var data = Enumerable.Range(0, byte.MaxValue).Select(x => ANSI.GetChars(new byte[] { (byte)x })[0]).ToArray(); var encodedBytes = (enc.GetPreamble().AsBucket() + enc.GetBytes(data).AsBucket()).ToArray(); using var b = encodedBytes.AsBucket(); // This wil test the peaking var bb = await b.NormalizeToUtf8().ReadFullAsync(1024); Assert.AreEqual(Escape(new String(data)), Escape(bb.ToUTF8String()), Escape(new String(encodedBytes.Select(x => (char)x).ToArray()))); if (enc is UnicodeEncoding u && !u.GetPreamble().Any()) { return; // Unicode without preamble not detectable without scan via .Peek() } // This will check the byte reading BucketBytes ec = encodedBytes; bb = await(ec.Slice(0, 1).ToArray().AsBucket() + ec.Slice(1).ToArray().AsBucket()).NormalizeToUtf8().ReadFullAsync(1024); Assert.AreEqual(Escape(new String(data)), Escape(bb.ToUTF8String()), Escape(new String(encodedBytes.Select(x => (char)x).ToArray()))); }