ruby_smb 1.0.5 → 2.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (191) hide show
  1. checksums.yaml +5 -5
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/.travis.yml +3 -2
  5. data/Gemfile +6 -2
  6. data/README.md +35 -47
  7. data/examples/anonymous_auth.rb +3 -3
  8. data/examples/append_file.rb +10 -8
  9. data/examples/authenticate.rb +9 -5
  10. data/examples/delete_file.rb +8 -6
  11. data/examples/enum_registry_key.rb +29 -0
  12. data/examples/enum_registry_values.rb +31 -0
  13. data/examples/list_directory.rb +8 -6
  14. data/examples/negotiate.rb +51 -8
  15. data/examples/negotiate_with_netbios_service.rb +9 -5
  16. data/examples/net_share_enum_all.rb +6 -4
  17. data/examples/pipes.rb +13 -13
  18. data/examples/query_service_status.rb +64 -0
  19. data/examples/read_file.rb +8 -6
  20. data/examples/read_file_encryption.rb +56 -0
  21. data/examples/read_registry_key_value.rb +33 -0
  22. data/examples/rename_file.rb +9 -7
  23. data/examples/tree_connect.rb +7 -5
  24. data/examples/write_file.rb +9 -7
  25. data/lib/ruby_smb.rb +4 -1
  26. data/lib/ruby_smb/client.rb +239 -21
  27. data/lib/ruby_smb/client/authentication.rb +27 -8
  28. data/lib/ruby_smb/client/encryption.rb +62 -0
  29. data/lib/ruby_smb/client/negotiation.rb +154 -12
  30. data/lib/ruby_smb/client/signing.rb +19 -0
  31. data/lib/ruby_smb/client/tree_connect.rb +4 -4
  32. data/lib/ruby_smb/client/utils.rb +8 -7
  33. data/lib/ruby_smb/client/winreg.rb +46 -0
  34. data/lib/ruby_smb/crypto.rb +30 -0
  35. data/lib/ruby_smb/dcerpc.rb +40 -0
  36. data/lib/ruby_smb/dcerpc/bind.rb +2 -2
  37. data/lib/ruby_smb/dcerpc/bind_ack.rb +2 -2
  38. data/lib/ruby_smb/dcerpc/error.rb +6 -0
  39. data/lib/ruby_smb/dcerpc/ndr.rb +260 -16
  40. data/lib/ruby_smb/dcerpc/pdu_header.rb +1 -1
  41. data/lib/ruby_smb/dcerpc/request.rb +41 -9
  42. data/lib/ruby_smb/dcerpc/rpc_security_attributes.rb +34 -0
  43. data/lib/ruby_smb/dcerpc/rrp_unicode_string.rb +38 -0
  44. data/lib/ruby_smb/dcerpc/srvsvc.rb +10 -0
  45. data/lib/ruby_smb/dcerpc/srvsvc/net_share_enum_all.rb +9 -0
  46. data/lib/ruby_smb/dcerpc/svcctl.rb +479 -0
  47. data/lib/ruby_smb/dcerpc/svcctl/change_service_config_w_request.rb +48 -0
  48. data/lib/ruby_smb/dcerpc/svcctl/change_service_config_w_response.rb +26 -0
  49. data/lib/ruby_smb/dcerpc/svcctl/close_service_handle_request.rb +25 -0
  50. data/lib/ruby_smb/dcerpc/svcctl/close_service_handle_response.rb +26 -0
  51. data/lib/ruby_smb/dcerpc/svcctl/control_service_request.rb +26 -0
  52. data/lib/ruby_smb/dcerpc/svcctl/control_service_response.rb +26 -0
  53. data/lib/ruby_smb/dcerpc/svcctl/open_sc_manager_w_request.rb +35 -0
  54. data/lib/ruby_smb/dcerpc/svcctl/open_sc_manager_w_response.rb +23 -0
  55. data/lib/ruby_smb/dcerpc/svcctl/open_service_w_request.rb +31 -0
  56. data/lib/ruby_smb/dcerpc/svcctl/open_service_w_response.rb +23 -0
  57. data/lib/ruby_smb/dcerpc/svcctl/query_service_config_w_request.rb +25 -0
  58. data/lib/ruby_smb/dcerpc/svcctl/query_service_config_w_response.rb +44 -0
  59. data/lib/ruby_smb/dcerpc/svcctl/query_service_status_request.rb +23 -0
  60. data/lib/ruby_smb/dcerpc/svcctl/query_service_status_response.rb +27 -0
  61. data/lib/ruby_smb/dcerpc/svcctl/service_status.rb +25 -0
  62. data/lib/ruby_smb/dcerpc/svcctl/start_service_w_request.rb +27 -0
  63. data/lib/ruby_smb/dcerpc/svcctl/start_service_w_response.rb +25 -0
  64. data/lib/ruby_smb/dcerpc/winreg.rb +421 -0
  65. data/lib/ruby_smb/dcerpc/winreg/close_key_request.rb +24 -0
  66. data/lib/ruby_smb/dcerpc/winreg/close_key_response.rb +27 -0
  67. data/lib/ruby_smb/dcerpc/winreg/create_key_request.rb +73 -0
  68. data/lib/ruby_smb/dcerpc/winreg/create_key_response.rb +36 -0
  69. data/lib/ruby_smb/dcerpc/winreg/enum_key_request.rb +45 -0
  70. data/lib/ruby_smb/dcerpc/winreg/enum_key_response.rb +42 -0
  71. data/lib/ruby_smb/dcerpc/winreg/enum_value_request.rb +39 -0
  72. data/lib/ruby_smb/dcerpc/winreg/enum_value_response.rb +36 -0
  73. data/lib/ruby_smb/dcerpc/winreg/open_key_request.rb +34 -0
  74. data/lib/ruby_smb/dcerpc/winreg/open_key_response.rb +25 -0
  75. data/lib/ruby_smb/dcerpc/winreg/open_root_key_request.rb +43 -0
  76. data/lib/ruby_smb/dcerpc/winreg/open_root_key_response.rb +35 -0
  77. data/lib/ruby_smb/dcerpc/winreg/query_info_key_request.rb +27 -0
  78. data/lib/ruby_smb/dcerpc/winreg/query_info_key_response.rb +40 -0
  79. data/lib/ruby_smb/dcerpc/winreg/query_value_request.rb +40 -0
  80. data/lib/ruby_smb/dcerpc/winreg/query_value_response.rb +57 -0
  81. data/lib/ruby_smb/dcerpc/winreg/regsam.rb +40 -0
  82. data/lib/ruby_smb/dcerpc/winreg/save_key_request.rb +37 -0
  83. data/lib/ruby_smb/dcerpc/winreg/save_key_response.rb +23 -0
  84. data/lib/ruby_smb/dispatcher/base.rb +1 -1
  85. data/lib/ruby_smb/dispatcher/socket.rb +5 -4
  86. data/lib/ruby_smb/error.rb +28 -1
  87. data/lib/ruby_smb/field/stringz16.rb +17 -1
  88. data/lib/ruby_smb/nbss/session_header.rb +4 -4
  89. data/lib/ruby_smb/smb1/commands.rb +1 -1
  90. data/lib/ruby_smb/smb1/file.rb +8 -14
  91. data/lib/ruby_smb/smb1/packet/session_setup_legacy_request.rb +1 -1
  92. data/lib/ruby_smb/smb1/packet/session_setup_legacy_response.rb +2 -2
  93. data/lib/ruby_smb/smb1/packet/session_setup_request.rb +1 -1
  94. data/lib/ruby_smb/smb1/packet/session_setup_response.rb +2 -2
  95. data/lib/ruby_smb/smb1/packet/write_andx_request.rb +1 -1
  96. data/lib/ruby_smb/smb1/pipe.rb +81 -3
  97. data/lib/ruby_smb/smb1/tree.rb +12 -3
  98. data/lib/ruby_smb/smb2/bit_field/session_flags.rb +2 -1
  99. data/lib/ruby_smb/smb2/bit_field/share_flags.rb +6 -4
  100. data/lib/ruby_smb/smb2/file.rb +51 -61
  101. data/lib/ruby_smb/smb2/negotiate_context.rb +108 -0
  102. data/lib/ruby_smb/smb2/packet.rb +2 -0
  103. data/lib/ruby_smb/smb2/packet/compression_transform_header.rb +41 -0
  104. data/lib/ruby_smb/smb2/packet/error_packet.rb +2 -4
  105. data/lib/ruby_smb/smb2/packet/negotiate_request.rb +51 -14
  106. data/lib/ruby_smb/smb2/packet/negotiate_response.rb +50 -4
  107. data/lib/ruby_smb/smb2/packet/transform_header.rb +84 -0
  108. data/lib/ruby_smb/smb2/packet/tree_connect_request.rb +92 -6
  109. data/lib/ruby_smb/smb2/packet/tree_connect_response.rb +8 -26
  110. data/lib/ruby_smb/smb2/pipe.rb +80 -3
  111. data/lib/ruby_smb/smb2/smb2_header.rb +1 -1
  112. data/lib/ruby_smb/smb2/tree.rb +32 -20
  113. data/lib/ruby_smb/version.rb +1 -1
  114. data/ruby_smb.gemspec +5 -3
  115. data/spec/lib/ruby_smb/client_spec.rb +1583 -102
  116. data/spec/lib/ruby_smb/crypto_spec.rb +25 -0
  117. data/spec/lib/ruby_smb/dcerpc/bind_ack_spec.rb +2 -2
  118. data/spec/lib/ruby_smb/dcerpc/bind_spec.rb +2 -2
  119. data/spec/lib/ruby_smb/dcerpc/ndr_spec.rb +1729 -0
  120. data/spec/lib/ruby_smb/dcerpc/request_spec.rb +50 -7
  121. data/spec/lib/ruby_smb/dcerpc/rpc_security_attributes_spec.rb +161 -0
  122. data/spec/lib/ruby_smb/dcerpc/rrp_unicode_string_spec.rb +135 -0
  123. data/spec/lib/ruby_smb/dcerpc/srvsvc/net_share_enum_all_spec.rb +13 -0
  124. data/spec/lib/ruby_smb/dcerpc/srvsvc_spec.rb +60 -0
  125. data/spec/lib/ruby_smb/dcerpc/svcctl/change_service_config_w_request_spec.rb +191 -0
  126. data/spec/lib/ruby_smb/dcerpc/svcctl/change_service_config_w_response_spec.rb +38 -0
  127. data/spec/lib/ruby_smb/dcerpc/svcctl/close_service_handle_request_spec.rb +30 -0
  128. data/spec/lib/ruby_smb/dcerpc/svcctl/close_service_handle_response_spec.rb +38 -0
  129. data/spec/lib/ruby_smb/dcerpc/svcctl/control_service_request_spec.rb +39 -0
  130. data/spec/lib/ruby_smb/dcerpc/svcctl/control_service_response_spec.rb +38 -0
  131. data/spec/lib/ruby_smb/dcerpc/svcctl/open_sc_manager_w_request_spec.rb +78 -0
  132. data/spec/lib/ruby_smb/dcerpc/svcctl/open_sc_manager_w_response_spec.rb +38 -0
  133. data/spec/lib/ruby_smb/dcerpc/svcctl/open_service_w_request_spec.rb +59 -0
  134. data/spec/lib/ruby_smb/dcerpc/svcctl/open_service_w_response_spec.rb +38 -0
  135. data/spec/lib/ruby_smb/dcerpc/svcctl/query_service_config_w_request_spec.rb +38 -0
  136. data/spec/lib/ruby_smb/dcerpc/svcctl/query_service_config_w_response_spec.rb +152 -0
  137. data/spec/lib/ruby_smb/dcerpc/svcctl/query_service_status_request_spec.rb +30 -0
  138. data/spec/lib/ruby_smb/dcerpc/svcctl/query_service_status_response_spec.rb +38 -0
  139. data/spec/lib/ruby_smb/dcerpc/svcctl/service_status_spec.rb +72 -0
  140. data/spec/lib/ruby_smb/dcerpc/svcctl/start_service_w_request_spec.rb +46 -0
  141. data/spec/lib/ruby_smb/dcerpc/svcctl/start_service_w_response_spec.rb +30 -0
  142. data/spec/lib/ruby_smb/dcerpc/svcctl_spec.rb +512 -0
  143. data/spec/lib/ruby_smb/dcerpc/winreg/close_key_request_spec.rb +28 -0
  144. data/spec/lib/ruby_smb/dcerpc/winreg/close_key_response_spec.rb +36 -0
  145. data/spec/lib/ruby_smb/dcerpc/winreg/create_key_request_spec.rb +110 -0
  146. data/spec/lib/ruby_smb/dcerpc/winreg/create_key_response_spec.rb +44 -0
  147. data/spec/lib/ruby_smb/dcerpc/winreg/enum_key_request_spec.rb +104 -0
  148. data/spec/lib/ruby_smb/dcerpc/winreg/enum_key_response_spec.rb +97 -0
  149. data/spec/lib/ruby_smb/dcerpc/winreg/enum_value_request_spec.rb +94 -0
  150. data/spec/lib/ruby_smb/dcerpc/winreg/enum_value_response_spec.rb +82 -0
  151. data/spec/lib/ruby_smb/dcerpc/winreg/open_key_request_spec.rb +74 -0
  152. data/spec/lib/ruby_smb/dcerpc/winreg/open_key_response_spec.rb +35 -0
  153. data/spec/lib/ruby_smb/dcerpc/winreg/open_root_key_request_spec.rb +95 -0
  154. data/spec/lib/ruby_smb/dcerpc/winreg/open_root_key_response_spec.rb +38 -0
  155. data/spec/lib/ruby_smb/dcerpc/winreg/query_info_key_request_spec.rb +35 -0
  156. data/spec/lib/ruby_smb/dcerpc/winreg/query_info_key_response_spec.rb +113 -0
  157. data/spec/lib/ruby_smb/dcerpc/winreg/query_value_request_spec.rb +88 -0
  158. data/spec/lib/ruby_smb/dcerpc/winreg/query_value_response_spec.rb +138 -0
  159. data/spec/lib/ruby_smb/dcerpc/winreg/regsam_spec.rb +32 -0
  160. data/spec/lib/ruby_smb/dcerpc/winreg/save_key_request_spec.rb +57 -0
  161. data/spec/lib/ruby_smb/dcerpc/winreg/save_key_response_spec.rb +22 -0
  162. data/spec/lib/ruby_smb/dcerpc/winreg_spec.rb +884 -0
  163. data/spec/lib/ruby_smb/dcerpc_spec.rb +81 -0
  164. data/spec/lib/ruby_smb/dispatcher/socket_spec.rb +12 -12
  165. data/spec/lib/ruby_smb/error_spec.rb +59 -0
  166. data/spec/lib/ruby_smb/field/stringz16_spec.rb +12 -0
  167. data/spec/lib/ruby_smb/nbss/session_header_spec.rb +4 -11
  168. data/spec/lib/ruby_smb/smb1/file_spec.rb +9 -1
  169. data/spec/lib/ruby_smb/smb1/packet/session_setup_legacy_request_spec.rb +2 -2
  170. data/spec/lib/ruby_smb/smb1/packet/session_setup_legacy_response_spec.rb +2 -2
  171. data/spec/lib/ruby_smb/smb1/packet/session_setup_request_spec.rb +2 -2
  172. data/spec/lib/ruby_smb/smb1/packet/session_setup_response_spec.rb +1 -1
  173. data/spec/lib/ruby_smb/smb1/pipe_spec.rb +216 -147
  174. data/spec/lib/ruby_smb/smb2/bit_field/session_flags_spec.rb +9 -0
  175. data/spec/lib/ruby_smb/smb2/bit_field/share_flags_spec.rb +27 -0
  176. data/spec/lib/ruby_smb/smb2/file_spec.rb +146 -68
  177. data/spec/lib/ruby_smb/smb2/negotiate_context_spec.rb +332 -0
  178. data/spec/lib/ruby_smb/smb2/packet/compression_transform_header_spec.rb +108 -0
  179. data/spec/lib/ruby_smb/smb2/packet/error_packet_spec.rb +3 -24
  180. data/spec/lib/ruby_smb/smb2/packet/negotiate_request_spec.rb +138 -3
  181. data/spec/lib/ruby_smb/smb2/packet/negotiate_response_spec.rb +120 -2
  182. data/spec/lib/ruby_smb/smb2/packet/transform_header_spec.rb +220 -0
  183. data/spec/lib/ruby_smb/smb2/packet/tree_connect_request_spec.rb +339 -9
  184. data/spec/lib/ruby_smb/smb2/packet/tree_connect_response_spec.rb +3 -30
  185. data/spec/lib/ruby_smb/smb2/pipe_spec.rb +226 -148
  186. data/spec/lib/ruby_smb/smb2/smb2_header_spec.rb +2 -2
  187. data/spec/lib/ruby_smb/smb2/tree_spec.rb +88 -9
  188. metadata +257 -81
  189. metadata.gz.sig +0 -0
  190. data/lib/ruby_smb/smb1/dcerpc.rb +0 -72
  191. data/lib/ruby_smb/smb2/dcerpc.rb +0 -75
