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