Ejemplo n.º 1
0
func TestPaymentRequired(t *testing.T) {
	addr := ":9443"
	conf := conf.Conf{
		Policies: []conf.Policy{
			conf.Policy{
				Selector: conf.PolicySelector{},
				Cmd:      conf.MIN_FEE,
				Args:     []interface{}{msg.PaymentValue{1, msg.BTC}},
			},
		},
	}
	handler := calc.CalcService{Conf: &conf}
	s := Server{
		Cred:    &cred.Cred{},
		Addr:    addr,
		Handler: handler,
		Conf:    &conf,
	}
	listener, err := net.Listen("tcp", s.Addr)
	defer listener.Close()
	if err != nil {
		log.Fatal(err)
	}
	go s.Serve(listener)

	c, err := newClient(nil)
	if err != nil {
		log.Fatal(err)
	}
	req := calc.NewCalcReq([]string{"1 2 +"})
	resp, err := c.SignAndSend(addr, req)
	if resp.Status != msg.PAYMENT_REQUIRED {
		t.Errorf("expected PAYMENT_REQUIRED, but got: %v\n", resp.Status)
	}
}
Ejemplo n.º 2
0
func TestRoundTrip(t *testing.T) {
	addr := ":9443"
	handler := calc.CalcService{}
	s := Server{
		Cred:    &cred.Cred{},
		Addr:    addr,
		Handler: handler,
	}
	listener, err := net.Listen("tcp", s.Addr)
	defer listener.Close()
	if err != nil {
		log.Fatal(err)
	}
	go s.Serve(listener)

	c, err := newClient(nil)
	if err != nil {
		log.Fatal(err)
	}

	req := calc.NewCalcReq([]string{"1 2 +"})
	println("send req")
	resp, err := c.SignAndSend(addr, req)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Printf("resp: %v\nbody: %v\n", resp, string(resp.Body))

	if resp.Status != msg.OK {
		t.Errorf("expected OK, but got: %v\n", resp.Status)
	}
}
Ejemplo n.º 3
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()
	}
}
Ejemplo n.º 4
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()
	}
}
Ejemplo n.º 5
0
func TestPaymentRoundTrip(t *testing.T) {
	printBitcoindExpected()
	btcConf, err := util.LoadBitcoindConf("")
	if err != nil {
		t.Errorf(err.Error())
	}

	addr := ":9443"
	services := make(map[string]Handler)
	services[calc.SERVICE_NAME] = calc.CalcService{
		Conf: &conf.Conf{
			Policies: []conf.Policy{
				conf.Policy{
					Selector: conf.PolicySelector{
						Service: calc.SERVICE_NAME,
						Method:  calc.CALCULATE_METHOD,
					},
					Cmd:  conf.MIN_FEE,
					Args: []interface{}{msg.PaymentValue{2e6, msg.BTC}},
				},
			},
		},
	}
	services[payment.SERVICE_NAME] = &payment.PaymentService{BitcoindConf: btcConf}
	mux := ServiceMux{
		Services: services,
	}
	s := Server{
		Cred:    &cred.Cred{},
		Addr:    addr,
		Handler: &mux,
	}
	listener, err := net.Listen("tcp", s.Addr)
	defer listener.Close()
	if err != nil {
		log.Fatal(err)
	}

	c, err := newClient(btcConf)
	if err != nil {
		log.Fatal(err)
	}

	// Quote
	go s.Serve(listener)
	fmt.Printf("quote\n")
	calcReq := calc.NewCalcReq([]string{"1 2 +"})
	work, err := calc.Measure(calcReq)
	if err != nil {
		log.Fatal(err)
	}
	quoteReq := calc.NewQuoteReq(work)
	fmt.Printf("quote req: %v\n", quoteReq)
	resp, err := c.SignAndSend(addr, quoteReq)
	if err != nil {
		log.Fatal(err)
	}
	pv, err := msg.NewPaymentValue(string(resp.Body))
	fmt.Printf("resp: %v\nbody: %v\n", resp, string(resp.Body))
	if err != nil {
		log.Fatal(err)
	}

	// Get payment address
	go s.Serve(listener)
	fmt.Printf("get payment addr\n")
	payAddrReq := payment.NewPaymentAddrReq(msg.BTC)
	fmt.Printf("req: %v\n", payAddrReq)
	resp, err = c.SignAndSend(addr, payAddrReq)
	if err != nil {
		log.Fatal(err)
	}
	pa, err := msg.NewPaymentAddr(string(resp.Body))
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("resp: %v\npa: %v\n", resp, pa)

	// Send low payment
	// TODO(ortutay): separate test for this
	go s.Serve(listener)
	fmt.Printf("send req with deferred payment")
	lowPv := msg.PaymentValue(*pv)
	lowPv.Amount -= 1
	calcReqLowPv := msg.OcReq(*calcReq)
	calcReqLowPv.AttachDeferredPayment(&lowPv)
	resp, err = c.SignAndSend(addr, &calcReqLowPv)
	if resp.Status != msg.TOO_LOW {
		log.Fatalf("expected status %v, got %v", msg.TOO_LOW, resp.Status)
	}

	// Send requested payment as deferred
	go s.Serve(listener)
	fmt.Printf("send req with deferred payment")
	calcReq.AttachDeferredPayment(pv)
	resp, err = c.SignAndSend(addr, calcReq)
	if resp.Status != msg.OK {
		log.Fatalf("expected status %v, got %v", msg.OK, resp.Status)
	}
	fmt.Printf("resp: %v\n", resp)

	// We got the response, now send the actual payment
	// (normally, we would want to verify the results)
	go s.Serve(listener)
	txid, err := c.SendBtcPayment(pv, pa)
	if err != nil {
		log.Fatal(err)
	}
	txidReq := payment.NewBtcTxidReq(txid)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("txid req: %v\n", txidReq)
	resp, err = c.SignAndSend(addr, txidReq)
	if resp.Status != msg.OK {
		log.Fatalf("expected status %v, got %v", msg.OK, resp.Status)
	}
	fmt.Printf("resp: %v\n", resp)
}