@@ -5,6 +5,7 @@ RSpec.describe RubySMB::SMB2::BitField::SessionFlags do
5
5
 
6
6
  it { is_expected.to respond_to :guest }
7
7
  it { is_expected.to respond_to :null }
8
+ it { is_expected.to respond_to :encrypt_data }
8
9
 
9
10
  it 'is little endian' do
10
11
  expect(described_class.fields.instance_variable_get(:@hints)[:endian]).to eq :little
@@ -25,4 +26,12 @@ RSpec.describe RubySMB::SMB2::BitField::SessionFlags do
25
26
 
26
27
  it_behaves_like 'bit field with one flag set', :null, 'v', 0x00000002
27
28
  end
29
+
30
+ describe '#encrypt_data' do
31
+ it 'should be a 1-bit field per the SMB spec' do
32
+ expect(flags.encrypt_data).to be_a BinData::Bit1
33
+ end
34
+
35
+ it_behaves_like 'bit field with one flag set', :encrypt_data, 'v', 0x00000004
36
+ end
28
37
  end
@@ -1,6 +1,8 @@
1
1
  RSpec.describe RubySMB::SMB2::BitField::ShareFlags do
2
2
  subject(:flags) { described_class.new }
3
3
 
4
+ it { is_expected.to respond_to :vdo_caching }
5
+ it { is_expected.to respond_to :auto_caching }
4
6
  it { is_expected.to respond_to :dfs_root }
