socket_proxy_testing/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright 2024 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use fidl_fuchsia_net::{IpAddress, SocketAddress};
use fidl_fuchsia_netpol_socketproxy::{
    DnsServerList, Network, NetworkDnsServers, NetworkInfo, StarnixNetworkInfo,
};

fn dns_server_list(id: u32) -> DnsServerList {
    DnsServerList { source_network_id: Some(id), addresses: Some(vec![]), ..Default::default() }
}

fn network_info(mark: u32) -> NetworkInfo {
    NetworkInfo::Starnix(StarnixNetworkInfo {
        mark: Some(mark),
        handle: Some(0),
        ..Default::default()
    })
}

fn network(network_id: u32) -> Network {
    Network {
        network_id: Some(network_id),
        info: Some(network_info(network_id)),
        dns_servers: Some(Default::default()),
        ..Default::default()
    }
}

pub trait ToNetwork {
    fn to_network(self) -> Network;
}

pub trait ToDnsServerList {
    fn to_dns_server_list(self) -> DnsServerList;
}

impl ToNetwork for u32 {
    fn to_network(self) -> Network {
        network(self)
    }
}

impl ToDnsServerList for u32 {
    fn to_dns_server_list(self) -> DnsServerList {
        dns_server_list(self)
    }
}

impl ToNetwork for (u32, Vec<IpAddress>) {
    fn to_network(self) -> Network {
        let (v4, v6) = self.1.iter().fold((Vec::new(), Vec::new()), |(mut v4s, mut v6s), s| {
            match s {
                IpAddress::Ipv4(v4) => v4s.push(*v4),
                IpAddress::Ipv6(v6) => v6s.push(*v6),
            }
            (v4s, v6s)
        });
        Network {
            dns_servers: Some(NetworkDnsServers {
                v4: Some(v4),
                v6: Some(v6),
                ..Default::default()
            }),
            ..network(self.0)
        }
    }
}

impl ToDnsServerList for (u32, Vec<SocketAddress>) {
    fn to_dns_server_list(self) -> DnsServerList {
        DnsServerList { addresses: Some(self.1), ..dns_server_list(self.0) }
    }
}

impl<N: ToNetwork + Clone> ToNetwork for &N {
    fn to_network(self) -> Network {
        self.clone().to_network()
    }
}

impl<D: ToDnsServerList + Clone> ToDnsServerList for &D {
    fn to_dns_server_list(self) -> DnsServerList {
        self.clone().to_dns_server_list()
    }
}