Newer
Older
package tmltranslator;
import common.ConfigurationTTool;
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import myutil.FileUtils;
import myutil.TraceManager;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import test.AbstractTest;
import tmltranslator.patternhandling.SecurityGenerationForTMAP;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import org.json.*;
import static org.junit.Assert.*;
public class AutoSecurityGenerationDiplodocusTest extends AbstractTest {
private final static String DIR_GEN = "tmltranslator/AutoSecGenDiplodocusTest/GeneratedFiles/";
private final static String DIR_MODELS = "tmltranslator/AutoSecGenDiplodocusTest/Models/";
private final static String JSON_FILE_NAME = "features.json";
private final static String PATH_MODEL = "pathModel";
private final static String CHANNELS_LIST = "channels";
private final static String HSM_LIST = "HSM";
private final static String PATH_GOLDEN = "pathGolden";
private final static String IS_DIRECT_CONNECT_HSM = "DirectConnectHSMs";
private final static int ENCRYPTION_COMPUTATION = 100;
private final static int DECRYPTION_COMPUTATION = 100;
private final static int OVERHEAD = 100;
static String PROVERIF_DIR;
static String MODELS_DIR;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
String baseResourcesDir = getBaseResourcesDir();
PROVERIF_DIR = baseResourcesDir + DIR_GEN;
MODELS_DIR = baseResourcesDir + DIR_MODELS;
RESOURCES_DIR = baseResourcesDir + DIR_MODELS;
}
public AutoSecurityGenerationDiplodocusTest() {
super();
}
@Before
public void setUp() throws Exception {
TraceManager.devPolicy = TraceManager.TO_CONSOLE;
final Runnable runnable = new Runnable() {
@Override
public void run() {
new RshServer( null ).startServer();
}
};
Thread threadServer = new Thread( runnable );
threadServer.start();
ConfigurationTTool.ProVerifVerifierPath = "proverif";
ConfigurationTTool.ProVerifVerifierHost = "localhost";
TraceManager.addDev("Starting test for Security Generation");
Path path = Paths.get(PROVERIF_DIR);
if (!Files.exists(path)) {
Files.createDirectory(path);
}
}
@Test
public void testSecurityModels() throws Exception {
// Test if proverif is installed in the path
TraceManager.addDev("Testing if \"proverif\" is in the PATH");
assertTrue(canExecute("proverif"));
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
TraceManager.addDev("Starting function testSecurityModels");
File dir = new File(MODELS_DIR);
Collection<File> allFiles = FileUtils.listFiles(dir, null, true);
List<File> jsonFiles = new ArrayList<>();
for (Iterator iterator = allFiles.iterator(); iterator.hasNext();) {
File file = (File) iterator.next();
if (file.getName().equals(JSON_FILE_NAME)) {
jsonFiles.add(file);
}
}
for (File jsonFile : jsonFiles) {
String fileName = jsonFile.getAbsolutePath();
String tab = fileName.split("/")[fileName.split("/").length - 2];
TraceManager.addDev("\n\n********** Generating security for " + tab + " ********\n");
String jsonString = new String(Files.readAllBytes(Paths.get(fileName)));
JSONObject jo = new JSONObject(jsonString);
String pathModel = getBaseResourcesDir() + jo.getString(PATH_MODEL);
String pathDirJsonFile = fileName.substring(0, fileName.lastIndexOf("/") + 1);
TMLMappingTextSpecification tmts = new TMLMappingTextSpecification(tab);
File f = new File(pathModel);
TraceManager.addDev("Loading file: " + f.getAbsolutePath());
String spec = null;
try {
spec = FileUtils.loadFileData(f);
} catch (Exception e) {
TraceManager.addDev("Exception executing: loading " + tab);
fail();
}
String pathDirModel = pathModel.substring(0, pathModel.lastIndexOf("/") + 1);
TraceManager.addDev("Testing spec " + tab);
assertNotNull(spec);
TraceManager.addDev("Going to parse " + tab);
boolean parsed = tmts.makeTMLMapping(spec, pathDirModel);
assertTrue(parsed);
TraceManager.addDev("Checking syntax " + tab);
// Checking syntax
TMLMapping tmap = tmts.getTMLMapping();
TMLSyntaxChecking syntax = new TMLSyntaxChecking(tmap);
syntax.checkSyntax();
if (syntax.hasErrors() > 0) {
for (TMLError error: syntax.getErrors()) {
TraceManager.addDev("Error: " + error.toString());
}
}
assertEquals(0, syntax.hasErrors());
TMLModeling<?> tml = tmap.getTMLModeling();
String tabName = tml.getTasks().get(0).getName().substring(0, tml.getTasks().get(0).getName().indexOf("__"));
JSONArray selectedChannels = jo.getJSONArray(CHANNELS_LIST);
for (int i = 0; i < selectedChannels.length(); i++) {
for (String channelName : selectedChannels.getJSONObject(i).keySet()) {
JSONArray channelSecGoals = selectedChannels.getJSONObject(i).getJSONArray(channelName);
boolean confGoal = channelSecGoals.getBoolean(0);
boolean weakAuthGoal = channelSecGoals.getBoolean(1);
boolean strongAuthGoal = channelSecGoals.getBoolean(2);
TMLChannel ch = tmap.getChannelByName(tabName + "__" + channelName);
if (ch != null) {
ch.setSecurityGoalConf(confGoal);
ch.checkConf = confGoal;
ch.setSecurityGoalWeakAuth(weakAuthGoal);
if (strongAuthGoal) {
ch.setSecurityGoalWeakAuth(true);
ch.setSecurityGoalStrongAuth(true);
}
if (weakAuthGoal || strongAuthGoal) {
ch.checkAuth = true;
}
}
}
}
JSONArray selectedTasksHSMJA = jo.getJSONArray(HSM_LIST);
List<String> selectedTasksHSM = new ArrayList<>();
boolean isDirectConnectHSM = jo.getBoolean(IS_DIRECT_CONNECT_HSM);
for (int i = 0; i < selectedTasksHSMJA.length(); i++) {
selectedTasksHSM.add(selectedTasksHSMJA.getString(i));
}
ConfigurationTTool.ProVerifCodeDirectory = pathDirJsonFile;
SecurityGenerationForTMAP secgen = new SecurityGenerationForTMAP(tabName, tmap, Integer.toString(ENCRYPTION_COMPUTATION),
Integer.toString(OVERHEAD), Integer.toString(DECRYPTION_COMPUTATION), selectedTasksHSM, isDirectConnectHSM);
tmap = secgen.startThread();
if (tmap != null) {
tmap = secgen.autoMapKeys();
}
tmap.NullifyAutomata();
TraceManager.addDev("Checking syntax of generated model " + tab);
// Checking syntax
TMLSyntaxChecking syntaxGenerated = new TMLSyntaxChecking(tmap);
syntaxGenerated.checkSyntax();
if (syntaxGenerated.hasErrors() > 0) {
for (TMLError error: syntaxGenerated.getErrors()) {
TraceManager.addDev("Error: " + error.toString());
}
}
assertEquals(0, syntaxGenerated.hasErrors());
TMLMappingTextSpecification<Class<?>> specNew = new TMLMappingTextSpecification<Class<?>>(tab);
specNew.toTextFormat((TMLMapping<Class<?>>) tmap);
specNew.saveFile(pathDirJsonFile, "spec");
TraceManager.addDev("End creating model with Security generation for " + tab);
// Generate golden model spec
TraceManager.addDev("Generating golden model spec for " + tab);
String pathGolden = getBaseResourcesDir() + jo.getString(PATH_GOLDEN);
TMLMappingTextSpecification tmtsGolden = new TMLMappingTextSpecification(tab);
File fGolden = new File(pathGolden);
TraceManager.addDev("Loading file: " + fGolden.getAbsolutePath());
String specGolden = null;
try {
specGolden = FileUtils.loadFileData(fGolden);
} catch (Exception e) {
TraceManager.addDev("Exception executing: loading " + tab);
fail();
}
TraceManager.addDev("Testing spec " + tab);
assertNotNull(specGolden);
TraceManager.addDev("Going to parse golden Model" + tab);
String pathDirGolden = pathGolden.substring(0, pathGolden.lastIndexOf("/") + 1);
boolean parsedGoden = tmtsGolden.makeTMLMapping(specGolden, pathDirGolden);
assertTrue(parsedGoden);
TraceManager.addDev("Checking syntax of golden model " + tab);
// Checking syntax
TMLMapping tmapGolden = tmtsGolden.getTMLMapping();
TMLSyntaxChecking syntaxGolden = new TMLSyntaxChecking(tmapGolden);
syntaxGolden.checkSyntax();
if (syntaxGolden.hasErrors() > 0) {
for (TMLError error: syntaxGolden.getErrors()) {
TraceManager.addDev("Error: " + error.toString());
}
}
assertEquals(0, syntaxGolden.hasErrors());
TraceManager.addDev("Comparing golden model with generated model " + tab);
for (Object obj : tmap.getTMLModeling().getTasks()) {
TMLTask task = (TMLTask) obj;
String[] taskNameSplit = task.getName().split("__");
task.setName(taskNameSplit[taskNameSplit.length - 1]);
}
for (Object obj : tmap.getTMLModeling().getChannels()) {
TMLChannel ch = (TMLChannel) obj;
String[] channelNameSplit = ch.getName().split("__");
ch.setName(channelNameSplit[channelNameSplit.length - 1]);
}
for (Object obj : tmap.getTMLModeling().getEvents()) {
TMLEvent evt = (TMLEvent) obj;
String[] eventNameSplit = evt.getName().split("__");
evt.setName(eventNameSplit[eventNameSplit.length - 1]);
}
for (Object obj : tmap.getTMLModeling().getRequests()) {
TMLRequest req = (TMLRequest) obj;
String[] requestNameSplit = req.getName().split("__");
req.setName(requestNameSplit[requestNameSplit.length - 1]);
}
for (Object obj : tmapGolden.getTMLModeling().getTasks()) {
TMLTask task = (TMLTask) obj;
String[] taskNameSplit = task.getName().split("__");
task.setName(taskNameSplit[taskNameSplit.length - 1]);
}
for (Object obj : tmapGolden.getTMLModeling().getChannels()) {
TMLChannel ch = (TMLChannel) obj;
String[] channelNameSplit = ch.getName().split("__");
ch.setName(channelNameSplit[channelNameSplit.length - 1]);
}
for (Object obj : tmapGolden.getTMLModeling().getEvents()) {
TMLEvent evt = (TMLEvent) obj;
String[] eventNameSplit = evt.getName().split("__");
evt.setName(eventNameSplit[eventNameSplit.length - 1]);
}
for (Object obj : tmapGolden.getTMLModeling().getRequests()) {
TMLRequest req = (TMLRequest) obj;
String[] requestNameSplit = req.getName().split("__");
req.setName(requestNameSplit[requestNameSplit.length - 1]);
}
for (Object obj : tmapGolden.getTMLModeling().getChannels()) {
TMLChannel ch = (TMLChannel) obj;
if (tmap.getChannelByName(ch.getName()) != null) {
ch.setSecurityGoalConf(tmap.getChannelByName(ch.getName()).isSecurityGoalConf());
ch.setSecurityGoalWeakAuth(tmap.getChannelByName(ch.getName()).isSecurityGoalWeakAuth());
ch.setSecurityGoalStrongAuth(tmap.getChannelByName(ch.getName()).isSecurityGoalStrongAuth());
ch.checkConf = tmap.getChannelByName(ch.getName()).isCheckConfChannel();
ch.checkAuth = tmap.getChannelByName(ch.getName()).isCheckAuthChannel();
}
}
assertTrue(tmapGolden.equalSpec(tmap));
TraceManager.addDev("Verifying Tmap equality ended " + tab);
TraceManager.addDev("Test for " + tab + " tab is ended");
}
}
}