5
7
  it { is_expected.to respond_to :dfs }
6
8
  it { is_expected.to respond_to :encrypt }
@@ -11,11 +13,28 @@ RSpec.describe RubySMB::SMB2::BitField::ShareFlags do
11
13
  it { is_expected.to respond_to :namespace_caching }
12
14
  it { is_expected.to respond_to :shared_delete }
13
15
  it { is_expected.to respond_to :restrict_exclusive_opens }
16
+ it { is_expected.to respond_to :identity_remoting }
14
17
 
15
18
  it 'is little endian' do
16
19
  expect(described_class.fields.instance_variable_get(:@hints)[:endian]).to eq :little
17
20
  end
18
21
 
22
+ describe '#vdo_caching' do
23
+ it 'should be a 1-bit field per the SMB spec' do
24
+ expect(flags.vdo_caching).to be_a BinData::Bit1
25
+ end
26
+
27
+ it_behaves_like 'bit field with one flag set', :vdo_caching, 'V', 0x00000020
28
+ end
29
+
30
+ describe '#auto_caching' do
31
+ it 'should be a 1-bit field per the SMB spec' do
32
+ expect(flags.auto_caching).to be_a BinData::Bit1
33
+ end
34
+
35
+ it_behaves_like 'bit field with one flag set', :auto_caching, 'V', 0x00000010
36
+ end
37
+
19
38
  describe '#dfs' do
20
39
  it 'should be a 1-bit field per the SMB spec' do
21
40
  expect(flags.dfs).to be_a BinData::Bit1
@@ -96,6 +115,14 @@ RSpec.describe RubySMB::SMB2::BitField::ShareFlags do
96
115
  it_behaves_like 'bit field with one flag set', :encrypt, 'V', 0x00008000
97
116
  end
98
117
 
118
+ describe '#identity_remoting' do
119
+ it 'should be a 1-bit field per the SMB spec' do
120
+ expect(flags.identity_remoting).to be_a BinData::Bit1
121
+ end
122
+
123
+ it_behaves_like 'bit field with one flag set', :identity_remoting, 'V', 0x00040000
124
+ end
125
+
99
126
  describe '#set_manual_caching' do
