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.

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