xdr 0.1.0 → 3.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +31 -0
  3. data/README.md +7 -2
  4. data/lib/xdr.rb +0 -1
  5. data/lib/xdr/concerns/converts_to_xdr.rb +47 -24
  6. data/lib/xdr/concerns/reads_bytes.rb +8 -0
  7. data/lib/xdr/dsl/enum.rb +1 -1
  8. data/lib/xdr/opaque.rb +1 -1
  9. data/lib/xdr/string.rb +1 -1
  10. data/lib/xdr/struct.rb +1 -1
  11. data/lib/xdr/union.rb +15 -6
  12. data/lib/xdr/var_opaque.rb +1 -1
  13. data/lib/xdr/version.rb +1 -1
  14. metadata +47 -133
  15. data/.gitignore +0 -15
  16. data/.travis.yml +0 -14
  17. data/.yardopts +0 -7
  18. data/Gemfile +0 -4
  19. data/Guardfile +0 -5
  20. data/Rakefile +0 -9
  21. data/examples/enum.rb +0 -30
  22. data/examples/struct.rb +0 -24
  23. data/examples/union.rb +0 -29
  24. data/spec/lib/xdr/array_spec.rb +0 -73
  25. data/spec/lib/xdr/bool_spec.rb +0 -43
  26. data/spec/lib/xdr/concerns/converts_to_xdr_spec.rb +0 -55
  27. data/spec/lib/xdr/concerns/reads_bytes_spec.rb +0 -31
  28. data/spec/lib/xdr/double_spec.rb +0 -38
  29. data/spec/lib/xdr/dsl/enum_spec.rb +0 -44
  30. data/spec/lib/xdr/dsl/struct_spec.rb +0 -29
  31. data/spec/lib/xdr/dsl/union_spec.rb +0 -69
  32. data/spec/lib/xdr/enum_spec.rb +0 -70
  33. data/spec/lib/xdr/float_spec.rb +0 -37
  34. data/spec/lib/xdr/hyper_spec.rb +0 -40
  35. data/spec/lib/xdr/int_spec.rb +0 -40
  36. data/spec/lib/xdr/opaque_spec.rb +0 -36
  37. data/spec/lib/xdr/option_spec.rb +0 -36
  38. data/spec/lib/xdr/quadruple_spec.rb +0 -14
  39. data/spec/lib/xdr/rpc/record_reader_spec.rb +0 -27
  40. data/spec/lib/xdr/string_spec.rb +0 -41
  41. data/spec/lib/xdr/struct_spec.rb +0 -101
  42. data/spec/lib/xdr/union_spec.rb +0 -248
  43. data/spec/lib/xdr/unsigned_hyper_spec.rb +0 -36
  44. data/spec/lib/xdr/unsigned_int_spec.rb +0 -36
  45. data/spec/lib/xdr/var_array_spec.rb +0 -71
  46. data/spec/lib/xdr/var_opaque_spec.rb +0 -43
  47. data/spec/lib/xdr/void_spec.rb +0 -46
  48. data/spec/spec_helper.rb +0 -15
  49. data/spec/support/matchers/eq_bytes.rb +0 -6
  50. data/xdr.gemspec +0 -29
