forked from cwtch.im/tapir
64 lines
1.8 KiB
Go
64 lines
1.8 KiB
Go
|
package privacypass
|
||
|
|
||
|
import (
|
||
|
"cwtch.im/tapir/primitives/core"
|
||
|
"testing"
|
||
|
)
|
||
|
|
||
|
func TestToken_SpendToken(t *testing.T) {
|
||
|
server := NewTokenServer()
|
||
|
|
||
|
token := new(Token)
|
||
|
blindedToken := token.GenBlindedToken()
|
||
|
|
||
|
signedToken := server.SignBlindedToken(blindedToken)
|
||
|
token.unblindSignedToken(signedToken)
|
||
|
|
||
|
spentToken := token.SpendToken([]byte("Hello"))
|
||
|
|
||
|
if server.IsValid(spentToken, []byte("Hello World")) == true {
|
||
|
t.Errorf("Token Should be InValid")
|
||
|
}
|
||
|
|
||
|
if server.IsValid(spentToken, []byte("Hello")) == false {
|
||
|
t.Errorf("Token Should be Valid")
|
||
|
}
|
||
|
|
||
|
if server.IsValid(spentToken, []byte("Hello")) == true {
|
||
|
t.Errorf("Token Should be Spent")
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestGenerateBlindedTokenBatch(t *testing.T) {
|
||
|
server := NewTokenServer()
|
||
|
|
||
|
clientTranscript := core.NewTranscript("privacyPass")
|
||
|
serverTranscript := core.NewTranscript("privacyPass")
|
||
|
|
||
|
tokens, blindedTokens := GenerateBlindedTokenBatch(10)
|
||
|
signedTokens, proof := server.SignBlindedTokenBatch(blindedTokens, serverTranscript)
|
||
|
|
||
|
verified := UnblindSignedTokenBatch(tokens, blindedTokens, signedTokens, server.Y, proof, clientTranscript)
|
||
|
|
||
|
if !verified {
|
||
|
t.Errorf("Something went wrong, the proof did not pass")
|
||
|
}
|
||
|
|
||
|
// Attempt to Spend All the tokens
|
||
|
for _, token := range tokens {
|
||
|
spentToken := token.SpendToken([]byte("Hello"))
|
||
|
if server.IsValid(spentToken, []byte("Hello")) == false {
|
||
|
t.Errorf("Token Should be Valid")
|
||
|
}
|
||
|
}
|
||
|
|
||
|
t.Logf("Client Transcript,: %s", clientTranscript.OutputTranscriptToAudit())
|
||
|
t.Logf("Server Transcript,: %s", serverTranscript.OutputTranscriptToAudit())
|
||
|
|
||
|
wrongTranscript := core.NewTranscript("wrongTranscript")
|
||
|
verified = UnblindSignedTokenBatch(tokens, blindedTokens, signedTokens, server.Y, proof, wrongTranscript)
|
||
|
if verified {
|
||
|
t.Errorf("Something went wrong, the proof passed with wrong transcript: %s", wrongTranscript.OutputTranscriptToAudit())
|
||
|
}
|
||
|
}
|