1717require 'tempfile'
1818
1919describe Localhost ::Authority do
20- def before
21- @old_root = File . expand_path ( "~/.localhost" )
22- @old_root_exists = File . directory? ( @old_root )
23-
24- if @old_root_exists
25- @tmp_folder = File . expand_path ( "~/.localhost_test" )
26- FileUtils . mkdir_p ( @tmp_folder , mode : 0700 )
27- FileUtils . cp_r ( "#{ @old_root } /." , @tmp_folder )
20+ def around
21+ Dir . mktmpdir do |path |
22+ @root = path
23+
24+ yield
25+ ensure
26+ @root = nil
2827 end
2928 end
30-
31- def after
32- if @old_root_exists
33- FileUtils . mkdir_p ( @old_root , mode : 0700 )
34- FileUtils . mv ( "#{ @tmp_folder } /." , @old_root , force : true )
35- FileUtils . rm_r ( @tmp_folder )
29+
30+ let ( :authority ) { subject . new ( "localhost" , root : @root ) }
31+
32+ with ".path" do
33+ it "uses XDG_STATE_HOME" do
34+ env = { 'XDG_STATE_HOME' => @root }
35+
36+ expect ( Localhost ::Authority . path ( env ) ) . to be == File . expand_path ( "localhost.rb" , @root )
37+ end
38+
39+ it "copies legacy directory" do
40+ xdg_state_home = File . join ( @root , ".local" , "state" )
41+ env = { 'XDG_STATE_HOME' => xdg_state_home }
42+
43+ old_root = File . join ( @root , ".localhost" )
44+ Dir . mkdir ( old_root )
45+ File . write ( File . join ( old_root , "localhost.crt" ) , "*fake certificate*" )
46+ File . write ( File . join ( old_root , "localhost.key" ) , "*fake key*" )
47+
48+ path = Localhost ::Authority . path ( env , old_root : old_root )
49+ expect ( path ) . to be == File . expand_path ( "localhost.rb" , xdg_state_home )
50+ expect ( File ) . to be ( :exist? , File . expand_path ( "localhost.crt" , path ) )
51+ expect ( File ) . to be ( :exist? , File . expand_path ( "localhost.key" , path ) )
52+
53+ expect ( File ) . not . to be ( :exist? , old_root )
3654 end
3755 end
38-
39- let ( :xdg_dir ) { File . join ( Dir . pwd , "state" ) }
40- let ( :authority ) {
41- ENV [ "XDG_STATE_HOME" ] = xdg_dir
42- subject . new
43- }
4456
4557 with '#certificate' do
4658 it "is not valid for more than 1 year" do
@@ -52,35 +64,15 @@ def after
5264 end
5365 end
5466
55- it "can generate key and certificate" do
56- Dir . mktmpdir ( 'localhost' ) do |dir |
57- authority . save ( dir )
58-
59- expect ( File ) . to be ( :exist? , File . expand_path ( "localhost.lock" , dir ) )
60- expect ( File ) . to be ( :exist? , File . expand_path ( "localhost.crt" , dir ) )
61- expect ( File ) . to be ( :exist? , File . expand_path ( "localhost.key" , dir ) )
62- end
63- end
64-
6567 it "have correct key and certificate path" do
66- authority . save ( authority . class . path )
67- expect ( File ) . to be ( :exist? , authority . certificate_path )
68- expect ( File ) . to be ( :exist? , authority . key_path )
68+ authority . save
6969
70- expect ( authority . key_path ) . to be == File . join ( xdg_dir , "localhost.rb" , "localhost.key" )
71- expect ( authority . certificate_path ) . to be == File . join ( xdg_dir , "localhost.rb" , "localhost.crt" )
72- end
73-
74- it "properly falls back when XDG_STATE_HOME is not set" do
75- ENV . delete ( "XDG_STATE_HOME" )
76- authority = subject . new
77-
78- authority . save ( authority . class . path )
7970 expect ( File ) . to be ( :exist? , authority . certificate_path )
8071 expect ( File ) . to be ( :exist? , authority . key_path )
8172
82- expect ( authority . key_path ) . to be == File . join ( File . expand_path ( "~/.local/state/" ) , "localhost.rb" , "localhost.key" )
83- expect ( authority . certificate_path ) . to be == File . join ( File . expand_path ( "~/.local/state/" ) , "localhost.rb" , "localhost.crt" )
73+ expect ( File ) . to be ( :exist? , File . expand_path ( "localhost.lock" , @root ) )
74+ expect ( File ) . to be ( :exist? , File . expand_path ( "localhost.crt" , @root ) )
75+ expect ( File ) . to be ( :exist? , File . expand_path ( "localhost.key" , @root ) )
8476 end
8577
8678 with '#store' do
@@ -94,40 +86,4 @@ def after
9486 expect ( authority . server_context ) . to be_a OpenSSL ::SSL ::SSLContext
9587 end
9688 end
97-
98- with 'client/server' do
99- include Sus ::Fixtures ::Async ::ReactorContext
100-
101- let ( :endpoint ) { Async ::IO ::Endpoint . tcp ( "localhost" , 4040 ) }
102- let ( :server_endpoint ) { Async ::IO ::SSLEndpoint . new ( endpoint , ssl_context : authority . server_context ) }
103- let ( :client_endpoint ) { Async ::IO ::SSLEndpoint . new ( endpoint , ssl_context : authority . client_context ) }
104-
105- let ( :client ) { client_endpoint . connect }
106-
107- def before
108- @bound_endpoint = Async ::IO ::SharedEndpoint . bound ( server_endpoint )
109-
110- @server_task = reactor . async do
111- @bound_endpoint . accept do |peer |
112- peer . write ( "Hello World!" )
113- peer . close
114- end
115- end
116-
117- super
118- end
119-
120- def after
121- @server_task &.stop
122- @bound_endpoint &.close
123-
124- super
125- end
126-
127- it "can verify peer" do
128- expect ( client . read ( 12 ) ) . to be == "Hello World!"
129-
130- client . close
131- end
132- end
13389end
0 commit comments