View Javadoc
1   package org.jastacry.test.utils;
2   
3   import java.io.DataOutputStream;
4   import java.io.File;
5   import java.io.FileNotFoundException;
6   import java.io.FileOutputStream;
7   import java.io.IOException;
8   import java.io.InputStream;
9   import java.io.OutputStream;
10  import java.security.Provider;
11  import java.security.Security;
12  import java.util.Enumeration;
13  
14  import org.apache.commons.io.FileUtils;
15  import org.apache.logging.log4j.LogManager;
16  import org.apache.logging.log4j.Logger;
17  import org.jastacry.JastacryException;
18  import org.jastacry.layer.Layer;
19  
20  /**
21   * Helper functions for automated tests.
22   * 
23   * @author kai
24   *
25   */
26  public final class Tooling
27  { // NOPMD by kai on 21.11.17 16:53
28      /**
29       * log4j2 object.
30       */
31      private static final Logger LOGGER = LogManager.getLogger();
32  
33      /**
34       * How big can a byte be.
35       */
36      private static final int MAXBYTE = 255;
37  
38      /**
39       * compare two files if they are equal.
40       *
41       * @param file1
42       *            file one
43       * @param file2
44       *            file two
45       * @return true if equal
46       */
47      public boolean compareFiles(final File file1, final File file2)
48      {
49          boolean bResult = false; // NOPMD by kai on 21.11.17 16:54
50          try
51          {
52              bResult = FileUtils.contentEquals(file1, file2);
53          }
54          catch (IOException e)
55          {
56              LOGGER.catching(e);
57          }
58          return bResult;
59      }
60  
61      /**
62       * List all installed crypto providers.
63       */
64      public void listProviders()
65      {
66          final Provider[] providers = Security.getProviders();
67          for (int i = 0; i < providers.length; i++)
68          {
69              LOGGER.info("Provider #{} {}", i, providers[i]);
70              for (final Enumeration<Object> e = providers[i].keys(); e.hasMoreElements();)
71              {
72                  LOGGER.info("\t{}", e.nextElement());
73              }
74          }
75      }
76  
77      /**
78       * Create a binary input file with all byte values possible.
79       *
80       * @param file
81       *            File to create
82       */
83      public void createBinaryTestfile(final File file)
84      {
85          try
86          {
87              try (DataOutputStream outputStream = new DataOutputStream(new FileOutputStream(file)))
88              {
89                  for (int i = 0; i <= MAXBYTE; i++)
90                  {
91                      outputStream.writeByte(i);
92                  }
93              }
94          }
95          catch (FileNotFoundException e)
96          {
97              LOGGER.catching(e);
98          }
99          catch (IOException e)
100         {
101             LOGGER.catching(e);
102         }
103     }
104 
105     /**
106      * Create a binary input file with only one byte values used.
107      *
108      * @param file
109      *            File to create
110      * @param length
111      *            how many bytes to write
112      * @param bValue
113      *            the byte to write
114      */
115     public void createBinaryTestfile(final File file, final long length, final byte bValue)
116     {
117         try
118         {
119             try (DataOutputStream outputStream = new DataOutputStream(new FileOutputStream(file)))
120             {
121                 for (int i = 0; i <= length; i++)
122                 {
123                     outputStream.writeByte(bValue);
124                 }
125             }
126         }
127         catch (FileNotFoundException e)
128         {
129             LOGGER.catching(e);
130         }
131         catch (IOException e)
132         {
133             LOGGER.catching(e);
134         }
135     }
136 
137     /**
138      * General mockup for IO Exceptions.
139      * @param layer The layer to be tested
140      * @throws JastacryException on error
141      * @throws IOException on error
142      */
143     public void mockupInputOutputEncStreams(Layer layer) throws  JastacryException, IOException
144     {
145         InputStream in = org.mockito.Mockito.mock(InputStream.class);
146         OutputStream out = org.mockito.Mockito.mock(OutputStream.class);
147         org.mockito.Mockito.when(in.read()).thenThrow(new IOException());
148         //org.mockito.Mockito.when(out.write(0)).thenThrow(new IOException());
149         layer.init("1");
150         layer.encStream(in, out);
151     }
152 
153     /**
154      * General mockup for IO Exceptions.
155      * @param layer The layer to be tested
156      * @throws JastacryException on error
157      * @throws IOException on error
158      */
159     public void mockupInputOutputDecStreams(Layer layer) throws  JastacryException, IOException
160     {
161         InputStream in = org.mockito.Mockito.mock(InputStream.class);
162         OutputStream out = org.mockito.Mockito.mock(OutputStream.class);
163         org.mockito.Mockito.when(in.read()).thenThrow(new IOException());
164         //org.mockito.Mockito.when(out.write(0)).thenThrow(new IOException());
165         layer.init("1");
166         layer.decStream(in, out);
167     }
168 }