|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.tika.pipes.core.server; |
| 18 | + |
| 19 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 20 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 21 | + |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | + |
| 24 | +import org.apache.tika.io.TikaInputStream; |
| 25 | +import org.apache.tika.metadata.HttpHeaders; |
| 26 | +import org.apache.tika.metadata.Metadata; |
| 27 | +import org.apache.tika.metadata.TikaCoreProperties; |
| 28 | +import org.apache.tika.mime.MediaType; |
| 29 | +import org.apache.tika.mime.MimeTypes; |
| 30 | +import org.apache.tika.parser.ParseContext; |
| 31 | + |
| 32 | +/** |
| 33 | + * Unit tests for {@link PipesWorker#carryCallerHints(Metadata, Metadata)}, which carries the |
| 34 | + * caller-supplied detection hints across the worker's fresh-metadata boundary. |
| 35 | + */ |
| 36 | +public class PipesWorkerCallerHintsTest { |
| 37 | + |
| 38 | + @Test |
| 39 | + public void testCarriesResourceNameAndContentType() { |
| 40 | + Metadata tuple = new Metadata(); |
| 41 | + tuple.set(TikaCoreProperties.RESOURCE_NAME_KEY, "photo.nef"); |
| 42 | + tuple.set(HttpHeaders.CONTENT_TYPE, "image/x-raw-nikon"); |
| 43 | + |
| 44 | + Metadata target = new Metadata(); |
| 45 | + PipesWorker.carryCallerHints(tuple, target); |
| 46 | + |
| 47 | + assertEquals("photo.nef", target.get(TikaCoreProperties.RESOURCE_NAME_KEY)); |
| 48 | + assertEquals("image/x-raw-nikon", target.get(HttpHeaders.CONTENT_TYPE)); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * The Content-Type is carried only as a soft hint. The unconditional override keys |
| 53 | + * must never be carried, or a caller could force any type past detection. |
| 54 | + */ |
| 55 | + @Test |
| 56 | + public void testDoesNotCarryOverrides() { |
| 57 | + Metadata tuple = new Metadata(); |
| 58 | + tuple.set(TikaCoreProperties.CONTENT_TYPE_USER_OVERRIDE, "image/x-raw-nikon"); |
| 59 | + tuple.set(TikaCoreProperties.CONTENT_TYPE_PARSER_OVERRIDE, "image/x-raw-nikon"); |
| 60 | + |
| 61 | + Metadata target = new Metadata(); |
| 62 | + PipesWorker.carryCallerHints(tuple, target); |
| 63 | + |
| 64 | + assertNull(target.get(TikaCoreProperties.CONTENT_TYPE_USER_OVERRIDE)); |
| 65 | + assertNull(target.get(TikaCoreProperties.CONTENT_TYPE_PARSER_OVERRIDE)); |
| 66 | + assertNull(target.get(HttpHeaders.CONTENT_TYPE)); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void testNullTupleIsNoOp() { |
| 71 | + Metadata target = new Metadata(); |
| 72 | + target.set(TikaCoreProperties.RESOURCE_NAME_KEY, "keep.me"); |
| 73 | + PipesWorker.carryCallerHints(null, target); |
| 74 | + assertEquals("keep.me", target.get(TikaCoreProperties.RESOURCE_NAME_KEY)); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void testBlankValuesNotCarried() { |
| 79 | + Metadata tuple = new Metadata(); |
| 80 | + tuple.set(TikaCoreProperties.RESOURCE_NAME_KEY, " "); |
| 81 | + tuple.set(HttpHeaders.CONTENT_TYPE, ""); |
| 82 | + |
| 83 | + Metadata target = new Metadata(); |
| 84 | + PipesWorker.carryCallerHints(tuple, target); |
| 85 | + |
| 86 | + assertNull(target.get(TikaCoreProperties.RESOURCE_NAME_KEY)); |
| 87 | + assertNull(target.get(HttpHeaders.CONTENT_TYPE)); |
| 88 | + } |
| 89 | + |
| 90 | + //content that magic-detects as image/tiff (little-endian TIFF marker, no CR2 marker) |
| 91 | + private static final byte[] TIFF_BYTES = {'I', 'I', 0x2A, 0x00, 0, 0, 0, 8}; |
| 92 | + |
| 93 | + private static MediaType detectWithCarriedContentType(String contentType) throws Exception { |
| 94 | + Metadata tuple = new Metadata(); |
| 95 | + tuple.set(HttpHeaders.CONTENT_TYPE, contentType); |
| 96 | + Metadata target = new Metadata(); |
| 97 | + PipesWorker.carryCallerHints(tuple, target); |
| 98 | + try (TikaInputStream tis = TikaInputStream.get(TIFF_BYTES)) { |
| 99 | + return MimeTypes.getDefaultMimeTypes().detect(tis, target, new ParseContext()); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * A carried Content-Type that specializes the content-detected type refines detection. |
| 105 | + * image/x-canon-cr2 is a sub-class-of image/tiff, and these bytes lack the CR2 marker. |
| 106 | + */ |
| 107 | + @Test |
| 108 | + public void testSpecializingContentTypeRefinesDetection() throws Exception { |
| 109 | + assertEquals(MediaType.image("x-canon-cr2"), |
| 110 | + detectWithCarriedContentType("image/x-canon-cr2")); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Security boundary: a carried Content-Type that does NOT specialize the content-detected |
| 115 | + * type is ignored, so a caller cannot force an unrelated type onto the document. |
| 116 | + */ |
| 117 | + @Test |
| 118 | + public void testNonSpecializingContentTypeIgnored() throws Exception { |
| 119 | + assertEquals(MediaType.image("tiff"), detectWithCarriedContentType("audio/mpeg")); |
| 120 | + assertEquals(MediaType.image("tiff"), detectWithCarriedContentType("not-a-media-type")); |
| 121 | + } |
| 122 | +} |
0 commit comments