View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.eclipse.aether.transfer;
20  
21  import org.junit.jupiter.api.Test;
22  
23  import static org.junit.jupiter.api.Assertions.assertEquals;
24  import static org.junit.jupiter.api.Assertions.assertFalse;
25  import static org.junit.jupiter.api.Assertions.assertTrue;
26  
27  public class ChecksumFailureExceptionTest {
28  
29      private static final String ESC = "\u001B";
30  
31      private static final String DEL = "\u007F";
32  
33      @Test
34      void mismatchMessageCarriesValues() {
35          ChecksumFailureException e = ChecksumFailureException.mismatch("cafebabe", "PROVIDED", "deadbeef");
36          assertTrue(e.getMessage().contains("cafebabe"));
37          assertTrue(e.getMessage().contains("PROVIDED"));
38          assertTrue(e.getMessage().contains("deadbeef"));
39          assertEquals("cafebabe", e.getExpected());
40          assertEquals("PROVIDED", e.getExpectedKind());
41          assertEquals("deadbeef", e.getActual());
42          assertTrue(e.isRetryWorthy());
43      }
44  
45      @Test
46      void mismatchMessageEscapesControlCharacters() {
47          // remote-supplied checksum values reach WARN logs via this message; terminal escapes must be neutralized
48          String hostileExpected = "abc" + ESC + "[2K\rdef";
49          String hostileActual = "actual" + DEL;
50          ChecksumFailureException e = ChecksumFailureException.mismatchDetail(
51                  "de" + ESC + "tail", hostileExpected, "PROVIDED", hostileActual);
52          String message = e.getMessage();
53          assertFalse(message.contains(ESC), "raw ESC must not survive into the message");
54          assertFalse(message.contains("\r"), "raw CR must not survive into the message");
55          assertFalse(message.contains(DEL), "raw DEL must not survive into the message");
56          assertTrue(message.contains("abc\\u001B[2K\\u000Ddef"));
57          assertTrue(message.contains("actual\\u007F"));
58          assertTrue(message.contains("de\\u001Btail"));
59          // the accessors keep returning the raw values for programmatic consumers
60          assertEquals(hostileExpected, e.getExpected());
61          assertEquals(hostileActual, e.getActual());
62          assertTrue(e.isRetryWorthy());
63      }
64  
65      @Test
66      @SuppressWarnings("deprecation")
67      void deprecatedMismatchConstructorEscapesControlCharacters() {
68          ChecksumFailureException e = new ChecksumFailureException("bad" + ESC + "[1A", "PROVIDED", "actual");
69          assertFalse(e.getMessage().contains(ESC));
70          assertTrue(e.getMessage().contains("bad\\u001B[1A"));
71          assertEquals("bad" + ESC + "[1A", e.getExpected());
72      }
73  }