|
| 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.ml.chardetect; |
| 18 | + |
| 19 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 20 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 21 | + |
| 22 | +import java.io.ByteArrayOutputStream; |
| 23 | +import java.io.IOException; |
| 24 | +import java.nio.charset.Charset; |
| 25 | +import java.nio.charset.StandardCharsets; |
| 26 | +import java.util.List; |
| 27 | + |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | + |
| 30 | +import org.apache.tika.detect.EncodingResult; |
| 31 | + |
| 32 | +/** |
| 33 | + * TIKA-4810: commit 360b3d354 (2026-06-10) dropped the {@code || utf8Tolerated} |
| 34 | + * branch that promoted a tolerated (near-clean) probe to STRUCTURAL UTF-8, |
| 35 | + * assuming NB's statistical layer always covers the fallback. It doesn't (a |
| 36 | + * real Bengali news page's NB pool came back empty) — but restoring the branch |
| 37 | + * unconditionally would re-open a false positive on short zip entry names |
| 38 | + * (9-30 bytes, routed through this detector by {@code ZipParser}), which is |
| 39 | + * why it was narrowed in the first place. |
| 40 | + */ |
| 41 | +public class ToleratedUtf8StructuralRegressionTest { |
| 42 | + |
| 43 | + private static final String BENGALI_SENTENCE = |
| 44 | + "সেমিতে ক্রোয়েশিয়া টাইব্রেকারে রাশিয়াকে হারিয়ে ফাইনালে উঠেছে। "; |
| 45 | + |
| 46 | + private static MojibusterEncodingDetector newDetector() { |
| 47 | + try { |
| 48 | + return new MojibusterEncodingDetector(); |
| 49 | + } catch (Exception e) { |
| 50 | + throw new RuntimeException(e); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void longDocumentWithOneStrayByteIsStillUtf8() throws IOException { |
| 56 | + byte[] probe = buildProbe(30); |
| 57 | + List<EncodingResult> results = newDetector().detect(probe); |
| 58 | + boolean hasStructuralUtf8 = results.stream().anyMatch(r -> |
| 59 | + "UTF-8".equals(r.getCharset().name()) |
| 60 | + && r.getResultType() == EncodingResult.ResultType.STRUCTURAL); |
| 61 | + assertTrue(hasStructuralUtf8, |
| 62 | + "A long, overwhelmingly UTF-8 document with a single tolerated " |
| 63 | + + "error byte must still yield a STRUCTURAL UTF-8 candidate; " |
| 64 | + + "results were: " + results); |
| 65 | + } |
| 66 | + |
| 67 | + /** Zip-entry-name-shaped probe: must not be promoted on tolerance alone. */ |
| 68 | + @Test |
| 69 | + public void shortProbeWithOneStrayByteIsNotPromoted() throws IOException { |
| 70 | + ByteArrayOutputStream bo = new ByteArrayOutputStream(); |
| 71 | + bo.write(0xA9); // raw © byte: invalid as a UTF-8 lead |
| 72 | + bo.writeBytes("café-Köln.txt".getBytes(StandardCharsets.UTF_8)); |
| 73 | + byte[] probe = bo.toByteArray(); |
| 74 | + |
| 75 | + List<EncodingResult> results = newDetector().detect(probe); |
| 76 | + boolean hasStructuralUtf8 = results.stream().anyMatch(r -> |
| 77 | + "UTF-8".equals(r.getCharset().name()) |
| 78 | + && r.getResultType() == EncodingResult.ResultType.STRUCTURAL); |
| 79 | + assertFalse(hasStructuralUtf8, |
| 80 | + "A short probe shaped like a zip entry name must not be promoted " |
| 81 | + + "to STRUCTURAL UTF-8 on a single tolerated error alone; " |
| 82 | + + "results were: " + results); |
| 83 | + } |
| 84 | + |
| 85 | + /** Real GBK filename from attachment_name_diffs.xlsx; must stay GB18030. */ |
| 86 | + @Test |
| 87 | + public void chineseGbkFilenameIsNotPromotedToUtf8() { |
| 88 | + byte[] probe = "说明.txt".getBytes(Charset.forName("GBK")); |
| 89 | + List<EncodingResult> results = newDetector().detect(probe); |
| 90 | + boolean hasStructuralUtf8 = results.stream().anyMatch(r -> |
| 91 | + "UTF-8".equals(r.getCharset().name()) |
| 92 | + && r.getResultType() == EncodingResult.ResultType.STRUCTURAL); |
| 93 | + assertFalse(hasStructuralUtf8, |
| 94 | + "A short GBK filename must not be promoted to STRUCTURAL UTF-8 " |
| 95 | + + "on a single tolerated error alone; results were: " + results); |
| 96 | + assertTrue(results.stream().anyMatch(r -> r.getCharset().name().startsWith("GB")), |
| 97 | + "Expected a GB18030/GBK candidate; results were: " + results); |
| 98 | + } |
| 99 | + |
| 100 | + /** Real windows-1252 filename from attachment_name_diffs.xlsx. */ |
| 101 | + @Test |
| 102 | + public void sauteFilenameIsNotPromotedToUtf8() { |
| 103 | + byte[] probe = "Sauté.txt".getBytes(Charset.forName("windows-1252")); |
| 104 | + List<EncodingResult> results = newDetector().detect(probe); |
| 105 | + boolean hasStructuralUtf8 = results.stream().anyMatch(r -> |
| 106 | + "UTF-8".equals(r.getCharset().name()) |
| 107 | + && r.getResultType() == EncodingResult.ResultType.STRUCTURAL); |
| 108 | + assertFalse(hasStructuralUtf8, |
| 109 | + "A short windows-1252 filename must not be promoted to STRUCTURAL " |
| 110 | + + "UTF-8 on a single tolerated error alone; results were: " + results); |
| 111 | + } |
| 112 | + |
| 113 | + /** Declared-windows-1252 HTML page, genuinely UTF-8, one stray raw © byte. */ |
| 114 | + private static byte[] buildProbe(int repeatCount) throws IOException { |
| 115 | + StringBuilder body = new StringBuilder(); |
| 116 | + for (int i = 0; i < repeatCount; i++) { |
| 117 | + body.append(BENGALI_SENTENCE); |
| 118 | + } |
| 119 | + ByteArrayOutputStream bo = new ByteArrayOutputStream(); |
| 120 | + bo.writeBytes(("<html><head><meta http-equiv=\"Content-Type\" " |
| 121 | + + "content=\"text/html; charset=windows-1252\">") |
| 122 | + .getBytes(StandardCharsets.US_ASCII)); |
| 123 | + bo.writeBytes("<meta name=\"copyright\" content=\"".getBytes(StandardCharsets.US_ASCII)); |
| 124 | + bo.write(0xA9); // raw © byte: invalid as a UTF-8 lead |
| 125 | + bo.writeBytes(" 2013\"></head><body><title>".getBytes(StandardCharsets.US_ASCII)); |
| 126 | + bo.writeBytes(body.toString().getBytes(StandardCharsets.UTF_8)); |
| 127 | + bo.writeBytes("</title></body></html>".getBytes(StandardCharsets.US_ASCII)); |
| 128 | + return bo.toByteArray(); |
| 129 | + } |
| 130 | +} |
0 commit comments