Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/spec/lib/rex/image_source/memory_spec.rb
Views: 11783
# -*- coding:binary -*-1require 'spec_helper'23require 'rex/image_source/memory'45RSpec.describe Rex::ImageSource::Memory do67let(:raw_data) { 'ABCDEFGHIJKLMNOP' }89subject do10described_class.new(raw_data)11end1213it_should_behave_like 'Rex::ImageSource::ImageSource'1415describe "#initialize" do16subject(:memory_class) do17described_class.allocate18end1920it "initializes size to data length" do21memory_class.send(:initialize, raw_data)22expect(memory_class.size).to eq(raw_data.length)23end2425it "initializes file_offset to 0 by default" do26memory_class.send(:initialize, raw_data)27expect(memory_class.file_offset).to eq(0)28end2930context "when using nil as data" do31it "raises an error" do32expect { memory_class.send(:initialize, nil) }.to raise_error(NoMethodError)33end34end35end3637describe "#read" do38context "when offset is positive" do39let(:offset) { 1 }40let(:len) { 10 }4142it "returns an String" do43expect(subject.read(offset, len)).to be_a_kind_of(String)44end4546it "returns an String of provided length" do47expect(subject.read(offset, len).length).to eq(10)48end4950it "returns an String with _raw_data contents starting at provided offset" do51expect(subject.read(offset, len)).to start_with('BCD')52end53end5455context "when offset is negative" do56let(:offset) { -5 }57let(:len) { 2 }5859it "returns an String" do60expect(subject.read(offset, len)).to be_a_kind_of(String)61end6263it "returns an String of provided length" do64expect(subject.read(offset, len).length).to eq(2)65end6667it "offset is counted from the end of the _raw_data" do68expect(subject.read(offset, len)).to eq('LM')69end70end7172context "when offset is out of range" do73let(:offset) { 20 }74let(:len) { 2 }7576it "returns nil" do77expect(subject.read(offset, len)).to be_nil78end79end8081context "when len is bigger than _raw_data" do82let(:offset) { 0 }83let(:len) { 20 }8485it "returns an String" do86expect(subject.read(offset, len)).to be_a_kind_of(String)87end8889it "returns an String truncated to available contents" do90expect(subject.read(offset, len).length).to eq(raw_data.length)91end92end93end9495describe "#subsource" do96let(:offset) { 2 }97let(:len) { 10 }9899it "returns a new Rex::ImageSource::Memory" do100expect(subject.subsource(offset, len)).to be_kind_of(described_class)101end102103it "returns a new Rex::ImageSource::Memory with provided size" do104expect(subject.subsource(offset, len).size).to eq(len)105end106107it "returns a new Rex::ImageSource::Memory with file_offset added to the original" do108expect(subject.subsource(offset, len).file_offset).to eq(offset + subject.file_offset)109end110111it "returns a new Rex::ImageSource::Memory with rawdata from the original" do112expect(subject.subsource(offset, len).rawdata).to eq(subject.rawdata[offset, len])113end114115context "when offset is out of range" do116let(:offset) { 20 }117let(:len) { 2 }118119it "raises an error" do120expect { subject.subsource(offset, len) }.to raise_error(NoMethodError)121end122end123124context "when len is bigger than source rawdata" do125let(:offset) { 2 }126let(:len) { 20 }127128it "returns a new Rex::ImageSource::Memory" do129expect(subject.subsource(offset, len)).to be_kind_of(described_class)130end131132it "returns a new Rex::ImageSource::Memory with provided size truncated" do133expect(subject.subsource(offset, len).size).to eq(14)134end135136it "returns a new Rex::ImageSource::Memory with file_offset added to the original" do137expect(subject.subsource(offset, len).file_offset).to eq(offset + subject.file_offset)138end139140it "returns a new Rex::ImageSource::Memory with rawdata truncated" do141expect(subject.subsource(offset, len).rawdata).to eq('CDEFGHIJKLMNOP')142end143end144end145146describe "#close" do147it "returns nil" do148expect(subject.close).to be_nil149end150end151152describe "#index" do153let(:found) { 'FG' }154let(:not_found) { 'XYZ' }155156context "when search available substring" do157it "returns the index of the first occurrence" do158expect(subject.index(found)).to eq(5)159end160161context "when using negative offset" do162let(:offset) { -14 }163it "returns the index of the first occurrence" do164expect(subject.index(found, offset)).to eq(5)165end166end167168context "when using positive offset" do169let(:offset) { 1 }170it "returns the index of the first occurrence" do171expect(subject.index(found, offset)).to eq(5)172end173end174end175176context "when search not available substring" do177it "returns nil" do178expect(subject.index(not_found)).to be_nil179end180end181182context "when using negative offset" do183let(:offset) { -1 }184it "start to search from offset from the end of the string" do185expect(subject.index(found, offset)).to be_nil186end187end188end189190end191192193