github.com/coreos/mantle@v0.13.0/storage/object_test.go (about) 1 // Copyright 2016 CoreOS, Inc. 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 storage 16 17 import ( 18 "strings" 19 "testing" 20 21 "google.golang.org/api/storage/v1" 22 ) 23 24 const ( 25 testPage = `<html><head> 26 <meta http-equiv="refresh" content="0;url=amd64-usr/"> 27 </head></html> 28 ` 29 testPageCRC = "xH9jaw==" 30 testPageMD5 = "2a6rirkVBEsl0bzTOzNtzA==" 31 testPageSize = 83 32 ) 33 34 func TestSortObjects(t *testing.T) { 35 slice := []*storage.Object{ 36 &storage.Object{Name: "a2"}, 37 &storage.Object{Name: "a10"}, 38 &storage.Object{Name: "a1"}, 39 } 40 SortObjects(slice) 41 if slice[0].Name != "a1" || 42 slice[1].Name != "a2" || 43 slice[2].Name != "a10" { 44 t.Errorf("Undexpected order: %#v", slice) 45 } 46 } 47 48 func TestCRCSum(t *testing.T) { 49 obj := storage.Object{} 50 if err := crcSum(&obj, strings.NewReader(testPage)); err != nil { 51 t.Fatal(err) 52 } 53 if obj.Crc32c != testPageCRC { 54 t.Errorf("Bad CRC32c: %q != %q", obj.Crc32c, testPageCRC) 55 } 56 if obj.Size != testPageSize { 57 t.Errorf("Bad Size: %d != %d", obj.Size, testPageSize) 58 } 59 } 60 61 func TestCRCEq(t *testing.T) { 62 obj := storage.Object{Crc32c: testPageCRC, Size: testPageSize} 63 if crcEq(&obj, nil) { 64 t.Errorf("%#v equal to nil", obj) 65 } 66 if crcEq(nil, &obj) { 67 t.Errorf("nil equal to %#v", obj) 68 } 69 if crcEq(nil, nil) { 70 t.Error("nil not equal to nil") 71 } 72 if crcEq(&obj, &storage.Object{Crc32c: testPageCRC}) { 73 t.Errorf("%#v equal ignored size", obj) 74 } 75 if crcEq(&obj, &storage.Object{Size: testPageSize}) { 76 t.Errorf("%#v equal ignored blank CRC", obj) 77 } 78 if !crcEq(&obj, &obj) { 79 t.Errorf("%#v not equal to itself", obj) 80 } 81 } 82 83 func TestCRCSumAndEq(t *testing.T) { 84 var a, b storage.Object 85 r := strings.NewReader(testPage) // reading twice should work 86 if err := crcSum(&a, r); err != nil { 87 t.Fatal(err) 88 } 89 r.Seek(0, 0) 90 if err := crcSum(&b, r); err != nil { 91 t.Fatal(err) 92 } 93 if !crcEq(&a, &b) { 94 t.Errorf("%#v not equal to %#v", a, b) 95 } 96 c := storage.Object{Crc32c: testPageCRC, Size: testPageSize} 97 if !crcEq(&a, &c) { 98 t.Errorf("%#v not equal to %#v", a, c) 99 } 100 }