Sha256: a3acc9a70a08077433501b511c934153b8372c51bc433c0299ae7979ad54bcb9

Contents?: true

Size: 1.8 KB

Versions: 48

Compression:

Stored size: 1.8 KB

Contents

require 'spec_helper'

RSpec.describe RubySMB::Field::Stringz16 do
  subject(:stringz16) { described_class.new }

  it 'starts as an empty string' do
    expect(stringz16).to eq ''
  end

  context 'with a value already set' do
    let(:abcd) { described_class.new('ABCD') }

    it 'should be UTF-16le' do
      expect(abcd).to eq 'ABCD'.encode('utf-16le')
    end

    it 'should include the NULL terminator on binary output' do
      expect(abcd.to_binary_s).to eq "A\x00B\x00C\x00D\x00\x00\x00"
    end
  end

  context 'with a null terminator in the middle' do
    let(:null_terminator) { described_class.new("ABCD\x00EFG") }

    it 'drops everything after the null terminator' do
      expect(null_terminator).to eq 'ABCD'.encode('utf-16le')
    end
  end

  context 'when reading data in' do
    it 'stops at the first double null byte' do
      io = StringIO.new("A\x00B\x00C\x00D\x00\x00\x00E\x00F\x00")
      stringz16.read(io)
      expect(stringz16.to_binary_s).to eq "A\x00B\x00C\x00D\x00\x00\x00"
    end

    it 'handles a zero length string' do
      io = StringIO.new("\x00\x00A\x00")
      stringz16.read(io)
      expect(stringz16).to eq ''
    end

    it 'fails if no null terminator is found' do
      io = StringIO.new("A\x00B\x00C\x00D\x00")
      expect { stringz16.read(io) }.to raise_error(EOFError)
    end

    it 'trims the string to #max_length and makes sure it ends with a null terminator' do
      io = StringIO.new("A\x00B\x00C\x00D\x00")
      str = described_class.new(max_length: 6)
      expect(str.read(io).to_binary_s).to eq("A\x00B\x00\x00\x00".b)
    end

    it 'raises an exception when #max_length is not a multiple of two' do
      io = StringIO.new("A\x00B\x00C\x00D\x00")
      str = described_class.new(max_length: 5)
      expect { str.read(io) }.to raise_error(ArgumentError)
    end
  end
end

Version data entries

48 entries across 48 versions & 1 rubygems

Version Path
ruby_smb-3.3.13 spec/lib/ruby_smb/field/stringz16_spec.rb
ruby_smb-3.3.12 spec/lib/ruby_smb/field/stringz16_spec.rb
ruby_smb-3.3.11 spec/lib/ruby_smb/field/stringz16_spec.rb
ruby_smb-3.3.10 spec/lib/ruby_smb/field/stringz16_spec.rb
ruby_smb-3.3.9 spec/lib/ruby_smb/field/stringz16_spec.rb
ruby_smb-3.3.7 spec/lib/ruby_smb/field/stringz16_spec.rb
ruby_smb-3.3.6 spec/lib/ruby_smb/field/stringz16_spec.rb
ruby_smb-3.3.5 spec/lib/ruby_smb/field/stringz16_spec.rb
ruby_smb-3.3.4 spec/lib/ruby_smb/field/stringz16_spec.rb
ruby_smb-3.3.3 spec/lib/ruby_smb/field/stringz16_spec.rb
ruby_smb-3.3.2 spec/lib/ruby_smb/field/stringz16_spec.rb
ruby_smb-3.3.1 spec/lib/ruby_smb/field/stringz16_spec.rb
ruby_smb-3.3.0 spec/lib/ruby_smb/field/stringz16_spec.rb
ruby_smb-3.2.8 spec/lib/ruby_smb/field/stringz16_spec.rb
ruby_smb-3.2.7 spec/lib/ruby_smb/field/stringz16_spec.rb
ruby_smb-3.2.6 spec/lib/ruby_smb/field/stringz16_spec.rb
ruby_smb-3.2.5 spec/lib/ruby_smb/field/stringz16_spec.rb
ruby_smb-3.2.4 spec/lib/ruby_smb/field/stringz16_spec.rb
ruby_smb-3.2.3 spec/lib/ruby_smb/field/stringz16_spec.rb
ruby_smb-3.2.2 spec/lib/ruby_smb/field/stringz16_spec.rb