-
Notifications
You must be signed in to change notification settings - Fork 38
Account test #112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ademuanthony
wants to merge
19
commits into
planetdecred:master
Choose a base branch
from
ademuanthony:account-test
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Account test #112
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
3848f5d
Black box testing of account related functions
ademuanthony f92f1d7
Go fmt
ademuanthony 1cc4e13
Run test with docker file
ademuanthony d54a789
Random password in test. Separate docker file for test
ademuanthony 267f46a
main: add dockerfiles for tests
oluwandabira 94fb7dc
main: change workflow build names
oluwandabira bb34f10
main: correct docker command
oluwandabira e6dd0b0
main: set log levels to error for test
oluwandabira 24e1a96
main: add 1 minute timeout to docker tests
oluwandabira 5b19ab0
Removed unnecessary context info
ademuanthony b2c4005
wallet: add wallet.InternalWallet for testing
oluwandabira 17bba03
Use internal wallet directly for assertion
ademuanthony 7e670f6
Remove test root before running new test
ademuanthony 9f8dad8
gofmt
ademuanthony a97f133
Deep assertion
ademuanthony 82445c2
gofmt
ademuanthony 904fd63
Lint
ademuanthony 39d13f3
Move befor and after suit to outside block
ademuanthony fbda18b
Rebased and cleaned up
ademuanthony File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| name: Tests | ||
|
|
||
| on: [pull_request, push] | ||
|
|
||
| jobs: | ||
| linux_test: | ||
| name: Tests - linux | ||
|
|
||
| runs-on: ubuntu-latest | ||
|
|
||
| steps: | ||
| - name: Check out code into the Go module directory | ||
| uses: actions/checkout@v1 | ||
|
|
||
| - name: Build the docker image | ||
| run: docker build -t testenv -f linux_test.dockerfile . | ||
|
|
||
| - name: Run the image | ||
| run: docker run testenv | ||
|
|
||
| windows_test: | ||
| name: Tests - windows | ||
|
|
||
| runs-on: windows-latest | ||
|
|
||
| steps: | ||
| - name: Check out code into the Go module directory | ||
| uses: actions/checkout@v1 | ||
|
|
||
| - name: Build the docker image | ||
| run: docker build -t testenv -f windows_test.dockerfile . | ||
|
|
||
| - name: Run the image | ||
| run: docker run testenv | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,3 +4,4 @@ vendor | |
| Dcrlibwallet.framework | ||
| dcrlibwallet.aar | ||
| dcrlibwallet-sources.jar | ||
| .dcrlibwallet_tests_root | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,4 +6,4 @@ ENV GO111MODULE on | |
|
|
||
| RUN go version | ||
|
|
||
| ENTRYPOINT [ "./run_tests.sh", "lint"] | ||
| ENTRYPOINT [ "./run_tests.sh", "lint"] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,253 @@ | ||
| package dcrlibwallet_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "strconv" | ||
|
|
||
| w "github.com/decred/dcrwallet/wallet/v3" | ||
| "github.com/decred/dcrwallet/wallet/v3/udb" | ||
| . "github.com/onsi/ginkgo" | ||
| . "github.com/onsi/gomega" | ||
| . "github.com/raedahgroup/dcrlibwallet" | ||
| ) | ||
|
|
||
| const rootDir = ".dcrlibwallet_tests_root" | ||
|
|
||
| func init() { | ||
| SetLogLevels("error") | ||
| } | ||
|
|
||
| var _ = Describe("Account", func() { | ||
|
|
||
| getWrongAccountNumber := func() uint32 { | ||
| var accountNumber uint32 = 1220 | ||
| var err error | ||
| for err == nil { | ||
| accountNumber++ | ||
| _, err = internalWallet.AccountName(context.Background(), accountNumber) | ||
| } | ||
| return accountNumber | ||
| } | ||
|
|
||
| assertBalanceEqual := func(intBal udb.Balances, bal *Balance) { | ||
| Expect(bal.ImmatureReward).To(BeEquivalentTo(intBal.ImmatureCoinbaseRewards)) | ||
| Expect(bal.ImmatureStakeGeneration).To(BeEquivalentTo(intBal.ImmatureStakeGeneration)) | ||
| Expect(bal.LockedByTickets).To(BeEquivalentTo(intBal.LockedByTickets)) | ||
| Expect(bal.Spendable).To(BeEquivalentTo(intBal.Spendable)) | ||
| Expect(bal.Total).To(BeEquivalentTo(intBal.Total)) | ||
| Expect(bal.UnConfirmed).To(BeEquivalentTo(intBal.Unconfirmed)) | ||
| Expect(bal.VotingAuthority).To(BeEquivalentTo(intBal.VotingAuthority)) | ||
| } | ||
|
|
||
| assertAccountsEquals := func(intAcc w.AccountResult, acc *Account) { | ||
| Expect(intAcc.AccountName).To(BeEquivalentTo(acc.Name)) | ||
| Expect(intAcc.AccountNumber).To(BeEquivalentTo(acc.Number)) | ||
| Expect(intAcc.AccountNumber).To(BeEquivalentTo(acc.Number)) | ||
| Expect(int32(intAcc.LastUsedExternalIndex + 20)).To(BeEquivalentTo(acc.ExternalKeyCount)) | ||
| Expect(int32(intAcc.LastUsedInternalIndex + 20)).To(BeEquivalentTo(acc.InternalKeyCount)) | ||
| Expect(intAcc.ImportedKeyCount).To(BeEquivalentTo(acc.ImportedKeyCount)) | ||
| intBalance, err := internalWallet.CalculateAccountBalance(context.Background(), uint32(acc.Number), 0) | ||
| Expect(err).To(BeNil()) | ||
| assertBalanceEqual(intBalance, acc.Balance) | ||
| } | ||
|
|
||
| Describe("GetAccounts", func() { | ||
| It("should get accounts", func() { | ||
| res, err := wallet.GetAccounts() | ||
| By("returning a nil error") | ||
| Expect(err).To(BeNil()) | ||
| By("returning nonempty accounts string") | ||
| Expect(res).ToNot(BeEquivalentTo("")) | ||
| var accountsRes Accounts | ||
| err = json.Unmarshal([]byte(res), &accountsRes) | ||
| Expect(err).To(BeNil()) | ||
| internalAccount, err := internalWallet.Accounts(context.Background()) | ||
| Expect(err).To(BeNil()) | ||
| Expect(accountsRes.Count).To(BeEquivalentTo(len(internalAccount.Accounts))) | ||
| Expect(accountsRes.CurrentBlockHash).To(BeEquivalentTo(internalAccount.CurrentBlockHash[:])) | ||
| Expect(accountsRes.CurrentBlockHeight).To(BeEquivalentTo(internalAccount.CurrentBlockHeight)) | ||
| for i := 0; i < len(internalAccount.Accounts); i++ { | ||
| assertAccountsEquals(internalAccount.Accounts[i], accountsRes.Acc[i]) | ||
| } | ||
| }) | ||
| }) | ||
|
|
||
| Describe("GetAccount", func() { | ||
| Context("when called with the right parameters", func() { | ||
| It("should get account", func() { | ||
| var accountNumber uint32 = 0 | ||
| account, err := wallet.GetAccount(int32(accountNumber)) | ||
| By("returning a nil error") | ||
| Expect(err).To(BeNil()) | ||
| By("returning a non nil account") | ||
| Expect(account).ToNot(BeNil()) | ||
| By("returning an account with the supplied account number") | ||
| Expect(account.Number).To(BeEquivalentTo(accountNumber)) | ||
| intAccount, err := internalWallet.AccountProperties(context.Background(), accountNumber) | ||
| Expect(err).To(BeNil()) | ||
| assertAccountsEquals(w.AccountResult{ | ||
| AccountProperties: *intAccount, | ||
| }, account) | ||
| }) | ||
| }) | ||
|
|
||
| Context("when called for non-existing account", func() { | ||
| It("it should fail", func() { | ||
| account, err := wallet.GetAccount(1000000000) | ||
| By("returning a non nil error") | ||
| Expect(err).ToNot(BeNil()) | ||
| By("by returning nil account") | ||
| Expect(account).To(BeNil()) | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| Describe("GetAccountBalance", func() { | ||
| It("should get the account balance", func() { | ||
| var accountNumber uint32 = 0 | ||
| var confirmations int32 = 0 | ||
| internalBalance, err := internalWallet.CalculateAccountBalance(context.Background(), accountNumber, confirmations) | ||
| Expect(err).To(BeNil()) | ||
| balance, err := wallet.GetAccountBalance(int32(accountNumber)) | ||
| By("returning a nil error") | ||
| Expect(err).To(BeNil()) | ||
| By("returning the expected balance") | ||
| Expect(balance).ToNot(BeNil()) | ||
| assertBalanceEqual(internalBalance, balance) | ||
| }) | ||
| }) | ||
|
|
||
| Describe("SpendableForAccount", func() { | ||
| It("should return the spendable balance", func() { | ||
| var accountNumber uint32 = 0 | ||
| var confirmations int32 = 0 | ||
| internalBalance, err := internalWallet.CalculateAccountBalance(context.Background(), accountNumber, confirmations) | ||
| Expect(err).To(BeNil()) | ||
| balance, err := wallet.SpendableForAccount(int32(accountNumber)) | ||
| By("returning a nil error") | ||
| Expect(err).To(BeNil()) | ||
| By("returning the expected balance") | ||
| Expect(balance).To(BeEquivalentTo(internalBalance.Spendable)) | ||
| }) | ||
| }) | ||
|
|
||
| Describe("NextAccount", func() { | ||
| Context("when called with the right args", func() { | ||
| It("should return the next account number", func() { | ||
| accountNumber, err := wallet.NextAccount("account 1", []byte(password)) | ||
| By("returning a nil error") | ||
| Expect(err).To(BeNil()) | ||
| By("returning the expected account number") | ||
| Expect(accountNumber).To(BeEquivalentTo(int32(1))) | ||
| }) | ||
| }) | ||
| Context("when called with a wrong password", func() { | ||
| It("should failed", func() { | ||
| var wrongPasword = randomPassword() | ||
| for wrongPasword == password { | ||
| wrongPasword += "1" | ||
| } | ||
| _, err := wallet.NextAccount("account 1", []byte(wrongPasword)) | ||
| By("returning a non nil error") | ||
| Expect(err).ToNot(BeNil()) | ||
| By("reporting invalid password") | ||
| Expect(err.Error()).To(BeEquivalentTo("invalid_passphrase")) | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| Describe("RenameAccount", func() { | ||
| Context("when called with the right args", func() { | ||
| It("should rename the account", func() { | ||
| updatedName := "first account" | ||
| err := wallet.RenameAccount(0, updatedName) | ||
| By("returning a nil error") | ||
| Expect(err).To(BeNil()) | ||
|
|
||
| account, err := wallet.GetAccount(0) | ||
| Expect(err).To(BeNil()) | ||
| By("changing the account name in the wallet") | ||
| Expect(account.Name).To(BeEquivalentTo(updatedName)) | ||
| }) | ||
| }) | ||
| Context("when called with a non-existing account number", func() { | ||
| It("should fail", func() { | ||
| err := wallet.RenameAccount(56, "first account") | ||
| By("returning a non nil error") | ||
| Expect(err).ToNot(BeNil()) | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| Describe("AccountName", func() { | ||
| Context("when called with an existing account number", func() { | ||
| It("should return the account name", func() { | ||
| var accountNumber uint32 = 0 | ||
| internalAccountName, err := internalWallet.AccountName(context.Background(), accountNumber) | ||
| Expect(err).To(BeNil()) | ||
| name := wallet.AccountName(0) | ||
| By("returning the expected account name") | ||
| Expect(name).To(BeEquivalentTo(internalAccountName)) | ||
| }) | ||
| }) | ||
|
|
||
| Context("when called with a non-existing account number", func() { | ||
| It("should fail", func() { | ||
| var wrongAccountNumber uint32 = getWrongAccountNumber() | ||
| name := wallet.AccountName(int32(wrongAccountNumber)) | ||
| By("returning 'Account not found'") | ||
| Expect(name).To(BeEquivalentTo("Account not found")) | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| Describe("AccountNameRaw", func() { | ||
| Context("when called with a valid account number", func() { | ||
| It("should return the account name", func() { | ||
| var accountNumber uint32 = 0 | ||
| internalAccountName, err := internalWallet.AccountName(context.Background(), accountNumber) | ||
| Expect(err).To(BeNil()) | ||
| name, err := wallet.AccountNameRaw(accountNumber) | ||
| By("returning a nil error") | ||
| Expect(err).To(BeNil()) | ||
| By("returning the expected account name") | ||
| Expect(name).To(BeEquivalentTo(internalAccountName)) | ||
| }) | ||
| }) | ||
| Context("when called with a non-existing account number", func() { | ||
| It("should fail", func() { | ||
| var wrongAccountNumber = getWrongAccountNumber() | ||
| _, err := wallet.AccountNameRaw(wrongAccountNumber) | ||
| By("returning a non nil error") | ||
| Expect(err).ToNot(BeNil()) | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| Describe("AccountNumber", func() { | ||
| It("should return the account number", func() { | ||
| var accountNumber uint32 = 0 | ||
| internalAccountName, err := internalWallet.AccountName(context.Background(), accountNumber) | ||
| Expect(err).To(BeNil()) | ||
|
|
||
| number, err := wallet.AccountNumber(internalAccountName) | ||
| By("returning a nil error") | ||
| Expect(err).To(BeNil()) | ||
| By("returning the expected account number") | ||
| Expect(number).To(BeEquivalentTo(accountNumber)) | ||
| }) | ||
| }) | ||
|
|
||
| Describe("HDPathForAccount", func() { | ||
| It("should return the HD path for account", func() { | ||
| var accountNumber int32 = 0 | ||
| path, err := wallet.HDPathForAccount(accountNumber) | ||
| By("returning a nil error") | ||
| Expect(err).To(BeNil()) | ||
| expectedPath := LegacyTestnetHDPath + strconv.Itoa(int(accountNumber)) | ||
| By("returning the expected path") | ||
| Expect(path).To(BeEquivalentTo(expectedPath)) | ||
| }) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| package dcrlibwallet_test | ||
|
|
||
| import ( | ||
| "math/rand" | ||
| "os" | ||
| "testing" | ||
| "time" | ||
|
|
||
| w "github.com/decred/dcrwallet/wallet/v3" | ||
| . "github.com/onsi/ginkgo" | ||
| . "github.com/onsi/gomega" | ||
| . "github.com/raedahgroup/dcrlibwallet" | ||
| ) | ||
|
|
||
| var ( | ||
| wallet *Wallet | ||
| internalWallet *w.Wallet | ||
| password string | ||
| ) | ||
|
|
||
| func TestDcrlibwallet(t *testing.T) { | ||
|
|
||
| BeforeSuite(func() { | ||
| os.RemoveAll(rootDir) | ||
| multi, err := NewMultiWallet(rootDir, "", "testnet3") | ||
| Expect(err).To(BeNil()) | ||
| Expect(multi).ToNot(BeNil()) | ||
|
|
||
| password = randomPassword() | ||
| wallet, err = multi.CreateNewWallet("main", password, 0) | ||
| Expect(err).To(BeNil()) | ||
| Expect(wallet).ToNot(BeNil()) | ||
| wallet.SaveUserConfigValue(SpendUnconfirmedConfigKey, true) | ||
| internalWallet = wallet.InternalWallet() | ||
| }) | ||
|
|
||
| AfterSuite(func() { | ||
| if wallet != nil { | ||
| wallet.Shutdown() | ||
| } | ||
| }) | ||
|
|
||
| RegisterFailHandler(Fail) | ||
| RunSpecs(t, "Dcrlibwallet Suite") | ||
| } | ||
|
|
||
| func randomPassword() string { | ||
| var random = rand.New(rand.NewSource(time.Now().UnixNano())) | ||
| const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" | ||
| b := make([]byte, 8) | ||
| for i := range b { | ||
| b[i] = charset[random.Intn(len(charset))] | ||
| } | ||
| return string(b) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.