예제 #1
0
func TestPeerFromReq(t *testing.T) {
	defer os.RemoveAll(testutil.InitDir(t))
	ocCred := cred.NewOcCred()
	btcConf, err := util.LoadBitcoindConf("")
	if err != nil {
		t.Fatal(err)
	}
	// TODO(ortutay): this test is flakey, as we may not have any BTC at all
	btcCreds, err := cred.GetBtcCredInRange(0, util.B2S(1000), btcConf)
	if err != nil {
		t.Fatal(err)
	}
	req := calc.NewCalcReq([]string{"1 2 +"})
	err = ocCred.SignOcReq(req)
	if err != nil {
		t.Fatal(err)
	}
	for _, bc := range *btcCreds {
		err = bc.SignOcReq(req, btcConf)
		if err != nil {
			t.Fatal(err)
		}
	}
	p, err := NewPeerFromReq(req, btcConf)
	if err != nil {
		t.Fatal(err)
	}
	if p.ID != req.ID {
		t.FailNow()
	}
}
예제 #2
0
func TestPaymentValueServedToOcID(t *testing.T) {
	defer os.RemoveAll(testutil.InitDir(t))
	id := msg.OcID("id-123")
	otherID := msg.OcID("id-other")
	_, err := Put(&Record{
		Role:         SERVER,
		ID:           id,
		Status:       SUCCESS_PAID,
		PaymentValue: &msg.PaymentValue{Amount: 1000, Currency: msg.BTC},
	})
	if err != nil {
		t.Fatal(err)
	}
	_, err = Put(&Record{
		Role:         SERVER,
		ID:           id,
		Status:       SUCCESS_UNPAID,
		PaymentValue: &msg.PaymentValue{Amount: 2000, Currency: msg.BTC},
	})
	if err != nil {
		t.Fatal(err)
	}
	_, err = Put(&Record{
		Role:         SERVER,
		ID:           otherID,
		Status:       SUCCESS_PAID,
		PaymentValue: &msg.PaymentValue{Amount: 3000, Currency: msg.BTC},
	})
	if err != nil {
		t.Fatal(err)
	}
	_, err = Put(&Record{
		Role:         SERVER,
		ID:           id,
		Status:       PENDING,
		PaymentValue: &msg.PaymentValue{Amount: 4000, Currency: msg.BTC},
	})
	if err != nil {
		t.Fatal(err)
	}
	_, err = Put(&Record{
		Role:         SERVER,
		ID:           id,
		Status:       FAILURE,
		PaymentValue: &msg.PaymentValue{Amount: 5000, Currency: msg.BTC},
	})
	if err != nil {
		t.Fatal(err)
	}

	pv, err := PaymentValueServedToOcID(id)
	if err != nil {
		t.Fatal(err)
	}
	if 3000 != pv.Amount {
		t.Fatalf("%v != %v", 3000, pv.Amount)
	}
}
예제 #3
0
func TestGetNotFound(t *testing.T) {
	defer os.RemoveAll(testutil.InitDir(t))
	v, err := ocIDForCoin("1abc")
	if err != nil {
		t.Fatal(err)
	}
	if v != nil {
		t.FailNow()
	}
}
예제 #4
0
func TestGetAddr(t *testing.T) {
	defer os.RemoveAll(testutil.InitDir(t))
	btcConf, err := util.LoadBitcoindConf("")
	if err != nil {
		t.Errorf("%v", err)
	}
	req := NewPaymentAddrReq(msg.BTC)
	ps := PaymentService{BitcoindConf: btcConf}
	_, err = ps.getPaymentAddr(req)
	if err != nil {
		log.Fatal(err)
	}
}
예제 #5
0
func TestAmountPaid(t *testing.T) {
	defer os.RemoveAll(testutil.InitDir(t))
	btcConf, err := util.LoadBitcoindConf("")
	if err != nil {
		t.Fatal(err)
	}
	peer := Peer{ID: msg.OcID("123id")}
	otherPeer := Peer{ID: msg.OcID("456otherid")}

	addr, err := peer.PaymentAddr(1, btcConf)
	if err != nil {
		t.Fatal(err)
	}
	otherAddr, err := otherPeer.PaymentAddr(1, btcConf)
	if err != nil {
		t.Fatal(err)
	}
	fmt.Printf("addr %v other addr %v\n", addr, otherAddr)

	// Send some BTC to ourselves.
	amt := int64(1e6)
	cmd, err := btcjson.NewSendToAddressCmd("", addr, amt)
	if err != nil {
		log.Fatal(err)
	}
	sendBtcResp, err := util.SendBtcRpc(cmd, btcConf)
	_, ok := sendBtcResp.Result.(string)
	if !ok {
		log.Fatal(sendBtcResp)
	}

	// Send some BTC to another address.
	cmd, err = btcjson.NewSendToAddressCmd("", otherAddr, 1e6)
	if err != nil {
		log.Fatal(err)
	}
	sendBtcResp, err = util.SendBtcRpc(cmd, btcConf)
	_, ok = sendBtcResp.Result.(string)
	if !ok {
		log.Fatal(sendBtcResp)
	}

	// Verify balance
	pv, err := peer.AmountPaid(0, btcConf)
	if err != nil {
		log.Fatal(err)
	}
	if pv.Amount != amt {
		t.Fatalf("%v != %v", paid, amt)
	}
}
예제 #6
0
func TestGetAndSet(t *testing.T) {
	defer os.RemoveAll(testutil.InitDir(t))
	id := msg.OcID("1abc")
	err := setOcIDForCoin("1abc", &id)
	if err != nil {
		t.Fatalf("err: %v", err.Error())
	}
	id2, err := ocIDForCoin("1abc")
	if err != nil {
		t.Fatalf("err: %v", err)
	}
	fmt.Printf("id2: %v\n", id2)
	if id != *id2 {
		t.Fatalf("%v %v", id, id2)
	}
}
예제 #7
0
func TestPeerFromReqCoinReuse(t *testing.T) {
	defer os.RemoveAll(testutil.InitDir(t))
	ocCred1 := cred.NewOcCred()
	ocCred2 := cred.NewOcCred()
	btcConf, err := util.LoadBitcoindConf("")
	if err != nil {
		t.Fatal(err)
	}
	// TODO(ortutay): this test is flakey, as we may not have any BTC at all
	btcCreds, err := cred.GetBtcCredInRange(0, util.B2S(1000), btcConf)
	if err != nil {
		t.Fatal(err)
	}
	req1 := calc.NewCalcReq([]string{"1 2 +"})
	err = ocCred1.SignOcReq(req1)
	if err != nil {
		t.Fatal(err)
	}
	req2 := calc.NewCalcReq([]string{"1 2 +"})
	err = ocCred2.SignOcReq(req2)
	if err != nil {
		t.Fatal(err)
	}
	for _, bc := range *btcCreds {
		err = bc.SignOcReq(req1, btcConf)
		err = bc.SignOcReq(req2, btcConf)
		if err != nil {
			t.Fatal(err)
		}
	}
	p1, err := NewPeerFromReq(req1, btcConf)
	if err != nil {
		t.Fatal(err)
	}
	if p1.ID != req1.ID {
		t.FailNow()
	}
	p2, err := NewPeerFromReq(req2, btcConf)
	if err == nil || err != COIN_REUSE {
		t.Fatal("Expected COIN_REUSE error")
	}
	if p2 != nil {
		t.FailNow()
	}
}
예제 #8
0
func TestGetAddr(t *testing.T) {
	defer os.RemoveAll(testutil.InitDir(t))
	btcConf, err := util.LoadBitcoindConf("")
	if err != nil {
		t.Errorf("%v", err)
	}
	p := Peer{ID: msg.OcID("123id")}
	addr1, err := p.PaymentAddr(1, btcConf)
	if err != nil {
		log.Fatal(err)
	}
	addr2, err := p.PaymentAddr(1, btcConf)
	if err != nil {
		log.Fatal(err)
	}
	if addr1 != addr2 {
		t.Fatalf("%v != %v\n", addr1, addr2)
	}
}
예제 #9
0
func TestRepPut(t *testing.T) {
	defer os.RemoveAll(testutil.InitDir(t))
	rec := Record{
		Role:         SERVER,
		Service:      "store",
		Method:       "put",
		Timestamp:    1234,
		ID:           msg.OcID("id-123"),
		Status:       SUCCESS_PAID,
		PaymentType:  msg.TXID,
		PaymentValue: &msg.PaymentValue{Amount: 1000, Currency: msg.BTC},
		Perf:         nil,
	}

	_, err := Put(&rec)
	if err != nil {
		t.Fatal(err)
	}
}
예제 #10
0
func TestStoreBlob(t *testing.T) {
	defer os.RemoveAll(testutil.InitDir(t))
	r := strings.NewReader(strings.Repeat("abc", 1))
	blob, err := NewBlobFromReader(r)
	if err != nil {
		t.Fatal(err)
	}
	storeBlob(blob)
	blob2, err := NewBlobFromDisk(blob.ID)
	if err != nil {
		t.Fatal(err)
	}
	fmt.Printf("blob: %v\n", blob.ShortString())
	fmt.Printf("blob2: %v\n", blob2.ShortString())
	if blob.String() != blob2.String() {
		t.Fatalf("blobs do not match: %v != %v\n",
			blob.ShortString(), blob2.ShortString())
	}
	// TODO(ortutay): verify that files were written
	// TODO(ortutay): verify only 2 files were written
}
예제 #11
0
func TestSuccessRate(t *testing.T) {
	defer os.RemoveAll(testutil.InitDir(t))
	id := msg.OcID("id-123")
	otherID := msg.OcID("id-other")
	_, err := Put(&Record{Role: SERVER, ID: id, Status: SUCCESS_PAID})
	if err != nil {
		t.Fatal(err)
	}
	_, err = Put(&Record{Role: SERVER, ID: id, Status: PENDING})
	if err != nil {
		t.Fatal(err)
	}
	_, err = Put(&Record{Role: SERVER, ID: id, Status: FAILURE})
	if err != nil {
		t.Fatal(err)
	}
	_, err = Put(&Record{Role: SERVER, ID: otherID, Status: FAILURE})
	if err != nil {
		t.Fatal(err)
	}

	rate, err := SuccessRate(&Record{Role: SERVER, ID: id})
	if err != nil {
		t.Fatal(err)
	}
	if .5 != rate {
		t.Fatal("Expected %v, got %v", .5, rate)
	}

	rate, err = SuccessRate(&Record{Role: CLIENT, ID: id})
	if err != nil {
		t.Fatal(err)
	}
	if -1 != rate {
		t.Fatal("Expected %v, got %v", -1, rate)
	}
}