Example #1
0
func TestCounters(t *testing.T) {
	// Open store.
	f, err := ioutil.TempFile("", "tempstore")
	if err != nil {
		t.Fatal(err)
	}
	fName := f.Name()
	f.Close()

	pass := []byte("password")
	l, err := store.Open(fName)
	s, _, err := l.Construct(pass)
	if err != nil {
		t.Fatal(err)
	}

	// Start.

	// Try getting counter for when it doesn't exist.
	c, err := s.GetCounter(0)
	if err != nil {
		t.Error(err)
	}
	if 1 != c {
		t.Errorf("For counter expected %d got %d", 1, c)
	}

	// Try setting counter value.
	err = s.SetCounter(0, 34)
	if err != nil {
		t.Error(err)
	}

	// Check if value was saved correctly.
	c, err = s.GetCounter(0)
	if err != nil {
		t.Error(err)
	}
	if 34 != c {
		t.Errorf("For counter expected %d got %d", 34, c)
	}

	// Close database.
	err = s.Close()
	if err != nil {
		t.Fatal(err)
	}
	os.Remove(fName)
}
Example #2
0
func NewUserData(t *testing.T) *store.UserData {
	// Open store.
	f, err := ioutil.TempFile("", "tempstore")
	if err != nil {
		t.Fatal(err)
	}
	fName := f.Name()
	f.Close()

	pass := []byte("password")
	l, err := store.Open(fName)
	s, _, err := l.Construct(pass)
	if err != nil {
		t.Fatal(err)
	}

	uname := "cosmos"
	u, err := s.NewUser(uname)
	if err != nil {
		t.Error(" could not create user: ", err)
	}

	return u
}
Example #3
0
// openDatabases returns an instance of keymgr.Manager, and store.Store based on
// the configuration.
func openDatabases(cfg *Config) (*keymgr.Manager, *store.Store, *store.PKRequests, error) {

	// Read key file.
	keyFile, err := ioutil.ReadFile(cfg.keyfilePath)
	if err != nil {
		return nil, nil, nil, err
	}

	var kmgr *keymgr.Manager

	if cfg.NoPass { // If allowed, check for plaintext key file.
		// Attempt to load unencrypted key file.
		kmgr, err = keymgr.FromPlaintext(bytes.NewBuffer(keyFile))
		if err != nil {
			return nil, nil, nil, err
		}
	}

	if kmgr == nil {

		// Read key file passphrase from console.
		keyfilePass, err := promptKeyfilePassPhrase()
		if err != nil {
			return nil, nil, nil, err
		}

		// Create an instance of key manager.
		kmgr, err = keymgr.FromEncrypted(keyFile, keyfilePass)
		if err != nil {
			return nil, nil, nil, fmt.Errorf("Failed to create key manager: %v", err)
		}

		cfg.keyfilePass = keyfilePass
	}

	load, err := store.Open(cfg.storePath)
	if err != nil {
		return nil, nil, nil, err
	}

	var dstore *store.Store
	var pk *store.PKRequests

	if cfg.NoPass && !load.IsEncrypted() {
		dstore, pk, err = load.Construct(nil)
	} else {

		// Read store passphrase from console.
		storePass, err := promptStorePassPhrase()
		if err != nil {
			return nil, nil, nil, err
		}

		// Open store.
		dstore, pk, err = load.Construct(storePass)
		if err != nil {
			return nil, nil, nil, fmt.Errorf("Failed to open data store: %v", err)
		}
	}

	return kmgr, dstore, pk, nil
}
Example #4
0
// createDatabases prompts the user for information needed to generate a new
// key file and data store and generates them accordingly. The new databases
// will reside at the provided path.
func createDatabases(cfg *Config) error {
	var keyfilePass, storePass []byte
	var err error
	var prompt string

	// Create default mailboxes and associated data.
	var username string
	if cfg.Username != "" {
		username = cfg.Username
	} else {
		// Prompt user for username.
		prompt = "\nEnter your username"

		username, err = promptUsername(prompt)
		if err != nil {
			return err
		}
		cfg.Username = username
	}

	// Ascertain the address generation seed.
	var seed []byte
	if cfg.Seed != "" {
		seed, err = hex.DecodeString(cfg.Seed)
	} else {
		seed, err = promptConsoleSeed()
	}
	if err != nil {
		return err
	}

	if cfg.NoPass {
		storePass = []byte{}
	} else {
		// Prompt for the private passphrase for the data store.
		prompt = "\nEnter passphrase for the data store"
		for {
			storePass, err = promptConsolePass(prompt, true)
			if err != nil {
				return err
			}

			if storePass != nil {
				break
			}
		}
	}

	// Intialize key manager with seed.
	kmgr, err := keymgr.New(seed)
	if err != nil {
		return err
	}

	// Create the data store.
	fmt.Println("Creating the data store...")
	load, err := store.Open(cfg.storePath)
	if err != nil {
		return fmt.Errorf("Failed to create data store: %v", err)
	}

	s, _, err := load.Construct(storePass)
	if err != nil {
		return fmt.Errorf("Failed to create data store: %v", err)
	}

	user, err := s.NewUser(username)
	if err != nil {
		return err
	}

	err = email.InitializeUser(user, kmgr, cfg.GenKeys)
	if err != nil {
		return err
	}
	fmt.Println("The data store has successfully been created with default mailboxes.")

	err = load.Close()
	if err != nil {
		return err
	}

	if cfg.NoPass {
		keyfilePass = []byte{}
	} else {
		// Prompt for the private passphrase for the key file.
		prompt = "Enter passphrase for the key file"
		for {
			keyfilePass, err = promptConsolePass(prompt, true)
			if err != nil {
				return err
			}

			if keyfilePass != nil {
				break
			}
		}
	}

	// Create the key file.
	fmt.Println("\nCreating the key file...")
	// Save key file to disk with the specified passphrase, if one was given.
	saveKeyfile(kmgr, cfg.keyfilePath, keyfilePass)
	fmt.Println("Keyfile saved.")

	return nil
}
Example #5
0
func TestOpenClose(t *testing.T) {
	f, err := ioutil.TempFile("", "tempstore")
	if err != nil {
		panic(err)
	}
	fName := f.Name()
	f.Close()

	pass := []byte("password")
	passNew := []byte("new_password")

	// Create a new database
	l, err := store.Open(fName)
	s, _, err := l.Construct(pass)
	if err != nil {
		t.Fatal("Failed to open database:", err)
	}
	err = s.Close()
	if err != nil {
		t.Error("Failed to close database:", err)
	}

	// Try opening same database but with incorrect passphrase
	l, err = store.Open(fName)
	if err != nil {
		t.Fatal(err)
	}
	s, _, err = l.Construct(passNew)
	if err != store.ErrDecryptionFailed {
		println("Ending test prematurely")
		t.Fatal("Expected ErrDecryptionFailed got", err)
	}

	// Try re-opening database with correct passphrase, to make sure decryption
	// works.
	l, err = store.Open(fName)
	if err != nil {
		t.Fatal("Failed to open database:", err)
	}
	s, _, err = l.Construct(pass)
	if err != nil {
		t.Fatal("Failed to open database:", err)
	}

	// Change passphrase and close database.
	err = s.ChangePassphrase(passNew)
	if err != nil {
		t.Fatal(err)
	}
	err = s.Close()
	if err != nil {
		t.Error("Failed to close database:", err)
	}

	// Re-open database with new passphrase to see if ChangePassphrase was
	// successful.
	l, err = store.Open(fName)
	s, _, err = l.Construct(passNew)
	if err != nil {
		t.Fatal("Failed to open database:", err)
	}
	err = s.Close()
	if err != nil {
		t.Error("Failed to close database:", err)
	}

	os.Remove(fName)
}
Example #6
0
func TestPKRequests(t *testing.T) {
	// Open store.
	f, err := ioutil.TempFile("", "tempstore")
	if err != nil {
		t.Fatal(err)
	}
	fName := f.Name()
	f.Close()

	l, err := store.Open(fName)
	s, pk, err := l.Construct([]byte("password"))
	if err != nil {
		t.Fatal(err)
	}

	addr1 := "BM-2DB6AzjZvzM8NkS3HMYWMP9R1Rt778mhN8"
	addr2 := "BM-2DAV89w336ovy6BUJnfVRD5B9qipFbRgmr"

	// Check if a non-existing address returns correct error.
	_, err = pk.LastRequestTime(addr1)
	if err != store.ErrNotFound {
		t.Error("Expected ErrNotFound, got", err)
	}

	// Test adding a new address.
	testNewPKRequest(pk, addr1, 1, t)

	// Check if a non-existing address returns correct error.
	_, err = pk.LastRequestTime(addr2)
	if err != store.ErrNotFound {
		t.Error("Expected ErrNotFound, got", err)
	}

	// Add same address that we got ErrNotFound for before.
	testNewPKRequest(pk, addr2, 1, t)

	// Check if we have the right number of addresses.
	count := 0
	err = pk.ForEach(func(address string, reqCount uint32, lastReqTime time.Time) error {
		count++
		return nil
	})
	if err != nil {
		t.Error("Got error", err)
	}
	if count != 2 {
		t.Errorf("For count, expected %v got %v", 2, count)
	}

	// Test calling New repeatedly; should increment count.
	testNewPKRequest(pk, addr1, 2, t)
	testNewPKRequest(pk, addr1, 3, t)
	testNewPKRequest(pk, addr1, 4, t)
	testNewPKRequest(pk, addr2, 2, t)

	// Remove addresses.
	err = pk.Remove(addr1)
	if err != nil {
		t.Error("Got error", err)
	}
	err = pk.Remove(addr2)
	if err != nil {
		t.Error("Got error", err)
	}

	// Check if they are there anymore.
	_, err = pk.LastRequestTime(addr1)
	if err != store.ErrNotFound {
		t.Error("Expected ErrNotFound, got", err)
	}
	_, err = pk.LastRequestTime(addr2)
	if err != store.ErrNotFound {
		t.Error("Expected ErrNotFound, got", err)
	}

	// Close database.
	err = s.Close()
	if err != nil {
		t.Fatal(err)
	}
	os.Remove(fName)
}
Example #7
0
func TestBroadcastAddresses(t *testing.T) {
	// Open store.
	f, err := ioutil.TempFile("", "tempstore")
	if err != nil {
		t.Fatal(err)
	}
	fName := f.Name()
	f.Close()

	pass := []byte("password")
	uname := "daniel"
	l, err := store.Open(fName)
	s, _, err := l.Construct(pass)
	if err != nil {
		t.Fatal(err)
	}

	u, err := s.NewUser(uname)
	if err != nil {
		t.Fatal(err)
	}

	// Start.
	addr1 := "BM-GtovgYdgs7qXPkoYaRgrLFuFKz1SFpsw"
	addr2 := "BM-BcJfZ82sHqW75YYBydFb868yAp1WGh3v"

	// Remove non-existing address.
	err = u.BroadcastAddresses.Remove(addr1)
	if err != store.ErrNotFound {
		t.Error("Expected ErrNotFound got ", err)
	}

	// Check if ForEach works correctly with no addresses in store.
	counter := 0
	u.BroadcastAddresses.ForEach(func(*bmutil.Address) error {
		counter++
		return nil
	})
	if counter != 0 {
		t.Errorf("For counter expected %d got %d", 0, counter)
	}

	// Add 2 address.
	err = u.BroadcastAddresses.Add(addr1)
	if err != nil {
		t.Error(err)
	}
	err = u.BroadcastAddresses.Add(addr2)
	if err != nil {
		t.Error(err)
	}

	// Check if both addresses have been correctly added.
	counter = 0
	u.BroadcastAddresses.ForEach(func(*bmutil.Address) error {
		counter++
		return nil
	})
	if counter != 2 {
		t.Errorf("For counter expected %d got %d", 2, counter)
	}
}
Example #8
0
func TestFolder(t *testing.T) {
	// Open store.
	f, err := ioutil.TempFile("", "tempstore")
	if err != nil {
		t.Fatal(err)
	}
	fName := f.Name()
	f.Close()

	pass := []byte("password")
	l, err := store.Open(fName)
	s, _, err := l.Construct(pass)
	if err != nil {
		t.Fatal(err)
	}

	uname := "cosmos"
	u, err := s.NewUser(uname)
	if err != nil {
		t.Error(" could not create user: "******"INBOX/Test Mailbox"

	// Try to get a mailbox that doesn't yet exist.
	_, err = u.FolderByName(name)
	if err != store.ErrNotFound {
		t.Error("Expected ErrNotFound got", err)
	}

	// Try to create a mailbox.
	mbox, err := u.NewFolder(name)
	if err != nil {
		t.Fatal("Got error", err)
	}

	// Check name.
	testName := mbox.Name()
	if name != testName {
		t.Errorf("Name, expected %s got %s", name, testName)
	}

	// Try getting non-existant message.
	_, _, err = mbox.GetMessage(1)
	if err != store.ErrNotFound {
		t.Error("Expected ErrNotFound got", err)
	}

	// Try getting last IDs when mailbox is empty.
	last, lastIdBySfx := mbox.LastID()
	if last != 0 {
		t.Error("Expected 0 got", last)
	}

	if lastIdBySfx[1] != 0 {
		t.Error("Expected 0 got", lastIdBySfx[1])
	}

	// Try deleting non-existant message.
	err = mbox.DeleteMessage(1)
	if err != store.ErrInvalidID {
		t.Error("Expected ErrInvalidID got", err)
	}

	// Try inserting a message.
	testInsertMessage(mbox, []byte("A test message."), 1, 1, t)

	// Try inserting another message with a different suffix.
	testInsertMessage(mbox, []byte("Another message"), 2, 2, t)

	// Close and re-open database to test if we can load mailboxes as intended.
	err = s.Close()
	if err != nil {
		t.Fatal(err)
	}
	l, err = store.Open(fName)
	s, _, err = l.Construct(pass)
	if err != nil {
		t.Fatal(err)
	}
	u, err = s.GetUser(uname)
	if err != nil {
		t.Fatal(err)
	}
	mbox, err = u.FolderByName(name)
	if err != nil {
		t.Fatal(err)
	}

	tc := &boltFolderTestContext{t: t, u: u, name: name}

	// Test ForEachMessage.

	// lowID = 0, highID = 0, suffix = 0 [Get all messages], expectedCount = 2
	testForEachMessage(mbox, 0, 0, 0, 2, t)

	// lowID = 0, highID = 1, suffix = 0, expectedCount = 1
	testForEachMessage(mbox, 0, 1, 0, 1, t)

	// lowID = 2, highID = 5, suffix = 0, expectedCount = 1
	testForEachMessage(mbox, 0, 1, 0, 1, t)

	// lowID = 3, highID = 5, suffix = 0, expectedCount = 0
	testForEachMessage(mbox, 3, 5, 0, 0, t)

	// lowID = 1, highID = 2, suffix = 1, expectedCount = 1
	testForEachMessage(mbox, 1, 2, 1, 1, t)

	// lowID = 1, highID = 2, suffix = 0, expectedCount = 2
	testForEachMessage(mbox, 1, 2, 0, 2, t)

	// lowID = 0, highID = 0, suffix = 1 [Get all messages of suffix 1],
	// expectedCount = 1
	testForEachMessage(mbox, 0, 0, 1, 1, t)

	// lowID = 0, highID = 0, suffix = 2 [Get all messages of suffix 2],
	// expectedCount = 1
	testForEachMessage(mbox, 0, 0, 2, 1, t)

	// lowID = 0, highID = 0, suffix = 3 [Get all messages of suffix 3],
	// expectedCount = 0
	testForEachMessage(mbox, 0, 0, 3, 0, t)

	// Test LastID. Should be 2.
	id, _ := mbox.LastID()
	if id != 2 {
		t.Errorf("Expected %d got %d", 2, id)
	}

	// Verify that the last ID for message with suffix 1 is 1.
	testLastIDBySuffix(tc, mbox, 1, 1, "A")

	// Verify that the last ID for message with suffix 2 is 2.
	testLastIDBySuffix(tc, mbox, 2, 2, "B")

	// Try deleting messages.
	testDeleteMessage(tc, mbox, 1, "C")

	// Check the last ID now. Should still be 2.
	testLastIDBySuffix(tc, mbox, 2, 2, "D")

	// Verify that the last ID for message with suffix 1 that was just deleted
	// is gone too.
	_, lastIdBySfx = mbox.LastID()
	if lastIdBySfx[1] != 0 {
		t.Error("Expected 0 got", lastIdBySfx[1])
	}

	// Verify that the last ID for message with suffix 2 is 2, as expected.
	testLastIDBySuffix(tc, mbox, 2, 2, "E")

	// Delete the last message.
	testDeleteMessage(tc, mbox, 2, "F")

	// LastID should error out.
	last, _ = mbox.LastID()
	if last != 0 {
		t.Error("Expected 0 got", last)
	}

	// Try adding mailbox with a duplicate name.
	_, err = u.NewFolder(name)
	if err != store.ErrDuplicateMailbox {
		t.Error("Expected ErrDuplicateMailbox got", err)
	}

	// Try deleting mailbox.
	err = u.DeleteFolder(name)
	if err != nil {
		t.Error("Got error", err)
	}

	// Check if mailbox was actually removed.
	_, err = u.FolderByName(name)
	if err != store.ErrNotFound {
		t.Error("Expected ErrNotFound got", err)
	}

	// Close database.
	err = s.Close()
	if err != nil {
		t.Fatal(err)
	}
	os.Remove(fName)
}