/*
 * This file is part of dependency-check-core.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Copyright (c) 2012 Jeremy Long. All Rights Reserved.
 */
package org.owasp.dependencycheck.analyzer;

import org.owasp.dependencycheck.analyzer.exception.AnalysisException;
import java.io.File;
import org.junit.After;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeFalse;
import org.junit.Before;
import org.junit.Test;
import org.owasp.dependencycheck.dependency.Confidence;
import org.owasp.dependencycheck.dependency.Dependency;
import org.owasp.dependencycheck.dependency.Evidence;
import org.owasp.dependencycheck.utils.Settings;

/**
 * Tests for the AssemblyAnalyzer.
 *
 * @author colezlaw
 *
 */
public class AssemblyAnalyzerTest {

    AssemblyAnalyzer analyzer;

    /**
     * Sets up the analyzer.
     *
     * @throws Exception if anything goes sideways
     */
    @Before
    public void setUp() throws Exception {
        analyzer = new AssemblyAnalyzer();
        analyzer.initialize();
    }

    /**
     * Tests to make sure the name is correct.
     */
    @Test
    public void testGetName() {
        assertEquals("Assembly Analyzer", analyzer.getName());
    }

    @Test
    public void testAnalysis() throws Exception {
        File f = new File(AssemblyAnalyzerTest.class.getClassLoader().getResource("GrokAssembly.exe").getPath());
        Dependency d = new Dependency(f);
        analyzer.analyze(d, null);
        assertTrue(d.getVersionEvidence().getEvidence().contains(new Evidence("grokassembly", "version", "1.0.5140.29700", Confidence.HIGHEST)));
    }

    @Test
    public void testLog4Net() throws Exception {
        File f = new File(AssemblyAnalyzerTest.class.getClassLoader().getResource("log4net.dll").getPath());
        Dependency d = new Dependency(f);
        analyzer.analyze(d, null);
        assertTrue(d.getVersionEvidence().getEvidence().contains(new Evidence("grokassembly", "version", "1.2.13.0", Confidence.HIGHEST)));
        assertTrue(d.getVendorEvidence().getEvidence().contains(new Evidence("grokassembly", "vendor", "The Apache Software Foundation", Confidence.HIGH)));
        assertTrue(d.getProductEvidence().getEvidence().contains(new Evidence("grokassembly", "product", "log4net", Confidence.HIGH)));
    }

    @Test(expected = AnalysisException.class)
    public void testNonexistent() throws Exception {
        File f = new File(AssemblyAnalyzerTest.class.getClassLoader().getResource("log4net.dll").getPath());
        File test = new File(f.getParent(), "nonexistent.dll");
        Dependency d = new Dependency(test);
        analyzer.analyze(d, null);
    }

    @Test(expected = AnalysisException.class)
    public void testWithSettingMono() throws Exception {

        //This test doesn't work on Windows.
        assumeFalse(System.getProperty("os.name").startsWith("Windows"));

        String oldValue = Settings.getString(Settings.KEYS.ANALYZER_ASSEMBLY_MONO_PATH);
        // if oldValue is null, that means that neither the system property nor the setting has
        // been set. If that's the case, then we have to make it such that when we recover,
        // null still comes back. But you can't put a null value in a HashMap, so we have to set
        // the system property rather than the setting.
        if (oldValue == null) {
            System.setProperty(Settings.KEYS.ANALYZER_ASSEMBLY_MONO_PATH, "/yooser/bine/mono");
        } else {
            Settings.setString(Settings.KEYS.ANALYZER_ASSEMBLY_MONO_PATH, "/yooser/bine/mono");
        }

        try {
            // Have to make a NEW analyzer because during setUp, it would have gotten the correct one
            AssemblyAnalyzer aanalyzer = new AssemblyAnalyzer();
            aanalyzer.initialize();
        } finally {
            // Now recover the way we came in. If we had to set a System property, delete it. Otherwise,
            // reset the old value
            if (oldValue == null) {
                System.getProperties().remove(Settings.KEYS.ANALYZER_ASSEMBLY_MONO_PATH);
            } else {
                Settings.setString(Settings.KEYS.ANALYZER_ASSEMBLY_MONO_PATH, oldValue);
            }
        }
    }

    @After
    public void tearDown() throws Exception {
        analyzer.close();
    }
}

@@ -709,16 +709,44 @@
 lyzer;%0A%0A
+import org.mortbay.log.Log;%0A
 import o
@@ -808,16 +808,17 @@
 eption;%0A
+%0A
 import j
@@ -830,16 +830,17 @@
 o.File;%0A
+%0A
 import o
@@ -855,16 +855,17 @@
 .After;%0A
+%0A
 import s
@@ -987,16 +987,42 @@
 meFalse;
+%0A%0Aimport org.junit.Assume;
 %0Aimport 
@@ -1555,35 +1555,37 @@
 setUp() 
-throws Exception %7B%0A
+ %7B%0A        try %7B%0A    
         
@@ -1619,32 +1619,36 @@
 yzer();%0A        
+  
+  
 analyzer.initial
@@ -1650,24 +1650,243 @@
 itialize();%0A
+        %7D catch (Exception e) %7B%0A            Log.warn(%22Exception setting up AssemblyAnalyzer. Tests will be incomplete%22);%0A            Assume.assumeNoException(%22Is mono installed? TESTS WILL BE INCOMPLETE%22, e);%0A        %7D%0A
     %7D%0A%0A    /

fix for issue #72