View Javadoc
1   package org.owasp.dependencycheck.analyzer;
2   
3   import org.junit.After;
4   import org.junit.Before;
5   import org.junit.Test;
6   import org.owasp.dependencycheck.BaseTest;
7   import org.owasp.dependencycheck.analyzer.exception.AnalysisException;
8   import org.owasp.dependencycheck.dependency.Dependency;
9   
10  import static org.hamcrest.CoreMatchers.containsString;
11  import static org.hamcrest.CoreMatchers.is;
12  import static org.junit.Assert.assertThat;
13  
14  import java.io.File;
15  
16  /**
17   * Unit tests for CocoaPodsAnalyzer.
18   *
19   * @author Bianca Jiang
20   */
21  public class SwiftAnalyzersTest extends BaseTest {
22  
23      /**
24       * The analyzer to test.
25       */
26  	CocoaPodsAnalyzer podsAnalyzer;
27  	SwiftPackageManagerAnalyzer spmAnalyzer;
28  
29      /**
30       * Correctly setup the analyzer for testing.
31       *
32       * @throws Exception thrown if there is a problem
33       */
34      @Before
35      public void setUp() throws Exception {
36          podsAnalyzer = new CocoaPodsAnalyzer();
37          podsAnalyzer.setFilesMatched(true);
38          podsAnalyzer.initialize();
39          
40          spmAnalyzer = new SwiftPackageManagerAnalyzer();
41          spmAnalyzer.setFilesMatched(true);
42          spmAnalyzer.initialize();
43      }
44  
45      /**
46       * Cleanup the analyzer's temp files, etc.
47       *
48       * @throws Exception thrown if there is a problem
49       */
50      @After
51      public void tearDown() throws Exception {
52          podsAnalyzer.close();
53          podsAnalyzer = null;
54  
55          spmAnalyzer.close();
56          spmAnalyzer = null;
57      }
58  
59      /**
60       * Test of getName method, of class CocoaPodsAnalyzer.
61       */
62      @Test
63      public void testPodsGetName() {
64          assertThat(podsAnalyzer.getName(), is("CocoaPods Package Analyzer"));
65      }
66  
67      /**
68       * Test of getName method, of class SwiftPackageManagerAnalyzer.
69       */
70      @Test
71      public void testSPMGetName() {
72          assertThat(spmAnalyzer.getName(), is("SWIFT Package Manager Analyzer"));
73      }
74  
75      /**
76       * Test of supportsFiles method, of class CocoaPodsAnalyzer.
77       */
78      @Test
79      public void testPodsSupportsFiles() {
80          assertThat(podsAnalyzer.accept(new File("test.podspec")), is(true));
81      }
82  
83      /**
84       * Test of supportsFiles method, of class SwiftPackageManagerAnalyzer.
85       */
86      @Test
87      public void testSPMSupportsFiles() {
88          assertThat(spmAnalyzer.accept(new File("Package.swift")), is(true));
89      }
90      
91      /**
92       * Test of analyze method, of class CocoaPodsAnalyzer.
93       *
94       * @throws AnalysisException is thrown when an exception occurs.
95       */
96      @Test
97      public void testCocoaPodsAnalyzer() throws AnalysisException {
98          final Dependency result = new Dependency(BaseTest.getResourceAsFile(this,
99                  "swift/cocoapods/EasyPeasy.podspec"));
100         podsAnalyzer.analyze(result, null);
101         final String vendorString = result.getVendorEvidence().toString();
102         
103         assertThat(vendorString, containsString("Carlos Vidal"));
104         assertThat(vendorString, containsString("https://github.com/nakiostudio/EasyPeasy"));
105         assertThat(vendorString, containsString("MIT"));
106         assertThat(result.getProductEvidence().toString(), containsString("EasyPeasy"));
107         assertThat(result.getVersionEvidence().toString(), containsString("0.2.3"));
108     }
109 
110     /**
111      * Test of analyze method, of class SwiftPackageManagerAnalyzer.
112      *
113      * @throws AnalysisException is thrown when an exception occurs.
114      */
115     @Test
116     public void testSPMAnalyzer() throws AnalysisException {
117         final Dependency result = new Dependency(BaseTest.getResourceAsFile(this,
118                 "swift/Gloss/Package.swift"));
119         spmAnalyzer.analyze(result, null);
120 
121         assertThat(result.getProductEvidence().toString(), containsString("Gloss"));
122     }
123 }