github.com/slspeek/camlistore_namedsearch@v0.0.0-20140519202248-ed6f70f7721a/pkg/blobserver/encrypt/encrypt_test.go (about) 1 /* 2 Copyright 2013 Google 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 encrypt 18 19 import ( 20 "encoding/binary" 21 "fmt" 22 "io/ioutil" 23 "sync" 24 "testing" 25 26 "camlistore.org/pkg/blob" 27 "camlistore.org/pkg/sorted" 28 "camlistore.org/pkg/test" 29 ) 30 31 var testKey = []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15} 32 33 type testStorage struct { 34 sto *storage 35 blobs *test.Fetcher 36 meta *test.Fetcher 37 38 mu sync.Mutex // guards iv 39 iv uint64 40 } 41 42 // fetchOrErrorString fetches br from sto and returns its body as a string. 43 // If an error occurs the stringified error is returned, prefixed by "Error: ". 44 func (ts *testStorage) fetchOrErrorString(br blob.Ref) string { 45 rc, _, err := ts.sto.Fetch(br) 46 var slurp []byte 47 if err == nil { 48 defer rc.Close() 49 slurp, err = ioutil.ReadAll(rc) 50 } 51 if err != nil { 52 return fmt.Sprintf("Error: %v", err) 53 } 54 return string(slurp) 55 } 56 57 func newTestStorage() *testStorage { 58 sto := &storage{ 59 index: sorted.NewMemoryKeyValue(), 60 } 61 if err := sto.setKey(testKey); err != nil { 62 panic(err) 63 } 64 ts := &testStorage{ 65 sto: sto, 66 blobs: new(test.Fetcher), 67 meta: new(test.Fetcher), 68 } 69 sto.blobs = ts.blobs 70 sto.meta = ts.meta 71 sto.testRandIV = func() []byte { 72 ts.mu.Lock() 73 defer ts.mu.Unlock() 74 var ret [16]byte 75 ts.iv++ 76 binary.BigEndian.PutUint64(ret[8:], ts.iv) 77 return ret[:] 78 } 79 return ts 80 } 81 82 func TestEncryptBasic(t *testing.T) { 83 ts := newTestStorage() 84 const blobData = "foo" 85 tb := &test.Blob{blobData} 86 tb.MustUpload(t, ts.sto) 87 88 if got := ts.fetchOrErrorString(tb.BlobRef()); got != blobData { 89 t.Errorf("Fetching plaintext blobref %v = %v; want %q", tb.BlobRef(), got, blobData) 90 } 91 92 if g, w := fmt.Sprintf("%q", ts.meta.BlobrefStrings()), `["sha1-370c753f7158504d11d8941efff4129112f2f975"]`; g != w { 93 t.Errorf("meta blobs = %v; want %v", g, w) 94 } 95 if g, w := fmt.Sprintf("%q", ts.blobs.BlobrefStrings()), `["sha1-64f05b6b313162b01db154fcc7b83238eb36c343"]`; g != w { 96 t.Errorf("enc blobs = %v; want %v", g, w) 97 } 98 99 // Make sure plainBR doesn't show up anywhere. 100 plainBR := tb.BlobRef().String() 101 for _, br := range append(ts.meta.BlobrefStrings(), ts.blobs.BlobrefStrings()...) { 102 if br == plainBR { 103 t.Fatal("plaintext blobref found in storage") 104 } 105 } 106 }