From 44670cfbcef8db3ab5d14408c081a84e205a83ea Mon Sep 17 00:00:00 2001 From: Eishun Kondoh Date: Mon, 18 Mar 2019 22:22:37 +0900 Subject: [PATCH] Add testcases for Openflow13 standard actions --- lib/openflow/actions/output.ex | 6 +- lib/openflow/actions/pop_mpls.ex | 4 +- lib/openflow/actions/push_mpls.ex | 4 +- test/ofp_action_test.exs | 167 ++++++++++++++++++++++++++++++ 4 files changed, 177 insertions(+), 4 deletions(-) diff --git a/lib/openflow/actions/output.ex b/lib/openflow/actions/output.ex index 070b123..96e52c5 100644 --- a/lib/openflow/actions/output.ex +++ b/lib/openflow/actions/output.ex @@ -8,13 +8,15 @@ defmodule Openflow.Action.Output do def ofpat, do: 0 + def new(options \\ []) + def new(port) when not is_list(port) do new(port_number: port) end def new(options) when is_list(options) do - port_no = Keyword.get(options, :port_number) - max_len = Keyword.get(options, :max_len, :no_buffer) + port_no = options[:port_number] || raise "port_number must be specified" + max_len = options[:max_len] || :no_buffer %Output{port_number: port_no, max_len: max_len} end diff --git a/lib/openflow/actions/pop_mpls.ex b/lib/openflow/actions/pop_mpls.ex index 7ea7950..91f6eaa 100644 --- a/lib/openflow/actions/pop_mpls.ex +++ b/lib/openflow/actions/pop_mpls.ex @@ -3,9 +3,11 @@ defmodule Openflow.Action.PopMpls do alias __MODULE__ + @eth_p_mpls_uc 0x8847 + def ofpat, do: 20 - def new(ethertype) do + def new(ethertype \\ @eth_p_mpls_uc) do %PopMpls{ethertype: ethertype} end diff --git a/lib/openflow/actions/push_mpls.ex b/lib/openflow/actions/push_mpls.ex index f9c7fe0..9d55a28 100644 --- a/lib/openflow/actions/push_mpls.ex +++ b/lib/openflow/actions/push_mpls.ex @@ -1,11 +1,13 @@ defmodule Openflow.Action.PushMpls do defstruct(ethertype: 0x8847) + @eth_p_mpls_uc 0x8847 + alias __MODULE__ def ofpat, do: 19 - def new(ethertype) do + def new(ethertype \\ @eth_p_mpls_uc) do %PushMpls{ethertype: ethertype} end diff --git a/test/ofp_action_test.exs b/test/ofp_action_test.exs index 0304e14..ddad5af 100644 --- a/test/ofp_action_test.exs +++ b/test/ofp_action_test.exs @@ -2,6 +2,173 @@ defmodule OfpActionTest do use ExUnit.Case doctest Openflow + describe "Openflow.Action.Output" do + test "with output:1" do + output = Openflow.Action.Output.new(1) + + output + |> Openflow.Action.to_binary() + |> Openflow.Action.read() + |> Enum.at(0) + |> Kernel.==(output) + |> assert() + end + + test "with CONTROLLER:65509" do + output = Openflow.Action.Output.new(port_number: :controller, max_len: :max) + + output + |> Openflow.Action.to_binary() + |> Openflow.Action.read() + |> Enum.at(0) + |> Kernel.==(output) + |> assert() + end + + test "with no port_number option" do + assert_raise RuntimeError, "port_number must be specified", fn -> + Openflow.Action.Output.new() + end + end + end + + describe "Openflow.Action.DecMplsTtl" do + test "with no options" do + dec_mpls_ttl = Openflow.Action.DecMplsTtl.new() + + dec_mpls_ttl + |> Openflow.Action.to_binary() + |> Openflow.Action.read() + |> Enum.at(0) + |> Kernel.==(dec_mpls_ttl) + |> assert() + end + end + + describe "Openflow.Action.PushVlan" do + test "with no options" do + push_vlan = Openflow.Action.PushVlan.new() + + push_vlan + |> Openflow.Action.to_binary() + |> Openflow.Action.read() + |> Enum.at(0) + |> Kernel.==(push_vlan) + |> assert() + end + + test "with an ethertype" do + push_vlan = Openflow.Action.PushVlan.new(0x8100) + + push_vlan + |> Openflow.Action.to_binary() + |> Openflow.Action.read() + |> Enum.at(0) + |> Kernel.==(push_vlan) + |> assert() + end + end + + describe "Openflow.Action.PopVlan" do + test "with no options" do + pop_vlan = Openflow.Action.PopVlan.new() + + pop_vlan + |> Openflow.Action.to_binary() + |> Openflow.Action.read() + |> Enum.at(0) + |> Kernel.==(pop_vlan) + |> assert() + end + end + + describe "Openflow.Action.PushMpls" do + test "with no options" do + push_vlan = Openflow.Action.PushMpls.new() + + push_vlan + |> Openflow.Action.to_binary() + |> Openflow.Action.read() + |> Enum.at(0) + |> Kernel.==(push_vlan) + |> assert() + end + + test "with an ethertype" do + push_vlan = Openflow.Action.PushMpls.new(0x8847) + + push_vlan + |> Openflow.Action.to_binary() + |> Openflow.Action.read() + |> Enum.at(0) + |> Kernel.==(push_vlan) + |> assert() + end + end + + describe "Openflow.Action.PopMpls" do + test "with no options" do + pop_mpls = Openflow.Action.PopMpls.new() + + pop_mpls + |> Openflow.Action.to_binary() + |> Openflow.Action.read() + |> Enum.at(0) + |> Kernel.==(pop_mpls) + |> assert() + end + + test "with an ethertype" do + pop_mpls = Openflow.Action.PopMpls.new(0x8847) + + pop_mpls + |> Openflow.Action.to_binary() + |> Openflow.Action.read() + |> Enum.at(0) + |> Kernel.==(pop_mpls) + |> assert() + end + end + + describe "Openflow.Action.SetQueue" do + test "with set_queue:1" do + set_queue = Openflow.Action.SetQueue.new(1) + + set_queue + |> Openflow.Action.to_binary() + |> Openflow.Action.read() + |> Enum.at(0) + |> Kernel.==(set_queue) + |> assert() + end + end + + describe "Openflow.Action.Group" do + test "with group:1" do + group = Openflow.Action.Group.new(1) + + group + |> Openflow.Action.to_binary() + |> Openflow.Action.read() + |> Enum.at(0) + |> Kernel.==(group) + |> assert() + end + end + + describe "Openflow.Action.DecNwTtl" do + test "with dec_ttl" do + dec_nw_ttl = Openflow.Action.DecNwTtl.new() + + dec_nw_ttl + |> Openflow.Action.to_binary() + |> Openflow.Action.read() + |> Enum.at(0) + |> Kernel.==(dec_nw_ttl) + |> assert() + end + end + describe "Openflow.Action.NxBundle" do test "with a binary" do test_file = "test/packet_data/nx_bundle.raw"