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