summaryrefslogtreecommitdiff
path: root/test/parallel/test-dgram-multicast-set-interface.js
blob: 2900cb89738e82e9c2d1aa8d2e5496fab305c859 (plain)
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
'use strict';
const common = require('../common');
const assert = require('assert');
const dgram = require('dgram');

{
  const socket = dgram.createSocket('udp4');

  socket.bind(0);
  socket.on('listening', common.mustCall(() => {
    // Explicitly request default system selection
    socket.setMulticastInterface('0.0.0.0');

    socket.close();
  }));
}

{
  const socket = dgram.createSocket('udp4');

  socket.bind(0);
  socket.on('listening', common.mustCall(() => {
    socket.close(common.mustCall(() => {
      assert.throws(() => { socket.setMulticastInterface('0.0.0.0'); },
                    /Not running/);
    }));
  }));
}

{
  const socket = dgram.createSocket('udp4');

  socket.bind(0);
  socket.on('listening', common.mustCall(() => {
    // Try to set with an invalid interfaceAddress (wrong address class)
    //
    // This operation succeeds on some platforms, throws `EINVAL` on some
    // platforms, and throws `ENOPROTOOPT` on others. This is unpleasant, but
    // we should at least test for it.
    try {
      socket.setMulticastInterface('::');
    } catch (e) {
      assert(e.code === 'EINVAL' || e.code === 'ENOPROTOOPT');
    }

    socket.close();
  }));
}

{
  const socket = dgram.createSocket('udp4');

  socket.bind(0);
  socket.on('listening', common.mustCall(() => {
    // Try to set with an invalid interfaceAddress (wrong Type)
    assert.throws(() => {
      socket.setMulticastInterface(1);
    }, /TypeError/);

    socket.close();
  }));
}

{
  const socket = dgram.createSocket('udp4');

  socket.bind(0);
  socket.on('listening', common.mustCall(() => {
    // Try to set with an invalid interfaceAddress (non-unicast)
    assert.throws(() => {
      socket.setMulticastInterface('224.0.0.2');
    }, /Error/);

    socket.close();
  }));
}

// If IPv6 is not supported, skip the rest of the test. However, don't call
// common.skip(), which calls process.exit() while there is outstanding
// common.mustCall() activity.
if (!common.hasIPv6)
  return;

{
  const socket = dgram.createSocket('udp6');

  socket.bind(0);
  socket.on('listening', common.mustCall(() => {
    // Try to set with an invalid interfaceAddress ('undefined')
    assert.throws(() => {
      socket.setMulticastInterface(String(undefined));
    }, /EINVAL/);

    socket.close();
  }));
}

{
  const socket = dgram.createSocket('udp6');

  socket.bind(0);
  socket.on('listening', common.mustCall(() => {
    // Try to set with an invalid interfaceAddress ('')
    assert.throws(() => {
      socket.setMulticastInterface('');
    }, /EINVAL/);

    socket.close();
  }));
}

{
  const socket = dgram.createSocket('udp6');

  socket.bind(0);
  socket.on('listening', common.mustCall(() => {
    // Using lo0 for OsX, on all other OSes, an invalid Scope gets
    // turned into #0 (default selection) which is also acceptable.
    socket.setMulticastInterface('::%lo0');

    socket.close();
  }));
}