| 1 |
/* |
| 2 |
* config_test.go - Tests the processing of the config file |
| 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 metadata |
| 21 |
|
| 22 |
import ( |
| 23 |
"bytes" |
| 24 |
"encoding/json" |
| 25 |
"testing" |
| 26 |
|
| 27 |
"google.golang.org/protobuf/proto" |
| 28 |
) |
| 29 |
|
| 30 |
var testConfig = &Config{ |
| 31 |
Source: SourceType_custom_passphrase, |
| 32 |
HashCosts: &HashingCosts{ |
| 33 |
Time: 10, |
| 34 |
Memory: 1 << 12, |
| 35 |
Parallelism: 8, |
| 36 |
TruncationFixed: true, |
| 37 |
}, |
| 38 |
Options: DefaultOptions, |
| 39 |
} |
| 40 |
|
| 41 |
var testConfigString = `{ |
| 42 |
"source": "custom_passphrase", |
| 43 |
"hash_costs": { |
| 44 |
"time": "10", |
| 45 |
"memory": "4096", |
| 46 |
"parallelism": "8", |
| 47 |
"truncation_fixed": true |
| 48 |
}, |
| 49 |
"options": { |
| 50 |
"padding": "32", |
| 51 |
"contents": "AES_256_XTS", |
| 52 |
"filenames": "AES_256_CTS", |
| 53 |
"policy_version": "1" |
| 54 |
}, |
| 55 |
"use_fs_keyring_for_v1_policies": false, |
| 56 |
"allow_cross_user_metadata": false |
| 57 |
} |
| 58 |
` |
| 59 |
|
| 60 |
// Used for JSON string comparison while ignoring whitespace |
| 61 |
func compact(t testing.TB, s string) string { |
| 62 |
var b bytes.Buffer |
| 63 |
if err := json.Compact(&b, []byte(s)); err != nil { |
| 64 |
t.Fatalf("failed to compact json: %v", err) |
| 65 |
} |
| 66 |
return b.String() |
| 67 |
} |
| 68 |
|
| 69 |
// Makes sure that writing a config and reading it back gives the same thing. |
| 70 |
func TestWrite(t *testing.T) { |
| 71 |
var b bytes.Buffer |
| 72 |
err := WriteConfig(testConfig, &b) |
| 73 |
if err != nil { |
| 74 |
t.Fatal(err) |
| 75 |
} |
| 76 |
t.Logf("json encoded config:\n%s", b.String()) |
| 77 |
if compact(t, b.String()) != compact(t, testConfigString) { |
| 78 |
t.Errorf("did not match: %s", testConfigString) |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
func TestRead(t *testing.T) { |
| 83 |
buf := bytes.NewBufferString(testConfigString) |
| 84 |
cfg, err := ReadConfig(buf) |
| 85 |
if err != nil { |
| 86 |
t.Fatal(err) |
| 87 |
} |
| 88 |
t.Logf("decoded config:\n%s", cfg) |
| 89 |
if !proto.Equal(cfg, testConfig) { |
| 90 |
t.Errorf("did not match: %s", testConfig) |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
// Makes sure we can parse a legacy config file that doesn't have the fields |
| 95 |
// that were added later and that has the removed "compatibility" field. |
| 96 |
func TestOptionalFields(t *testing.T) { |
| 97 |
contents := `{ |
| 98 |
"source": "custom_passphrase", |
| 99 |
"hash_costs": { |
| 100 |
"time": "10", |
| 101 |
"memory": "4096", |
| 102 |
"parallelism": "8" |
| 103 |
}, |
| 104 |
"compatibility": "legacy", |
| 105 |
"options": { |
| 106 |
"padding": "32", |
| 107 |
"contents": "AES_256_XTS", |
| 108 |
"filenames": "AES_256_CTS" |
| 109 |
} |
| 110 |
} |
| 111 |
` |
| 112 |
buf := bytes.NewBufferString(contents) |
| 113 |
cfg, err := ReadConfig(buf) |
| 114 |
if err != nil { |
| 115 |
t.Fatal(err) |
| 116 |
} |
| 117 |
if cfg.GetUseFsKeyringForV1Policies() { |
| 118 |
t.Error("use_fs_keyring_for_v1_policies should be false, but was true") |
| 119 |
} |
| 120 |
if cfg.Options.PolicyVersion != 0 { |
| 121 |
t.Errorf("policy version should be 0, but was %d", cfg.Options.PolicyVersion) |
| 122 |
} |
| 123 |
if err = cfg.CheckValidity(); err != nil { |
| 124 |
t.Error(err) |
| 125 |
} |
| 126 |
// CheckValidity() should change an unset policy version to 1. |
| 127 |
if cfg.Options.PolicyVersion != 1 { |
| 128 |
t.Errorf("policy version should be 1 now, but was %d", cfg.Options.PolicyVersion) |
| 129 |
} |
| 130 |
} |