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
22
23
24
25
26 public final class Tooling
27 {
28
29
30
31 private static final Logger LOGGER = LogManager.getLogger();
32
33
34
35
36 private static final int MAXBYTE = 255;
37
38
39
40
41
42
43
44
45
46
47 public boolean compareFiles(final File file1, final File file2)
48 {
49 boolean bResult = false;
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
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
79
80
81
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
107
108
109
110
111
112
113
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
139
140
141
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
149 layer.init("1");
150 layer.encStream(in, out);
151 }
152
153
154
155
156
157
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
165 layer.init("1");
166 layer.decStream(in, out);
167 }
168 }