File: ipv6_spec.rb

package info (click to toggle)
ruby-mongo 2.21.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 14,764 kB
  • sloc: ruby: 108,806; makefile: 5; sh: 2
file content (137 lines) | stat: -rw-r--r-- 3,417 bytes parent folder | download
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# frozen_string_literal: true
# rubocop:todo all

require 'spec_helper'

describe Mongo::Address::IPv6 do

  let(:resolver) do
    described_class.new(*described_class.parse(address))
  end

  describe 'self.parse' do

    context 'when a port is provided' do

      it 'returns the host and port' do
        expect(described_class.parse('[::1]:27017')).to eq(['::1', 27017])
      end
    end

    context 'when no port is provided and host is in brackets' do

      it 'returns the host and port' do
        expect(described_class.parse('[::1]')).to eq(['::1', 27017])
      end
    end

    context 'when no port is provided and host is not in brackets' do

      it 'returns the host and port' do
        expect(described_class.parse('::1')).to eq(['::1', 27017])
      end
    end

    context 'when invalid address is provided' do

      it 'raises ArgumentError' do
        expect do
          described_class.parse('::1:27017')
        end.to raise_error(ArgumentError, 'Invalid IPv6 address: ::1:27017')
      end

      it 'rejects extra data around the address' do
        expect do
          described_class.parse('[::1]:27017oh')
        end.to raise_error(ArgumentError, 'Invalid IPv6 address: [::1]:27017oh')
      end

      it 'rejects bogus data in brackets' do
        expect do
          described_class.parse('[::hello]:27017')
        end.to raise_error(ArgumentError, 'Invalid IPv6 address: [::hello]:27017')
      end
    end
  end

  describe '#initialize' do

    context 'when a port is provided' do

      let(:address) do
        '[::1]:27017'
      end

      it 'sets the port' do
        expect(resolver.port).to eq(27017)
      end

      it 'sets the host' do
        expect(resolver.host).to eq('::1')
      end
    end

    context 'when no port is provided' do

      let(:address) do
        '[::1]'
      end

      it 'sets the port to 27017' do
        expect(resolver.port).to eq(27017)
      end

      it 'sets the host' do
        expect(resolver.host).to eq('::1')
      end
    end
  end

  describe '#socket' do

    # In JRuby 9.3.2.0 Socket::PF_INET6 is nil, causing IPv6 tests to fail.
    # https://github.com/jruby/jruby/issues/7069
    # JRuby 9.2 works correctly, this test is skipped on all JRuby versions
    # because we intend to remove JRuby support altogether and therefore
    # adding logic to condition on JRuby versions does not make sense.
    fails_on_jruby

    let(:address) do
      '[::1]'
    end

    context 'when ssl options are provided' do

      let(:socket) do
        resolver.socket(5, :ssl => true)
      end

      it 'returns an ssl socket' do
        allow_any_instance_of(Mongo::Socket::SSL).to receive(:connect!)
        expect(socket).to be_a(Mongo::Socket::SSL)
      end

      it 'sets the family as ipv6' do
        allow_any_instance_of(Mongo::Socket::SSL).to receive(:connect!)
        expect(socket.family).to eq(Socket::PF_INET6)
      end
    end

    context 'when ssl options are not provided' do

      let(:socket) do
        resolver.socket(5)
      end

      it 'returns a tcp socket' do
        allow_any_instance_of(Mongo::Socket::TCP).to receive(:connect!)
        expect(socket).to be_a(Mongo::Socket::TCP)
      end

      it 'sets the family a ipv6' do
        allow_any_instance_of(Mongo::Socket::TCP).to receive(:connect!)
        expect(socket.family).to eq(Socket::PF_INET6)
      end
    end
  end
end