|
1 | 1 | const std = @import("std"); |
2 | 2 | const file_config = @import("../config/file.zig"); |
3 | 3 | const identity_store = @import("../config/identity_store.zig"); |
| 4 | +const runtime = @import("../runtime/runtime.zig"); |
4 | 5 |
|
5 | 6 | const Subcommand = enum { |
6 | 7 | validate, |
@@ -35,10 +36,18 @@ fn handleValidate(args: []const []const u8, default_config_path: []const u8) !vo |
35 | 36 | var cfg = try file_config.parse(allocator, raw); |
36 | 37 | defer cfg.deinit(allocator); |
37 | 38 | try validateFilesystem(config_path, cfg.node.identity_path); |
| 39 | + var runtime_cfg = try runtime.runtime_config.load(allocator, config_path); |
| 40 | + defer runtime_cfg.deinit(allocator); |
38 | 41 |
|
39 | 42 | std.debug.print( |
40 | | - "config valid\npath={s}\nnetwork_id={s}\ntun={s}\n", |
41 | | - .{ config_path, cfg.node.network_id, cfg.tun.name }, |
| 43 | + "config valid\npath={s}\nnetwork_id={s}\ntun={s}\nallowed_peers={d}\nbootstrap_peers={d}\n", |
| 44 | + .{ |
| 45 | + config_path, |
| 46 | + cfg.node.network_id, |
| 47 | + cfg.tun.name, |
| 48 | + runtime_cfg.enrolled_peers.len, |
| 49 | + runtime_cfg.startup_bootstrap_peers.len, |
| 50 | + }, |
42 | 51 | ); |
43 | 52 | } |
44 | 53 |
|
@@ -146,6 +155,103 @@ test "validate accepts a well formed config file" { |
146 | 155 | try handleValidate(&.{ "-c", config_path }, "/etc/libvine/vine.toml"); |
147 | 156 | } |
148 | 157 |
|
| 158 | +test "validate rejects tun names that cannot be loaded into runtime state" { |
| 159 | + var tmp = std.testing.tmpDir(.{}); |
| 160 | + defer tmp.cleanup(); |
| 161 | + |
| 162 | + const dir_path = try tmp.dir.realpathAlloc(std.testing.allocator, "."); |
| 163 | + defer std.testing.allocator.free(dir_path); |
| 164 | + |
| 165 | + const identity_path = try std.fmt.allocPrint(std.testing.allocator, "{s}/identity", .{dir_path}); |
| 166 | + defer std.testing.allocator.free(identity_path); |
| 167 | + _ = try identity_store.generateAndWrite(identity_path); |
| 168 | + |
| 169 | + const config_body = try std.fmt.allocPrint( |
| 170 | + std.testing.allocator, |
| 171 | + \\[node] |
| 172 | + \\name = "alpha" |
| 173 | + \\network_id = "home-net" |
| 174 | + \\identity_path = "{s}" |
| 175 | + \\ |
| 176 | + \\[tun] |
| 177 | + \\name = "vine-interface-too-long" |
| 178 | + \\address = "10.42.0.1" |
| 179 | + \\prefix_len = 24 |
| 180 | + \\mtu = 1400 |
| 181 | + , |
| 182 | + .{identity_path}, |
| 183 | + ); |
| 184 | + defer std.testing.allocator.free(config_body); |
| 185 | + |
| 186 | + const config_file = try tmp.dir.createFile("vine.toml", .{ .truncate = true, .mode = 0o600 }); |
| 187 | + defer config_file.close(); |
| 188 | + try config_file.writeAll(config_body); |
| 189 | + |
| 190 | + const config_path = try std.fmt.allocPrint(std.testing.allocator, "{s}/vine.toml", .{dir_path}); |
| 191 | + defer std.testing.allocator.free(config_path); |
| 192 | + |
| 193 | + try std.testing.expectError( |
| 194 | + error.InvalidConfig, |
| 195 | + handleValidate(&.{ "-c", config_path }, "/etc/libvine/vine.toml"), |
| 196 | + ); |
| 197 | +} |
| 198 | + |
| 199 | +test "validate rejects overlapping allowed peer prefixes before startup" { |
| 200 | + var tmp = std.testing.tmpDir(.{}); |
| 201 | + defer tmp.cleanup(); |
| 202 | + |
| 203 | + const dir_path = try tmp.dir.realpathAlloc(std.testing.allocator, "."); |
| 204 | + defer std.testing.allocator.free(dir_path); |
| 205 | + |
| 206 | + const identity_path = try std.fmt.allocPrint(std.testing.allocator, "{s}/identity", .{dir_path}); |
| 207 | + defer std.testing.allocator.free(identity_path); |
| 208 | + _ = try identity_store.generateAndWrite(identity_path); |
| 209 | + |
| 210 | + const peer_a = [_]u8{0x41} ** 32; |
| 211 | + const peer_b = [_]u8{0x42} ** 32; |
| 212 | + const peer_a_hex = std.fmt.bytesToHex(peer_a, .lower); |
| 213 | + const peer_b_hex = std.fmt.bytesToHex(peer_b, .lower); |
| 214 | + |
| 215 | + const config_body = try std.fmt.allocPrint( |
| 216 | + std.testing.allocator, |
| 217 | + \\[node] |
| 218 | + \\name = "alpha" |
| 219 | + \\network_id = "home-net" |
| 220 | + \\identity_path = "{s}" |
| 221 | + \\ |
| 222 | + \\[tun] |
| 223 | + \\name = "vine0" |
| 224 | + \\address = "10.42.0.1" |
| 225 | + \\prefix_len = 24 |
| 226 | + \\mtu = 1400 |
| 227 | + \\ |
| 228 | + \\[[allowed_peers]] |
| 229 | + \\peer_id = "{s}" |
| 230 | + \\prefix = "10.42.1.0/24" |
| 231 | + \\relay_capable = false |
| 232 | + \\ |
| 233 | + \\[[allowed_peers]] |
| 234 | + \\peer_id = "{s}" |
| 235 | + \\prefix = "10.42.1.128/25" |
| 236 | + \\relay_capable = false |
| 237 | + , |
| 238 | + .{ identity_path, &peer_a_hex, &peer_b_hex }, |
| 239 | + ); |
| 240 | + defer std.testing.allocator.free(config_body); |
| 241 | + |
| 242 | + const config_file = try tmp.dir.createFile("vine.toml", .{ .truncate = true, .mode = 0o600 }); |
| 243 | + defer config_file.close(); |
| 244 | + try config_file.writeAll(config_body); |
| 245 | + |
| 246 | + const config_path = try std.fmt.allocPrint(std.testing.allocator, "{s}/vine.toml", .{dir_path}); |
| 247 | + defer std.testing.allocator.free(config_path); |
| 248 | + |
| 249 | + try std.testing.expectError( |
| 250 | + error.InvalidConfig, |
| 251 | + handleValidate(&.{ "-c", config_path }, "/etc/libvine/vine.toml"), |
| 252 | + ); |
| 253 | +} |
| 254 | + |
149 | 255 | test "validate rejects relative config paths before daemon startup" { |
150 | 256 | try std.testing.expectError( |
151 | 257 | ConfigError.InvalidConfigPath, |
|
0 commit comments