100
127
  it 'turns off the caching bits' do
101
128
  flags.set_manual_caching
@@ -43,6 +43,7 @@ RSpec.describe RubySMB::SMB2::File do
43
43
  it { is_expected.to respond_to :size }
44
44
  it { is_expected.to respond_to :size_on_disk }
45
45
  it { is_expected.to respond_to :tree }
46
+ it { is_expected.to respond_to :tree_connect_encrypt_data }
46
47
 
47
48
  it 'pulls the attributes from the response packet' do
48
49
  expect(file.attributes).to eq create_response.file_attributes
@@ -52,10 +53,18 @@ RSpec.describe RubySMB::SMB2::File do
52
53
  expect(file.guid).to eq create_response.file_id
53
54
  end
54
55
 
55
- it 'pulls the timestamps from the response packet' do
56
+ it 'pulls the last access timestamps from the response packet' do
56
57
  expect(file.last_access).to eq create_response.last_access.to_datetime
57
58
  end
58
59
 
60
+ it 'pulls the last change timestamps from the response packet' do
61
+ expect(file.last_change).to eq create_response.last_change.to_datetime
62
+ end
63
+
64
+ it 'pulls the last write timestamps from the response packet' do
65
+ expect(file.last_write).to eq create_response.last_write.to_datetime
66
+ end
67
+
59
68
  it 'pulls the size from the response packet' do
60
69
  expect(file.size).to eq create_response.end_of_file
61
70
  end
@@ -64,6 +73,10 @@ RSpec.describe RubySMB::SMB2::File do
64
73
  expect(file.size_on_disk).to eq create_response.allocation_size
65
74
  end
66
75
 
76
+ it 'sets the tree_connect_encrypt_data flag to false by default' do
77
+ expect(file.tree_connect_encrypt_data).to be false
78
+ end
79
+
67
80
  describe '#set_header_fields' do
68
81
  let(:request) { RubySMB::SMB2::Packet::ReadRequest.new }
69
82
  it 'calls the set_header_field method from the Tree' do
@@ -94,6 +107,10 @@ RSpec.describe RubySMB::SMB2::File do
94
107
  it 'sets the offset of the packet' do
95
108
  expect(file.read_packet(offset: 55).offset).to eq 55
96
109
  end
110
+
111
+ it 'sets the credit_charge of the packet' do
112
+ expect(file.read_packet(credit_charge: 3).smb2_header.credit_charge).to eq 3
113
+ end
97
114
  end
98
115
 
99
116
  describe '#read' do
@@ -112,12 +129,18 @@ RSpec.describe RubySMB::SMB2::File do
112
129
  end
113
130
 
114
131
  it 'uses a single packet to read the entire file' do
115
- expect(file).to receive(:read_packet).with(read_length: 108, offset: 0).and_return(small_read)
116
- expect(client).to receive(:send_recv).with(small_read).and_return 'fake data'
132
+ expect(file).to receive(:read_packet).with(read_length: 108, offset: 0, credit_charge: 0).and_return(small_read)
133
+ expect(client).to receive(:send_recv).with(small_read, encrypt: false).and_return 'fake data'
117
134
  expect(RubySMB::SMB2::Packet::ReadResponse).to receive(:read).with('fake data').and_return(small_response)
118
135
  expect(file.read).to eq 'fake data'
119
136
  end
120
137
 
138
+ it 'calls Client #send_recv with encryption set if required' do
139
+ file.tree_connect_encrypt_data = true
140
+ expect(client).to receive(:send_recv).with(small_read, encrypt: true)
141
+ file.read
142
+ end
143
+
121
144
  context 'when the response is not valid' do
122
145
  it 'raise an InvalidPacket exception' do
123
146
  small_response.smb2_header.command = RubySMB::SMB2::Commands::LOGOFF
@@ -142,22 +165,31 @@ RSpec.describe RubySMB::SMB2::File do
142
165
  }
143
166
 
144
167
  before :example do
168
+ client.server_supports_multi_credit = 1
145
169
  allow(file).to receive(:read_packet)
146
170
  allow(client).to receive(:send_recv)
147
171
  allow(RubySMB::SMB2::Packet::ReadResponse).to receive(:read).and_return(big_response)
148
172
  end
149
173
 
150
- it 'uses a multiple packet to read the file in chunks' do
151
- expect(file).to receive(:read_packet).once.with(read_length: described_class::MAX_PACKET_SIZE, offset: 0).and_return(big_read)
152
- expect(file).to receive(:read_packet).once.with(read_length: described_class::MAX_PACKET_SIZE, offset: described_class::MAX_PACKET_SIZE).and_return(big_read)
174
+ it 'uses multiple packets to read the file in chunks' do
175
+ expect(file).to receive(:read_packet).once.with(read_length: described_class::MAX_PACKET_SIZE, offset: 0, credit_charge: 1).and_return(big_read)
176
+ expect(file).to receive(:read_packet).once.with(read_length: described_class::MAX_PACKET_SIZE, offset: described_class::MAX_PACKET_SIZE, credit_charge: 1).and_return(big_read)
153
177
  expect(client).to receive(:send_recv).twice.and_return 'fake data'
154
178
  expect(RubySMB::SMB2::Packet::ReadResponse).to receive(:read).twice.with('fake data').and_return(big_response)
155
179
  file.read(bytes: (described_class::MAX_PACKET_SIZE * 2))
156
180
  end
157
181
 
182
+ it 'calls Client #send_recv with encryption set if required' do
183
+ read_request = double('Read Request')
184
+ allow(file).to receive(:read_packet).and_return(read_request)
185
+ file.tree_connect_encrypt_data = true
186
+ expect(client).to receive(:send_recv).twice.with(read_request, encrypt: true)
187
+ file.read(bytes: (described_class::MAX_PACKET_SIZE * 2))
188
+ end
189
+
158
190
  context 'when the second response is not valid' do
159
191
  it 'raise an InvalidPacket exception' do
160
- allow(file).to receive(:read_packet).with(read_length: described_class::MAX_PACKET_SIZE, offset: described_class::MAX_PACKET_SIZE) do
192
+ allow(file).to receive(:read_packet).with(read_length: described_class::MAX_PACKET_SIZE, offset: described_class::MAX_PACKET_SIZE, credit_charge: 1) do
161
193
  big_response.smb2_header.command = RubySMB::SMB2::Commands::LOGOFF
162
194
  end
163
195
  expect { file.read(bytes: (described_class::MAX_PACKET_SIZE * 2)) }.to raise_error(RubySMB::Error::InvalidPacket)
@@ -166,12 +198,31 @@ RSpec.describe RubySMB::SMB2::File do
166
198
 
167
199
  context 'when the second response status code is not STATUS_SUCCESS' do
168
200
  it 'raise an UnexpectedStatusCode exception' do
