Debian Codegraph @

actions/protector_test.go

raw
1 /*
2 * protector_test.go - tests for creating protectors
3 *
4 * Copyright 2017 Google Inc.
5 * Author: Joe Richey ([email protected])
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
8 * use this file except in compliance with the License. You may obtain a copy of
9 * the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16 * License for the specific language governing permissions and limitations under
17 * the License.
18 */
19
20 package actions
21
22 import (
23 "bytes"
24 "testing"
25
26 "github.com/pkg/errors"
27
28 "github.com/google/fscrypt/crypto"
29 )
30
31 const testProtectorName = "my favorite protector"
32 const testProtectorName2 = testProtectorName + "2"
33
34 var errCallback = errors.New("bad callback")
35
36 func goodCallback(info ProtectorInfo, retry bool) (*crypto.Key, error) {
37 return crypto.NewFixedLengthKeyFromReader(bytes.NewReader(timingPassphrase), len(timingPassphrase))
38 }
39
40 func badCallback(info ProtectorInfo, retry bool) (*crypto.Key, error) {
41 return nil, errCallback
42 }
43
44 // Tests that we can create a valid protector.
45 func TestCreateProtector(t *testing.T) {
46 p, err := CreateProtector(testContext, testProtectorName, goodCallback, nil)
47 if err != nil {
48 t.Error(err)
49 } else {
50 p.Lock()
51 p.Destroy()
52 }
53 }
54
55 // Tests that a failure in the callback is relayed back to the caller.
56 func TestBadCallback(t *testing.T) {
57 p, err := CreateProtector(testContext, testProtectorName, badCallback, nil)
58 if err == nil {
59 p.Lock()
60 p.Destroy()
61 }
62 if err != errCallback {
63 t.Error("callback error was not relayed back to caller")
64 }
65 }