|
| 1 | +defmodule Radius.PacketTest do |
| 2 | + use ExUnit.Case, async: true |
| 3 | + |
| 4 | + @secret "mykey" |
| 5 | + @sample_req %Radius.Packet{ |
| 6 | + code: "Access-Request", |
| 7 | + id: 118, |
| 8 | + length: 173, |
| 9 | + auth: <<55, 91, 232, 245, 150, 233, 11, 207, 252, 94, 50, 146, 157, 20, 39, 91>>, |
| 10 | + attrs: [ |
| 11 | + {"NAS-IP-Address", {10, 62, 1, 238}}, |
| 12 | + {"NAS-Port", 50001}, |
| 13 | + {"NAS-Port-Type", "Ethernet"}, |
| 14 | + {"User-Name", "host/drswin7tracyp.drsl.co.uk"}, |
| 15 | + {"Called-Station-Id", "00-12-00-E3-41-C1"}, |
| 16 | + {"Calling-Station-Id", "B4-99-BA-F2-8A-D6"}, |
| 17 | + {"Service-Type", "Framed-User"}, |
| 18 | + {"Framed-MTU", 1500}, |
| 19 | + {"EAP-Message", |
| 20 | + <<2, 0, 0, 34, 1, 104, 111, 115, 116, 47, 100, 114, 115, 119, 105, 110, 55, 116, 114, 97, |
| 21 | + 99, 121, 112, 46, 100, 114, 115, 108, 46, 99, 111, 46, 117, 107>>}, |
| 22 | + {"Message-Authenticator", |
| 23 | + <<201, 62, 246, 40, 105, 10, 87, 139, 49, 112, 155, 11, 188, 202, 222, 65>>} |
| 24 | + ], |
| 25 | + raw: nil, |
| 26 | + secret: "mykey" |
| 27 | + } |
| 28 | + @sample_binary_req <<1, 118, 0, 173, 55, 91, 232, 245, 150, 233, 11, 207, 252, 94, 50, 146, 157, |
| 29 | + 20, 39, 91, 4, 6, 10, 62, 1, 238, 5, 6, 0, 0, 195, 81, 61, 6, 0, 0, 0, 15, |
| 30 | + 1, 31, 104, 111, 115, 116, 47, 100, 114, 115, 119, 105, 110, 55, 116, 114, |
| 31 | + 97, 99, 121, 112, 46, 100, 114, 115, 108, 46, 99, 111, 46, 117, 107, 30, |
| 32 | + 19, 48, 48, 45, 49, 50, 45, 48, 48, 45, 69, 51, 45, 52, 49, 45, 67, 49, 31, |
| 33 | + 19, 66, 52, 45, 57, 57, 45, 66, 65, 45, 70, 50, 45, 56, 65, 45, 68, 54, 6, |
| 34 | + 6, 0, 0, 0, 2, 12, 6, 0, 0, 5, 220, 79, 36, 2, 0, 0, 34, 1, 104, 111, 115, |
| 35 | + 116, 47, 100, 114, 115, 119, 105, 110, 55, 116, 114, 97, 99, 121, 112, 46, |
| 36 | + 100, 114, 115, 108, 46, 99, 111, 46, 117, 107, 80, 18, 201, 62, 246, 40, |
| 37 | + 105, 10, 87, 139, 49, 112, 155, 11, 188, 202, 222, 65>> |
| 38 | + @sample_rep %Radius.Packet{ |
| 39 | + code: "Access-Accept", |
| 40 | + id: 118, |
| 41 | + length: 173, |
| 42 | + auth: nil, |
| 43 | + attrs: [ |
| 44 | + {"NAS-IP-Address", {10, 62, 1, 238}}, |
| 45 | + {"NAS-Port", 50001}, |
| 46 | + {"NAS-Port-Type", "Ethernet"}, |
| 47 | + {"User-Name", "host/drswin7tracyp.drsl.co.uk"}, |
| 48 | + {"Called-Station-Id", "00-12-00-E3-41-C1"}, |
| 49 | + {"Calling-Station-Id", "B4-99-BA-F2-8A-D6"}, |
| 50 | + {"Service-Type", "Framed-User"}, |
| 51 | + {"Framed-MTU", 1500}, |
| 52 | + {"EAP-Message", |
| 53 | + <<2, 0, 0, 34, 1, 104, 111, 115, 116, 47, 100, 114, 115, 119, 105, 110, 55, 116, 114, 97, |
| 54 | + 99, 121, 112, 46, 100, 114, 115, 108, 46, 99, 111, 46, 117, 107>>} |
| 55 | + ], |
| 56 | + raw: nil, |
| 57 | + secret: "mykey" |
| 58 | + } |
| 59 | + @sample_binary_rep <<2, 118, 0, 155, 25, 149, 189, 198, 178, 14, 197, 28, 131, 240, 157, 146, |
| 60 | + 150, 38, 53, 105, 4, 6, 10, 62, 1, 238, 5, 6, 0, 0, 195, 81, 61, 6, 0, 0, |
| 61 | + 0, 15, 1, 31, 104, 111, 115, 116, 47, 100, 114, 115, 119, 105, 110, 55, |
| 62 | + 116, 114, 97, 99, 121, 112, 46, 100, 114, 115, 108, 46, 99, 111, 46, 117, |
| 63 | + 107, 30, 19, 48, 48, 45, 49, 50, 45, 48, 48, 45, 69, 51, 45, 52, 49, 45, |
| 64 | + 67, 49, 31, 19, 66, 52, 45, 57, 57, 45, 66, 65, 45, 70, 50, 45, 56, 65, 45, |
| 65 | + 68, 54, 6, 6, 0, 0, 0, 2, 12, 6, 0, 0, 5, 220, 79, 36, 2, 0, 0, 34, 1, 104, |
| 66 | + 111, 115, 116, 47, 100, 114, 115, 119, 105, 110, 55, 116, 114, 97, 99, 121, |
| 67 | + 112, 46, 100, 114, 115, 108, 46, 99, 111, 46, 117, 107>> |
| 68 | + @sample_binary_rep_signed <<2, 118, 0, 173, 132, 213, 98, 44, 33, 151, 126, 7, 160, 110, 91, 18, |
| 69 | + 56, 125, 67, 245, 80, 18, 27, 203, 27, 162, 52, 156, 30, 25, 241, |
| 70 | + 43, 80, 77, 28, 109, 228, 77, 4, 6, 10, 62, 1, 238, 5, 6, 0, 0, 195, |
| 71 | + 81, 61, 6, 0, 0, 0, 15, 1, 31, 104, 111, 115, 116, 47, 100, 114, |
| 72 | + 115, 119, 105, 110, 55, 116, 114, 97, 99, 121, 112, 46, 100, 114, |
| 73 | + 115, 108, 46, 99, 111, 46, 117, 107, 30, 19, 48, 48, 45, 49, 50, 45, |
| 74 | + 48, 48, 45, 69, 51, 45, 52, 49, 45, 67, 49, 31, 19, 66, 52, 45, 57, |
| 75 | + 57, 45, 66, 65, 45, 70, 50, 45, 56, 65, 45, 68, 54, 6, 6, 0, 0, 0, |
| 76 | + 2, 12, 6, 0, 0, 5, 220, 79, 36, 2, 0, 0, 34, 1, 104, 111, 115, 116, |
| 77 | + 47, 100, 114, 115, 119, 105, 110, 55, 116, 114, 97, 99, 121, 112, |
| 78 | + 46, 100, 114, 115, 108, 46, 99, 111, 46, 117, 107>> |
| 79 | + |
| 80 | + test "decode request" do |
| 81 | + packet = Radius.Packet.decode(@sample_binary_req, @secret) |
| 82 | + assert packet.code == @sample_req.code |
| 83 | + assert packet.id == @sample_req.id |
| 84 | + assert packet.length == @sample_req.length |
| 85 | + |
| 86 | + assert packet.auth == @sample_req.auth |
| 87 | + end |
| 88 | + |
| 89 | + test "encode request" do |
| 90 | + # cut authenticator as it will be generated on each encoding |
| 91 | + <<before::size(32), _random::size(128), rest::binary>> = |
| 92 | + @sample_req |> Radius.Packet.encode_request() |> Map.get(:raw) |> IO.iodata_to_binary() |
| 93 | + |
| 94 | + <<sample_before::size(32), _random::size(128), sample_rest::binary>> = @sample_binary_req |
| 95 | + assert <<before::size(32), rest::binary>> == <<sample_before::size(32), sample_rest::binary>> |
| 96 | + end |
| 97 | + |
| 98 | + test "encode reply" do |
| 99 | + reply = |
| 100 | + @sample_rep |
| 101 | + |> Radius.Packet.encode_reply(@sample_req.auth) |
| 102 | + |> Map.get(:raw) |
| 103 | + |> IO.iodata_to_binary() |
| 104 | + |
| 105 | + assert reply == @sample_binary_rep |
| 106 | + end |
| 107 | + |
| 108 | + test "encode and sign reply" do |
| 109 | + reply = |
| 110 | + @sample_rep |
| 111 | + |> Radius.Packet.encode_reply(@sample_req.auth, sign: true) |
| 112 | + |> Map.get(:raw) |
| 113 | + |> IO.iodata_to_binary() |
| 114 | + |
| 115 | + assert reply == @sample_binary_rep_signed |
| 116 | + end |
| 117 | + |
| 118 | + test "verify message authenticator signature on request" do |
| 119 | + assert Radius.Packet.verify(@sample_req) |
| 120 | + refute Radius.Packet.verify(%{@sample_req | id: 14}) |
| 121 | + refute Radius.Packet.verify(%{@sample_req | attrs: []}) |
| 122 | + end |
| 123 | + |
| 124 | + test "verify message authenticator signature on reply" do |
| 125 | + packet = Radius.Packet.decode(@sample_binary_rep_signed, @secret) |
| 126 | + assert Radius.Packet.verify(packet, @sample_req.auth) |
| 127 | + refute Radius.Packet.verify(packet) |
| 128 | + refute Radius.Packet.verify(%{packet | id: 14}, @sample_req.auth) |
| 129 | + refute Radius.Packet.verify(%{packet | attrs: []}, @sample_req.auth) |
| 130 | + end |
| 131 | +end |
0 commit comments