opencomposite: add meta.platforms (#357198)
[NixPkgs.git] / lib / tests / network.sh
blob54ca476d2debb2154502e569511f1f3c118d09f1
1 #!/usr/bin/env bash
3 # Tests lib/network.nix
4 # Run:
5 # [nixpkgs]$ lib/tests/network.sh
6 # or:
7 # [nixpkgs]$ nix-build lib/tests/release.nix
9 set -euo pipefail
10 shopt -s inherit_errexit
12 if [[ -n "${TEST_LIB:-}" ]]; then
13 NIX_PATH=nixpkgs="$(dirname "$TEST_LIB")"
14 else
15 NIX_PATH=nixpkgs="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.."; pwd)"
17 export NIX_PATH
19 die() {
20 echo >&2 "test case failed: " "$@"
21 exit 1
24 tmp="$(mktemp -d)"
25 clean_up() {
26 rm -rf "$tmp"
28 trap clean_up EXIT SIGINT SIGTERM
29 work="$tmp/work"
30 mkdir "$work"
31 cd "$work"
33 prefixExpression='
34 let
35 lib = import <nixpkgs/lib>;
36 internal = import <nixpkgs/lib/network/internal.nix> {
37 inherit lib;
40 with lib;
41 with lib.network;
44 expectSuccess() {
45 local expr=$1
46 local expectedResult=$2
47 if ! result=$(nix-instantiate --eval --strict --json --show-trace \
48 --expr "$prefixExpression ($expr)"); then
49 die "$expr failed to evaluate, but it was expected to succeed"
51 if [[ ! "$result" == "$expectedResult" ]]; then
52 die "$expr == $result, but $expectedResult was expected"
56 expectSuccessRegex() {
57 local expr=$1
58 local expectedResultRegex=$2
59 if ! result=$(nix-instantiate --eval --strict --json --show-trace \
60 --expr "$prefixExpression ($expr)"); then
61 die "$expr failed to evaluate, but it was expected to succeed"
63 if [[ ! "$result" =~ $expectedResultRegex ]]; then
64 die "$expr == $result, but $expectedResultRegex was expected"
68 expectFailure() {
69 local expr=$1
70 local expectedErrorRegex=$2
71 if result=$(nix-instantiate --eval --strict --json --show-trace 2>"$work/stderr" \
72 --expr "$prefixExpression ($expr)"); then
73 die "$expr evaluated successfully to $result, but it was expected to fail"
75 if [[ ! "$(<"$work/stderr")" =~ $expectedErrorRegex ]]; then
76 die "Error was $(<"$work/stderr"), but $expectedErrorRegex was expected"
80 # Internal functions
81 expectSuccess '(internal._ipv6.split "0:0:0:0:0:0:0:0").address' '[0,0,0,0,0,0,0,0]'
82 expectSuccess '(internal._ipv6.split "000a:000b:000c:000d:000e:000f:ffff:aaaa").address' '[10,11,12,13,14,15,65535,43690]'
83 expectSuccess '(internal._ipv6.split "::").address' '[0,0,0,0,0,0,0,0]'
84 expectSuccess '(internal._ipv6.split "::0000").address' '[0,0,0,0,0,0,0,0]'
85 expectSuccess '(internal._ipv6.split "::1").address' '[0,0,0,0,0,0,0,1]'
86 expectSuccess '(internal._ipv6.split "::ffff").address' '[0,0,0,0,0,0,0,65535]'
87 expectSuccess '(internal._ipv6.split "::000f").address' '[0,0,0,0,0,0,0,15]'
88 expectSuccess '(internal._ipv6.split "::1:1:1:1:1:1:1").address' '[0,1,1,1,1,1,1,1]'
89 expectSuccess '(internal._ipv6.split "1::").address' '[1,0,0,0,0,0,0,0]'
90 expectSuccess '(internal._ipv6.split "1:1:1:1:1:1:1::").address' '[1,1,1,1,1,1,1,0]'
91 expectSuccess '(internal._ipv6.split "1:1:1:1::1:1:1").address' '[1,1,1,1,0,1,1,1]'
92 expectSuccess '(internal._ipv6.split "1::1").address' '[1,0,0,0,0,0,0,1]'
94 expectFailure 'internal._ipv6.split "0:0:0:0:0:0:0:-1"' "contains malformed characters for IPv6 address"
95 expectFailure 'internal._ipv6.split "::0:"' "is not a valid IPv6 address"
96 expectFailure 'internal._ipv6.split ":0::"' "is not a valid IPv6 address"
97 expectFailure 'internal._ipv6.split "0::0:"' "is not a valid IPv6 address"
98 expectFailure 'internal._ipv6.split "0:0:"' "is not a valid IPv6 address"
99 expectFailure 'internal._ipv6.split "0:0:0:0:0:0:0:0:0"' "is not a valid IPv6 address"
100 expectFailure 'internal._ipv6.split "0:0:0:0:0:0:0:0:"' "is not a valid IPv6 address"
101 expectFailure 'internal._ipv6.split "::0:0:0:0:0:0:0:0"' "is not a valid IPv6 address"
102 expectFailure 'internal._ipv6.split "0::0:0:0:0:0:0:0"' "is not a valid IPv6 address"
103 expectFailure 'internal._ipv6.split "::10000"' "0x10000 is not a valid u16 integer"
105 expectSuccess '(internal._ipv6.split "::").prefixLength' '128'
106 expectSuccess '(internal._ipv6.split "::/1").prefixLength' '1'
107 expectSuccess '(internal._ipv6.split "::/128").prefixLength' '128'
109 expectFailure '(internal._ipv6.split "::/0").prefixLength' "IPv6 subnet should be in range \[1;128\], got 0"
110 expectFailure '(internal._ipv6.split "::/129").prefixLength' "IPv6 subnet should be in range \[1;128\], got 129"
111 expectFailure '(internal._ipv6.split "/::/").prefixLength' "is not a valid IPv6 address in CIDR notation"
113 # Library API
114 expectSuccess 'lib.network.ipv6.fromString "2001:DB8::ffff/64"' '{"address":"2001:db8:0:0:0:0:0:ffff","prefixLength":64}'
115 expectSuccess 'lib.network.ipv6.fromString "1234:5678:90ab:cdef:fedc:ba09:8765:4321/44"' '{"address":"1234:5678:90ab:cdef:fedc:ba09:8765:4321","prefixLength":44}'
117 echo >&2 tests ok