Debian Codegraph @

encoding_test.go

raw symbols SCIP
1 /*-
2 * Copyright 2014 Square Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package jose
18
19 import (
20 "bytes"
21 "strings"
22 "testing"
23 )
24
25 func TestBase64URLEncode(t *testing.T) {
26 // Test arrays with various sizes
27 if base64URLEncode([]byte{}) != "" {
28 t.Error("failed to encode empty array")
29 }
30
31 if base64URLEncode([]byte{0}) != "AA" {
32 t.Error("failed to encode [0x00]")
33 }
34
35 if base64URLEncode([]byte{0, 1}) != "AAE" {
36 t.Error("failed to encode [0x00, 0x01]")
37 }
38
39 if base64URLEncode([]byte{0, 1, 2}) != "AAEC" {
40 t.Error("failed to encode [0x00, 0x01, 0x02]")
41 }
42
43 if base64URLEncode([]byte{0, 1, 2, 3}) != "AAECAw" {
44 t.Error("failed to encode [0x00, 0x01, 0x02, 0x03]")
45 }
46 }
47
48 func TestBase64URLDecode(t *testing.T) {
49 // Test arrays with various sizes
50 val, err := base64URLDecode("")
51 if err != nil || !bytes.Equal(val, []byte{}) {
52 t.Error("failed to decode empty array")
53 }
54
55 val, err = base64URLDecode("AA")
56 if err != nil || !bytes.Equal(val, []byte{0}) {
57 t.Error("failed to decode [0x00]")
58 }
59
60 val, err = base64URLDecode("AAE")
61 if err != nil || !bytes.Equal(val, []byte{0, 1}) {
62 t.Error("failed to decode [0x00, 0x01]")
63 }
64
65 val, err = base64URLDecode("AAEC")
66 if err != nil || !bytes.Equal(val, []byte{0, 1, 2}) {
67 t.Error("failed to decode [0x00, 0x01, 0x02]")
68 }
69
70 val, err = base64URLDecode("AAECAw")
71 if err != nil || !bytes.Equal(val, []byte{0, 1, 2, 3}) {
72 t.Error("failed to decode [0x00, 0x01, 0x02, 0x03]")
73 }
74 }
75
76 func TestDeflateRoundtrip(t *testing.T) {
77 original := []byte("Lorem ipsum dolor sit amet")
78
79 compressed, err := deflate(original)
80 if err != nil {
81 panic(err)
82 }
83
84 output, err := inflate(compressed)
85 if err != nil {
86 panic(err)
87 }
88
89 if bytes.Compare(output, original) != 0 {
90 t.Error("Input and output do not match")
91 }
92 }
93
94 func TestInvalidCompression(t *testing.T) {
95 _, err := compress("XYZ", []byte{})
96 if err == nil {
97 t.Error("should not accept invalid algorithm")
98 }
99
100 _, err = decompress("XYZ", []byte{})
101 if err == nil {
102 t.Error("should not accept invalid algorithm")
103 }
104
105 _, err = decompress(DEFLATE, []byte{1, 2, 3, 4})
106 if err == nil {
107 t.Error("should not accept invalid data")
108 }
109 }
110
111 func TestByteBufferTrim(t *testing.T) {
112 buf := newBufferFromInt(1)
113 if !bytes.Equal(buf.data, []byte{1}) {
114 t.Error("Byte buffer for integer '1' should contain [0x01]")
115 }
116
117 buf = newBufferFromInt(65537)
118 if !bytes.Equal(buf.data, []byte{1, 0, 1}) {
119 t.Error("Byte buffer for integer '65537' should contain [0x01, 0x00, 0x01]")
120 }
121 }
122
123 func TestFixedSizeBuffer(t *testing.T) {
124 data0 := []byte{}
125 data1 := []byte{1}
126 data2 := []byte{1, 2}
127 data3 := []byte{1, 2, 3}
128 data4 := []byte{1, 2, 3, 4}
129
130 buf0 := newFixedSizeBuffer(data0, 4)
131 buf1 := newFixedSizeBuffer(data1, 4)
132 buf2 := newFixedSizeBuffer(data2, 4)
133 buf3 := newFixedSizeBuffer(data3, 4)
134 buf4 := newFixedSizeBuffer(data4, 4)
135
136 if !bytes.Equal(buf0.data, []byte{0, 0, 0, 0}) {
137 t.Error("Invalid padded buffer for buf0")
138 }
139 if !bytes.Equal(buf1.data, []byte{0, 0, 0, 1}) {
140 t.Error("Invalid padded buffer for buf1")
141 }
142 if !bytes.Equal(buf2.data, []byte{0, 0, 1, 2}) {
143 t.Error("Invalid padded buffer for buf2")
144 }
145 if !bytes.Equal(buf3.data, []byte{0, 1, 2, 3}) {
146 t.Error("Invalid padded buffer for buf3")
147 }
148 if !bytes.Equal(buf4.data, []byte{1, 2, 3, 4}) {
149 t.Error("Invalid padded buffer for buf4")
150 }
151 }
152
153 func TestSerializeJSONRejectsNil(t *testing.T) {
154 defer func() {
155 r := recover()
156 if r == nil || !strings.Contains(r.(string), "nil pointer") {
157 t.Error("serialize function should not accept nil pointer")
158 }
159 }()
160
161 mustSerializeJSON(nil)
162 }
163
164 func TestFixedSizeBufferTooLarge(t *testing.T) {
165 defer func() {
166 r := recover()
167 if r == nil {
168 t.Error("should not be able to create fixed size buffer with oversized data")
169 }
170 }()
171
172 newFixedSizeBuffer(make([]byte, 2), 1)
173 }