1 import ./make-test-python.nix {
4 nodes.machine = { pkgs, ... }: {
5 security.dhparams.enable = true;
6 environment.systemPackages = [ pkgs.openssl ];
9 gen1.configuration = { config, ... }: {
10 security.dhparams.params = {
11 # Use low values here because we don't want the test to run for ages.
13 # Also use the old format to make sure the type is coerced in the right
18 systemd.services.foo = {
19 description = "Check systemd Ordering";
20 wantedBy = [ "multi-user.target" ];
22 # This is to make sure that the dhparams generation of foo occurs
23 # before this service so we need this service to start as early as
24 # possible to provoke a race condition.
25 DefaultDependencies = false;
27 # We check later whether the service has been started or not.
28 ConditionPathExists = config.security.dhparams.params.foo.path;
30 serviceConfig.Type = "oneshot";
31 serviceConfig.RemainAfterExit = true;
32 # The reason we only provide an ExecStop here is to ensure that we don't
33 # accidentally trigger an error because a file system is not yet ready
34 # during very early startup (we might not even have the Nix store
35 # available, for example if future changes in NixOS use systemd mount
36 # units to do early file system initialisation).
37 serviceConfig.ExecStop = "${pkgs.coreutils}/bin/true";
40 gen2.configuration = {
41 security.dhparams.params.foo.bits = 1026;
43 gen3.configuration = {};
44 gen4.configuration = {
45 security.dhparams.stateful = false;
46 security.dhparams.params.foo2.bits = 1027;
47 security.dhparams.params.bar2.bits = 1028;
49 gen5.configuration = {
50 security.dhparams.defaultBitSize = 1029;
51 security.dhparams.params.foo3 = {};
52 security.dhparams.params.bar3 = {};
57 testScript = { nodes, ... }: let
58 getParamPath = gen: name: let
59 node = "gen${toString gen}";
60 in nodes.machine.config.specialisation.${node}.configuration.security.dhparams.params.${name}.path;
62 switchToGeneration = gen: let
63 switchCmd = "${nodes.machine.config.system.build.toplevel}/specialisation/gen${toString gen}/bin/switch-to-configuration test";
65 with machine.nested("switch to generation ${toString gen}"):
66 machine.succeed("${switchCmd}")
73 def assert_param_bits(path, bits):
74 with machine.nested(f"check bit size of {path}"):
75 output = machine.succeed(f"openssl dhparam -in {path} -text")
76 pattern = re.compile(r"^\s*DH Parameters:\s+\((\d+)\s+bit\)\s*$", re.M)
77 match = pattern.match(output)
79 raise Exception("bla")
80 if match[1] != str(bits):
81 raise Exception(f"bit size should be {bits} but it is {match[1]} instead.")
83 machine.wait_for_unit("multi-user.target")
84 ${switchToGeneration 1}
86 with subtest("verify startup order"):
87 machine.succeed("systemctl is-active foo.service")
89 with subtest("check bit sizes of dhparam files"):
90 assert_param_bits("${getParamPath 1 "foo"}", 1024)
91 assert_param_bits("${getParamPath 1 "bar"}", 1025)
93 ${switchToGeneration 2}
95 with subtest("check whether bit size has changed"):
96 assert_param_bits("${getParamPath 2 "foo"}", 1026)
98 with subtest("ensure that dhparams file for 'bar' was deleted"):
99 machine.fail("test -e ${getParamPath 1 "bar"}")
101 ${switchToGeneration 3}
103 with subtest("ensure that 'security.dhparams.path' has been deleted"):
104 machine.fail("test -e ${nodes.machine.config.specialisation.gen3.configuration.security.dhparams.path}")
106 ${switchToGeneration 4}
108 with subtest("check bit sizes dhparam files"):
110 "${getParamPath 4 "foo2"}", 1027
113 "${getParamPath 4 "bar2"}", 1028
116 with subtest("check whether dhparam files are in the Nix store"):
118 "expr match ${getParamPath 4 "foo2"} ${builtins.storeDir}",
119 "expr match ${getParamPath 4 "bar2"} ${builtins.storeDir}",
122 ${switchToGeneration 5}
124 with subtest("check whether defaultBitSize works as intended"):
125 assert_param_bits("${getParamPath 5 "foo3"}", 1029)
126 assert_param_bits("${getParamPath 5 "bar3"}", 1029)