Sha256: 03d4d384b6f51070c7bcecd4b24d158b6f01734331ddb1831c27557234243050

Contents?: true

Size: 1.61 KB

Versions: 8

Compression:

Stored size: 1.61 KB

Contents

require 'spec_helper'

RSpec.describe ROTP::Base32 do

  describe '.random_base32' do
    context 'without arguments' do
      let(:base32) { ROTP::Base32.random_base32 }

      it 'is 16 characters long' do
        expect(base32.length).to eq 16
      end

      it 'is hexadecimal' do
        expect(base32).to match %r{\A[a-z2-7]+\z}
      end
    end

    context 'with arguments' do
      let(:base32) { ROTP::Base32.random_base32 32 }

      it 'allows a specific length' do
        expect(base32.length).to eq 32
      end
    end
  end

  describe '.decode' do
    context 'corrupt input data' do
      it 'raises a sane error' do
        expect { ROTP::Base32.decode('4BCDEFG234BCDEF1') }.to \
          raise_error(ROTP::Base32::Base32Error, "Invalid Base32 Character - '1'")
      end
    end

    context 'valid input data' do
      it 'correctly decodes a string' do
        expect(ROTP::Base32.decode('F').unpack('H*').first).to eq '28'
        expect(ROTP::Base32.decode('23').unpack('H*').first).to eq 'd6'
        expect(ROTP::Base32.decode('234').unpack('H*').first).to eq 'd6f8'
        expect(ROTP::Base32.decode('234A').unpack('H*').first).to eq 'd6f800'
        expect(ROTP::Base32.decode('234B').unpack('H*').first).to eq 'd6f810'
        expect(ROTP::Base32.decode('234BCD').unpack('H*').first).to eq 'd6f8110c'
        expect(ROTP::Base32.decode('234BCDE').unpack('H*').first).to eq 'd6f8110c80'
        expect(ROTP::Base32.decode('234BCDEFG').unpack('H*').first).to eq 'd6f8110c8530'
        expect(ROTP::Base32.decode('234BCDEFG234BCDEFG').unpack('H*').first).to eq 'd6f8110c8536b7c0886429'
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
rotp-3.3.0 spec/lib/rotp/base32_spec.rb
rotp-3.2.0 spec/lib/rotp/base32_spec.rb
rotp-3.1.0 spec/lib/rotp/base32_spec.rb
rotp-3.0.1 spec/lib/rotp/base32_spec.rb
rotp-3.0.0 spec/lib/rotp/base32_spec.rb
rotp-2.1.2 spec/lib/rotp/base32_spec.rb
rotp-2.1.1 spec/lib/rotp/base32_spec.rb
rotp-2.1.0 spec/lib/rotp/base32_spec.rb