You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
2.4 KiB

  1. package prover
  2. import (
  3. "context"
  4. "math/big"
  5. "os"
  6. "testing"
  7. "time"
  8. "github.com/hermeznetwork/hermez-node/common"
  9. "github.com/stretchr/testify/assert"
  10. "github.com/stretchr/testify/require"
  11. )
  12. const apiURL = "http://localhost:3000/api"
  13. const pollInterval = 1 * time.Second
  14. var proofServerClient *ProofServerClient
  15. func TestMain(m *testing.M) {
  16. exitVal := 0
  17. if os.Getenv("INTEGRATION") != "" {
  18. proofServerClient = NewProofServerClient(apiURL, pollInterval)
  19. err := proofServerClient.WaitReady(context.Background())
  20. if err != nil {
  21. panic(err)
  22. }
  23. exitVal = m.Run()
  24. }
  25. os.Exit(exitVal)
  26. }
  27. func TestApiServer(t *testing.T) {
  28. t.Run("testAPIStatus", testAPIStatus)
  29. t.Run("testCalculateProof", testCalculateProof)
  30. time.Sleep(time.Second / 4)
  31. err := proofServerClient.WaitReady(context.Background())
  32. require.NoError(t, err)
  33. t.Run("testGetProof", testGetProof)
  34. t.Run("testCancel", testCancel)
  35. }
  36. func testAPIStatus(t *testing.T) {
  37. status, err := proofServerClient.apiStatus(context.Background())
  38. require.NoError(t, err)
  39. assert.Equal(t, true, status.Status.IsReady())
  40. }
  41. func testCalculateProof(t *testing.T) {
  42. zkInputs := common.NewZKInputs(100, 16, 512, 24, 32, big.NewInt(1))
  43. err := proofServerClient.CalculateProof(context.Background(), zkInputs)
  44. require.NoError(t, err)
  45. }
  46. func testGetProof(t *testing.T) {
  47. proof, pubInputs, err := proofServerClient.GetProof(context.Background())
  48. require.NoError(t, err)
  49. assert.NotNil(t, proof.PiA)
  50. assert.NotEqual(t, [2]*big.Int{}, proof.PiA)
  51. assert.NotNil(t, proof.PiB)
  52. assert.NotEqual(t, [3][2]*big.Int{}, proof.PiB)
  53. assert.NotNil(t, proof.PiC)
  54. assert.NotEqual(t, [2]*big.Int{}, proof.PiC)
  55. assert.NotNil(t, proof.Protocol)
  56. assert.NotEqual(t, 0, len(pubInputs))
  57. }
  58. func testCancel(t *testing.T) {
  59. zkInputs := common.NewZKInputs(100, 16, 512, 24, 32, big.NewInt(1))
  60. err := proofServerClient.CalculateProof(context.Background(), zkInputs)
  61. require.NoError(t, err)
  62. // TODO: remove sleep when the server has been reviewed
  63. time.Sleep(time.Second / 4)
  64. err = proofServerClient.Cancel(context.Background())
  65. require.NoError(t, err)
  66. status, err := proofServerClient.apiStatus(context.Background())
  67. require.NoError(t, err)
  68. for status.Status == StatusCodeBusy {
  69. time.Sleep(proofServerClient.pollInterval)
  70. status, err = proofServerClient.apiStatus(context.Background())
  71. require.NoError(t, err)
  72. }
  73. assert.Equal(t, StatusCodeAborted, status.Status)
  74. }