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) 2012 Jeremy Long. All Rights Reserved.
17   */
18  package org.owasp.dependencycheck.data.nvdcve;
19  
20  import java.io.BufferedInputStream;
21  import java.io.BufferedOutputStream;
22  import java.io.File;
23  import java.io.FileInputStream;
24  import java.io.FileOutputStream;
25  import java.util.zip.ZipEntry;
26  import java.util.zip.ZipInputStream;
27  import org.junit.Before;
28  import org.owasp.dependencycheck.BaseTest;
29  import org.owasp.dependencycheck.utils.Settings;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  
33  /**
34   *
35   * @author Jeremy Long
36   */
37  public abstract class BaseDBTestCase extends BaseTest {
38  
39      protected final static int BUFFER_SIZE = 2048;
40  
41      private final static Logger LOGGER = LoggerFactory.getLogger(BaseDBTestCase.class);
42  
43      @Before
44      public void setUp() throws Exception {
45          ensureDBExists();
46      }
47  
48      public static void ensureDBExists() throws Exception {
49  
50          java.io.File dataPath = Settings.getDataDirectory();
51          String fileName = Settings.getString(Settings.KEYS.DB_FILE_NAME);
52          LOGGER.trace("DB file name {}", fileName);
53          java.io.File dataFile = new File(dataPath, fileName);
54          LOGGER.trace("Ensuring {} exists", dataFile.toString());
55          if (!dataPath.exists() || !dataFile.exists()) {
56              LOGGER.trace("Extracting database to {}", dataPath.toString());
57              dataPath.mkdirs();
58              FileInputStream fis = null;
59              ZipInputStream zin = null;
60              try {
61                  File path = new File(BaseDBTestCase.class.getClassLoader().getResource("data.zip").getPath());
62                  fis = new FileInputStream(path);
63                  zin = new ZipInputStream(new BufferedInputStream(fis));
64                  ZipEntry entry;
65                  while ((entry = zin.getNextEntry()) != null) {
66                      if (entry.isDirectory()) {
67                          final File d = new File(dataPath, entry.getName());
68                          d.mkdir();
69                          continue;
70                      }
71                      FileOutputStream fos = null;
72                      BufferedOutputStream dest = null;
73                      try {
74                          File o = new File(dataPath, entry.getName());
75                          o.createNewFile();
76                          fos = new FileOutputStream(o, false);
77                          dest = new BufferedOutputStream(fos, BUFFER_SIZE);
78                          byte data[] = new byte[BUFFER_SIZE];
79                          int count;
80                          while ((count = zin.read(data, 0, BUFFER_SIZE)) != -1) {
81                              dest.write(data, 0, count);
82                          }
83                      } catch (Throwable ex) {
84                          LOGGER.error("", ex);
85                      } finally {
86                          try {
87                              if (dest != null) {
88                                  dest.flush();
89                                  dest.close();
90                              }
91                          } catch (Throwable ex) {
92                              LOGGER.trace("", ex);
93                          }
94                          try {
95                              if (fos != null) {
96                                  fos.close();
97                              }
98                          } catch (Throwable ex) {
99                              LOGGER.trace("", ex);
100                         }
101                     }
102                 }
103             } finally {
104                 try {
105                     if (zin != null) {
106                         zin.close();
107                     }
108                 } catch (Throwable ex) {
109                     LOGGER.trace("", ex);
110                 }
111                 try {
112                     if (fis != null) {
113                         fis.close();
114                     }
115                 } catch (Throwable ex) {
116                     LOGGER.trace("", ex);
117                 }
118             }
119         }
120     }
121 }