169
- allow(file).to receive(:read_packet).with(read_length: described_class::MAX_PACKET_SIZE, offset: described_class::MAX_PACKET_SIZE) do
201
+ allow(file).to receive(:read_packet).with(read_length: described_class::MAX_PACKET_SIZE, offset: described_class::MAX_PACKET_SIZE, credit_charge: 1) do
170
202
  big_response.smb2_header.nt_status = WindowsError::NTStatus::STATUS_INVALID_HANDLE.value
171
203
  end
172
204
  expect { file.read(bytes: (described_class::MAX_PACKET_SIZE * 2)) }.to raise_error(RubySMB::Error::UnexpectedStatusCode)
173
205
  end
174
206
  end
207
+
208
+ context 'when the server does not support multi credits' do
209
+ it 'reads 65536 bytes at a time with no credit charge' do
210
+ client.server_supports_multi_credit = false
211
+ expect(file).to receive(:read_packet).once.with(read_length: 65536, offset: 0, credit_charge: 0).and_return(big_read)
212
+ expect(file).to receive(:read_packet).once.with(read_length: 65536, offset: 65536, credit_charge: 0).and_return(big_read)
213
+ file.read(bytes: (described_class::MAX_PACKET_SIZE * 4))
214
+ end
215
+ end
216
+
217
+ context 'when the server supports multi credits' do
218
+ it 'reads a number of bytes equal to #server_max_read_size, with the expected credit charge' do
219
+ credit_charge = (90000 - 1) / 65536 + 1
220
+ client.server_max_read_size = 90000
221
+ expect(file).to receive(:read_packet).once.with(read_length: 90000, offset: 0, credit_charge: credit_charge).and_return(big_read)
222
+ expect(file).to receive(:read_packet).once.with(read_length: (described_class::MAX_PACKET_SIZE * 4 - 90000), offset: 90000, credit_charge: credit_charge).and_return(big_read)
223
+ file.read(bytes: (described_class::MAX_PACKET_SIZE * 4))
224
+ end
225
+ end
175
226
  end
176
227
  end
177
228
 
@@ -195,6 +246,10 @@ RSpec.describe RubySMB::SMB2::File do
195
246
  it 'sets the buffer on the packet' do
196
247
  expect(file.write_packet(data:'hello').buffer).to eq 'hello'
197
248
  end
249
+
250
+ it 'sets the credit_charge on the packet header' do
251
+ expect(file.write_packet(credit_charge: 5).smb2_header.credit_charge).to eq 5
252
+ end
198
253
  end
199
254
 
200
255
  describe '#write' do
@@ -204,13 +259,55 @@ RSpec.describe RubySMB::SMB2::File do
204
259
  expect(client).to receive(:send_recv).once.and_return(write_response.to_binary_s)
205
260
  file.write(data: 'test')
206
261
  end
262
+
263
+ it 'calls Client #send_recv with encryption set if required' do
264
+ write_request = double('Write Request')
265
+ allow(file).to receive(:write_packet).and_return(write_request)
266
+ file.tree_connect_encrypt_data = true
267
+ expect(client).to receive(:send_recv).once.with(write_request, encrypt: true).and_return(write_response.to_binary_s)
268
+ file.write(data: 'test')
269
+ end
207
270
  end
208
271
 
209
272
  context 'for a large write' do
273
+ before :example do
274
+ allow(client).to receive(:send_recv).and_return(write_response.to_binary_s)
275
+ client.server_supports_multi_credit = 1
276
+ end
277
+
210
278
  it 'sends multiple packets' do
211
279
  expect(client).to receive(:send_recv).twice.and_return(write_response.to_binary_s)
212
280
  file.write(data: SecureRandom.random_bytes(described_class::MAX_PACKET_SIZE + 1))
213
281
  end
282
+
283
+ it 'calls Client #send_recv with encryption set if required' do
284
+ write_request = double('Write Request')
285
+ allow(file).to receive(:write_packet).and_return(write_request)
286
+ file.tree_connect_encrypt_data = true
287
+ expect(client).to receive(:send_recv).twice.with(write_request, encrypt: true).and_return(write_response.to_binary_s)
288
+ file.write(data: SecureRandom.random_bytes(described_class::MAX_PACKET_SIZE + 1))
289
+ end
290
+
291
+ context 'when the server does not support multi credits' do
292
+ it 'writes 65536 bytes at a time with no credit charge' do
293
+ client.server_supports_multi_credit = false
294
+ data = SecureRandom.random_bytes(65536 * 2)
295
+ expect(file).to receive(:write_packet).once.with(data: data[0, 65536], offset: 0, credit_charge: 0)
296
+ expect(file).to receive(:write_packet).once.with(data: data[65536, (data.size - 65536)], offset: 65536, credit_charge: 0)
297
+ file.write(data: data)
298
+ end
299
+ end
300
+
301
+ context 'when the server supports multi credits' do
302
+ it 'reads a number of bytes equal to #server_max_write_size, with the expected credit charge' do
303
+ data = SecureRandom.random_bytes(90000 * 2)
304
+ credit_charge = (90000 - 1) / 65536 + 1
305
+ client.server_max_write_size = 90000
306
+ expect(file).to receive(:write_packet).once.with(data: data[0, 90000], offset: 0, credit_charge: credit_charge)
307
+ expect(file).to receive(:write_packet).once.with(data: data[90000, (data.size - 90000)], offset: 90000, credit_charge: credit_charge)
308
+ file.write(data: data)
309
+ end
310
+ end
214
311
  end
215
312
 
216
313
  it 'raises an InvalidPacket exception if the response is not valid' do
@@ -248,7 +345,7 @@ RSpec.describe RubySMB::SMB2::File do
248
345
 
249
346
  it 'uses a single packet to delete the entire file' do
250
347
  expect(file).to receive(:delete_packet).and_return(small_delete)
251
- expect(client).to receive(:send_recv).with(small_delete).and_return 'raw_response'
348
+ expect(client).to receive(:send_recv).with(small_delete, encrypt: false).and_return 'raw_response'
252
349
  expect(RubySMB::SMB2::Packet::SetInfoResponse).to receive(:read).with('raw_response').and_return(small_response)
253
350
  expect(file.delete).to eq WindowsError::NTStatus::STATUS_SUCCESS
254
351
  end
@@ -260,6 +357,14 @@ RSpec.describe RubySMB::SMB2::File do
260
357
  allow(small_response).to receive(:valid?).and_return(false)
261
358
  expect { file.delete }.to raise_error(RubySMB::Error::InvalidPacket)
262
359
  end
360
+
361
+ it 'calls Client #send_recv with encryption set if required' do
362
+ allow(file).to receive(:delete_packet)
363
+ allow(RubySMB::SMB2::Packet::SetInfoResponse).to receive(:read).and_return(small_response)
364
+ file.tree_connect_encrypt_data = true
365
+ expect(client).to receive(:send_recv).with(small_delete, encrypt: true)
366
+ file.delete
367
+ end
263
368
  end
264
369
  end
265
370
 
@@ -291,11 +396,18 @@ RSpec.describe RubySMB::SMB2::File do
291
396
 
292
397
  it 'uses a single packet to rename the entire file' do
