Open Kilda Java Documentation
PathVerificationPacketSignTest.java
Go to the documentation of this file.
1 /* Copyright 2017 Telstra Open Source
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package org.openkilda.floodlight.pathverification;
17 
18 
19 import static org.easymock.EasyMock.anyObject;
20 import static org.easymock.EasyMock.expect;
21 import static org.easymock.EasyMock.replay;
22 import static org.easymock.EasyMock.reset;
23 import static org.easymock.EasyMock.verify;
24 import static org.junit.Assert.assertEquals;
25 
26 import junit.framework.AssertionFailedError;
27 import net.floodlightcontroller.core.FloodlightContext;
28 import net.floodlightcontroller.core.IFloodlightProviderService;
29 import net.floodlightcontroller.core.IListener.Command;
30 import net.floodlightcontroller.core.IOFSwitch;
31 import net.floodlightcontroller.core.module.FloodlightModuleException;
32 import net.floodlightcontroller.packet.Ethernet;
33 import net.floodlightcontroller.packet.IPacket;
34 import net.floodlightcontroller.packet.PacketParsingException;
35 import org.apache.kafka.clients.producer.KafkaProducer;
36 import org.easymock.EasyMock;
37 import org.easymock.EasyMockRunner;
38 import org.easymock.Mock;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 import org.projectfloodlight.openflow.protocol.OFPacketIn;
43 import org.projectfloodlight.openflow.protocol.OFPacketOut;
44 import org.projectfloodlight.openflow.protocol.OFType;
45 import org.projectfloodlight.openflow.protocol.match.Match;
46 import org.projectfloodlight.openflow.protocol.match.MatchField;
47 import org.projectfloodlight.openflow.types.DatapathId;
48 import org.projectfloodlight.openflow.types.OFPort;
49 
50 import java.util.HashMap;
51 
52 @RunWith(EasyMockRunner.class)
54 
55  private OFPacketIn ofPacketIn;
56  private FloodlightContext context;
57 
58  @Mock
59  private KafkaProducer<String, String> producer;
60 
61  @Before
62  public void setUp() throws Exception {
63 
64  super.setUp();
65 
66  OFPacketOut packetOut = pvs.generateVerificationPacket(sw1, OFPort.of(1));
67 
68  ofPacketIn = EasyMock.createMock(OFPacketIn.class);
69 
70  context = new FloodlightContext();
71 
72  expect(ofPacketIn.getType()).andReturn(OFType.PACKET_IN).anyTimes();
73  expect(ofPacketIn.getXid()).andReturn(0L).anyTimes();
74  expect(ofPacketIn.getVersion()).andReturn(packetOut.getVersion()).anyTimes();
75 
76  Match match = EasyMock.createMock(Match.class);
77  expect(match.get(MatchField.IN_PORT)).andReturn(OFPort.of(1)).anyTimes();
78  replay(match);
79  expect(ofPacketIn.getMatch()).andReturn(match).anyTimes();
80  replay(ofPacketIn);
81 
82  IPacket expected = new Ethernet().deserialize(packetOut.getData(), 0,
83  packetOut.getData().length);
84 
85  context.getStorage().put(IFloodlightProviderService.CONTEXT_PI_PAYLOAD, expected);
86 
87  HashMap<DatapathId, IOFSwitch> switches = new HashMap<>();
88  switches.put(sw1.getId(), sw1);
89  switches.put(sw2.getId(), sw2);
90  mockSwitchManager.setSwitches(switches);
91 
92  reset(producer);
93 
94  pvs.setKafkaProducer(producer);
95  }
96 
97  @Test
98  public void testSignPacketPositive() throws PacketParsingException, FloodlightModuleException {
99  expect(producer.send(anyObject())).andReturn(null).once();
100  replay(producer);
101  assertEquals(Command.STOP, pvs.receive(sw2, ofPacketIn, context));
102  verify(producer);
103  }
104 
105  @Test
106  public void testSignPacketMissedSign() throws PacketParsingException, FloodlightModuleException {
107  OFPacketOut noSignPacket = pvs.generateVerificationPacket(sw1, OFPort.of(1), null, false);
108  IPacket noSignPacketData = new Ethernet().deserialize(noSignPacket.getData(), 0,
109  noSignPacket.getData().length);
110  context.getStorage().put(IFloodlightProviderService.CONTEXT_PI_PAYLOAD, noSignPacketData);
111  expect(producer.send(anyObject())).andThrow(new AssertionFailedError()).anyTimes();
112  replay(producer);
113  assertEquals(Command.STOP, pvs.receive(sw2, ofPacketIn, context));
114  verify(producer);
115  }
116 
117  @Test
118  public void testSignPacketInvalidSign() throws PacketParsingException, FloodlightModuleException {
119  expect(producer.send(anyObject())).andThrow(new AssertionFailedError()).anyTimes();
120  replay(producer);
121  pvs.initAlgorithm("secret2");
122  assertEquals(Command.STOP, pvs.receive(sw2, ofPacketIn, context));
123  verify(producer);
124  }
125 }
net
Definition: plan-b.py:46