File: authenticator.rb

package info (click to toggle)
ruby-rbnacl 7.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 700 kB
  • sloc: ruby: 2,884; makefile: 3
file content (78 lines) | stat: -rw-r--r-- 2,378 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# encoding: binary
# frozen_string_literal: true

RSpec.shared_examples "authenticator" do
  context ".new" do
    it "accepts a key" do
      expect { described_class.new(key) }.to_not raise_error
    end

    it "requires a key" do
      expect { described_class.new }.to raise_error(ArgumentError)
    end

    it "raises TypeError on a nil key" do
      expect { described_class.new(nil) }.to raise_error(TypeError)
    end
  end

  context ".auth" do
    it "produces an authenticator" do
      expect(described_class.auth(key, message)).to eq tag
    end

    it "raises TypeError on a nil key" do
      expect { described_class.auth(nil, message) }.to raise_error(TypeError)
    end
  end

  context ".verify" do
    it "verify an authenticator" do
      expect(described_class.verify(key, tag, message)).to eq true
    end

    it "raises TypeError on a nil key" do
      expect { described_class.verify(nil, tag, message) }.to raise_error(TypeError)
    end

    it "fails to validate an invalid authenticator" do
      expect { described_class.verify(key, tag, message + "\0") }.to raise_error(RbNaCl::BadAuthenticatorError)
    end

    it "fails to validate a short authenticator" do
      expect { described_class.verify(key, tag[0, tag.bytesize - 2], message) }.to raise_error(RbNaCl::LengthError)
    end

    it "fails to validate a long authenticator" do
      expect { described_class.verify(key, tag + "\0", message) }.to raise_error(RbNaCl::LengthError)
    end
  end

  context "Instance methods" do
    let(:authenticator) { described_class.new(key) }

    context "#auth" do
      it "produces an authenticator" do
        expect(authenticator.auth(message)).to eq tag
      end
    end

    context "#verify" do
      it "verifies an authenticator" do
        expect(authenticator.verify(tag, message)).to be true
      end

      it "fails to validate an invalid authenticator" do
        expect { authenticator.verify(tag, message + "\0") }.to raise_error(RbNaCl::BadAuthenticatorError)
      end

      it "fails to validate a short authenticator" do
        expect { authenticator.verify(tag[0, tag.bytesize - 2], message) }.to raise_error(RbNaCl::LengthError)
      end

      it "fails to validate a long authenticator" do
        expect { authenticator.verify(tag + "\0", message) }.to raise_error(RbNaCl::LengthError)
      end
    end
  end
end