| 1 | //go:build linux |
| 2 | |
| 3 | package provisioner |
| 4 | |
| 5 | import ( |
| 6 | family "amadan.net/rastrillo/rastrillo/crypto" |
| 7 | "bytes" |
| 8 | "encoding/base64" |
| 9 | "os" |
| 10 | "path/filepath" |
| 11 | "testing" |
| 12 | ) |
| 13 | |
| 14 | func TestInitializeResumesSameDurableIdentity(t *testing.T) { |
| 15 | state, floor := persistentDirectory(t), persistentDirectory(t) |
| 16 | password := []byte("synthetic init password") |
| 17 | first, e := Initialize(state, floor, password) |
| 18 | if e != nil { |
| 19 | t.Fatal(e) |
| 20 | } |
| 21 | credential, e := os.ReadFile(filepath.Join(state, "credential.age")) |
| 22 | if e != nil { |
| 23 | t.Fatal(e) |
| 24 | } |
| 25 | next, e := Initialize(state, floor, password) |
| 26 | if e != nil || next != first { |
| 27 | t.Fatal("init identity changed", e) |
| 28 | } |
| 29 | again, e := os.ReadFile(filepath.Join(state, "credential.age")) |
| 30 | if e != nil || !bytes.Equal(credential, again) { |
| 31 | t.Fatal("credential replaced", e) |
| 32 | } |
| 33 | if _, e = Initialize(state, floor, []byte("wrong")); e != ErrRefused { |
| 34 | t.Fatal("wrong password", e) |
| 35 | } |
| 36 | if e = os.Remove(filepath.Join(state, "credential.age")); e != nil { |
| 37 | t.Fatal(e) |
| 38 | } |
| 39 | if _, e = Initialize(state, floor, password); e != ErrRefused { |
| 40 | t.Fatal("lost credential silently recreated", e) |
| 41 | } |
| 42 | } |
| 43 | func TestInitializeRefusesLostFloorAndNestedStorage(t *testing.T) { |
| 44 | state, floor := persistentDirectory(t), persistentDirectory(t) |
| 45 | password := []byte("synthetic floor-loss password") |
| 46 | if _, e := Initialize(state, floor, password); e != nil { |
| 47 | t.Fatal(e) |
| 48 | } |
| 49 | entries, e := os.ReadDir(floor) |
| 50 | if e != nil { |
| 51 | t.Fatal(e) |
| 52 | } |
| 53 | for _, entry := range entries { |
| 54 | if e = os.Remove(filepath.Join(floor, entry.Name())); e != nil { |
| 55 | t.Fatal(e) |
| 56 | } |
| 57 | } |
| 58 | if _, e = Initialize(state, floor, password); e != ErrRefused { |
| 59 | t.Fatal("lost floor silently recreated", e) |
| 60 | } |
| 61 | nested := filepath.Join(state, "nested") |
| 62 | if e = os.Mkdir(nested, 0700); e != nil { |
| 63 | t.Fatal(e) |
| 64 | } |
| 65 | if _, e = Initialize(state, nested, password); e != ErrRefused { |
| 66 | t.Fatal("floor included in registry backup scope", e) |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | func TestInitializeResumesExactGenesisBeforeDatabaseSchema(t *testing.T) { |
| 71 | state, floorPath := persistentDirectory(t), persistentDirectory(t) |
| 72 | d, e := openPrivateDirectory(state) |
| 73 | if e != nil { |
| 74 | t.Fatal(e) |
| 75 | } |
| 76 | defer d.close() |
| 77 | fd, e := openPrivateDirectory(floorPath) |
| 78 | if e != nil { |
| 79 | t.Fatal(e) |
| 80 | } |
| 81 | defer fd.close() |
| 82 | key, e := family.Generate() |
| 83 | if e != nil { |
| 84 | t.Fatal(e) |
| 85 | } |
| 86 | identity := floorIdentity{testToken(31), testToken(32), base64.RawURLEncoding.EncodeToString(key.SignPub())} |
| 87 | password := []byte("synthetic interrupted init") |
| 88 | wrapped, e := wrapCredential(key, identity, password) |
| 89 | if e != nil { |
| 90 | t.Fatal(e) |
| 91 | } |
| 92 | if e = d.create("credential.age", wrapped); e != nil { |
| 93 | t.Fatal(e) |
| 94 | } |
| 95 | floor, e := newNativeFloor(fd, identity) |
| 96 | if e != nil { |
| 97 | t.Fatal(e) |
| 98 | } |
| 99 | genesis, e := floor.signed(key, floorPoint{}, "") |
| 100 | if e != nil { |
| 101 | t.Fatal(e) |
| 102 | } |
| 103 | if e = d.create("initializing.json", genesis); e != nil { |
| 104 | t.Fatal(e) |
| 105 | } |
| 106 | if e = floor.initialize(key); e != nil { |
| 107 | t.Fatal(e) |
| 108 | } |
| 109 | if e = d.create("registry.db", nil); e != nil { |
| 110 | t.Fatal(e) |
| 111 | } |
| 112 | info, e := Initialize(state, floorPath, password) |
| 113 | if e != nil || info.Registry != identity.Registry || info.Device != identity.Device || info.PublicKey != identity.Public { |
| 114 | t.Fatal("exact genesis did not resume", e) |
| 115 | } |
| 116 | } |
| 117 | |