001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.fileupload2.core; 018 019import static org.junit.jupiter.api.Assertions.assertArrayEquals; 020import static org.junit.jupiter.api.Assertions.assertThrows; 021import static org.junit.jupiter.api.Assertions.assertTrue; 022import static org.junit.jupiter.api.Assertions.fail; 023 024import java.io.ByteArrayOutputStream; 025import java.io.IOException; 026import java.nio.charset.StandardCharsets; 027 028import org.junit.jupiter.api.Test; 029 030/** 031 */ 032public final class QuotedPrintableDecoderTestCase { 033 034 private static void assertEncoded(final String clearText, final String encoded) throws Exception { 035 final var expected = clearText.getBytes(StandardCharsets.US_ASCII); 036 037 final var out = new ByteArrayOutputStream(encoded.length()); 038 final var encodedData = encoded.getBytes(StandardCharsets.US_ASCII); 039 QuotedPrintableDecoder.decode(encodedData, out); 040 final var actual = out.toByteArray(); 041 042 assertArrayEquals(expected, actual); 043 } 044 045 private static void assertIOException(final String messageText, final String encoded) { 046 final var out = new ByteArrayOutputStream(encoded.length()); 047 final var encodedData = encoded.getBytes(StandardCharsets.US_ASCII); 048 try { 049 QuotedPrintableDecoder.decode(encodedData, out); 050 fail("Expected IOException"); 051 } catch (final IOException e) { 052 final var em = e.getMessage(); 053 assertTrue(em.contains(messageText), "Expected to find " + messageText + " in '" + em + "'"); 054 } 055 } 056 057 @Test 058 public void testBasicEncodeDecode() throws Exception { 059 assertEncoded("= Hello there =\r\n", "=3D Hello there =3D=0D=0A"); 060 } 061 062 @Test 063 public void testEmptyDecode() throws Exception { 064 assertEncoded("", ""); 065 } 066 067 @Test 068 public void testInvalidCharDecode() { 069 assertThrows(IOException.class, () -> assertEncoded("=\r\n", "=3D=XD=XA")); 070 } 071 072 @Test 073 public void testInvalidQuotedPrintableEncoding() throws Exception { 074 assertIOException("truncated escape sequence", "YWJjMTIzXy0uKn4hQCMkJV4mKCkre31cIlxcOzpgLC9bXQ=="); 075 } 076 077 @Test 078 public void testInvalidSoftBreak1() throws Exception { 079 assertIOException("CR must be followed by LF", "=\r\r"); 080 } 081 082 @Test 083 public void testInvalidSoftBreak2() throws Exception { 084 assertIOException("CR must be followed by LF", "=\rn"); 085 } 086 087 @Test 088 public void testPlainDecode() throws Exception { 089 // spaces are allowed in encoded data 090 // There are special rules for trailing spaces; these are not currently implemented. 091 assertEncoded("The quick brown fox jumps over the lazy dog.", "The quick brown fox jumps over the lazy dog."); 092 } 093 094 /** 095 * This is NOT supported by Commons-Codec, see CODEC-121. 096 * 097 * @throws Exception 098 * @see <a href="https://issues.apache.org/jira/browse/CODEC-121">CODEC-121</a> 099 */ 100 @Test 101 public void testSoftLineBreakDecode() throws Exception { 102 assertEncoded("If you believe that truth=beauty, then surely mathematics is the most " + "beautiful branch of philosophy.", 103 "If you believe that truth=3Dbeauty, then " + "surely=20=\r\nmathematics is the most beautiful branch of philosophy."); 104 } 105 106 @Test 107 public void testUnsafeDecode() throws Exception { 108 assertEncoded("=\r\n", "=3D=0D=0A"); 109 } 110 111 @Test 112 public void testUnsafeDecodeLowerCase() throws Exception { 113 assertEncoded("=\r\n", "=3d=0d=0a"); 114 } 115 116 @Test 117 public void testTruncatedEscape() throws Exception { 118 assertIOException("truncated", "=1"); 119 } 120 121}