...

Source file src/gopkg.in/go-jose/go-jose.v2/encoding_test.go

Documentation: gopkg.in/go-jose/go-jose.v2

     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  	"crypto/rand"
    22  	"io"
    23  	"strings"
    24  	"testing"
    25  )
    26  
    27  func TestDeflateRoundtrip(t *testing.T) {
    28  	original := []byte("Lorem ipsum dolor sit amet")
    29  
    30  	compressed, err := deflate(original)
    31  	if err != nil {
    32  		panic(err)
    33  	}
    34  
    35  	output, err := inflate(compressed)
    36  	if err != nil {
    37  		panic(err)
    38  	}
    39  
    40  	if bytes.Compare(output, original) != 0 {
    41  		t.Error("Input and output do not match")
    42  	}
    43  }
    44  
    45  func TestInvalidCompression(t *testing.T) {
    46  	_, err := compress("XYZ", []byte{})
    47  	if err == nil {
    48  		t.Error("should not accept invalid algorithm")
    49  	}
    50  
    51  	_, err = decompress("XYZ", []byte{})
    52  	if err == nil {
    53  		t.Error("should not accept invalid algorithm")
    54  	}
    55  
    56  	_, err = decompress(DEFLATE, []byte{1, 2, 3, 4})
    57  	if err == nil {
    58  		t.Error("should not accept invalid data")
    59  	}
    60  }
    61  
    62  // TestLargeZip tests that we can decompress a large input, so long as its
    63  // compression ratio is reasonable.
    64  func TestLargeZip(t *testing.T) {
    65  	input := new(bytes.Buffer)
    66  	_, err := io.CopyN(input, rand.Reader, 251000)
    67  	if err != nil {
    68  		t.Fatalf("generating input: %s", err)
    69  	}
    70  	compressed, err := compress(DEFLATE, input.Bytes())
    71  	if err != nil {
    72  		t.Errorf("compressing: %s", err)
    73  	}
    74  	t.Logf("compression ratio: %g", float64(len(input.Bytes()))/float64(len(compressed)))
    75  	_, err = decompress(DEFLATE, compressed)
    76  	if err != nil {
    77  		t.Errorf("decompressing large input with low compression ratio: %s", err)
    78  	}
    79  }
    80  
    81  func TestZipBomb(t *testing.T) {
    82  	input := strings.Repeat("a", 251000)
    83  	compressed, err := compress(DEFLATE, []byte(input))
    84  	if err != nil {
    85  		t.Errorf("compressing: %s", err)
    86  	}
    87  	t.Logf("compression ratio: %d %g", len(compressed), float64(len(input))/float64(len(compressed)))
    88  	out, err := decompress(DEFLATE, compressed)
    89  	if err == nil {
    90  		t.Errorf("expected error decompressing zip bomb, got none. output size %d", len(out))
    91  	}
    92  }
    93  
    94  func TestByteBufferTrim(t *testing.T) {
    95  	buf := newBufferFromInt(1)
    96  	if !bytes.Equal(buf.data, []byte{1}) {
    97  		t.Error("Byte buffer for integer '1' should contain [0x01]")
    98  	}
    99  
   100  	buf = newBufferFromInt(65537)
   101  	if !bytes.Equal(buf.data, []byte{1, 0, 1}) {
   102  		t.Error("Byte buffer for integer '65537' should contain [0x01, 0x00, 0x01]")
   103  	}
   104  }
   105  
   106  func TestFixedSizeBuffer(t *testing.T) {
   107  	data0 := []byte{}
   108  	data1 := []byte{1}
   109  	data2 := []byte{1, 2}
   110  	data3 := []byte{1, 2, 3}
   111  	data4 := []byte{1, 2, 3, 4}
   112  
   113  	buf0 := newFixedSizeBuffer(data0, 4)
   114  	buf1 := newFixedSizeBuffer(data1, 4)
   115  	buf2 := newFixedSizeBuffer(data2, 4)
   116  	buf3 := newFixedSizeBuffer(data3, 4)
   117  	buf4 := newFixedSizeBuffer(data4, 4)
   118  
   119  	if !bytes.Equal(buf0.data, []byte{0, 0, 0, 0}) {
   120  		t.Error("Invalid padded buffer for buf0")
   121  	}
   122  	if !bytes.Equal(buf1.data, []byte{0, 0, 0, 1}) {
   123  		t.Error("Invalid padded buffer for buf1")
   124  	}
   125  	if !bytes.Equal(buf2.data, []byte{0, 0, 1, 2}) {
   126  		t.Error("Invalid padded buffer for buf2")
   127  	}
   128  	if !bytes.Equal(buf3.data, []byte{0, 1, 2, 3}) {
   129  		t.Error("Invalid padded buffer for buf3")
   130  	}
   131  	if !bytes.Equal(buf4.data, []byte{1, 2, 3, 4}) {
   132  		t.Error("Invalid padded buffer for buf4")
   133  	}
   134  }
   135  
   136  func TestSerializeJSONRejectsNil(t *testing.T) {
   137  	defer func() {
   138  		r := recover()
   139  		if r == nil || !strings.Contains(r.(string), "nil pointer") {
   140  			t.Error("serialize function should not accept nil pointer")
   141  		}
   142  	}()
   143  
   144  	mustSerializeJSON(nil)
   145  }
   146  
   147  func TestFixedSizeBufferTooLarge(t *testing.T) {
   148  	defer func() {
   149  		r := recover()
   150  		if r == nil {
   151  			t.Error("should not be able to create fixed size buffer with oversized data")
   152  		}
   153  	}()
   154  
   155  	newFixedSizeBuffer(make([]byte, 2), 1)
   156  }
   157  

View as plain text