public void TestSetLength() { using (var memory = new MemoryStream()) { using (var filtered = new FilteredStream(memory)) { Assert.Throws <NotSupportedException> (() => filtered.SetLength(500)); } } }
public void TestBestEncodingFilter() { const string fromLines = "This text is meant to test that the filter will armor lines beginning with\nFrom (like mbox).\n"; const string ascii = "This is some ascii text to make sure that\nthe filter returns 7bit encoding...\n"; const string french = "Wikipédia est un projet d’encyclopédie collective en ligne, universelle, multilingue et fonctionnant sur le principe du wiki. Wikipédia a pour objectif d’offrir un contenu librement réutilisable, objectif et vérifiable, que chacun peut modifier et améliorer.\n\nTous les rédacteurs des articles de Wikipédia sont bénévoles. Ils coordonnent leurs efforts au sein d'une communauté collaborative, sans dirigeant."; var filter = new BestEncodingFilter(); TestArgumentExceptions(filter); Assert.Throws <ArgumentOutOfRangeException> (() => filter.GetBestEncoding(EncodingConstraint.SevenBit, 10)); // Test ASCII text using (var stream = new MemoryStream()) { using (var filtered = new FilteredStream(stream)) { var buffer = Encoding.UTF8.GetBytes(ascii); ContentEncoding encoding; Assert.IsFalse(filtered.CanTimeout, "CanTimeout"); Assert.Throws <InvalidOperationException> (() => { var x = filtered.ReadTimeout; }); Assert.Throws <InvalidOperationException> (() => { var x = filtered.WriteTimeout; }); Assert.Throws <InvalidOperationException> (() => filtered.ReadTimeout = 50); Assert.Throws <InvalidOperationException> (() => filtered.WriteTimeout = 50); Assert.Throws <NotSupportedException> (() => { long x = filtered.Length; }); Assert.Throws <NotSupportedException> (() => filtered.SetLength(100)); Assert.Throws <NotSupportedException> (() => { long x = filtered.Position; }); Assert.Throws <NotSupportedException> (() => filtered.Position = 0); Assert.Throws <ArgumentNullException> (() => filtered.Add(null)); Assert.Throws <ArgumentNullException> (() => filtered.Contains(null)); Assert.Throws <ArgumentNullException> (() => filtered.Remove(null)); filtered.Add(filter); Assert.IsTrue(filtered.Contains(filter), "Contains"); filtered.Write(buffer, 0, buffer.Length); filtered.Flush(); encoding = filter.GetBestEncoding(EncodingConstraint.SevenBit); Assert.AreEqual(ContentEncoding.SevenBit, encoding, "ASCII 7bit constraint."); encoding = filter.GetBestEncoding(EncodingConstraint.EightBit); Assert.AreEqual(ContentEncoding.SevenBit, encoding, "ASCII 8bit constraint."); encoding = filter.GetBestEncoding(EncodingConstraint.None); Assert.AreEqual(ContentEncoding.SevenBit, encoding, "ASCII no constraint."); Assert.IsTrue(filtered.Remove(filter), "Remove"); } } filter.Reset(); // Test ASCII text with a line beginning with "From " using (var stream = new MemoryStream()) { using (var filtered = new FilteredStream(stream)) { int fromIndex = fromLines.IndexOf("\nFrom ", StringComparison.Ordinal); var buffer = Encoding.UTF8.GetBytes(fromLines); ContentEncoding encoding; filtered.Add(filter); // write out a buffer where the end boundary falls in the middle of "From " int endIndex = fromIndex + 3; filtered.Write(buffer, 0, endIndex); // write out the rest filtered.Write(buffer, endIndex, buffer.Length - endIndex); filtered.Flush(); encoding = filter.GetBestEncoding(EncodingConstraint.SevenBit); Assert.AreEqual(ContentEncoding.QuotedPrintable, encoding, "From-line 7bit constraint."); encoding = filter.GetBestEncoding(EncodingConstraint.EightBit); Assert.AreEqual(ContentEncoding.QuotedPrintable, encoding, "From-line 8bit constraint."); encoding = filter.GetBestEncoding(EncodingConstraint.None); Assert.AreEqual(ContentEncoding.QuotedPrintable, encoding, "From-line no constraint."); } } filter.Reset(); // Test some French Latin1 text using (var stream = new MemoryStream()) { using (var filtered = new FilteredStream(stream)) { var buffer = Encoding.UTF8.GetBytes(french); ContentEncoding encoding; filtered.Add(filter); // We'll write only 60 chars at first to not exceed the 78 char max filtered.Write(buffer, 0, 60); filtered.Flush(); encoding = filter.GetBestEncoding(EncodingConstraint.SevenBit); Assert.AreEqual(ContentEncoding.QuotedPrintable, encoding, "French 7bit constraint."); encoding = filter.GetBestEncoding(EncodingConstraint.EightBit); Assert.AreEqual(ContentEncoding.EightBit, encoding, "French 8bit constraint."); encoding = filter.GetBestEncoding(EncodingConstraint.None); Assert.AreEqual(ContentEncoding.EightBit, encoding, "French no constraint."); filter.Reset(); // write the entire French text this time (longest line exceeds 78 chars) filtered.Write(buffer, 0, buffer.Length); filtered.Flush(); encoding = filter.GetBestEncoding(EncodingConstraint.SevenBit); Assert.AreEqual(ContentEncoding.QuotedPrintable, encoding, "French (long lines) 7bit constraint."); encoding = filter.GetBestEncoding(EncodingConstraint.EightBit); Assert.AreEqual(ContentEncoding.QuotedPrintable, encoding, "French (long lines) 8bit constraint."); encoding = filter.GetBestEncoding(EncodingConstraint.None); Assert.AreEqual(ContentEncoding.QuotedPrintable, encoding, "French (long lines) no constraint."); } } }