github.com/cayleygraph/cayley@v0.7.7/internal/decompressor/decompressor_test.go (about) 1 // Copyright 2014 The Cayley Authors. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package decompressor 16 17 import ( 18 "bytes" 19 "compress/bzip2" 20 "compress/gzip" 21 "io" 22 "strings" 23 "testing" 24 ) 25 26 var testDecompressor = []struct { 27 message string 28 input io.Reader 29 expect []byte 30 err error 31 readErr error 32 }{ 33 { 34 message: "text input", 35 input: strings.NewReader("cayley data\n"), 36 err: nil, 37 expect: []byte("cayley data\n"), 38 readErr: nil, 39 }, 40 { 41 message: "gzip input", 42 input: bytes.NewReader([]byte{ 43 0x1f, 0x8b, 0x08, 0x00, 0x5c, 0xbc, 0xcd, 0x53, 0x00, 0x03, 0x4b, 0x4e, 0xac, 0xcc, 0x49, 0xad, 44 0x54, 0x48, 0x49, 0x2c, 0x49, 0xe4, 0x02, 0x00, 0x03, 0xe1, 0xfc, 0xc3, 0x0c, 0x00, 0x00, 0x00, 45 }), 46 err: nil, 47 expect: []byte("cayley data\n"), 48 readErr: nil, 49 }, 50 { 51 message: "bzip2 input", 52 input: bytes.NewReader([]byte{ 53 0x42, 0x5a, 0x68, 0x39, 0x31, 0x41, 0x59, 0x26, 0x53, 0x59, 0xb5, 0x4b, 0xe3, 0xc4, 0x00, 0x00, 54 0x02, 0xd1, 0x80, 0x00, 0x10, 0x40, 0x00, 0x2e, 0x04, 0x04, 0x20, 0x20, 0x00, 0x31, 0x06, 0x4c, 55 0x41, 0x4c, 0x1e, 0xa7, 0xa9, 0x2a, 0x18, 0x26, 0xb1, 0xc2, 0xee, 0x48, 0xa7, 0x0a, 0x12, 0x16, 56 0xa9, 0x7c, 0x78, 0x80, 57 }), 58 err: nil, 59 expect: []byte("cayley data\n"), 60 readErr: nil, 61 }, 62 { 63 message: "bad gzip input", 64 input: strings.NewReader("\x1f\x8bcayley data\n"), 65 err: gzip.ErrHeader, 66 expect: nil, 67 readErr: nil, 68 }, 69 { 70 message: "bad bzip2 input", 71 input: strings.NewReader("\x42\x5a\x68cayley data\n"), 72 err: nil, 73 expect: nil, 74 readErr: bzip2.StructuralError("invalid compression level"), 75 }, 76 } 77 78 func TestDecompressor(t *testing.T) { 79 for _, test := range testDecompressor { 80 r, err := New(test.input) 81 if err != test.err { 82 t.Fatalf("Unexpected error for %s, got:%v expect:%v", test.message, err, test.err) 83 } 84 if err != nil { 85 continue 86 } 87 p := make([]byte, len(test.expect)*2) 88 n, err := r.Read(p) 89 if err != test.readErr && err != io.EOF { 90 t.Fatalf("Unexpected error for reading %s, got:%v expect:%v", test.message, err, test.err) 91 } 92 if bytes.Compare(p[:n], test.expect) != 0 { 93 t.Errorf("Unexpected read result for %s, got:%q expect:%q", test.message, p[:n], test.expect) 94 } 95 } 96 }