1
16
17 package jose
18
19 import (
20 "bytes"
21 "strings"
22 "testing"
23 )
24
25 func TestDeflateRoundtrip(t *testing.T) {
26 original := []byte("Lorem ipsum dolor sit amet")
27
28 compressed, err := deflate(original)
29 if err != nil {
30 panic(err)
31 }
32
33 output, err := inflate(compressed)
34 if err != nil {
35 panic(err)
36 }
37
38 if bytes.Compare(output, original) != 0 {
39 t.Error("Input and output do not match")
40 }
41 }
42
43 func TestInvalidCompression(t *testing.T) {
44 _, err := compress("XYZ", []byte{})
45 if err == nil {
46 t.Error("should not accept invalid algorithm")
47 }
48
49 _, err = decompress("XYZ", []byte{})
50 if err == nil {
51 t.Error("should not accept invalid algorithm")
52 }
53
54 _, err = decompress(DEFLATE, []byte{1, 2, 3, 4})
55 if err == nil {
56 t.Error("should not accept invalid data")
57 }
58 }
59
60 func TestByteBufferTrim(t *testing.T) {
61 buf := newBufferFromInt(1)
62 if !bytes.Equal(buf.data, []byte{1}) {
63 t.Error("Byte buffer for integer '1' should contain [0x01]")
64 }
65
66 buf = newBufferFromInt(65537)
67 if !bytes.Equal(buf.data, []byte{1, 0, 1}) {
68 t.Error("Byte buffer for integer '65537' should contain [0x01, 0x00, 0x01]")
69 }
70 }
71
72 func TestFixedSizeBuffer(t *testing.T) {
73 data0 := []byte{}
74 data1 := []byte{1}
75 data2 := []byte{1, 2}
76 data3 := []byte{1, 2, 3}
77 data4 := []byte{1, 2, 3, 4}
78
79 buf0 := newFixedSizeBuffer(data0, 4)
80 buf1 := newFixedSizeBuffer(data1, 4)
81 buf2 := newFixedSizeBuffer(data2, 4)
82 buf3 := newFixedSizeBuffer(data3, 4)
83 buf4 := newFixedSizeBuffer(data4, 4)
84
85 if !bytes.Equal(buf0.data, []byte{0, 0, 0, 0}) {
86 t.Error("Invalid padded buffer for buf0")
87 }
88 if !bytes.Equal(buf1.data, []byte{0, 0, 0, 1}) {
89 t.Error("Invalid padded buffer for buf1")
90 }
91 if !bytes.Equal(buf2.data, []byte{0, 0, 1, 2}) {
92 t.Error("Invalid padded buffer for buf2")
93 }
94 if !bytes.Equal(buf3.data, []byte{0, 1, 2, 3}) {
95 t.Error("Invalid padded buffer for buf3")
96 }
97 if !bytes.Equal(buf4.data, []byte{1, 2, 3, 4}) {
98 t.Error("Invalid padded buffer for buf4")
99 }
100 }
101
102 func TestSerializeJSONRejectsNil(t *testing.T) {
103 defer func() {
104 r := recover()
105 if r == nil || !strings.Contains(r.(string), "nil pointer") {
106 t.Error("serialize function should not accept nil pointer")
107 }
108 }()
109
110 mustSerializeJSON(nil)
111 }
112
113 func TestFixedSizeBufferTooLarge(t *testing.T) {
114 defer func() {
115 r := recover()
116 if r == nil {
117 t.Error("should not be able to create fixed size buffer with oversized data")
118 }
119 }()
120
121 newFixedSizeBuffer(make([]byte, 2), 1)
122 }
123
View as plain text