Ejemplo n.º 1
0
        public void DataFlowGraphTest()
        {
            var content          = System.IO.File.ReadAllText(@"..\..\..\..\AngularApp\Files\demo1.txt");
            var controlFlowgraph = P4ToGraph.ControlFlowGraph(ref content);
            var graph            = P4ToGraph.DataFlowGraph(content, controlFlowgraph);

            Assert.Equal("Start", graph[0].Text);

            Assert.Equal("hdr.ipv4.dstAddr", graph[1].Text);
            Assert.Equal(NodeType.Key, graph[1].Type);

            Assert.Equal("meta.fwd_metadata.l2ptr", graph[2].Text);
            Assert.Equal(NodeType.ActionMethod, graph[2].Type);

            Assert.Equal("l2ptr", graph[3].Text);
            Assert.Equal(NodeType.ActionMethod, graph[3].Type);

            Assert.Equal("mark_to_drop(stdmeta)", graph[4].Text);
            Assert.Equal(NodeType.ActionMethod, graph[4].Type);

            Assert.Equal("meta.fwd_metadata.l2ptr", graph[5].Text);
            Assert.Equal(NodeType.Key, graph[5].Type);

            Assert.Equal("meta.fwd_metadata.out_bd", graph[6].Text);
            Assert.Equal(NodeType.ActionMethod, graph[6].Type);

            Assert.Equal("bd", graph[7].Text);
            Assert.Equal(NodeType.ActionMethod, graph[7].Type);

            Assert.Equal("hdr.ethernet.dstAddr", graph[8].Text);
            Assert.Equal(NodeType.ActionMethod, graph[8].Type);

            Assert.Equal("dmac", graph[9].Text);
            Assert.Equal(NodeType.ActionMethod, graph[9].Type);

            Assert.Equal("stdmeta.egress_spec", graph[10].Text);
            Assert.Equal(NodeType.ActionMethod, graph[10].Type);

            Assert.Equal("intf", graph[11].Text);
            Assert.Equal(NodeType.ActionMethod, graph[11].Type);

            Assert.Equal("hdr.ipv4.ttl", graph[12].Text);
            Assert.Equal(NodeType.ActionMethod, graph[12].Type);

            Assert.Equal("hdr.ipv4.ttl - 1", graph[13].Text);
            Assert.Equal(NodeType.ActionMethod, graph[13].Type);

            Assert.Equal("mark_to_drop(stdmeta)", graph[14].Text);
            Assert.Equal(NodeType.ActionMethod, graph[14].Type);

            Assert.Equal("End", graph[15].Text);
        }
Ejemplo n.º 2
0
        public void GetControlFlowGraphTest(int no)
        {
            var graphController  = new GraphController(mockHttpContextAccessor.Object);
            var file             = System.IO.File.ReadAllText($@"..\..\..\..\AngularApp\Files\demo{no}.txt");
            var controlFlowGraph = P4ToGraph.ControlFlowGraph(ref file);

            var value = Encoding.ASCII.GetBytes(controlFlowGraph.ToJson());

            mockSession.Setup(_ => _.TryGetValue(Key.ControlFlowGraph.ToString("g"), out value)).Returns(true);
            var result = graphController.GetGraph(Key.ControlFlowGraph.ToString("g"));

            var objectResult = Assert.IsType <OkObjectResult>(result);
            var model        = Assert.IsAssignableFrom <IEnumerable <ViewNode> >(objectResult.Value).ToList();
            var viewNodes    = controlFlowGraph.Serialize().ToList();

            CompareViewNodeLists(viewNodes, model);
        }
