View Javadoc
1   /*
2    * This file is part of dependency-check-core.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   * Copyright (c) 2015 The OWASP Foundatio. All Rights Reserved.
17   */
18  package org.owasp.dependencycheck.data.composer;
19  
20  import org.junit.Before;
21  import org.junit.Test;
22  
23  import java.io.ByteArrayInputStream;
24  import java.io.InputStream;
25  import java.nio.charset.Charset;
26  
27  import static org.junit.Assert.*;
28  import org.owasp.dependencycheck.BaseTest;
29  
30  /**
31   * Created by colezlaw on 9/5/15.
32   */
33  public class ComposerLockParserTest extends BaseTest  {
34  
35      private InputStream inputStream;
36  
37      @Before
38      public void setUp() {
39          inputStream = this.getClass().getClassLoader().getResourceAsStream("composer.lock");
40      }
41  
42      @Test
43      public void testValidComposerLock() {
44          ComposerLockParser clp = new ComposerLockParser(inputStream);
45          clp.process();
46          assertEquals(30, clp.getDependencies().size());
47          assertTrue(clp.getDependencies().contains(new ComposerDependency("symfony", "translation", "2.7.3")));
48      }
49  
50      @Test(expected = ComposerException.class)
51      public void testNotJSON() throws Exception {
52          String input = "NOT VALID JSON";
53          ComposerLockParser clp = new ComposerLockParser(new ByteArrayInputStream(input.getBytes(Charset.defaultCharset())));
54          clp.process();
55      }
56  
57      @Test(expected = ComposerException.class)
58      public void testNotComposer() throws Exception {
59          String input = "[\"ham\",\"eggs\"]";
60          ComposerLockParser clp = new ComposerLockParser(new ByteArrayInputStream(input.getBytes(Charset.defaultCharset())));
61          clp.process();
62      }
63  
64      @Test(expected = ComposerException.class)
65      public void testNotPackagesArray() throws Exception {
66          String input = "{\"packages\":\"eleventy\"}";
67          ComposerLockParser clp = new ComposerLockParser(new ByteArrayInputStream(input.getBytes(Charset.defaultCharset())));
68          clp.process();
69      }
70  }