293
398
  expect(file).to receive(:rename_packet).and_return(small_rename)
294
- expect(client).to receive(:send_recv).with(small_rename).and_return 'raw_response'
399
+ expect(client).to receive(:send_recv).with(small_rename, encrypt: false).and_return 'raw_response'
295
400
  expect(RubySMB::SMB2::Packet::SetInfoResponse).to receive(:read).with('raw_response').and_return(small_response)
296
401
  expect(file.rename('new_file.txt')).to eq WindowsError::NTStatus::STATUS_SUCCESS
297
402
  end
298
403
 
404
+ it 'calls Client #send_recv with encryption set if required' do
405
+ allow(RubySMB::SMB2::Packet::SetInfoResponse).to receive(:read).and_return(small_response)
406
+ file.tree_connect_encrypt_data = true
407
+ expect(client).to receive(:send_recv).with(small_rename, encrypt: true)
408
+ file.rename('new_file.txt')
409
+ end
410
+
299
411
  it 'raises an InvalidPacket exception if the response is not valid' do
300
412
  allow(client).to receive(:send_recv)
301
413
  allow(RubySMB::SMB2::Packet::SetInfoResponse).to receive(:read).and_return(small_response)
@@ -330,7 +442,13 @@ RSpec.describe RubySMB::SMB2::File do
330
442
  end
331
443
 
332
444
  it 'calls Client #send_recv with the expected request' do
333
- expect(client).to receive(:send_recv).with(request)
445
+ expect(client).to receive(:send_recv).with(request, encrypt: false)
446
+ file.close
447
+ end
448
+
449
+ it 'calls Client #send_recv with encryption set if required' do
450
+ file.tree_connect_encrypt_data = true
451
+ expect(client).to receive(:send_recv).with(request, encrypt: true)
334
452
  file.close
335
453
  end
336
454
 
@@ -394,7 +512,15 @@ RSpec.describe RubySMB::SMB2::File do
394
512
  it 'calls Client #send_recv with the expected request' do
395
513
  request = double('Request')
396
514
  allow(file).to receive(:read_packet).and_return(request)
397
- expect(client).to receive(:send_recv).with(request)
515
+ expect(client).to receive(:send_recv).with(request, encrypt: false)
516
+ file.send_recv_read
517
+ end
518
+
519
+ it 'calls Client #send_recv with encryption set if required' do
520
+ request = double('Request')
521
+ allow(file).to receive(:read_packet).and_return(request)
522
+ file.tree_connect_encrypt_data = true
523
+ expect(client).to receive(:send_recv).with(request, encrypt: true)
398
524
  file.send_recv_read
399
525
  end
400
526
 
@@ -408,33 +534,6 @@ RSpec.describe RubySMB::SMB2::File do
408
534
  expect { file.send_recv_read }.to raise_error(RubySMB::Error::InvalidPacket)
409
535
  end
410
536
 
411
- context 'when the response status code is STATUS_PENDING' do
412
- before :example do
413
- allow(file).to receive(:sleep)
414
- allow(read_response).to receive(:status_code).and_return(WindowsError::NTStatus::STATUS_PENDING)
415
- allow(dispatcher).to receive(:recv_packet).and_return(raw_response)
416
- end
417
-
418
- it 'wait 1 second and calls Client dispatcher #recv_packet method one more time' do
419
- expect(file).to receive(:sleep).with(1)
420
- expect(dispatcher).to receive(:recv_packet)
421
- file.send_recv_read
422
- end
423
-
424
- it 'parses the response as a SMB2 ReadResponse packet' do
425
- expect(RubySMB::SMB2::Packet::ReadResponse).to receive(:read).twice.with(raw_response)
426
- file.send_recv_read
427
- end
428
-
429
- it 'raises an InvalidPacket exception if the response is not valid' do
430
- allow(dispatcher).to receive(:recv_packet) do
431
- allow(read_response).to receive(:valid?).and_return(false)
432
- raw_response
433
- end
434
- expect { file.send_recv_read }.to raise_error(RubySMB::Error::InvalidPacket)
435
- end
436
- end
437
-
438
537
  it 'raises an UnexpectedStatusCode exception if the response status code is not STATUS_SUCCESS' do
439
538
  allow(read_response).to receive(:status_code).and_return(WindowsError::NTStatus::STATUS_OBJECT_NAME_NOT_FOUND)
440
539
  expect { file.send_recv_read }.to raise_error(RubySMB::Error::UnexpectedStatusCode)
@@ -457,7 +556,7 @@ RSpec.describe RubySMB::SMB2::File do
457
556
 
458
557
  before :example do
459
558
  allow(file).to receive(:write_packet).and_return(request)
460
- allow(client).to receive(:send_recv).and_return(raw_response)
559
+ allow(client).to receive(:send_recv).and_return(raw_response, encrypt: false)
461
560
  allow(RubySMB::SMB2::Packet::WriteResponse).to receive(:read).with(raw_response).and_return(write_response)
462
561
  end
463
562
 
@@ -478,40 +577,19 @@ RSpec.describe RubySMB::SMB2::File do
478
577
  end
479
578
 
480
579
  it 'calls Client #send_recv with the expected request' do
481
- expect(client).to receive(:send_recv).with(request)
580
+ expect(client).to receive(:send_recv).with(request, encrypt: false)
482
581
  file.send_recv_write
483
582
  end
484
583
 
485
- it 'parses the response as a SMB1 WriteResponse packet' do
486
- expect(RubySMB::SMB2::Packet::WriteResponse).to receive(:read).with(raw_response)
584
+ it 'calls Client #send_recv with encryption set if required' do
585
+ file.tree_connect_encrypt_data = true
586
+ expect(client).to receive(:send_recv).with(request, encrypt: true)
487
587
  file.send_recv_write
488
588
  end
489
589
 
490
- context 'when the response status code is STATUS_PENDING' do
491
- before :example do
492
- allow(file).to receive(:sleep)
493
- allow(write_response).to receive(:status_code).and_return(WindowsError::NTStatus::STATUS_PENDING)
494
- allow(dispatcher).to receive(:recv_packet).and_return(raw_response)
495
- end
496
-
497
- it 'wait 1 second and calls Client dispatcher #recv_packet method one more time' do
498
- expect(file).to receive(:sleep).with(1)
499
- expect(dispatcher).to receive(:recv_packet)
500
- file.send_recv_write
501
- end
502
-
503
- it 'parses the response as a SMB2 WriteResponse packet' do
504
- expect(RubySMB::SMB2::Packet::WriteResponse).to receive(:read).twice.with(raw_response)
505
- file.send_recv_write
506
- end
507
-
508
- it 'raises an InvalidPacket exception if the response is not valid' do
509
- allow(dispatcher).to receive(:recv_packet) do
510
- allow(write_response).to receive(:valid?).and_return(false)
511
- raw_response
512
- end
513
- expect { file.send_recv_write }.to raise_error(RubySMB::Error::InvalidPacket)
514
- end
590
+ it 'parses the response as a SMB1 WriteResponse packet' do
591
+ expect(RubySMB::SMB2::Packet::WriteResponse).to receive(:read).with(raw_response)
592
+ file.send_recv_write
515
593
  end
