File: ssl_server_spec.rb

package info (click to toggle)
ruby-celluloid-io 0.16.2-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, stretch
  • size: 432 kB
  • ctags: 189
  • sloc: ruby: 1,727; makefile: 6
file content (91 lines) | stat: -rw-r--r-- 2,778 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
require 'spec_helper'

describe Celluloid::IO::SSLServer do
  let(:client_cert) { OpenSSL::X509::Certificate.new fixture_dir.join("client.crt").read }
  let(:client_key)  { OpenSSL::PKey::RSA.new fixture_dir.join("client.key").read }
  let(:client_context) do
    OpenSSL::SSL::SSLContext.new.tap do |context|
      context.cert = client_cert
      context.key  = client_key
    end
  end

  let(:server_cert) { OpenSSL::X509::Certificate.new fixture_dir.join("server.crt").read }
  let(:server_key)  { OpenSSL::PKey::RSA.new fixture_dir.join("server.key").read }
  let(:server_context) do
    OpenSSL::SSL::SSLContext.new.tap do |context|
      context.cert = server_cert
      context.key  = server_key
    end
  end

  describe "#accept" do
    let(:payload) { 'ohai' }

    context "inside Celluloid::IO" do
      it "should be evented" do
        with_ssl_server do |subject|
          within_io_actor { Celluloid::IO.evented? }.should be_truthy
        end
      end

      it "accepts a connection and returns a Celluloid::IO::SSLSocket" do
        with_ssl_server do |subject|
          thread = Thread.new do
            raw = TCPSocket.new(example_addr, example_ssl_port)
            OpenSSL::SSL::SSLSocket.new(raw, client_context).connect
          end
          peer = within_io_actor { subject.accept }
          peer.should be_a Celluloid::IO::SSLSocket

          client = thread.value
          client.write payload
          peer.read(payload.size).should eq payload
        end
      end
    end

    context "outside Celluloid::IO" do
      it "should be blocking" do
        with_ssl_server do |subject|
          Celluloid::IO.should_not be_evented
        end
      end

      it "accepts a connection and returns a Celluloid::IO::SSLSocket" do
        with_ssl_server do |subject|
          thread = Thread.new do
            raw = TCPSocket.new(example_addr, example_ssl_port)
            OpenSSL::SSL::SSLSocket.new(raw, client_context).connect
          end
          peer = subject.accept
          peer.should be_a Celluloid::IO::SSLSocket

          client = thread.value
          client.write payload
          peer.read(payload.size).should eq payload
        end
      end
    end
  end

  describe "#initialize" do
    it "should auto-wrap a raw ::TCPServer" do
      raw_server = ::TCPServer.new(example_addr, example_ssl_port)
      with_ssl_server(raw_server) do |ssl_server|
        ssl_server.tcp_server.class.should == Celluloid::IO::TCPServer
      end
    end
  end

  def with_ssl_server(raw_server = nil)
    raw_server ||= Celluloid::IO::TCPServer.new(example_addr, example_ssl_port)
    server = Celluloid::IO::SSLServer.new(raw_server, server_context)
    begin
      yield server
    ensure
      server.close
    end
  end
end