defmodule OfpPacketOutTest do
  use ExUnit.Case
  doctest Openflow

  @packet <<
    0xF2,
    0x0B,
    0xA4,
    0xD0,
    0x3F,
    0x70,
    0xF2,
    0x0B,
    0xA4,
    0x7D,
    0xF8,
    0xEA,
    0x08,
    0x00,
    0x45,
    0x00,
    0x00,
    0x54,
    0xF8,
    0x1A,
    0x00,
    0x00,
    0xFF,
    0x01,
    0xAF,
    0x8B,
    0x0A,
    0x00,
    0x00,
    0x01,
    0x0A,
    0x00,
    0x00,
    0x02,
    0x08,
    0x00,
    0x02,
    0x08,
    0xF7,
    0x60,
    0x00,
    0x00,
    0x31,
    0xD6,
    0x02,
    0x00,
    0x00,
    0x00,
    0x00,
    0x00,
    0xAB,
    0x8D,
    0x2D,
    0x31,
    0x00,
    0x00,
    0x00,
    0x00,
    0x10,
    0x11,
    0x12,
    0x13,
    0x14,
    0x15,
    0x16,
    0x17,
    0x18,
    0x19,
    0x1A,
    0x1B,
    0x1C,
    0x1D,
    0x1E,
    0x1F,
    0x20,
    0x21,
    0x22,
    0x23,
    0x24,
    0x25,
    0x26,
    0x27,
    0x28,
    0x29,
    0x2A,
    0x2B,
    0x2C,
    0x2D,
    0x2E,
    0x2F,
    0x00,
    0x00,
    0x00,
    0x00,
    0x00,
    0x00,
    0x00,
    0x00
  >>

  describe "Openflow.read/1" do
    test "with OFP_PACKET_OUT packet" do
      {:ok, pktout, ""} =
        "test/packet_data/libofproto-OFP13-ofp_packet_out_packet_library.packet"
        |> File.read!()
        |> Openflow.read()

      assert pktout.version == 4
      assert pktout.xid == 0
      assert pktout.buffer_id == :no_buffer
      assert pktout.in_port == :controller
      assert pktout.actions == [%Openflow.Action.Output{max_len: :no_buffer, port_number: :all}]
      assert pktout.data == @packet
    end
  end

  describe "Openflow.to_binary/1" do
    pktout = %Openflow.PacketOut{
      actions: [Openflow.Action.Output.new(port_number: :all)],
      data: @packet
    }

    expect =
      "test/packet_data/libofproto-OFP13-ofp_packet_out_packet_library.packet"
      |> File.read!()

    assert Openflow.to_binary(pktout) == expect
  end
end