516
594
 
517
595
  it 'raises an InvalidPacket exception if the response is not valid' do
@@ -0,0 +1,332 @@
1
+ RSpec.describe RubySMB::SMB2::PreauthIntegrityCapabilities do
2
+ subject(:capability) { described_class.new }
3
+
4
+ it { is_expected.to respond_to :hash_algorithm_count }
5
+ it { is_expected.to respond_to :salt_length }
6
+ it { is_expected.to respond_to :hash_algorithms }
7
+ it { is_expected.to respond_to :salt }
8
+
9
+ it 'is little endian' do
10
+ expect(described_class.fields.instance_variable_get(:@hints)[:endian]).to eq :little
11
+ end
12
+
13
+ describe '#hash_algorithm_count' do
14
+ it 'is a 16-bit unsigned integer' do
15
+ expect(capability.hash_algorithm_count).to be_a BinData::Uint16le
16
+ end
17
+
18
+ it 'is set to the #hash_algorithms array size' do
19
+ array = [1, 2, 3]
20
+ capability.hash_algorithms = array
21
+ expect(capability.hash_algorithm_count).to eq(array.size)
22
+ end
23
+ end
24
+
25
+ describe '#salt_length' do
26
+ it 'is a 16-bit unsigned integer' do
27
+ expect(capability.salt_length).to be_a BinData::Uint16le
28
+ end
29
+
30
+ it 'is set to the #salt string size' do
31
+ salt = 'my_random_salt'
32
+ capability.salt = salt
33
+ expect(capability.salt_length).to eq(salt.size)
34
+ end
35
+ end
36
+
37
+ describe '#hash_algorithms' do
38
+ it 'is a BinData Array' do
39
+ expect(capability.hash_algorithms).to be_a BinData::Array
40
+ end
41
+
42
+ it 'has #hash_algorithm_count elements' do
43
+ capability.hash_algorithm_count = 3
44
+ expect(capability.hash_algorithms.size).to eq 3
45
+ end
46
+ end
47
+
48
+ describe '#salt' do
49
+ it 'is a string' do
50
+ expect(capability.salt).to be_a BinData::String
51
+ end
52
+
53
+ it 'should read #salt_length bytes' do
54
+ salt = 'my_random_salt'
55
+ capability.salt_length = 5
56
+ expect(capability.salt.read(salt)).to eq(salt[0,5])
57
+ end
58
+ end
59
+
60
+ it 'reads binary data as expected' do
61
+ data = described_class.new(
62
+ hash_algorithms: [described_class::SHA_512],
63
+ salt: 'test salt'
64
+ )
65
+ expect(described_class.read(data.to_binary_s)).to eq(data)
66
+ end
67
+ end
68
+
69
+ RSpec.describe RubySMB::SMB2::EncryptionCapabilities do
70
+ subject(:capability) { described_class.new }
71
+
72
+ it { is_expected.to respond_to :cipher_count }
73
+ it { is_expected.to respond_to :ciphers }
74
+
75
+ it 'is little endian' do
76
+ expect(described_class.fields.instance_variable_get(:@hints)[:endian]).to eq :little
77
+ end
78
+
79
+ describe '#cipher_count' do
80
+ it 'is a 16-bit unsigned integer' do
81
+ expect(capability.cipher_count).to be_a BinData::Uint16le
82
+ end
83
+
84
+ it 'is set to the #ciphers array size' do
85
+ array = [1, 2, 3]
86
+ capability.ciphers = array
87
+ expect(capability.cipher_count).to eq(array.size)
88
+ end
89
+ end
90
+
91
+ describe '#ciphers' do
92
+ it 'is a BinData Array' do
93
+ expect(capability.ciphers).to be_a BinData::Array
94
+ end
95
+
96
+ it 'has #cipher_count elements' do
97
+ capability.cipher_count = 3
98
+ expect(capability.ciphers.size).to eq 3
99
+ end
100
+ end
101
+
102
+ it 'reads binary data as expected' do
103
+ data = described_class.new(
104
+ ciphers: [described_class::AES_128_CCM, described_class::AES_128_GCM]
105
+ )
106
+ expect(described_class.read(data.to_binary_s)).to eq(data)
107
+ end
108
+ end
109
+
110
+ RSpec.describe RubySMB::SMB2::CompressionCapabilities do
111
+ subject(:capability) { described_class.new }
112
+
113
+ it { is_expected.to respond_to :compression_algorithm_count }
114
+ it { is_expected.to respond_to :padding }
115
+ it { is_expected.to respond_to :flags }
116
+ it { is_expected.to respond_to :compression_algorithms }
117
+
118
+ it 'is little endian' do
119
+ expect(described_class.fields.instance_variable_get(:@hints)[:endian]).to eq :little
120
+ end
121
+
122
+ describe '#compression_algorithm_count' do
123
+ it 'is a 16-bit unsigned integer' do
124
+ expect(capability.compression_algorithm_count).to be_a BinData::Uint16le
125
+ end
126
+
127
+ it 'is set to the #compression_algorithms array size' do
128
+ array = [1, 2, 3]
129
+ capability.compression_algorithms = array
130
+ expect(capability.compression_algorithm_count).to eq(array.size)
131
+ end
132
+ end
133
+
134
+ describe '#padding' do
135
+ it 'is a 16-bit unsigned integer' do
136
+ expect(capability.padding).to be_a BinData::Uint16le
137
+ end
138
+ end
139
+
140
+ describe '#flags' do
141
+ it 'is a 32-bit unsigned integer' do
142
+ expect(capability.flags).to be_a BinData::Uint32le
143
+ end
144
+ end
145
+
146
+ describe '#compression_algorithms' do
147
+ it 'is a BinData Array' do
148
+ expect(capability.compression_algorithms).to be_a BinData::Array
149
+ end
150
+
151
+ it 'has #compression_algorithm_count elements' do
152
+ capability.compression_algorithm_count = 3
153
+ expect(capability.compression_algorithms.size).to eq 3
154
+ end
155
+ end
156
+
157
+ it 'reads binary data as expected' do
158
+ data = described_class.new(
159
+ compression_algorithms: [described_class::LZNT1, described_class::LZ77]
160
+ )
161
+ expect(described_class.read(data.to_binary_s)).to eq(data)
162
+ end
163
+ end
164
+
165
+ RSpec.describe RubySMB::SMB2::NetnameNegotiateContextId do
166
+ subject(:capability) { described_class.new }
167
+
168
+ it { is_expected.to respond_to :net_name }
169
+
170
+ it 'is little endian' do
171
+ expect(described_class.fields.instance_variable_get(:@hints)[:endian]).to eq :little
172
+ end
173
+
174
+ describe '#net_name' do
175
+ it 'is a unicode string' do
176
+ expect(capability.net_name).to be_a RubySMB::Field::Stringz16
177
+ end
178
+ end
179
+
180
+ it 'reads binary data as expected' do
181
+ data = described_class.new(
182
+ net_name: 'netname test'
183
+ )
184
+ expect(described_class.read(data.to_binary_s)).to eq(data)
185
+ end
186
+ end
187
+
188
+ RSpec.describe RubySMB::SMB2::NegotiateContext do
189
+ class FakePacket < BinData::Record
190
+ endian :little
191
+ string :garbage
192
+ negotiate_context :nc
193
+ end
194
+
195
+ let(:test_packet) do
196
+ packet = FakePacket.new
197
+ packet.nc.context_type = described_class::SMB2_PREAUTH_INTEGRITY_CAPABILITIES
198
+ packet
199
+ end
200
+ subject(:negotiate_context) { described_class.new }
201
+
202
+ it { is_expected.to respond_to :pad }
203
+ it { is_expected.to respond_to :context_type }
204
+ it { is_expected.to respond_to :data_length }
205
+ it { is_expected.to respond_to :reserved }
206
+ it { is_expected.to respond_to :data }
207
+
208
+ it 'is little endian' do
209
+ expect(described_class.fields.instance_variable_get(:@hints)[:endian]).to eq :little
210
+ end
211
+
212
+ describe '#pad' do
213
+ it 'is a string' do
214
+ expect(negotiate_context.pad).to be_a BinData::String
215
+ end
216
+
217
+ it 'should keep the #context_type 8-byte aligned' do
218
+ test_packet.garbage = 'foo'
219
+ expect(test_packet.nc.context_type.abs_offset % 8).to eq(0)
220
+ end
221
+ end
222
+
223
+ describe '#context_type ' do
224
+ it 'is a 16-bit unsigned integer' do
225
+ expect(negotiate_context.context_type).to be_a BinData::Uint16le
226
+ end
227
+ end
228
+
229
+ describe '#data_length' do
230
+ it 'is a 16-bit unsigned integer' do
231
+ expect(negotiate_context.data_length).to be_a BinData::Uint16le
232
+ end
233
+
234
+ it 'should give the #data field length in bytes' do
235
+ expect(described_class.new(context_type: described_class::SMB2_ENCRYPTION_CAPABILITIES).data_length)
236
+ .to eq(RubySMB::SMB2::EncryptionCapabilities.new.num_bytes)
237
+ end
238
+ end
239
+
240
+ describe '#data' do
241
+ it 'is a BinData choice field' do
242
+ expect(negotiate_context.data).to be_a BinData::Choice
243
+ end
244
+
245
+ context 'with a SMB2_PREAUTH_INTEGRITY_CAPABILITIES context type' do
246
+ it 'selects the PreauthIntegrityCapabilities structure' do
247
+ expect(described_class.new(context_type: described_class::SMB2_PREAUTH_INTEGRITY_CAPABILITIES).data)
248
+ .to eq(RubySMB::SMB2::PreauthIntegrityCapabilities.new)
249
+ end
250
+ end
251
+
252
+ context 'with a SMB2_ENCRYPTION_CAPABILITIES context type' do
253
+ it 'selects the PreauthIntegrityCapabilities structure' do
254
+ expect(described_class.new(context_type: described_class::SMB2_ENCRYPTION_CAPABILITIES).data)
255
+ .to eq(RubySMB::SMB2::EncryptionCapabilities.new)
256
+ end
257
+ end
258
+
259
+ context 'with a SMB2_COMPRESSION_CAPABILITIES context type' do
260
+ it 'selects the PreauthIntegrityCapabilities structure' do
261
+ expect(described_class.new(context_type: described_class::SMB2_COMPRESSION_CAPABILITIES).data)
262
+ .to eq(RubySMB::SMB2::CompressionCapabilities.new)
263
+ end
264
+ end
265
+
266
+ context 'with a SMB2_NETNAME_NEGOTIATE_CONTEXT_ID context type' do
267
+ it 'selects the PreauthIntegrityCapabilities structure' do
268
+ expect(described_class.new(context_type: described_class::SMB2_NETNAME_NEGOTIATE_CONTEXT_ID).data)
269
+ .to eq(RubySMB::SMB2::NetnameNegotiateContextId.new)
270
+ end
271
+ end
272
+ end
273
+
274
+ describe '#pad_length' do
275
+ it 'returns 0 when #context_type is already 8-byte aligned' do
276
+ expect(test_packet.nc.pad_length).to eq(0)
277
+ end
278
+
279
+ it 'returns 2 when #context_type is only 2-byte aligned' do
280
+ test_packet.garbage = 'align' + 'A'
281
+ expect(test_packet.nc.pad_length).to eq(2)
282
+ end
283
+ end
284
+
285
+ context 'with a SMB2_PREAUTH_INTEGRITY_CAPABILITIES context type' do
286
+ it 'reads binary data as expected' do
287
+ data = described_class.new(
288
+ context_type: described_class::SMB2_PREAUTH_INTEGRITY_CAPABILITIES
289
+ )
290
+ data.data.hash_algorithms << RubySMB::SMB2::PreauthIntegrityCapabilities::SHA_512
291
+ data.data.salt = 'test salt'
292
+ expect(described_class.read(data.to_binary_s)).to eq(data)
293
+ end
294
+ end
295
+
296
+ context 'with a SMB2_ENCRYPTION_CAPABILITIES context type' do
297
+ it 'reads binary data as expected' do
298
+ data = described_class.new(
299
+ context_type: described_class::SMB2_ENCRYPTION_CAPABILITIES
300
+ )
301
+ data.data.ciphers = [
302
+ RubySMB::SMB2::EncryptionCapabilities::AES_128_CCM,
303
+ RubySMB::SMB2::EncryptionCapabilities::AES_128_GCM
304
+ ]
305
+ expect(described_class.read(data.to_binary_s)).to eq(data)
306
+ end
307
+ end
308
+
309
+ context 'with a SMB2_COMPRESSION_CAPABILITIES context type' do
310
+ it 'reads binary data as expected' do
311
+ data = described_class.new(
312
+ context_type: described_class::SMB2_COMPRESSION_CAPABILITIES
313
+ )
314
+ data.data.compression_algorithms = [
315
+ RubySMB::SMB2::CompressionCapabilities::LZNT1,
316
+ RubySMB::SMB2::CompressionCapabilities::LZ77
317
+ ]
318
+ expect(described_class.read(data.to_binary_s)).to eq(data)
319
+ end
320
+ end
321
+
322
+ context 'with a SMB2_NETNAME_NEGOTIATE_CONTEXT_ID context type' do
323
+ it 'reads binary data as expected' do
324
+ data = described_class.new(
325
+ context_type: described_class::SMB2_NETNAME_NEGOTIATE_CONTEXT_ID
326
+ )
327
+ data.data.net_name = 'netname test'
328
+ expect(described_class.read(data.to_binary_s)).to eq(data)
329
+ end
330
+ end
331
+ end
332
+