1   /* Copyright 2002-2025 CS GROUP
2    * Licensed to CS GROUP (CS) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * CS 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.orekit.files.ccsds.utils.lexical;
18  
19  import java.net.MalformedURLException;
20  
21  import org.hamcrest.CoreMatchers;
22  import org.hamcrest.MatcherAssert;
23  import org.junit.jupiter.api.Assertions;
24  import org.junit.jupiter.api.Test;
25  import org.orekit.data.DataSource;
26  import org.orekit.errors.OrekitException;
27  import org.orekit.errors.OrekitMessages;
28  import org.orekit.files.ccsds.ndm.ParserBuilder;
29  import org.orekit.files.ccsds.ndm.odm.ocm.OcmParser;
30  
31  public class XmlLexicalAnalyzerTest {
32  
33      @Test
34      public void testNullBinary() {
35          XmlLexicalAnalyzer la = new XmlLexicalAnalyzer(new DataSource("empty", (DataSource.StreamOpener) () -> null));
36          try {
37              la.accept(new ParserBuilder().buildOcmParser());
38              Assertions.fail("an exception should have been thrown");
39          } catch (OrekitException oe) {
40              Assertions.assertEquals(OrekitMessages.UNABLE_TO_FIND_FILE, oe.getSpecifier());
41              Assertions.assertEquals("empty", oe.getParts()[0]);
42          }
43      }
44  
45      @Test
46      public void testNullCharacter() {
47          XmlLexicalAnalyzer la = new XmlLexicalAnalyzer(new DataSource("empty", (DataSource.ReaderOpener) () -> null));
48          try {
49              la.accept(new ParserBuilder().buildOcmParser());
50              Assertions.fail("an exception should have been thrown");
51          } catch (OrekitException oe) {
52              Assertions.assertEquals(OrekitMessages.UNABLE_TO_FIND_FILE, oe.getSpecifier());
53              Assertions.assertEquals("empty", oe.getParts()[0]);
54          }
55      }
56  
57      /**
58       * Check the XML parser is configured to ignore XML entities to avoid
59       * security risks.
60       */
61      @Test
62      public void testExternalResourcesAreIgnored() {
63          // setup
64          XmlLexicalAnalyzer la = new XmlLexicalAnalyzer(new DataSource(
65                  "entity",
66                  () -> this.getClass().getResourceAsStream("/ccsds/ndm/NDM-opm-entity.xml")));
67          OcmParser parser = new ParserBuilder().buildOcmParser();
68  
69          // action
70          try {
71              la.accept(parser);
72              // verify
73              Assertions.fail("Expected Exception");
74          } catch (OrekitException e) {
75              // Malformed URL exception indicates external resource was disabled
76              // file not found exception indicates parser tried to load the resource
77              MatcherAssert.assertThat(e.getCause(),
78                      CoreMatchers.instanceOf(MalformedURLException.class));
79          }
80      }
81  
82  }