1 /*******************************************************************************
2 * Copyright (c) 2017 Politecnico di Torino and others.
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Apache License, Version 2.0
6 * which accompanies this distribution, and is available at
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *******************************************************************************/
10 package it.polito.grpc.test;
13 import java.io.IOException;
14 import java.util.ArrayList;
15 import java.util.List;
18 import org.junit.After;
19 import org.junit.Before;
20 import org.junit.FixMethodOrder;
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23 import org.junit.runners.JUnit4;
24 import org.junit.runners.MethodSorters;
26 import static org.junit.Assert.assertEquals;
28 import java.io.BufferedReader;
29 import java.io.FilenameFilter;
30 import java.io.InputStreamReader;
32 import com.fasterxml.jackson.core.JsonParseException;
33 import com.fasterxml.jackson.databind.JsonMappingException;
34 import com.fasterxml.jackson.databind.JsonNode;
35 import com.fasterxml.jackson.databind.ObjectMapper;
36 import com.github.fge.jsonschema.core.exceptions.ProcessingException;
37 import com.github.fge.jsonschema.main.JsonSchema;
39 import io.grpc.verigraph.GraphGrpc;
40 import io.grpc.verigraph.NewGraph;
41 import io.grpc.verigraph.Policy;
42 import io.grpc.verigraph.VerificationGrpc;
43 import it.polito.escape.verify.client.VerifyClientException;
44 import it.polito.escape.verify.service.ValidationUtils;
45 import it.polito.escape.verify.test.TestCase;
46 import it.polito.escape.verify.test.TestExecutionException;
47 import it.polito.grpc.Client;
48 import it.polito.grpc.GrpcUtils;
49 import it.polito.grpc.Service;
51 @RunWith(JUnit4.class)
52 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
53 public class ReachabilityTest {
55 private List<File> testFiles = new ArrayList<File>();
56 private List<TestCase> testCases = new ArrayList<TestCase>();
57 private Client client;
58 private Service server;
61 public void setUpBeforeClass() throws Exception {
62 client = new Client("localhost" , 50051);
63 server = new Service(50051);
66 String folderName = System.getProperty("user.dir") + "/tester/testcases";
67 File folder = new File(folderName);
68 if (!folder.exists()) {
69 BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
72 System.out.println("Please enter the testcases folder path: ");
78 }while (s != null && s.length() != 0);
82 String schemaName = System.getProperty("user.dir") + "/tester/testcase_schema.json";
83 File schema = new File(schemaName);
84 if (!schema.exists()) {
85 BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
88 System.out.println("Please enter the full path of 'testcase_schema.json': ");
94 }while (s != null && s.length() != 0);
100 this.testFiles = getTests(folder);
101 this.testCases = getTestCases(this.testFiles);
105 public void tearDown() throws Exception {
111 public final void wrongReachability() {
112 System.out.println("DEBUG: starting testWrongReachability");
114 VerificationGrpc nullVer = VerificationGrpc.newBuilder()
115 .setErrorMessage("Graph with id 52 not found").build();
116 //verification on uncreated graph
117 Policy policyToVerify = Client.createPolicy("Node1", "Node4", "reachability", null, 52);
118 VerificationGrpc ver = client.verify(policyToVerify);
119 assertEquals(ver, nullVer);
121 //verification on uncreated nodes
122 nullVer = VerificationGrpc.newBuilder()
123 .setErrorMessage("The \'source\' parameter \'Node5\' is not valid, please insert the name of an existing node").build();
124 policyToVerify = Client.createPolicy("Node5", "Node4", "reachability", null, 1);
125 ver = client.verify(policyToVerify);
126 assertEquals(ver, nullVer);
128 //verification on uncreated nodes
129 nullVer = VerificationGrpc.newBuilder()
130 .setErrorMessage("The \'source\' parameter \'Node1\' is not valid, please insert the name of an existing node").build();
132 policyToVerify = Client.createPolicy("Node1", "Node10", "reachability", null, 1);
133 ver = client.verify(policyToVerify);
134 assertEquals(ver, nullVer);
138 public List<File> getTests(File folder) {
139 List<File> filesList = new ArrayList<File>();
141 System.out.println("Test folder set to '" + folder.getAbsolutePath() + "'");
143 File[] files = folder.listFiles(new FilenameFilter() {
146 public boolean accept(File dir, String name) {
147 return name.endsWith(".json");
151 for (File f : files) {
153 System.out.println("File '" + f.getName() + "' added to test files");
159 public List<TestCase> getTestCases(List<File> files) throws JsonParseException, JsonMappingException, IOException,
161 List<TestCase> testCases = new ArrayList<TestCase>();
163 for (File file : files) {
164 validateTestFile(file);
166 TestCase tc = new ObjectMapper().readValue(file, TestCase.class);
169 catch (Exception e) {
178 public void runTestCases() throws VerifyClientException, TestExecutionException {
180 for (TestCase tc : this.testCases) {
181 String result = runTestCase(tc);
182 if (!result.equals(tc.getResult()))
183 throw new TestExecutionException("Error running test given in file '" + this.testFiles.get(counter).getName()
184 + "'. Test returned '" + result + "' instead of '" + tc.getResult() + "'.");
186 System.out.println("Test given in file '" + this.testFiles.get(counter).getName() + "' returned '"
187 + result + "' as expected");
191 System.out.println("All tests PASSED");
194 private String runTestCase(TestCase tc) throws VerifyClientException, TestExecutionException{
195 GraphGrpc graph = GrpcUtils.obtainGraph(tc.getGraph());
197 NewGraph newGraph = this.client.createGraph(graph);
198 if(newGraph.getSuccess() == false)
199 throw new VerifyClientException("gRPC request failed");
200 GraphGrpc createdGraph = newGraph.getGraph();
202 GraphGrpc addedgraph = client.getGraph(createdGraph.getId());
203 System.out.println(addedgraph);
205 final Map<String, String> map = GrpcUtils.getParamGivenString(tc.getPolicyUrlParameters());
207 Policy policy = Client.createPolicy(map.get("source"),
208 map.get("destination"),
210 map.get("middlebox"),
211 createdGraph.getId());
212 VerificationGrpc verification = this.client.verify(policy);
213 return verification.getResult();
216 public void validateTestFile(File testFile) throws Exception {
217 JsonSchema schemaNode = null;
219 schemaNode = ValidationUtils.getSchemaNode(schema);
221 catch (IOException e) {
222 throw new Exception("Unable to load '" + schema.getAbsolutePath() + "' schema file");
224 catch (ProcessingException e) {
225 throw new Exception("Unable to resolve '" + schema.getAbsolutePath() + "' schema file as a schema node");
230 jsonNode = ValidationUtils.getJsonNode(testFile);
232 catch (IOException e) {
233 throw new Exception("Unable to load '" + testFile.getAbsolutePath() + "' as a json node");
237 ValidationUtils.validateJson(schemaNode, jsonNode);
239 catch (ProcessingException e) {
240 throw new Exception("There were errors in the validation of file '" + testFile.getAbsolutePath()
241 + "' against the json schema '" + schema.getAbsolutePath() + "': " + e.getMessage());
246 private static boolean isValidpath(String s) {
249 File file = new File(s);
250 return file.exists();