File: huffman_spec.rb

package info (click to toggle)
ruby-protocol-hpack 1.4.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 312 kB
  • sloc: ruby: 1,841; makefile: 4
file content (108 lines) | stat: -rw-r--r-- 3,602 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# frozen_string_literal: true

# Copyright, 2018, by Samuel G. D. Williams. <http://www.codeotaku.com>
# 
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

require 'protocol/hpack/huffman'

RSpec.describe Protocol::HPACK::Huffman do
	huffman_examples = [ # plain, encoded
		['www.example.com', 'f1e3c2e5f23a6ba0ab90f4ff'],
		['no-cache',        'a8eb10649cbf'],
		['Mon, 21 Oct 2013 20:13:21 GMT', 'd07abe941054d444a8200595040b8166e082a62d1bff'],
	]
	
	context 'encode' do
		huffman_examples.each do |plain, encoded|
			it "should encode #{plain} into #{encoded}" do
				expect(subject.encode(plain).unpack1('H*')).to eq encoded
			end
		end
	end
	
	context 'decode' do
		huffman_examples.each do |plain, encoded|
			it "should decode #{encoded} into #{plain}" do
				expect(subject.decode([encoded].pack('H*'))).to eq plain
			end
		end

		[
			'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:16.0) Gecko/20100101 Firefox/16.0',
			'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
			'http://www.craigslist.org/about/sites/',
			'cl_b=AB2BKbsl4hGM7M4nH5PYWghTM5A; cl_def_lang=en; cl_def_hp=shoals',
			'image/png,image/*;q=0.8,*/*;q=0.5',
			'BX=c99r6jp89a7no&b=3&s=q4; localization=en-us%3Bus%3Bus',
			'UTF-8でエンコードした日本語文字列',
		].each do |string|
			it "should encode then decode '#{string}' into the same" do
				encoded = subject.encode(string.b)
				expect(subject.decode(encoded)).to eq string.b
			end
		end
		
		it 'should encode/decode all_possible 2-byte sequences' do
			(2**16).times do |n|
				string = [n].pack('V')[0, 2].b
				
				expect(subject.decode(subject.encode(string))).to eq string
			end
		end
		
		it 'should raise when input is shorter than expected' do
			encoded = huffman_examples.first.last
			encoded = [encoded].pack('H*')
			
			expect do
				subject.decode(encoded[0...-1].b)
			end.to raise_error(/EOS invalid/)
		end
		
		it 'should raise when input is not padded by 1s' do
			# note the fe at end
			encoded = 'f1e3c2e5f23a6ba0ab90f4fe'
			encoded = [encoded].pack('H*')
			
			expect do
				subject.decode(encoded.b)
			end.to raise_error(/EOS invalid/)
		end
		
		it 'should raise when exceedingly padded' do
			# note the extra ff
			encoded = 'e7cf9bebe89b6fb16fa9b6ffff'
			encoded = [encoded].pack('H*')
			
			expect do
				subject.decode(encoded.b)
			end.to raise_error(/EOS invalid/)
		end
		
		it 'should raise when EOS is explicitly encoded' do
			# a b EOS
			encoded = ['1c7fffffffff'].pack('H*')
			
			expect do
				subject.decode(encoded.b)
			end.to raise_error(/EOS found/)
		end
	end
end