Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions cert_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2018 The Decred developers
// Copyright (c) 2018-2026 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

Expand All @@ -9,25 +9,26 @@ import (
"crypto/x509"
"encoding/pem"
"os"
"path/filepath"
"testing"
)

// TestCertCreationWithHosts creates a certificate pair with extra hosts and
// ensures the extra hosts are present in the generated files.
func TestCertCreationWithHosts(t *testing.T) {
certFile, err := os.CreateTemp(t.TempDir(), "certfile")
tempDir := t.TempDir()

certFile, err := os.Create(filepath.Join(tempDir, "certfile"))
if err != nil {
t.Fatalf("Unable to create temp certfile: %s", err)
}
certFile.Close()
defer os.Remove(certFile.Name())

keyFile, err := os.CreateTemp(t.TempDir(), "keyfile")
keyFile, err := os.Create(filepath.Join(tempDir, "keyfile"))
if err != nil {
t.Fatalf("Unable to create temp keyfile: %s", err)
}
keyFile.Close()
defer os.Remove(keyFile.Name())

// Generate cert pair with extra hosts.
hostnames := []string{"hostname1", "hostname2"}
Expand Down Expand Up @@ -57,19 +58,19 @@ func TestCertCreationWithHosts(t *testing.T) {
// TestCertCreationWithOutHosts ensures the creating a certificate pair without
// any hosts works as intended.
func TestCertCreationWithOutHosts(t *testing.T) {
certFile, err := os.CreateTemp(t.TempDir(), "certfile")
tempDir := t.TempDir()

certFile, err := os.Create(filepath.Join(tempDir, "certfile"))
if err != nil {
t.Fatalf("Unable to create temp certfile: %s", err)
}
certFile.Close()
defer os.Remove(certFile.Name())

keyFile, err := os.CreateTemp(t.TempDir(), "keyfile")
keyFile, err := os.Create(filepath.Join(tempDir, "keyfile"))
if err != nil {
t.Fatalf("Unable to create temp keyfile: %s", err)
}
keyFile.Close()
defer os.Remove(keyFile.Name())

// Generate cert pair with no extra hosts.
err = genCertPair(certFile.Name(), keyFile.Name(), nil, elliptic.P521())
Expand Down
5 changes: 2 additions & 3 deletions database/ffldb/whitebox_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) 2015-2016 The btcsuite developers
// Copyright (c) 2016-2025 The Decred developers
// Copyright (c) 2016-2026 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

Expand Down Expand Up @@ -141,8 +141,7 @@ func TestCornerCases(t *testing.T) {
t.Parallel()

// Create a file at the database path to force the open below to fail.
dbPath := t.TempDir()
_ = os.RemoveAll(dbPath)
dbPath := filepath.Join(t.TempDir(), "db")
fi, err := os.Create(dbPath)
if err != nil {
t.Errorf("os.Create: unexpected error: %v", err)
Expand Down
31 changes: 10 additions & 21 deletions internal/blockchain/common_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) 2013-2016 The btcsuite developers
// Copyright (c) 2015-2025 The Decred developers
// Copyright (c) 2015-2026 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

Expand All @@ -13,7 +13,6 @@ import (
"fmt"
"math"
"math/bits"
"os"
"reflect"
"testing"
"time"
Expand Down Expand Up @@ -86,10 +85,9 @@ func createTestDatabase(t testing.TB, dbType string, net wire.CurrencyNet) (data
}

// createTestUtxoDatabase creates a test UTXO database with the provided
// database name. It also returns a teardown function the caller should invoke
// when done testing to clean up.
func createTestUtxoDatabase(t testing.TB) (*leveldb.DB, func(), error) {
// Construct the database filepath
// database name, and ensures the database is closed when the test is completed.
func createTestUtxoDatabase(t testing.TB) (*leveldb.DB, error) {
// Construct the database filepath.
dbPath := t.TempDir()

// Open the database (will create it if needed).
Expand All @@ -100,17 +98,15 @@ func createTestUtxoDatabase(t testing.TB) (*leveldb.DB, func(), error) {
}
db, err := leveldb.OpenFile(dbPath, &opts)
if err != nil {
return nil, nil, err
return nil, err
}

// Setup a teardown function for cleaning up. This function is returned to
// the caller to be invoked when it is done testing.
teardown := func() {
// Close database when the test is complete.
t.Cleanup(func() {
_ = db.Close()
_ = os.RemoveAll(dbPath)
}
})

return db, teardown, nil
return db, nil
}

// chainSetup is used to create a new db and chain instance with the genesis
Expand All @@ -127,13 +123,7 @@ func chainSetup(t testing.TB, params *chaincfg.Params) (*BlockChain, error) {
}

// Create a test UTXO database.
utxoDb, teardownUtxoDb, err := createTestUtxoDatabase(t)
if err != nil {
return nil, err
}
t.Cleanup(func() {
teardownUtxoDb()
})
utxoBackend := createTestUtxoBackend(t)

// Copy the chain params to ensure any modifications the tests do to
// the chain parameters do not affect the global instance.
Expand All @@ -146,7 +136,6 @@ func chainSetup(t testing.TB, params *chaincfg.Params) (*BlockChain, error) {
}

// Create the main chain instance.
utxoBackend := NewLevelDbUtxoBackend(utxoDb)
chain, err := New(context.Background(),
&Config{
DB: db,
Expand Down
6 changes: 2 additions & 4 deletions internal/blockchain/indexers/existsaddrindex_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
// Copyright (c) 2021-2022 The Decred developers
// Copyright (c) 2021-2026 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

package indexers

import (
"context"
"testing"
"time"

Expand Down Expand Up @@ -35,8 +34,7 @@ func TestExistsAddrIndexAsync(t *testing.T) {
bk3 := addBlock(t, chain, &g, "bk3")

// Initialize the exists address index.
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx := t.Context()

subber := NewIndexSubscriber(ctx)
go subber.Run(ctx)
Expand Down
6 changes: 2 additions & 4 deletions internal/blockchain/indexers/indexsubscriber_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
// Copyright (c) 2021-2022 The Decred developers
// Copyright (c) 2021-2026 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

package indexers

import (
"context"
"testing"
"time"

Expand Down Expand Up @@ -34,8 +33,7 @@ func TestIndexSubscriberAsync(t *testing.T) {
bk3 := addBlock(t, chain, &g, "bk3")

// Initialize all indexes.
ctx, pCancel := context.WithCancel(context.Background())
defer pCancel()
ctx := t.Context()

subber := NewIndexSubscriber(ctx)

Expand Down
6 changes: 2 additions & 4 deletions internal/blockchain/indexers/txindex_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
// Copyright (c) 2021-2023 The Decred developers
// Copyright (c) 2021-2026 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

package indexers

import (
"context"
"fmt"
"sync"
"testing"
Expand Down Expand Up @@ -326,8 +325,7 @@ func TestTxIndexAsync(t *testing.T) {
bk3 := addBlock(t, chain, &g, "bk3")

// Initialize the tx index.
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx := t.Context()

subber := NewIndexSubscriber(ctx)
go subber.Run(ctx)
Expand Down
9 changes: 3 additions & 6 deletions internal/blockchain/utxobackend_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021-2023 The Decred developers
// Copyright (c) 2021-2026 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

Expand All @@ -17,16 +17,13 @@ import (
)

// createTestUtxoBackend creates a test backend with the utxo set bucket.
func createTestUtxoBackend(t *testing.T) UtxoBackend {
func createTestUtxoBackend(t testing.TB) UtxoBackend {
t.Helper()

db, teardown, err := createTestUtxoDatabase(t)
db, err := createTestUtxoDatabase(t)
if err != nil {
t.Fatalf("error creating test database: %v", err)
}
t.Cleanup(func() {
teardown()
})

return NewLevelDbUtxoBackend(db)
}
Expand Down
9 changes: 2 additions & 7 deletions internal/blockchain/validate_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) 2013-2016 The btcsuite developers
// Copyright (c) 2015-2025 The Decred developers
// Copyright (c) 2015-2026 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

Expand Down Expand Up @@ -286,15 +286,10 @@ func TestCheckBlockHeaderContext(t *testing.T) {
}

// Create a test UTXO database.
utxoDb, teardownUtxoDb, err := createTestUtxoDatabase(t)
if err != nil {
t.Fatalf("Failed to create UTXO database: %v\n", err)
}
defer teardownUtxoDb()
utxoBackend := createTestUtxoBackend(t)

// Create a new BlockChain instance using the underlying database for
// the simnet network.
utxoBackend := NewLevelDbUtxoBackend(utxoDb)
chain, err := New(context.Background(),
&Config{
DB: db,
Expand Down
5 changes: 2 additions & 3 deletions internal/integration/rpctests/treasury_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020-2022 The Decred developers
// Copyright (c) 2020-2026 The Decred developers

// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
Expand Down Expand Up @@ -242,8 +242,7 @@ func TestTreasury(t *testing.T) {
t.Fatal(err)
}

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx := t.Context()

err = hn.SetUp(ctx, false, 0)
if err != nil {
Expand Down