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.

128 lines
4.2 KiB

  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/stretchr/testify/require"
  9. "github.com/tendermint/tendermint/privval"
  10. )
  11. const lastSignBytes = "750802110500000000000000220B08B398F3E00510F48DA6402A480A20FC25" +
  12. "8973076512999C3E6839A22E9FBDB1B77CF993E8A9955412A41A59D4CAD312240A20C971B286ACB8AA" +
  13. "A6FCA0365EB0A660B189EDC08B46B5AF2995DEFA51A28D215B10013211746573742D636861696E2D533245415533"
  14. const oldPrivvalContent = `{
  15. "address": "1D8089FAFDFAE4A637F3D616E17B92905FA2D91D",
  16. "pub_key": {
  17. "type": "tendermint/PubKeyEd25519",
  18. "value": "r3Yg2AhDZ745CNTpavsGU+mRZ8WpRXqoJuyqjN8mJq0="
  19. },
  20. "last_height": "5",
  21. "last_round": "0",
  22. "last_step": 3,
  23. "last_signature": "CTr7b9ZQlrJJf+12rPl5t/YSCUc/KqV7jQogCfFJA24e7hof69X6OMT7eFLVQHyodPjD/QTA298XHV5ejxInDQ==",
  24. "last_signbytes": "` + lastSignBytes + `",
  25. "priv_key": {
  26. "type": "tendermint/PrivKeyEd25519",
  27. "value": "7MwvTGEWWjsYwjn2IpRb+GYsWi9nnFsw8jPLLY1UtP6vdiDYCENnvjkI1Olq+wZT6ZFnxalFeqgm7KqM3yYmrQ=="
  28. }
  29. }`
  30. func TestLoadAndUpgrade(t *testing.T) {
  31. oldFilePath := initTmpOldFile(t)
  32. defer os.Remove(oldFilePath)
  33. newStateFile, err := ioutil.TempFile("", "priv_validator_state*.json")
  34. defer os.Remove(newStateFile.Name())
  35. require.NoError(t, err)
  36. newKeyFile, err := ioutil.TempFile("", "priv_validator_key*.json")
  37. defer os.Remove(newKeyFile.Name())
  38. require.NoError(t, err)
  39. emptyOldFile, err := ioutil.TempFile("", "priv_validator_empty*.json")
  40. require.NoError(t, err)
  41. defer os.Remove(emptyOldFile.Name())
  42. type args struct {
  43. oldPVPath string
  44. newPVKeyPath string
  45. newPVStatePath string
  46. }
  47. tests := []struct {
  48. name string
  49. args args
  50. wantErr bool
  51. wantPanic bool
  52. }{
  53. {"successful upgrade",
  54. args{oldPVPath: oldFilePath, newPVKeyPath: newKeyFile.Name(), newPVStatePath: newStateFile.Name()},
  55. false, false,
  56. },
  57. {"unsuccessful upgrade: empty old privval file",
  58. args{oldPVPath: emptyOldFile.Name(), newPVKeyPath: newKeyFile.Name(), newPVStatePath: newStateFile.Name()},
  59. true, false,
  60. },
  61. {"unsuccessful upgrade: invalid new paths (1/3)",
  62. args{oldPVPath: oldFilePath, newPVKeyPath: "", newPVStatePath: newStateFile.Name()},
  63. false, true,
  64. },
  65. {"unsuccessful upgrade: invalid new paths (2/3)",
  66. args{oldPVPath: oldFilePath, newPVKeyPath: newKeyFile.Name(), newPVStatePath: ""},
  67. false, true,
  68. },
  69. {"unsuccessful upgrade: invalid new paths (3/3)",
  70. args{oldPVPath: oldFilePath, newPVKeyPath: "", newPVStatePath: ""},
  71. false, true,
  72. },
  73. }
  74. for _, tt := range tests {
  75. tt := tt
  76. t.Run(tt.name, func(t *testing.T) {
  77. // need to re-write the file everytime because upgrading renames it
  78. err := ioutil.WriteFile(oldFilePath, []byte(oldPrivvalContent), 0600)
  79. require.NoError(t, err)
  80. if tt.wantPanic {
  81. require.Panics(t, func() { loadAndUpgrade(tt.args.oldPVPath, tt.args.newPVKeyPath, tt.args.newPVStatePath) })
  82. } else {
  83. err = loadAndUpgrade(tt.args.oldPVPath, tt.args.newPVKeyPath, tt.args.newPVStatePath)
  84. if tt.wantErr {
  85. assert.Error(t, err)
  86. fmt.Println("ERR", err)
  87. } else {
  88. assert.NoError(t, err)
  89. upgradedPV := privval.LoadFilePV(tt.args.newPVKeyPath, tt.args.newPVStatePath)
  90. oldPV, err := privval.LoadOldFilePV(tt.args.oldPVPath + ".bak")
  91. require.NoError(t, err)
  92. assert.Equal(t, oldPV.Address, upgradedPV.Key.Address)
  93. assert.Equal(t, oldPV.Address, upgradedPV.GetAddress())
  94. assert.Equal(t, oldPV.PubKey, upgradedPV.Key.PubKey)
  95. upv, err := upgradedPV.GetPubKey()
  96. require.NoError(t, err)
  97. assert.Equal(t, oldPV.PubKey, upv)
  98. assert.Equal(t, oldPV.PrivKey, upgradedPV.Key.PrivKey)
  99. assert.Equal(t, oldPV.LastHeight, upgradedPV.LastSignState.Height)
  100. assert.Equal(t, oldPV.LastRound, upgradedPV.LastSignState.Round)
  101. assert.Equal(t, oldPV.LastSignature, upgradedPV.LastSignState.Signature)
  102. assert.Equal(t, oldPV.LastSignBytes, upgradedPV.LastSignState.SignBytes)
  103. assert.Equal(t, oldPV.LastStep, upgradedPV.LastSignState.Step)
  104. }
  105. }
  106. })
  107. }
  108. }
  109. func initTmpOldFile(t *testing.T) string {
  110. tmpfile, err := ioutil.TempFile("", "priv_validator_*.json")
  111. require.NoError(t, err)
  112. t.Logf("created test file %s", tmpfile.Name())
  113. _, err = tmpfile.WriteString(oldPrivvalContent)
  114. require.NoError(t, err)
  115. return tmpfile.Name()
  116. }