Ejemplo n.º 3
0
        public void ControlFlowGraphTest()
        {
            var content = System.IO.File.ReadAllText(@"..\..\..\..\AngularApp\Files\demo1.txt");
            var graph   = P4ToGraph.ControlFlowGraph(ref content);

            Assert.Equal("Start", graph[0].Text);

            Assert.Equal("ipv4_da_lpm", graph[1].Text);
            Assert.Equal(NodeType.Table, graph[1].Type);

            Assert.Equal("set_l2ptr", graph[2].Text);
            Assert.Equal(NodeType.Action, graph[2].Type);

            Assert.Equal("my_drop", graph[3].Text);
            Assert.Equal(NodeType.Action, graph[3].Type);

            Assert.Equal("mac_da", graph[4].Text);
            Assert.Equal(NodeType.Table, graph[4].Type);

            Assert.Equal("set_bd_dmac_intf", graph[5].Text);
            Assert.Equal(NodeType.Action, graph[5].Type);

            Assert.Equal("my_drop", graph[6].Text);
            Assert.Equal(NodeType.Action, graph[6].Type);

            Assert.Equal("End", graph[7].Text);

            Assert.Equal(graph[0].Edges[0].Child, graph[1]);

            Assert.Equal(graph[1].Edges[0].Child, graph[2]);

            Assert.Equal(graph[1].Edges[1].Child, graph[3]);

            Assert.Equal(graph[2].Edges[0].Child, graph[4]);

            Assert.Equal(graph[3].Edges[0].Child, graph[4]);

            Assert.Equal(graph[4].Edges[0].Child, graph[5]);

            Assert.Equal(graph[4].Edges[1].Child, graph[6]);

            Assert.Equal(graph[5].Edges[0].Child, graph[7]);

            Assert.Equal(graph[6].Edges[0].Child, graph[7]);
        }
Ejemplo n.º 4
0
        public void AnalyzeTest()
        {
            var content = System.IO.File.ReadAllText(@"..\..\..\..\AngularApp\Files\demo1.txt");

            var structs = Analyzer.GetStructs(content);
            var graph   = P4ToGraph.ControlFlowGraph(ref content);
            var graph1  = P4ToGraph.DataFlowGraph(content, graph);

            structs.ForEach(_struct =>
            {
                foreach (var header in _struct.Headers.Values)
                {
                    header.Valid = true;
                    header.Variables.ForEach(x => x.IsInitialize = true);
                }
            });

            var analyzeData = new AnalyzeData
            {
                EndState   = structs,
                Id         = 1,
                StartState = JsonSerializer.Deserialize <List <Struct> >(JsonSerializer.Serialize(structs))
            };

            var analyzer = new Analyzer(graph.ToJson(), graph1.ToJson(), analyzeData, content);

            analyzer.Analyze();
            analyzer.FinishOperations();

            var controlFlowGraph = analyzer.ControlFlowGraph;

            Assert.Equal(0, controlFlowGraph[0].Use);
            Assert.Equal(1, controlFlowGraph[1].Use);
            Assert.Equal(1, controlFlowGraph[2].Use);
            Assert.Equal(1, controlFlowGraph[3].Use);
            Assert.Equal(2, controlFlowGraph[4].Use);
            Assert.Equal(2, controlFlowGraph[5].Use);
            Assert.Equal(2, controlFlowGraph[6].Use);
            Assert.Equal(4, controlFlowGraph[7].Use);
        }
Ejemplo n.º 5
0
        public IActionResult FileUpload([FromBody] FileData file)
        {
            return(ActionExecute(() =>
            {
                if (file.Content == null || string.IsNullOrWhiteSpace(file.Content))
                {
                    return BadRequest("Üres fájl!");
                }

                var content = file.Content;

                SessionExtension.Set(session, Key.File, file);

                var controlFlowGraph = P4ToGraph.ControlFlowGraph(ref content);
                SessionExtension.SetGraph(session, Key.ControlFlowGraph, controlFlowGraph);

                var dataFlowGraph = P4ToGraph.DataFlowGraph(content, controlFlowGraph);
                SessionExtension.SetGraph(session, Key.DataFlowGraph, dataFlowGraph);

                return Ok(file);
            }));
        }