@@ -1,69 +0,0 @@
1
- require 'spec_helper'
2
-
3
-
4
- describe XDR::DSL::Union, "#switch" do
5
-
6
- it "allows symbols in switch declarations" do
7
- expect do
8
- klass = Class.new(XDR::Union) do
9
- switch_on ResultType, :type
10
- switch :ok
11
- end
12
-
13
- klass.new(:ok)
14
- end.to_not raise_error
15
- end
16
-
17
- class ResultType < XDR::Enum
18
- member :ok, 0
19
- member :error, 1
20
- seal
21
- end
22
- end
23
-
24
- describe XDR::DSL::Union, "#switch_on" do
25
- klass = nil
26
-
27
- it "allows int types" do
28
- expect do
29
- klass = Class.new(XDR::Union) do
30
- switch_on XDR::Int, :type
31
- switch 0
32
- switch 1
33
- end
34
- end.to_not raise_error
35
-
36
- expect{ klass.new(0) }.to_not raise_error
37
- expect{ klass.new(1) }.to_not raise_error
38
- expect{ klass.new(2) }.to raise_error(XDR::InvalidSwitchError)
39
- end
40
-
41
- it "allows unsigned int types" do
42
- expect do
43
- klass = Class.new(XDR::Union) do
44
- switch_on XDR::UnsignedInt, :type
45
- switch 0
46
- switch 1
47
- end
48
- end.to_not raise_error
49
-
50
- expect{ klass.new(0) }.to_not raise_error
51
- expect{ klass.new(1) }.to_not raise_error
52
- expect{ klass.new(2) }.to raise_error(XDR::InvalidSwitchError)
53
- expect{ klass.new(-1) }.to raise_error(XDR::InvalidSwitchError)
54
- end
55
-
56
- it "allows bool types", :focus do
57
- klass = nil
58
-
59
- expect do
60
- klass = Class.new(XDR::Union) do
61
- switch_on XDR::Bool, :type
62
- switch true
63
- end
64
- end.to_not raise_error
65
-
66
- expect{ klass.new(true) }.to_not raise_error
67
- expect{ klass.new(false) }.to raise_error(XDR::InvalidSwitchError)
68
- end
69
- end
@@ -1,70 +0,0 @@
1
- require 'spec_helper'
2
-
3
- class TestColor < XDR::Enum
4
- member :red, 0
5
- member :green, 1
6
- member :even_more_green, 3
7
-
8
- seal
9
- end
10
-
11
- describe XDR::Enum, ".read" do
12
- let(:zero) { "\x00\x00\x00\x00" }
13
- let(:one) { "\x00\x00\x00\x01" }
14
- let(:two) { "\x00\x00\x00\x02" }
15
-
16
- subject{ TestColor }
17
-
18
- it "decodes values correctly" do
19
- expect( read zero ).to eq(TestColor.red)
20
- expect( read one ).to eq(TestColor.green)
21
- end
22
-
23
- it "raises EnumValueError if the decoded value is not in the defined constants" do
24
- expect{ read two }.to raise_error XDR::EnumValueError
25
- end
26
-
27
- def read(str)
28
- io = StringIO.new(str)
29
- subject.read(io)
30
- end
31
- end
32
-
33
- describe XDR::Enum, ".write" do
34
- subject{ TestColor }
35
-
36
- it "encodes values correctly" do
37
- expect( write TestColor.red ).to eq("\x00\x00\x00\x00")
38
- expect( write TestColor.green ).to eq("\x00\x00\x00\x01")
39
- end
40
-
41
- it "raises WriteError if value isn't a member" do
42
- expect{ write 0 }.to raise_error XDR::WriteError
43
- expect{ write 1 }.to raise_error XDR::WriteError
44
- end
45
-
46
- def write(val)
47
- io = StringIO.new()
48
- subject.write(val, io)
49
- io.string
50
- end
51
- end
52
-
53
- describe XDR::Enum, ".from_name" do
54
- subject{ TestColor }
55
-
56
- it "returns the correct value" do
57
- expect(subject.from_name("red")).to eq(TestColor.red)
58
- end
59
-
60
- it "allows various casings, strings or symbols" do
61
- expect(subject.from_name("even_more_green")).to eq(TestColor.even_more_green)
62
- expect(subject.from_name("EVEN_MORE_GREEN")).to eq(TestColor.even_more_green)
63
- expect(subject.from_name(:even_more_green)).to eq(TestColor.even_more_green)
64
- expect(subject.from_name(:EVEN_MORE_GREEN)).to eq(TestColor.even_more_green)
65
- end
66
-
67
- it "raises EnumNameError when the name is not a member" do
68
- expect{ subject.from_name("chartreuse")}.to raise_error(XDR::EnumNameError)
69
- end
70
- end
@@ -1,37 +0,0 @@
1
- require 'spec_helper'
2
-
3
-
4
- describe XDR::Float, ".read" do
5
- it "decodes values correctly" do
6
- expect(read("\x00\x00\x00\x00")).to eq(0.0)
7
- expect(read("\x80\x00\x00\x00")).to eq(-0.0)
8
- expect(read("\x3F\x80\x00\x00")).to eq(1.0)
9
- expect(read("\xBF\x80\x00\x00")).to eq(-1.0)
10
- end
11
-
12
- def read(str)
13
- io = StringIO.new(str)
14
- subject.read(io)
15
- end
16
- end
17
-
18
- describe XDR::Float, ".write" do
19
- it "encodes values correctly" do
20
- expect(write(0.0)).to eq_bytes("\x00\x00\x00\x00")
21
- expect(write(-0.0)).to eq_bytes("\x80\x00\x00\x00")
22
- expect(write(1.0)).to eq_bytes("\x3F\x80\x00\x00")
23
- expect(write(-1.0)).to eq_bytes("\xBF\x80\x00\x00")
24
- end
25
-
26
- it "raises a WriteError when the value is not Float" do
27
- expect{ write 3 }.to raise_error(XDR::WriteError)
28
- expect{ write "hello" }.to raise_error(XDR::WriteError)
29
- expect{ write "1.0" }.to raise_error(XDR::WriteError)
30
- end
31
-
32
- def write(val)
33
- io = StringIO.new()
34
- subject.write(val, io)
35
- io.string
36
- end
37
- end
@@ -1,40 +0,0 @@
1
- require 'spec_helper'
2
-
3
-
4
- describe XDR::Hyper, ".read" do
5
-
6
- it "decodes values correctly" do
7
- expect(read("\x00\x00\x00\x00\x00\x00\x00\x00")).to eq(0)
8
- expect(read("\x00\x00\x00\x00\x00\x00\x00\x01")).to eq(1)
9
- expect(read("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF")).to eq(-1)
10
- expect(read("\x7F\xFF\xFF\xFF\xFF\xFF\xFF\xFF")).to eq(2**63 - 1)
11
- expect(read("\x80\x00\x00\x00\x00\x00\x00\x00")).to eq(-(2**63))
12
- end
13
-
14
- def read(str)
15
- io = StringIO.new(str)
16
- subject.read(io)
17
- end
18
- end
19
-
20
- describe XDR::Hyper, ".write" do
21
-
22
- it "decodes values correctly" do
23
- expect(write 0).to eq_bytes("\x00\x00\x00\x00\x00\x00\x00\x00")
24
- expect(write 1).to eq_bytes("\x00\x00\x00\x00\x00\x00\x00\x01")
25
- expect(write -1).to eq_bytes("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF")
26
- expect(write 2**63 - 1).to eq_bytes("\x7F\xFF\xFF\xFF\xFF\xFF\xFF\xFF")
27
- expect(write -(2**63)).to eq_bytes("\x80\x00\x00\x00\x00\x00\x00\x00")
28
- end
29
-
30
- it "raises WriteError when an Integer isn't passed" do
31
- expect{ write 1.0 }.to raise_error(XDR::WriteError)
32
- expect{ write "hi" }.to raise_error(XDR::WriteError)
33
- end
34
-
35
- def write(val)
36
- io = StringIO.new()
37
- subject.write(val, io)
38
- io.string
39
- end
40
- end
@@ -1,40 +0,0 @@
1
- require 'spec_helper'
2
-
3
-
4
- describe XDR::Int, ".read" do
5
-
6
- it "decodes values correctly" do
7
- expect(read("\x00\x00\x00\x00")).to eq(0)
8
- expect(read("\x00\x00\x00\x01")).to eq(1)
9
- expect(read("\xFF\xFF\xFF\xFF")).to eq(-1)
10
- expect(read("\x7F\xFF\xFF\xFF")).to eq(2**31 - 1)
11
- expect(read("\x80\x00\x00\x00")).to eq(-(2**31))
12
- end
13
-
14
- def read(str)
15
- io = StringIO.new(str)
16
- subject.read(io)
17
- end
18
- end
19
-
20
- describe XDR::Int, ".write" do
21
-
22
- it "decodes values correctly" do
23
- expect(write 0).to eq_bytes("\x00\x00\x00\x00")
24
- expect(write 1).to eq_bytes("\x00\x00\x00\x01")
25
- expect(write -1).to eq_bytes("\xFF\xFF\xFF\xFF")
26
- expect(write 2**31 - 1).to eq_bytes("\x7F\xFF\xFF\xFF")
27
- expect(write -(2**31)).to eq_bytes("\x80\x00\x00\x00")
28
- end
29
-
30
- it "raises WriteError when an Integer isn't passed" do
31
- expect{ write 1.0 }.to raise_error(XDR::WriteError)
32
- expect{ write "hi" }.to raise_error(XDR::WriteError)
33
- end
34
-
35
- def write(val)
36
- io = StringIO.new()
37
- subject.write(val, io)
38
- io.string
39
- end
40
- end
@@ -1,36 +0,0 @@
1
- require 'spec_helper'
2
-
3
-
4
- describe XDR::Opaque, "#read" do
5
- subject{ XDR::Opaque.new(3) }
6
-
7
- it "decodes values correctly" do
8
- expect(read("\x00\x00\x00\x00")).to eq("\x00\x00\x00")
9
- expect(read("\x00\x01\x00\x00")).to eq("\x00\x01\x00")
10
- end
11
-
12
- def read(str)
13
- io = StringIO.new(str)
14
- subject.read(io)
15
- end
16
- end
17
-
18
- describe XDR::Opaque, "#write" do
19
- subject{ XDR::Opaque.new(3) }
20
-
21
- it "encodes values correctly" do
22
- expect(write("123")).to eq("123\x00")
23
- expect(write("124")).to eq("124\x00")
24
- end
25
-
26
- it "raises a WriteError if the value is not the correct length" do
27
- expect{ write("1234") }.to raise_error(XDR::WriteError)
28
- expect{ write("12") }.to raise_error(XDR::WriteError)
29
- end
30
-
31
- def write(val)
32
- io = StringIO.new()
33
- subject.write(val, io)
34
- io.string
35
- end
36
- end
@@ -1,36 +0,0 @@
1
- require 'spec_helper'
2
-
3
-
4
- describe XDR::Option, ".read" do
5
- subject{ XDR::Option[XDR::Int] }
6
-
7
- it "decodes values correctly" do
8
- expect(read "\x00\x00\x00\x01\x00\x00\x00\x00" ).to eq(0)
9
- expect(read "\x00\x00\x00\x00" ).to eq(nil)
10
- end
11
-
12
- def read(str)
13
- io = StringIO.new(str)
14
- subject.read(io)
15
- end
16
- end
17
-
18
- describe XDR::Option, ".write" do
19
- subject{ XDR::Option[XDR::Int] }
20
-
21
- it "decodes values correctly" do
22
- expect(write 0).to eq_bytes("\x00\x00\x00\x01\x00\x00\x00\x00")
23
- expect(write nil).to eq_bytes("\x00\x00\x00\x00")
24
- end
25
-
26
- it "raises WriteError when the provided value is non-nil bust invalid for the child type" do
27
- expect{ write 1.0 }.to raise_error(XDR::WriteError)
28
- expect{ write "hi" }.to raise_error(XDR::WriteError)
29
- end
30
-
31
- def write(val)
32
- io = StringIO.new()
33
- subject.write(val, io)
34
- io.string
35
- end
36
- end
@@ -1,14 +0,0 @@
1
- require 'spec_helper'
2
-
3
-
4
- describe XDR::Quadruple, ".read" do
5
- let(:zero){ "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" }
6
- it "cannot decode values yet" do
7
- expect{ read zero }.to raise_error(NotImplementedError)
8
- end
9
-
10
- def read(str)
11
- io = StringIO.new(str)
12
- subject.read(io)
13
- end
14
- end
@@ -1,27 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe XDR::RPC::RecordReader, "#read" do
4
-
5
- it "decodes values correctly" do
6
- empty_record = read "\x00\x00\x00\x00"
7
- last_record = read "\x80\x00\x00\x02\x00\x00"
8
-
9
- expect(empty_record).to_not be_last
10
- expect(empty_record.length).to eq(0)
11
- expect(empty_record.content.string).to eq("")
12
-
13
- expect(last_record).to be_last
14
- expect(last_record.length).to eq(2)
15
- expect(last_record.content.string).to eq("\x00\x00")
16
- end
17
-
18
- it "raises EOFError the byte stream isn't large enough" do
19
- expect{ read "\x00\x00\x00\x01" }.to raise_error(EOFError)
20
- expect{ read "\x00\x00\x00\x08\x00\x00\x00\x01" }.to raise_error(EOFError)
21
- end
22
-
23
- def read(str)
24
- io = StringIO.new(str)
25
- subject.read(io)
26
- end
27
- end
@@ -1,41 +0,0 @@
1
- require 'spec_helper'
2
-
3
-
4
- describe XDR::String, "#read" do
5
- subject{ XDR::String.new(3) }
6
-
7
- it "decodes values correctly" do
8
- expect(read("\x00\x00\x00\x00")).to eq("")
9
- expect(read("\x00\x00\x00\x01h\x00\x00\x00")).to eq("h")
10
- expect(read("\x00\x00\x00\x02hi\x00\x00")).to eq("hi")
11
- end
12
-
13
- it "raises a ReadError when the encoded length is greater than the allowed max" do
14
- expect{ read "\x00\x00\x00\x04hiya" }.to raise_error(XDR::ReadError)
15
- end
16
-
17
- def read(str)
18
- io = StringIO.new(str)
19
- subject.read(io)
20
- end
21
- end
22
-
23
- describe XDR::String, "#write" do
24
- subject{ XDR::String[2] }
25
-
26
- it "encodes values correctly" do
27
- expect(write("")).to eq("\x00\x00\x00\x00")
28
- expect(write("h")).to eq("\x00\x00\x00\x01h\x00\x00\x00")
29
- expect(write("hi")).to eq("\x00\x00\x00\x02hi\x00\x00")
30
- end
31
-
32
- it "raises a WriteError when the provided string is too long" do
33
- expect{ write "123" }.to raise_error(XDR::WriteError)
34
- end
35
-
36
- def write(val)
37
- io = StringIO.new()
38
- subject.write(val, io)
39
- io.string
40
- end
41
- end
@@ -1,101 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module StructSpec
4
- class SampleError < XDR::Struct
5
- attribute :code, XDR::Int
6
- attribute :msg, XDR::String[100]
7
- end
8
-
9
-
10
- # class SampleEnvelope < XDR::Struct
11
- # attribute :body, XDR::VarOpaque[]
12
- # attribute :sigs, XDR::VarArray[XDR::Opaque[32]]
13
- # end
14
-
15
- # class SampleOptions < XDR::Struct
16
- # attribute :limit, XDR::Option[XDR::Int]
17
- # attribute :subscribed, XDR::Option[XDR::Bool]
18
- # end
19
- end
20
-
21
-
22
- describe XDR::Struct, "creation" do
23
- let(:args){ {} }
24
- subject{ StructSpec::SampleError.new(args) }
25
-
26
- context "with no args" do
27
- it "creates an instance" do
28
- expect(subject).to be_a(StructSpec::SampleError)
29
- end
30
- end
31
-
32
- context "with valid args" do
33
- let(:args){{code: 3, msg: "It broke!"}}
34
-
35
- it "assigns values correctly" do
36
- expect(subject.code).to eq(3)
37
- expect(subject.msg).to eq("It broke!")
38
- end
39
- end
40
- end
41
-
42
- describe XDR::Struct, "attribute assignment" do
43
- subject{ StructSpec::SampleError.new }
44
-
45
- it "roundtrips correctly" do
46
- expect(subject.code).to be_nil
47
- expect(subject.msg).to be_nil
48
-
49
- subject.code = 10
50
- subject.msg = "busted"
51
-
52
- expect(subject.code).to eq(10)
53
- expect(subject.msg).to eq("busted")
54
- end
55
- end
56
-
57
-
58
- describe XDR::Struct, "valid?"
59
- describe XDR::Struct, "optional members"
60
-
61
- describe XDR::Struct, "#to_xdr" do
62
- subject{ StructSpec::SampleError.new({code: 3, msg: "It broke!"}) }
63
- let(:result){ subject.to_xdr }
64
-
65
- it "serialized each field in order" do
66
- expect(result[0...4]).to eq("\x00\x00\x00\x03")
67
- expect(result[4...8]).to eq([9].pack("l>"))
68
- expect(result[8..-1]).to eq("It broke!\x00\x00\x00")
69
- end
70
-
71
- it "raises an exception if the struct is not valid" do
72
- subject.code = nil
73
- expect{ result }.to raise_error(XDR::WriteError)
74
- end
75
-
76
- it "produces hex" do
77
- result = subject.to_xdr(:hex)
78
- expect(result).to eq("000000030000000949742062726f6b6521000000")
79
- end
80
-
81
- it "produces base64" do
82
- result = subject.to_xdr(:base64)
83
- expect(result).to eq("AAAAAwAAAAlJdCBicm9rZSEAAAA=")
84
- end
85
- end
86
-
87
-
88
- describe XDR::Struct, ".read" do
89
- subject{ StructSpec::SampleError }
90
- let(:result){ read "\x00\x00\x00\x01\x00\x00\x00\x0812345678" }
91
- it "decodes values correctly" do
92
- expect( result ).to be_a(subject)
93
- expect( result.code ).to eq(1)
94
- expect( result.msg ).to eq("12345678")
95
- end
96
-
97
- def read(str)
98
- io = StringIO.new(str)
99
- subject.read(io)
100
- end
101
- end