View Javadoc
1   /*
2    * Copyright 2014 OWASP.
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  package org.owasp.dependencycheck.data.update;
17  
18  import java.text.DateFormat;
19  import java.text.SimpleDateFormat;
20  import java.util.Properties;
21  import mockit.Mock;
22  import mockit.MockUp;
23  import org.joda.time.DateTime;
24  import org.joda.time.format.DateTimeFormat;
25  import org.joda.time.format.DateTimeFormatter;
26  import static org.junit.Assert.assertEquals;
27  import static org.junit.Assert.assertTrue;
28  import org.junit.Test;
29  import org.owasp.dependencycheck.BaseTest;
30  import org.owasp.dependencycheck.data.nvdcve.DatabaseProperties;
31  import org.owasp.dependencycheck.data.update.exception.UpdateException;
32  import org.owasp.dependencycheck.utils.DependencyVersion;
33  
34  /**
35   *
36   * @author Jeremy Long
37   */
38  public class EngineVersionCheckTest extends BaseTest {
39  
40  //    /**
41  //     * Test of update method, of class EngineVersionCheck.
42  //     */
43  //    @Test
44  //    public void testUpdate() throws Exception {
45  //        EngineVersionCheck instance = new EngineVersionCheck();
46  //        instance.update();
47  //    }
48      /**
49       * Converts a date in the form of yyyy-MM-dd into the epoch milliseconds.
50       *
51       * @param date a date in the format of yyyy-MM-dd
52       * @return milliseconds
53       */
54      private long dateToMilliseconds(String date) {
55          //removed for compatability with joda-time 1.6
56          //DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyy-MM-dd");
57          //return DateTime.parse(date, dtf).toInstant().getMillis();
58          String[] dp = date.split("-");
59          int y = Integer.parseInt(dp[0]);
60          int m = Integer.parseInt(dp[1]);
61          int d = Integer.parseInt(dp[2]);
62          DateTime dt = new DateTime(y, m, d, 0, 0, 0, 0);
63          return dt.toInstant().getMillis();
64      }
65  
66      /**
67       * Test of shouldUpdate method, of class EngineVersionCheck.
68       */
69      @Test
70      public void testShouldUpdate() throws Exception {
71          DatabaseProperties properties = new MockUp<DatabaseProperties>() {
72              final private Properties properties = new Properties();
73  
74              @Mock
75              public void save(String key, String value) throws UpdateException {
76                  properties.setProperty(key, value);
77              }
78  
79              @Mock
80              public String getProperty(String key) {
81                  return properties.getProperty(key);
82              }
83  
84          }.getMockInstance();
85  
86          String updateToVersion = "1.2.6";
87          String currentVersion = "1.2.6";
88  
89          long lastChecked = dateToMilliseconds("2014-12-01");
90          long now = dateToMilliseconds("2014-12-01");
91  
92          EngineVersionCheck instance = new EngineVersionCheck();
93          boolean expResult = false;
94          instance.setUpdateToVersion(updateToVersion);
95          boolean result = instance.shouldUpdate(lastChecked, now, properties, currentVersion);
96          assertEquals(expResult, result);
97  
98          updateToVersion = "1.2.5";
99          currentVersion = "1.2.5";
100         lastChecked = dateToMilliseconds("2014-10-01");
101         now = dateToMilliseconds("2014-12-01");
102         expResult = true;
103         instance.setUpdateToVersion(updateToVersion);
104         result = instance.shouldUpdate(lastChecked, now, properties, currentVersion);
105         assertEquals(expResult, result);
106         //System.out.println(properties.getProperty(CURRENT_ENGINE_RELEASE));
107 
108         updateToVersion = "1.2.5";
109         currentVersion = "1.2.5";
110         lastChecked = dateToMilliseconds("2014-12-01");
111         now = dateToMilliseconds("2014-12-03");
112         expResult = false;
113         instance.setUpdateToVersion(updateToVersion);
114         result = instance.shouldUpdate(lastChecked, now, properties, currentVersion);
115         assertEquals(expResult, result);
116 
117         updateToVersion = "1.2.6";
118         currentVersion = "1.2.5";
119         lastChecked = dateToMilliseconds("2014-12-01");
120         now = dateToMilliseconds("2014-12-03");
121         expResult = true;
122         instance.setUpdateToVersion(updateToVersion);
123         result = instance.shouldUpdate(lastChecked, now, properties, currentVersion);
124         assertEquals(expResult, result);
125 
126         updateToVersion = "1.2.5";
127         currentVersion = "1.2.6";
128         lastChecked = dateToMilliseconds("2014-12-01");
129         now = dateToMilliseconds("2014-12-08");
130         expResult = false;
131         instance.setUpdateToVersion(updateToVersion);
132         result = instance.shouldUpdate(lastChecked, now, properties, currentVersion);
133         assertEquals(expResult, result);
134 
135         updateToVersion = "";
136         currentVersion = "1.2.5";
137         lastChecked = dateToMilliseconds("2014-12-01");
138         now = dateToMilliseconds("2014-12-03");
139         expResult = false;
140         instance.setUpdateToVersion(updateToVersion);
141         result = instance.shouldUpdate(lastChecked, now, properties, currentVersion);
142         assertEquals(expResult, result);
143 
144         updateToVersion = "";
145         currentVersion = "1.2.5";
146         lastChecked = dateToMilliseconds("2014-12-01");
147         now = dateToMilliseconds("2015-12-08");
148         expResult = true;
149         instance.setUpdateToVersion(updateToVersion);
150         result = instance.shouldUpdate(lastChecked, now, properties, currentVersion);
151         assertEquals(expResult, result);
152     }
153 
154     /**
155      * Test of getCurrentReleaseVersion method, of class EngineVersionCheck.
156      */
157     @Test
158     public void testGetCurrentReleaseVersion() {
159         EngineVersionCheck instance = new EngineVersionCheck();
160         DependencyVersion minExpResult = new DependencyVersion("1.2.6");
161         String release = instance.getCurrentReleaseVersion();
162         DependencyVersion result = new DependencyVersion(release);
163         assertTrue(minExpResult.compareTo(result) <= 0);
164     }
165 }