|
| 1 | +/* |
| 2 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
| 3 | + * |
| 4 | + * Copyright (c) 1997-2017 Oracle and/or its affiliates. All rights reserved. |
| 5 | + * |
| 6 | + * The contents of this file are subject to the terms of either the GNU |
| 7 | + * General Public License Version 2 only ("GPL") or the Common Development |
| 8 | + * and Distribution License("CDDL") (collectively, the "License"). You |
| 9 | + * may not use this file except in compliance with the License. You can |
| 10 | + * obtain a copy of the License at |
| 11 | + * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html |
| 12 | + * or packager/legal/LICENSE.txt. See the License for the specific |
| 13 | + * language governing permissions and limitations under the License. |
| 14 | + * |
| 15 | + * When distributing the software, include this License Header Notice in each |
| 16 | + * file and include the License file at packager/legal/LICENSE.txt. |
| 17 | + * |
| 18 | + * GPL Classpath Exception: |
| 19 | + * Oracle designates this particular file as subject to the "Classpath" |
| 20 | + * exception as provided by Oracle in the GPL Version 2 section of the License |
| 21 | + * file that accompanied this code. |
| 22 | + * |
| 23 | + * Modifications: |
| 24 | + * If applicable, add the following below the License Header, with the fields |
| 25 | + * enclosed by brackets [] replaced by your own identifying information: |
| 26 | + * "Portions Copyright [year] [name of copyright owner]" |
| 27 | + * |
| 28 | + * Contributor(s): |
| 29 | + * If you wish your version of this file to be governed by only the CDDL or |
| 30 | + * only the GPL Version 2, indicate your decision by adding "[Contributor] |
| 31 | + * elects to include this software in this distribution under the [CDDL or GPL |
| 32 | + * Version 2] license." If you don't indicate a single choice of license, a |
| 33 | + * recipient has the option to distribute your version of this file under |
| 34 | + * either the CDDL, the GPL Version 2 or to extend the choice of license to |
| 35 | + * its licensees as provided above. However, if you add GPL Version 2 code |
| 36 | + * and therefore, elected the GPL Version 2 license, then the option applies |
| 37 | + * only if the new code is made subject to such option by the copyright |
| 38 | + * holder. |
| 39 | + * |
| 40 | + * |
| 41 | + * This file incorporates work covered by the following copyright and |
| 42 | + * permission notice: |
| 43 | + * |
| 44 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 45 | + * contributor license agreements. See the NOTICE file distributed with |
| 46 | + * this work for additional information regarding copyright ownership. |
| 47 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 48 | + * (the "License"); you may not use this file except in compliance with |
| 49 | + * the License. You may obtain a copy of the License at |
| 50 | + * |
| 51 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 52 | + * |
| 53 | + * Unless required by applicable law or agreed to in writing, software |
| 54 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 55 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 56 | + * See the License for the specific language governing permissions and |
| 57 | + * limitations under the License. |
| 58 | + */ |
| 59 | +package org.apache.catalina.fileupload; |
| 60 | + |
| 61 | +import static org.junit.Assert.assertNotNull; |
| 62 | + |
| 63 | +import java.io.ByteArrayInputStream; |
| 64 | +import java.io.InputStream; |
| 65 | + |
| 66 | +import org.junit.Test; |
| 67 | + |
| 68 | + |
| 69 | +/** |
| 70 | + * Unit tests {@link org.apache.commons.fileupload.MultipartStream}. |
| 71 | + * |
| 72 | + * @version $Id$ |
| 73 | + */ |
| 74 | +public class MultipartStreamTest { |
| 75 | + |
| 76 | + static private final String BOUNDARY_TEXT = "myboundary"; |
| 77 | + |
| 78 | + /** |
| 79 | + * The Carriage Return ASCII character value. |
| 80 | + */ |
| 81 | + public static final byte CR = 0x0D; |
| 82 | + |
| 83 | + |
| 84 | + /** |
| 85 | + * The Line Feed ASCII character value. |
| 86 | + */ |
| 87 | + public static final byte LF = 0x0A; |
| 88 | + |
| 89 | + |
| 90 | + /** |
| 91 | + * The dash (-) ASCII character value. |
| 92 | + */ |
| 93 | + public static final byte DASH = 0x2D; |
| 94 | + |
| 95 | + @Test |
| 96 | + public void testThreeParamConstructor() throws Exception { |
| 97 | + final String strData = "foobar"; |
| 98 | + final byte[] contents = strData.getBytes(); |
| 99 | + InputStream input = new ByteArrayInputStream(contents); |
| 100 | + byte[] boundary = BOUNDARY_TEXT.getBytes(); |
| 101 | + byte[] BOUNDARY_PREFIX = {CR, LF, DASH, DASH}; |
| 102 | + int iBufSize = |
| 103 | + boundary.length + BOUNDARY_PREFIX.length + 1; |
| 104 | + MultipartStream ms = new MultipartStream( |
| 105 | + input, |
| 106 | + boundary, |
| 107 | + iBufSize, |
| 108 | + new MultipartStream.ProgressNotifier(null, contents.length)); |
| 109 | + assertNotNull(ms); |
| 110 | + } |
| 111 | + |
| 112 | + @Test(expected=IllegalArgumentException.class) |
| 113 | + public void testSmallBuffer() throws Exception { |
| 114 | + final String strData = "foobar"; |
| 115 | + final byte[] contents = strData.getBytes(); |
| 116 | + InputStream input = new ByteArrayInputStream(contents); |
| 117 | + byte[] boundary = BOUNDARY_TEXT.getBytes(); |
| 118 | + int iBufSize = 1; |
| 119 | + @SuppressWarnings("unused") |
| 120 | + MultipartStream ms = new MultipartStream( |
| 121 | + input, |
| 122 | + boundary, |
| 123 | + iBufSize, |
| 124 | + new MultipartStream.ProgressNotifier(null, contents.length)); |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + public void testTwoParamConstructor() throws Exception { |
| 129 | + final String strData = "foobar"; |
| 130 | + final byte[] contents = strData.getBytes(); |
| 131 | + InputStream input = new ByteArrayInputStream(contents); |
| 132 | + byte[] boundary = BOUNDARY_TEXT.getBytes(); |
| 133 | + MultipartStream ms = new MultipartStream( |
| 134 | + input, |
| 135 | + boundary, |
| 136 | + new MultipartStream.ProgressNotifier(null, contents.length)); |
| 137 | + assertNotNull(ms); |
| 138 | + } |
| 139 | + |
| 140 | +} |
0 commit comments