summaryrefslogtreecommitdiffstats
path: root/nixos/tests/ax25.nix
blob: abfa003a077e093a50327032379f1bb581f7ba7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
{ pkgs, lib, ... }:

let
  baud = 57600;
  tty = "/dev/ttyACM0";
  port = "tnc0";
  socatPort = 1234;

  createAX25Node = nodeId: {
    boot.kernelModules = [ "ax25" ];

    networking.firewall.allowedTCPPorts = [ socatPort ];

    environment.systemPackages = with pkgs; [
      libax25
      ax25-tools
      ax25-apps
      socat
    ];

    services.ax25.axports."${port}" = {
      inherit baud tty;
      enable = true;
      callsign = "NOCALL-${toString nodeId}";
      description = "mocked tnc";
    };

    services.ax25.axlisten = {
      enable = true;
    };

    # All mocks radios will connect back to socat-broker on node 1 in order to get
    # all messages that are "broadcasted over the ether"
    systemd.services.ax25-mock-hardware = {
      description = "mock AX.25 TNC and Radio";
      wantedBy = [ "default.target" ];
      before = [
        "ax25-kissattach-${port}.service"
        "axlisten.service"
      ];
      after = [ "network.target" ];
      serviceConfig = {
        Type = "exec";
        ExecStart = "${pkgs.socat}/bin/socat -d -d tcp:192.168.1.1:${toString socatPort} pty,link=${tty},b${toString baud},raw";
      };
    };
  };
in
{
  name = "ax25Simple";
  nodes = {
    node1 = lib.mkMerge [
      (createAX25Node 1)
      # mimicking radios on the same frequency
      {
        systemd.services.ax25-mock-ether = {
          description = "mock radio ether";
          wantedBy = [ "default.target" ];
          requires = [ "network.target" ];
          before = [ "ax25-mock-hardware.service" ];
          # broken needs access to "ss" or "netstat"
          path = [ pkgs.iproute2 ];
          serviceConfig = {
            Type = "exec";
            ExecStart = "${pkgs.socat}/bin/socat-broker.sh tcp4-listen:${toString socatPort}";
          };
          postStart = "${pkgs.coreutils}/bin/sleep 2";
        };
      }
    ];
    node2 = createAX25Node 2;
    node3 = createAX25Node 3;
  };
  testScript =
    { ... }:
    ''
      def wait_for_machine(m):
        m.succeed("lsmod | grep ax25")
        m.wait_for_unit("ax25-axports.target")
        m.wait_for_unit("axlisten.service")
        m.fail("journalctl -o cat -u axlisten.service | grep -i \"no AX.25 port data configured\"")

      # start the first node since the socat-broker needs to be running
      node1.start()
      node1.wait_for_unit("ax25-mock-ether.service")
      wait_for_machine(node1)

      node2.start()
      node3.start()
      wait_for_machine(node2)
      wait_for_machine(node3)

      # Node 1 -> Node 2
      node1.succeed("echo hello | ax25_call ${port} NOCALL-1 NOCALL-2")
      node2.sleep(1)
      node2.succeed("journalctl -o cat -u axlisten.service | grep -A1 \"NOCALL-1 to NOCALL-2 ctl I00\" | grep hello")

      # Node 1 -> Node 3
      node1.succeed("echo hello | ax25_call ${port} NOCALL-1 NOCALL-3")
      node3.sleep(1)
      node3.succeed("journalctl -o cat -u axlisten.service | grep -A1 \"NOCALL-1 to NOCALL-3 ctl I00\" | grep hello")

      # Node 2 -> Node 1
      # must sleep due to previous ax25_call lingering
      node2.sleep(5)
      node2.succeed("echo hello | ax25_call ${port} NOCALL-2 NOCALL-1")
      node1.sleep(1)
      node1.succeed("journalctl -o cat -u axlisten.service | grep -A1 \"NOCALL-2 to NOCALL-1 ctl I00\" | grep hello")

      # Node 2 -> Node 3
      node2.succeed("echo hello | ax25_call ${port} NOCALL-2 NOCALL-3")
      node3.sleep(1)
      node3.succeed("journalctl -o cat -u axlisten.service | grep -A1 \"NOCALL-2 to NOCALL-3 ctl I00\" | grep hello")

      # Node 3 -> Node 1
      # must sleep due to previous ax25_call lingering
      node3.sleep(5)
      node3.succeed("echo hello | ax25_call ${port} NOCALL-3 NOCALL-1")
      node1.sleep(1)
      node1.succeed("journalctl -o cat -u axlisten.service | grep -A1 \"NOCALL-3 to NOCALL-1 ctl I00\" | grep hello")

      # Node 3 -> Node 2
      node3.succeed("echo hello | ax25_call ${port} NOCALL-3 NOCALL-2")
      node2.sleep(1)
      node2.succeed("journalctl -o cat -u axlisten.service | grep -A1 \"NOCALL-3 to NOCALL-2 ctl I00\" | grep hello")
    '';
}