File: compressor_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 (158 lines) | stat: -rw-r--r-- 5,114 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# frozen_string_literal: true

# Copyright, 2018, by Samuel G. D. Williams. <http://www.codeotaku.com>
# Copyrigh, 2013, by Ilya Grigorik.
# 
# 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/compressor'
require 'protocol/hpack/decompressor'

require 'json'

RSpec.describe Protocol::HPACK::Compressor do
	describe '#write_integer' do
		let(:buffer) {String.new.b}
		subject {described_class.new(buffer)}
		
		context '0-bit prefix' do
			it "can encode the value 10" do
				subject.write_integer(10, 0)
				expect(buffer).to be == [10].pack('C')
			end
			
			it "can encode the value 1337" do
				subject.write_integer(1337, 0)
				expect(buffer).to be == [128 + 57, 10].pack('C*')
			end
		end
		
		context '5-bit prefix' do
			it "can encode the value 10" do
				subject.write_integer(10, 5)
				expect(buffer).to be == [10].pack('C')
			end
			
			it "can encode the value 1337" do
				subject.write_integer(1337, 5)
				expect(buffer).to be == [31, 128 + 26, 10].pack('C*')
			end
		end
	end
	
	describe '#write_string' do
		[
			['with huffman', :always, 0x80],
			['without huffman', :never, 0],
		].each do |description, huffman, msb|
			context description do
				let(:context) {Protocol::HPACK::Context.new(huffman: huffman)}
				let(:buffer) {String.new.b}
				
				subject {Protocol::HPACK::Compressor.new(buffer, context)}
				let(:decompressor) {Protocol::HPACK::Decompressor.new(buffer, context)}
				
				[
					['ascii codepoints', 'abcdefghij'],
					['utf-8 codepoints', 'éáűőúöüó€'],
					['long utf-8 strings', 'éáűőúöüó€' * 100],
				].each do |datatype, plain|
					it "should handle #{datatype} #{description}" do
						subject.write_string(plain)
						expect(buffer.getbyte(0) & 0x80).to eq msb
						
						expect(decompressor.read_string).to eq plain
					end
				end
			end
		end
		
		context 'choosing shorter representation' do
			let(:context) {Protocol::HPACK::Context.new(huffman: :shorter)}
			let(:buffer) {String.new.b}
			
			subject {Protocol::HPACK::Compressor.new(buffer, context)}
			
			[
				['日本語', :plain],
				['200', :huffman],
				['xq', :plain],   # prefer plain if equal size
			].each do |string, choice|
				it "should return #{choice} representation" do
					subject.write_string(string)
					expect(buffer.getbyte(0) & 0x80).to eq(choice == :plain ? 0 : 0x80)
				end
			end
		end
	end
	
	describe '#encode' do
		let(:path) {File.expand_path("sequence1.json", __dir__)}
		let(:sequence) {JSON.parse(File.read(path))}
		
		let(:encoder) {Protocol::HPACK::Context.new}
		let(:decoder) {Protocol::HPACK::Context.new}
		
		it "can encode with small table size" do
			sequence.each do |headers|
				data = Protocol::HPACK::Compressor.new(String.new.b, encoder, table_size_limit: 512).encode(headers)
				
				expect(Protocol::HPACK::Decompressor.new(data, decoder).decode).to be == headers
				
				expect(encoder.table).to be == decoder.table
			end
			
			expect(decoder.table_size).to be == 512
		end
		
		it "can encode with default table size" do
			sequence.each do |headers|
				data = Protocol::HPACK::Compressor.new(String.new.b, encoder).encode(headers)
				
				expect(Protocol::HPACK::Decompressor.new(data, decoder).decode).to be == headers
				
				expect(encoder.table).to be == decoder.table
			end
		end
		
		it "can encode with large table size" do
			sequence.each do |headers|
				data = Protocol::HPACK::Compressor.new(String.new.b, encoder, table_size_limit: 65536).encode(headers)
				
				expect(Protocol::HPACK::Decompressor.new(data, decoder).decode).to be == headers
				
				expect(encoder.table).to be == decoder.table
			end
			
			expect(decoder.table_size).to be == 65536
		end
		
		it "can encode with random table sizes" do
			sequence.each do |headers|
				table_size = rand(128..65536)
				
				data = Protocol::HPACK::Compressor.new(String.new.b, encoder, table_size_limit: table_size).encode(headers)
				
				expect(Protocol::HPACK::Decompressor.new(data, decoder).decode).to be == headers
				
				expect(encoder.table).to be == decoder.table
			end
		end
	end
end