forked from cwtch.im/cwtch
108 lines
2.8 KiB
Go
108 lines
2.8 KiB
Go
package storage
|
|
|
|
import (
|
|
"cwtch.im/cwtch/model"
|
|
"fmt"
|
|
"os"
|
|
"testing"
|
|
)
|
|
|
|
const (
|
|
PasswordMaster = "asdf1234"
|
|
DeniablePassword = "qwerty567"
|
|
ProfilesFile = "profiles"
|
|
)
|
|
|
|
func statFileTest(t *testing.T) {
|
|
fi, err := os.Stat(ProfilesFile)
|
|
if err != nil {
|
|
fmt.Printf("no profile file: %v", err)
|
|
t.Errorf("Errors stating profile file: %v", err)
|
|
}
|
|
|
|
if fi.Size() != (DataBlockSizeSmall+HeaderSize)*6 {
|
|
fmt.Printf("Profile file size error, expected: %v actual: %v\n", (DataBlockSizeSmall+HeaderSize)*6, fi.Size())
|
|
t.Errorf("Profile file size error, expected: %v actual: %v", (DataBlockSizeSmall+HeaderSize)*6, fi.Size())
|
|
}
|
|
}
|
|
|
|
func TestProfileStore(t *testing.T) {
|
|
os.Remove(ProfilesFile)
|
|
ps := NewProfileStore(ProfilesFile, DataBlockSizeSmall, 6, 2)
|
|
|
|
statFileTest(t)
|
|
|
|
profiles, err := ps.InitializeProfileGroup(GroupMaster, PasswordMaster)
|
|
if err != nil {
|
|
t.Errorf("Error Initializing master profile group: %v", err)
|
|
}
|
|
if len(profiles) > 0 {
|
|
t.Errorf("Somehow profiles were loaded from a expected blank group")
|
|
}
|
|
|
|
statFileTest(t)
|
|
|
|
alice := model.GenerateNewProfile("Alice")
|
|
|
|
err = alice.Save()
|
|
if err == nil {
|
|
t.Error("Expected error on trying to save profile before added to ProfileStore")
|
|
}
|
|
|
|
err = ps.AddProfile(GroupMaster, alice)
|
|
if err != nil {
|
|
t.Errorf("Error adding profile to profile store: %v", err)
|
|
}
|
|
|
|
// Redundtant, but testing path
|
|
err = alice.Save()
|
|
if err != nil {
|
|
t.Errorf("Error saving profile: %v", err)
|
|
}
|
|
|
|
statFileTest(t)
|
|
|
|
carol := model.GenerateNewProfile("Carol")
|
|
err = ps.AddProfile(GroupMaster, carol)
|
|
if err != nil {
|
|
t.Errorf("Error adding profile to profile store: %v", err)
|
|
}
|
|
|
|
ps.InitializeProfileGroup(GroupDeniable1, DeniablePassword)
|
|
secretBob := model.GenerateNewProfile("Secret Bob")
|
|
err = ps.AddProfile(GroupDeniable1, secretBob)
|
|
if err != nil {
|
|
t.Errorf("Error adding profile to profile store: %v", err)
|
|
}
|
|
|
|
// Confirm loading works
|
|
psL := NewProfileStore(ProfilesFile, DataBlockSizeSmall, 6, 2)
|
|
profilesL, err := psL.InitializeProfileGroup(GroupMaster, PasswordMaster)
|
|
if err != nil {
|
|
t.Errorf("Error loading master profile group: %v", err)
|
|
}
|
|
if len(profilesL) != 2 {
|
|
t.Errorf("Error loading profiles: Expected: 2, Actual: %v", len(profilesL))
|
|
}
|
|
|
|
p1 := profilesL[0]
|
|
|
|
if p1.Name != alice.Name {
|
|
t.Errorf("Error loading profiles name expected: %v Actual: %v", alice.Name, p1.Name)
|
|
}
|
|
|
|
deniableProfiles, err := psL.InitializeProfileGroup(GroupDeniable1, DeniablePassword)
|
|
if err != nil {
|
|
t.Errorf("Error loading deniable profiles: %v", err)
|
|
}
|
|
|
|
if len(deniableProfiles) != 1 {
|
|
t.Errorf("Error loading deniable profiles: expected: 1, actual: %v", len(deniableProfiles))
|
|
} else {
|
|
if deniableProfiles[0].Name != secretBob.Name {
|
|
t.Errorf("Error loading secret Bob profile: expected: '%v' actaul: '%v'", secretBob.Name, deniableProfiles[0].Name)
|
|
}
|
|
}
|
|
|
|
}
|