1# Test whether `networking.proxy' work as expected.
2
3# TODO: use a real proxy node and put this test into networking.nix
4# TODO: test whether nix tools work as expected behind a proxy
5
6let
7 default-config = {
8 imports = [ ./common/user-account.nix ];
9
10 services.xserver.enable = false;
11
12 };
13in
14import ./make-test-python.nix (
15 { pkgs, ... }:
16 {
17 name = "networking-proxy";
18 meta = with pkgs.lib.maintainers; {
19 maintainers = [ ];
20 };
21
22 nodes = {
23 # no proxy
24 machine =
25 { ... }:
26
27 default-config;
28
29 # proxy default
30 machine2 =
31 { ... }:
32
33 default-config
34 // {
35 networking.proxy.default = "http://user:pass@host:port";
36 };
37
38 # specific proxy options
39 machine3 =
40 { ... }:
41
42 default-config
43 // {
44 networking.proxy = {
45 # useless because overridden by the next options
46 default = "http://user:pass@host:port";
47 # advanced proxy setup
48 httpProxy = "123-http://user:pass@http-host:port";
49 httpsProxy = "456-http://user:pass@https-host:port";
50 rsyncProxy = "789-http://user:pass@rsync-host:port";
51 ftpProxy = "101112-http://user:pass@ftp-host:port";
52 noProxy = "131415-127.0.0.1,localhost,.localdomain";
53 };
54 };
55
56 # mix default + proxy options
57 machine4 =
58 { ... }:
59
60 default-config
61 // {
62 networking.proxy = {
63 # open for all *_proxy env var
64 default = "000-http://user:pass@default-host:port";
65 # except for those 2
66 rsyncProxy = "123-http://user:pass@http-host:port";
67 noProxy = "131415-127.0.0.1,localhost,.localdomain";
68 };
69 };
70 };
71
72 testScript = ''
73 from typing import Dict, Optional
74
75
76 def get_machine_env(machine: Machine, user: Optional[str] = None) -> Dict[str, str]:
77 """
78 Gets the environment from a given machine, and returns it as a
79 dictionary in the form:
80 {"lowercase_var_name": "value"}
81
82 Duplicate environment variables with the same name
83 (e.g. "foo" and "FOO") are handled in an undefined manner.
84 """
85 if user is not None:
86 env = machine.succeed("su - {} -c 'env -0'".format(user))
87 else:
88 env = machine.succeed("env -0")
89 ret = {}
90 for line in env.split("\0"):
91 if "=" not in line:
92 continue
93
94 key, val = line.split("=", 1)
95 ret[key.lower()] = val
96 return ret
97
98
99 start_all()
100
101 with subtest("no proxy"):
102 assert "proxy" not in machine.succeed("env").lower()
103 assert "proxy" not in machine.succeed("su - alice -c env").lower()
104
105 with subtest("default proxy"):
106 assert "proxy" in machine2.succeed("env").lower()
107 assert "proxy" in machine2.succeed("su - alice -c env").lower()
108
109 with subtest("explicitly-set proxy"):
110 env = get_machine_env(machine3)
111 assert "123" in env["http_proxy"]
112 assert "456" in env["https_proxy"]
113 assert "789" in env["rsync_proxy"]
114 assert "101112" in env["ftp_proxy"]
115 assert "131415" in env["no_proxy"]
116
117 env = get_machine_env(machine3, "alice")
118 assert "123" in env["http_proxy"]
119 assert "456" in env["https_proxy"]
120 assert "789" in env["rsync_proxy"]
121 assert "101112" in env["ftp_proxy"]
122 assert "131415" in env["no_proxy"]
123
124 with subtest("default proxy + some other specifics"):
125 env = get_machine_env(machine4)
126 assert "000" in env["http_proxy"]
127 assert "000" in env["https_proxy"]
128 assert "123" in env["rsync_proxy"]
129 assert "000" in env["ftp_proxy"]
130 assert "131415" in env["no_proxy"]
131
132 env = get_machine_env(machine4, "alice")
133 assert "000" in env["http_proxy"]
134 assert "000" in env["https_proxy"]
135 assert "123" in env["rsync_proxy"]
136 assert "000" in env["ftp_proxy"]
137 assert "131415" in env["no_proxy"]
138 '';
139 }
140)