Ejemplo n.º 1
0
func TestLoadPoolAndDepositScript(t *testing.T) {
	tearDown, manager, pool := vp.TstCreatePool(t)
	defer tearDown()
	// setup
	poolID := "test"
	pubKeys := vp.TstPubKeys[0:3]
	err := vp.LoadAndCreateSeries(pool.TstNamespace(), manager, 1, poolID, 1, 2, pubKeys)
	if err != nil {
		t.Fatalf("Failed to create voting pool and series: %v", err)
	}

	// execute
	script, err := vp.LoadAndGetDepositScript(pool.TstNamespace(), manager, poolID, 1, 0, 0)
	if err != nil {
		t.Fatalf("Failed to get deposit script: %v", err)
	}

	// validate
	strScript := hex.EncodeToString(script)
	want := "5221035e94da75731a2153b20909017f62fcd49474c45f3b46282c0dafa8b40a3a312b2102e983a53dd20b7746dd100dfd2925b777436fc1ab1dd319433798924a5ce143e32102908d52a548ee9ef6b2d0ea67a3781a0381bc3570ad623564451e63757ff9393253ae"
	if want != strScript {
		t.Fatalf("Failed to get the right deposit script. Got %v, want %v",
			strScript, want)
	}
}
Ejemplo n.º 2
0
func TestDecryptExtendedKeyCannotDecrypt(t *testing.T) {
	tearDown, _, pool := vp.TstCreatePool(t)
	defer tearDown()

	_, err := pool.TstDecryptExtendedKey(waddrmgr.CKTPublic, []byte{})

	vp.TstCheckError(t, "", err, vp.ErrCrypto)
}
Ejemplo n.º 3
0
func TestDepositScriptAddressForNonExistentSeries(t *testing.T) {
	tearDown, _, pool := vp.TstCreatePool(t)
	defer tearDown()

	_, err := pool.DepositScriptAddress(1, 0, 0)

	vp.TstCheckError(t, "", err, vp.ErrSeriesNotExists)
}
Ejemplo n.º 4
0
func TestPoolCreateSeriesInvalidID(t *testing.T) {
	tearDown, _, pool := vp.TstCreatePool(t)
	defer tearDown()

	err := pool.CreateSeries(vp.CurrentVersion, 0, 1, vp.TstPubKeys[0:3])

	vp.TstCheckError(t, "", err, vp.ErrSeriesIDInvalid)
}
Ejemplo n.º 5
0
func TestCreatePoolWhenAlreadyExists(t *testing.T) {
	tearDown, mgr, pool := vp.TstCreatePool(t)
	defer tearDown()

	_, err := vp.Create(pool.TstNamespace(), mgr, pool.ID)

	vp.TstCheckError(t, "", err, vp.ErrPoolAlreadyExists)
}
Ejemplo n.º 6
0
func TestReplaceNonExistingSeries(t *testing.T) {
	tearDown, _, pool := vp.TstCreatePool(t)
	defer tearDown()

	pubKeys := vp.TstPubKeys[0:3]

	err := pool.ReplaceSeries(1, 1, 3, pubKeys)

	vp.TstCheckError(t, "", err, vp.ErrSeriesNotExists)
}
Ejemplo n.º 7
0
func TestCreatePool(t *testing.T) {
	tearDown, mgr, pool := vp.TstCreatePool(t)
	defer tearDown()

	pool2, err := vp.Create(pool.TstNamespace(), mgr, []byte{0x02})
	if err != nil {
		t.Errorf("Error creating Pool: %v", err)
	}
	if !bytes.Equal(pool2.ID, []byte{0x02}) {
		t.Errorf("Pool ID mismatch: got %v, want %v", pool2.ID, []byte{0x02})
	}
}
Ejemplo n.º 8
0
func TestLoadPool(t *testing.T) {
	tearDown, mgr, pool := vp.TstCreatePool(t)
	defer tearDown()

	pool2, err := vp.Load(pool.TstNamespace(), mgr, pool.ID)
	if err != nil {
		t.Errorf("Error loading Pool: %v", err)
	}
	if !bytes.Equal(pool2.ID, pool.ID) {
		t.Errorf("Voting pool obtained from DB does not match the created one")
	}
}
Ejemplo n.º 9
0
func TestPoolCreateSeriesWhenAlreadyExists(t *testing.T) {
	tearDown, _, pool := vp.TstCreatePool(t)
	defer tearDown()
	pubKeys := vp.TstPubKeys[0:3]
	if err := pool.CreateSeries(1, 1, 1, pubKeys); err != nil {
		t.Fatalf("Cannot create series: %v", err)
	}

	err := pool.CreateSeries(1, 1, 1, pubKeys)

	vp.TstCheckError(t, "", err, vp.ErrSeriesAlreadyExists)
}
Ejemplo n.º 10
0
func TestPoolCreateSeriesIDNotSequential(t *testing.T) {
	tearDown, _, pool := vp.TstCreatePool(t)
	defer tearDown()

	pubKeys := vp.TstPubKeys[0:4]
	if err := pool.CreateSeries(1, 1, 2, pubKeys); err != nil {
		t.Fatalf("Cannot create series: %v", err)
	}

	err := pool.CreateSeries(1, 3, 2, pubKeys)

	vp.TstCheckError(t, "", err, vp.ErrSeriesIDNotSequential)
}
Ejemplo n.º 11
0
func TestDepositScriptAddressForHardenedPubKey(t *testing.T) {
	tearDown, _, pool := vp.TstCreatePool(t)
	defer tearDown()
	if err := pool.CreateSeries(1, 1, 2, vp.TstPubKeys[0:3]); err != nil {
		t.Fatalf("Cannot creates series")
	}

	// Ask for a DepositScriptAddress using an index for a hardened child, which should
	// fail as we use the extended public keys to derive childs.
	_, err := pool.DepositScriptAddress(1, 0, vp.Index(hdkeychain.HardenedKeyStart+1))

	vp.TstCheckError(t, "", err, vp.ErrKeyChain)
}
Ejemplo n.º 12
0
func TestDecryptExtendedKeyCannotCreateResultKey(t *testing.T) {
	tearDown, mgr, pool := vp.TstCreatePool(t)
	defer tearDown()

	// the plaintext not being base58 encoded triggers the error
	cipherText, err := mgr.Encrypt(waddrmgr.CKTPublic, []byte("not-base58-encoded"))
	if err != nil {
		t.Fatalf("Failed to encrypt plaintext: %v", err)
	}

	_, err = pool.TstDecryptExtendedKey(waddrmgr.CKTPublic, cipherText)

	vp.TstCheckError(t, "", err, vp.ErrKeyChain)
}
Ejemplo n.º 13
0
func TestCreateSeries(t *testing.T) {
	tearDown, _, pool := vp.TstCreatePool(t)
	defer tearDown()

	tests := []struct {
		version uint32
		series  uint32
		reqSigs uint32
		pubKeys []string
	}{
		{
			version: 1,
			series:  1,
			reqSigs: 2,
			pubKeys: vp.TstPubKeys[0:3],
		},
		{
			version: 1,
			series:  2,
			reqSigs: 3,
			pubKeys: vp.TstPubKeys[0:5],
		},
		{
			version: 1,
			series:  3,
			reqSigs: 4,
			pubKeys: vp.TstPubKeys[0:7],
		},
		{
			version: 1,
			series:  4,
			reqSigs: 5,
			pubKeys: vp.TstPubKeys[0:9],
		},
	}

	for testNum, test := range tests {
		err := pool.CreateSeries(test.version, test.series, test.reqSigs, test.pubKeys[:])
		if err != nil {
			t.Fatalf("%d: Cannot create series %d", testNum, test.series)
		}
		exists, err := pool.TstExistsSeries(test.series)
		if err != nil {
			t.Fatal(err)
		}
		if !exists {
			t.Errorf("%d: Series %d not in database", testNum, test.series)
		}
	}
}
Ejemplo n.º 14
0
func TestPoolWithdrawalAddress(t *testing.T) {
	tearDown, _, pool := vp.TstCreatePool(t)
	defer tearDown()

	pubKeys := vp.TstPubKeys[1:4]
	vp.TstCreateSeries(t, pool, []vp.TstSeriesDef{{ReqSigs: 2, PubKeys: pubKeys, SeriesID: 1}})
	addr := vp.TstNewWithdrawalAddress(t, pool, 1, 0, 0)
	checkPoolAddress(t, addr, 1, 0, 0)

	// When the requested address is not present in the set of used addresses
	// for that Pool, we should get an error.
	_, err := pool.WithdrawalAddress(1, 2, 3)
	vp.TstCheckError(t, "", err, vp.ErrWithdrawFromUnusedAddr)
}
Ejemplo n.º 15
0
func TestEmpowerSeries(t *testing.T) {
	tearDown, mgr, pool := vp.TstCreatePool(t)
	defer tearDown()

	seriesID := uint32(1)
	if err := pool.CreateSeries(1, seriesID, 2, vp.TstPubKeys[0:3]); err != nil {
		t.Fatalf("Failed to create series: %v", err)
	}

	vp.TstRunWithManagerUnlocked(t, mgr, func() {
		if err := pool.EmpowerSeries(seriesID, vp.TstPrivKeys[0]); err != nil {
			t.Errorf("Failed to empower series: %v", err)
		}
	})
}
Ejemplo n.º 16
0
func TestEmpowerSeriesErrors(t *testing.T) {
	tearDown, _, pool := vp.TstCreatePool(t)
	defer tearDown()

	seriesID := uint32(1)
	if err := pool.CreateSeries(1, seriesID, 2, vp.TstPubKeys[0:3]); err != nil {
		t.Fatalf("Failed to create series: %v", err)
	}

	tests := []struct {
		seriesID uint32
		key      string
		err      vp.ErrorCode
	}{
		{
			seriesID: 2,
			key:      vp.TstPrivKeys[0],
			// Invalid series.
			err: vp.ErrSeriesNotExists,
		},
		{
			seriesID: seriesID,
			key:      "NONSENSE",
			// Invalid private key.
			err: vp.ErrKeyChain,
		},
		{
			seriesID: seriesID,
			key:      vp.TstPubKeys[5],
			// Wrong type of key.
			err: vp.ErrKeyIsPublic,
		},
		{
			seriesID: seriesID,
			key:      vp.TstPrivKeys[5],
			// Key not corresponding to public key.
			err: vp.ErrKeysPrivatePublicMismatch,
		},
	}

	for i, test := range tests {
		err := pool.EmpowerSeries(test.seriesID, test.key)
		vp.TstCheckError(t, fmt.Sprintf("EmpowerSeries #%d", i), err, test.err)
	}

}
Ejemplo n.º 17
0
func TestPoolChangeAddress(t *testing.T) {
	tearDown, _, pool := vp.TstCreatePool(t)
	defer tearDown()

	pubKeys := vp.TstPubKeys[1:4]
	vp.TstCreateSeries(t, pool, []vp.TstSeriesDef{{ReqSigs: 2, PubKeys: pubKeys, SeriesID: 1}})

	addr := vp.TstNewChangeAddress(t, pool, 1, 0)
	checkPoolAddress(t, addr, 1, 0, 0)

	// When the series is not active, we should get an error.
	pubKeys = vp.TstPubKeys[3:6]
	vp.TstCreateSeries(t, pool,
		[]vp.TstSeriesDef{{ReqSigs: 2, PubKeys: pubKeys, SeriesID: 2, Inactive: true}})
	_, err := pool.ChangeAddress(2, 0)
	vp.TstCheckError(t, "", err, vp.ErrSeriesNotActive)
}
Ejemplo n.º 18
0
func TestLoadAllSeries(t *testing.T) {
	tearDown, manager, pool := vp.TstCreatePool(t)
	defer tearDown()

	for _, test := range testLoadAllSeriesTests {
		pool := setUpLoadAllSeries(t, pool.TstNamespace(), manager, test)
		pool.TstEmptySeriesLookup()
		vp.TstRunWithManagerUnlocked(t, manager, func() {
			if err := pool.LoadAllSeries(); err != nil {
				t.Fatalf("Test #%d: failed to load voting pool: %v", test.id, err)
			}
		})
		for _, seriesData := range test.series {
			validateLoadAllSeries(t, pool, test.id, seriesData)
		}
	}
}
Ejemplo n.º 19
0
func TestPoolSeries(t *testing.T) {
	tearDown, _, pool := vp.TstCreatePool(t)
	defer tearDown()
	expectedPubKeys := vp.CanonicalKeyOrder(vp.TstPubKeys[0:3])
	if err := pool.CreateSeries(vp.CurrentVersion, 1, 2, expectedPubKeys); err != nil {
		t.Fatalf("Failed to create series: %v", err)
	}

	series := pool.Series(1)

	if series == nil {
		t.Fatal("Series() returned nil")
	}
	pubKeys := series.TstGetRawPublicKeys()
	if !reflect.DeepEqual(pubKeys, expectedPubKeys) {
		t.Errorf("Series pubKeys mismatch. Got %v, want %v", pubKeys, expectedPubKeys)
	}
}
Ejemplo n.º 20
0
func TestEmpowerSeriesNeuterFailed(t *testing.T) {
	tearDown, _, pool := vp.TstCreatePool(t)
	defer tearDown()

	seriesID := uint32(1)
	err := pool.CreateSeries(1, seriesID, 2, vp.TstPubKeys[0:3])
	if err != nil {
		t.Fatalf("Failed to create series: %v", err)
	}

	// A private key with bad version (0xffffffff) will trigger an
	// error in (k *ExtendedKey).Neuter and the associated error path
	// in EmpowerSeries.
	badKey := "wM5uZBNTYmaYGiK8VaGi7zPGbZGLuQgDiR2Zk4nGfbRFLXwHGcMUdVdazRpNHFSR7X7WLmzzbAq8dA1ViN6eWKgKqPye1rJTDQTvBiXvZ7E3nmdx"
	err = pool.EmpowerSeries(seriesID, badKey)

	vp.TstCheckError(t, "", err, vp.ErrKeyNeuter)
}
Ejemplo n.º 21
0
func TestLoadPoolAndReplaceSeries(t *testing.T) {
	tearDown, manager, pool := vp.TstCreatePool(t)
	defer tearDown()

	// setup
	poolID := "test"
	pubKeys := vp.TstPubKeys[0:3]
	err := vp.LoadAndCreateSeries(pool.TstNamespace(), manager, 1, poolID, 1, 2, pubKeys)
	if err != nil {
		t.Fatalf("Failed to create voting pool and series: %v", err)
	}

	pubKeys = vp.TstPubKeys[3:6]
	err = vp.LoadAndReplaceSeries(pool.TstNamespace(), manager, 1, poolID, 1, 2, pubKeys)
	if err != nil {
		t.Fatalf("Failed to replace series: %v", err)
	}
}
Ejemplo n.º 22
0
func TestPutSeriesErrors(t *testing.T) {
	tearDown, _, pool := vp.TstCreatePool(t)
	defer tearDown()

	tests := []struct {
		version uint32
		reqSigs uint32
		pubKeys []string
		err     vp.ErrorCode
		msg     string
	}{
		{
			pubKeys: vp.TstPubKeys[0:1],
			err:     vp.ErrTooFewPublicKeys,
			msg:     "Should return error when passed too few pubkeys",
		},
		{
			reqSigs: 5,
			pubKeys: vp.TstPubKeys[0:3],
			err:     vp.ErrTooManyReqSignatures,
			msg:     "Should return error when reqSigs > len(pubKeys)",
		},
		{
			pubKeys: []string{vp.TstPubKeys[0], vp.TstPubKeys[1], vp.TstPubKeys[2], vp.TstPubKeys[0]},
			err:     vp.ErrKeyDuplicate,
			msg:     "Should return error when passed duplicate pubkeys",
		},
		{
			pubKeys: []string{"invalidxpub1", "invalidxpub2", "invalidxpub3"},
			err:     vp.ErrKeyChain,
			msg:     "Should return error when passed invalid pubkey",
		},
		{
			pubKeys: vp.TstPrivKeys[0:3],
			err:     vp.ErrKeyIsPrivate,
			msg:     "Should return error when passed private keys",
		},
	}

	for i, test := range tests {
		err := pool.TstPutSeries(test.version, uint32(i+1), test.reqSigs, test.pubKeys)
		vp.TstCheckError(t, fmt.Sprintf("Create series #%d", i), err, test.err)
	}
}
Ejemplo n.º 23
0
func TestDepositScriptAddress(t *testing.T) {
	tearDown, _, pool := vp.TstCreatePool(t)
	defer tearDown()

	tests := []struct {
		version uint32
		series  uint32
		reqSigs uint32
		pubKeys []string
		// map of branch:address (we only check the branch index at 0)
		addresses map[uint32]string
	}{
		{
			version: 1,
			series:  1,
			reqSigs: 2,
			pubKeys: vp.TstPubKeys[0:3],
			addresses: map[uint32]string{
				0: "3Hb4xcebcKg4DiETJfwjh8sF4uDw9rqtVC",
				1: "34eVkREKgvvGASZW7hkgE2uNc1yycntMK6",
				2: "3Qt1EaKRD9g9FeL2DGkLLswhK1AKmmXFSe",
				3: "3PbExiaztsSYgh6zeMswC49hLUwhTQ86XG",
			},
		},
	}

	for i, test := range tests {
		if err := pool.CreateSeries(test.version, test.series,
			test.reqSigs, test.pubKeys); err != nil {
			t.Fatalf("Cannot creates series %v", test.series)
		}
		for branch, expectedAddress := range test.addresses {
			addr, err := pool.DepositScriptAddress(test.series, vp.Branch(branch), vp.Index(0))
			if err != nil {
				t.Fatalf("Failed to get DepositScriptAddress #%d: %v", i, err)
			}
			address := addr.EncodeAddress()
			if expectedAddress != address {
				t.Errorf("DepositScript #%d returned the wrong deposit script. Got %v, want %v",
					i, address, expectedAddress)
			}
		}
	}
}
Ejemplo n.º 24
0
func TestLoadPoolAndEmpowerSeries(t *testing.T) {
	tearDown, manager, pool := vp.TstCreatePool(t)
	defer tearDown()

	// setup
	poolID := "test"
	pubKeys := vp.TstPubKeys[0:3]
	err := vp.LoadAndCreateSeries(pool.TstNamespace(), manager, 1, poolID, 1, 2, pubKeys)
	if err != nil {
		t.Fatalf("Creating voting pool and Creating series failed: %v", err)
	}

	vp.TstRunWithManagerUnlocked(t, pool.Manager(), func() {
		err = vp.LoadAndEmpowerSeries(pool.TstNamespace(), manager, poolID, 1, vp.TstPrivKeys[0])
	})
	if err != nil {
		t.Fatalf("Load voting pool and Empower series failed: %v", err)
	}
}
Ejemplo n.º 25
0
func TestReplaceExistingSeries(t *testing.T) {
	tearDown, _, pool := vp.TstCreatePool(t)
	defer tearDown()

	for _, data := range replaceSeriesTestData {
		seriesID := data.orig.id
		testID := data.testID

		if err := pool.CreateSeries(data.orig.version, seriesID, data.orig.reqSigs, data.orig.pubKeys); err != nil {
			t.Fatalf("Test #%d: failed to create series in replace series setup: %v",
				testID, err)
		}

		if err := pool.ReplaceSeries(data.replaceWith.version, seriesID,
			data.replaceWith.reqSigs, data.replaceWith.pubKeys); err != nil {
			t.Errorf("Test #%d: replaceSeries failed: %v", testID, err)
		}

		validateReplaceSeries(t, pool, testID, data.replaceWith)
	}
}
Ejemplo n.º 26
0
func TestCannotReplaceEmpoweredSeries(t *testing.T) {
	tearDown, mgr, pool := vp.TstCreatePool(t)
	defer tearDown()

	seriesID := uint32(1)

	if err := pool.CreateSeries(1, seriesID, 3, vp.TstPubKeys[0:4]); err != nil {
		t.Fatalf("Failed to create series: %v", err)
	}

	vp.TstRunWithManagerUnlocked(t, mgr, func() {
		if err := pool.EmpowerSeries(seriesID, vp.TstPrivKeys[1]); err != nil {
			t.Fatalf("Failed to empower series: %v", err)
		}
	})

	err := pool.ReplaceSeries(1, seriesID, 2, []string{vp.TstPubKeys[0], vp.TstPubKeys[2],
		vp.TstPubKeys[3]})

	vp.TstCheckError(t, "", err, vp.ErrSeriesAlreadyEmpowered)
}
Ejemplo n.º 27
0
func TestLoadPoolAndCreateSeries(t *testing.T) {
	tearDown, manager, pool := vp.TstCreatePool(t)
	defer tearDown()

	poolID := "test"

	// first time, the voting pool is created
	pubKeys := vp.TstPubKeys[0:3]
	err := vp.LoadAndCreateSeries(pool.TstNamespace(), manager, 1, poolID, 1, 2, pubKeys)
	if err != nil {
		t.Fatalf("Creating voting pool and Creating series failed: %v", err)
	}

	// create another series where the voting pool is loaded this time
	pubKeys = vp.TstPubKeys[3:6]
	err = vp.LoadAndCreateSeries(pool.TstNamespace(), manager, 1, poolID, 2, 2, pubKeys)

	if err != nil {
		t.Fatalf("Loading voting pool and Creating series failed: %v", err)
	}
}