Ejemplo n.º 6
0
        public void UpdateTest(int no)
        {
            var analyzerController = new AnalyzerController(mockHttpContextAccessor.Object);

            var file = new FileData {
                Name = "Valami", Content = System.IO.File.ReadAllText($@"..\..\..\..\AngularApp\Files\demo{no}.txt")
            };
            var value = Encoding.ASCII.GetBytes(JsonSerializer.Serialize(file));

            mockSession.Setup(_ => _.TryGetValue(Key.File.ToString("g"), out value)).Returns(true);

            var content = file.Content;

            var controlFlowGraph = P4ToGraph.ControlFlowGraph(ref content);
            var controlValue     = Encoding.ASCII.GetBytes(controlFlowGraph.ToJson());

            mockSession.Setup(_ => _.TryGetValue(Key.ControlFlowGraph.ToString("g"), out controlValue)).Returns(true);

            var dataFlowGraph = P4ToGraph.DataFlowGraph(content, controlFlowGraph);
            var dataValue     = Encoding.ASCII.GetBytes(dataFlowGraph.ToJson());

            mockSession.Setup(_ => _.TryGetValue(Key.DataFlowGraph.ToString("g"), out dataValue)).Returns(true);

            var analyzeDatas = new List <AnalyzeData>();

            for (var i = 0; i < 5; ++i)
            {
                var    structs      = Analyzer.GetStructs(file.Content);
                var    _struct      = structs.FirstOrDefault(x => x.Name == "headers_t");
                Random random       = new Random();
                var    randomNumber = random.Next(0, 2);

                var ethernet = _struct.Headers["ethernet"];
                if (randomNumber == 1)
                {
                    ethernet.Valid = true;
                    ethernet.Variables.ForEach(x => x.IsInitialize = true);
                }

                randomNumber = random.Next(0, 2);

                var ipv4 = _struct.Headers["ipv4"];
                if (randomNumber == 1)
                {
                    ipv4.Valid = true;
                    ipv4.Variables.ForEach(x => x.IsInitialize = true);
                }

                analyzeDatas.Add(new AnalyzeData
                {
                    Id         = i,
                    StartState = structs,
                    EndState   = structs
                });
            }

            var result = analyzerController.Update(analyzeDatas);

            var objectResult = Assert.IsType <OkObjectResult>(result);
            var model        = Assert.IsAssignableFrom <CalculatedData>(objectResult.Value);

            var analyzers = new List <Analyzer>();

            analyzeDatas.ForEach(x =>
            {
                analyzers.Add(new Analyzer(controlFlowGraph.ToJson(), dataFlowGraph.ToJson(), x, file.Content));
            });

            Parallel.ForEach(analyzers, (analyzer) =>
            {
                analyzer.Analyze();
                analyzer.FinishOperations();
            });

            analyzers.DistinctGraphs(out List <List <ViewNode> > controlFlowGraphs, out List <List <ViewNode> > dataFlowGraphs);
            analyzers.CreateCharts(out BarChartData readAndWriteChartData, out PieChartData useVariable, out PieChartData useful, out BarChartData headers);

            var calculateData = new CalculatedData
            {
                ControlFlowGraphs     = controlFlowGraphs,
                DataFlowGraphs        = dataFlowGraphs,
                ReadAndWriteChartData = readAndWriteChartData,
                UseVariable           = useVariable,
                Useful  = useful,
                Headers = headers,
                File    = file
            };

            Assert.Equal(calculateData.File.Name, model.File.Name);
            Assert.Equal(calculateData.File.Content, model.File.Content);
            CompareBarChartData(calculateData.ReadAndWriteChartData, model.ReadAndWriteChartData);
            CompareBarChartData(calculateData.Headers, model.Headers);
            ComparePieChartData(calculateData.UseVariable, model.UseVariable);
            ComparePieChartData(calculateData.Useful, model.Useful);
        }