replyParser_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package goSam
  2. import (
  3. "testing"
  4. )
  5. var validCases = []struct {
  6. Input string
  7. Expected Reply
  8. }{
  9. // hello handshake reply
  10. {
  11. "HELLO REPLY RESULT=OK VERSION=3.0\n",
  12. Reply{
  13. Topic: "HELLO",
  14. Type: "REPLY",
  15. Pairs: map[string]string{
  16. "RESULT": "OK",
  17. "VERSION": "3.0",
  18. },
  19. },
  20. },
  21. // result of a naming lookup
  22. {
  23. "NAMING REPLY RESULT=OK NAME=zzz.i2p VALUE=SomeValueForTesting\n",
  24. Reply{
  25. Topic: "NAMING",
  26. Type: "REPLY",
  27. Pairs: map[string]string{
  28. "RESULT": "OK",
  29. "NAME": "zzz.i2p",
  30. "VALUE": "SomeValueForTesting",
  31. },
  32. },
  33. },
  34. // result of a b32.i2p naming lookup
  35. {
  36. "NAMING REPLY RESULT=OK NAME=gkso46tc47hdua2kva5zahj3unmyh6ia7bv5oc2ybn2hmeowpz7a.b32.i2p VALUE=mlHQraXLpcE7A4MVeVniRHM~2yoaW1fOYVKj3ZiNTe4UPIAlIReYUMHSnZnloFWX7bh2OoEg08JBGoSQPtGkCZjqSBmfeDdMqtwbZ~~sok-jNo4e5rWnfCOHYYPVcuE8jB~7M5ioJzk8QZRqh3AjCQsKBUZgTzUfGlP12j3GtAf5C9iAGxTTB1sGE96752EKP0dzyGOs4NAujwkgm6NzVoqlkXD~fognUrQOeG~~OqChsAeqIRqj40FsJmsJREmZ4GhjFAqzLUQ4hMpKSbqMI~wtfjeNs-WKtM7pCD09uwSmYwW84wu-WxLGZiIt2GKmbPv~JrqYFNv9EM0SzUonAF8pw9GAhUn8-26kkgCXTs05Kut7NuQHghu3jHfS-frlPmAt-Uu5T4ZcLiHiFrnG2lYTtjxBFXh7W72IBncHSixhVhd4lYM7REKFj7G~5ttW9EBeL1unbNYWiQOEQjtGlmwxYt~~2EV16w339aQQ~S~69-tS6vFA1n2DgkMdg06pBQAEAAEAAA==\n",
  37. Reply{
  38. Topic: "NAMING",
  39. Type: "REPLY",
  40. Pairs: map[string]string{
  41. "RESULT": "OK",
  42. "NAME": "gkso46tc47hdua2kva5zahj3unmyh6ia7bv5oc2ybn2hmeowpz7a.b32.i2p",
  43. "VALUE": "mlHQraXLpcE7A4MVeVniRHM~2yoaW1fOYVKj3ZiNTe4UPIAlIReYUMHSnZnloFWX7bh2OoEg08JBGoSQPtGkCZjqSBmfeDdMqtwbZ~~sok-jNo4e5rWnfCOHYYPVcuE8jB~7M5ioJzk8QZRqh3AjCQsKBUZgTzUfGlP12j3GtAf5C9iAGxTTB1sGE96752EKP0dzyGOs4NAujwkgm6NzVoqlkXD~fognUrQOeG~~OqChsAeqIRqj40FsJmsJREmZ4GhjFAqzLUQ4hMpKSbqMI~wtfjeNs-WKtM7pCD09uwSmYwW84wu-WxLGZiIt2GKmbPv~JrqYFNv9EM0SzUonAF8pw9GAhUn8-26kkgCXTs05Kut7NuQHghu3jHfS-frlPmAt-Uu5T4ZcLiHiFrnG2lYTtjxBFXh7W72IBncHSixhVhd4lYM7REKFj7G~5ttW9EBeL1unbNYWiQOEQjtGlmwxYt~~2EV16w339aQQ~S~69-tS6vFA1n2DgkMdg06pBQAEAAEAAA==",
  44. },
  45. },
  46. },
  47. // session status reply
  48. {
  49. "SESSION STATUS RESULT=I2P_ERROR MESSAGE=TheMessageFromI2p\n",
  50. Reply{
  51. Topic: "SESSION",
  52. Type: "STATUS",
  53. Pairs: map[string]string{
  54. "RESULT": "I2P_ERROR",
  55. "MESSAGE": "TheMessageFromI2p",
  56. },
  57. },
  58. },
  59. {
  60. "STREAM STATUS RESULT=CANT_REACH_PEER\n",
  61. Reply{
  62. Topic: "STREAM",
  63. Type: "STATUS",
  64. Pairs: map[string]string{
  65. "RESULT": "CANT_REACH_PEER",
  66. },
  67. },
  68. },
  69. }
  70. func TestParseReplyValidCases(t *testing.T) {
  71. for _, tcase := range validCases {
  72. parsed, err := parseReply(tcase.Input)
  73. if err != nil {
  74. t.Fatalf("parseReply should not throw an error!\n%s", err)
  75. }
  76. if parsed.Topic != tcase.Expected.Topic {
  77. t.Fatalf("Wrong Topic. Got %s expected %s", parsed.Topic, tcase.Expected.Topic)
  78. }
  79. if len(parsed.Pairs) != len(tcase.Expected.Pairs) {
  80. t.Fatalf("Wrong ammount of Pairs. Got %d expected 3", len(parsed.Pairs))
  81. }
  82. for expK, expV := range tcase.Expected.Pairs {
  83. if expV != parsed.Pairs[expK] {
  84. t.Fatalf("Wrong %s.\nGot '%s'\nExpected '%s'", expK, parsed.Pairs[expK], expV)
  85. }
  86. }
  87. }
  88. }
  89. func TestParseInvalidReply(t *testing.T) {
  90. str := "asd asd="
  91. r, err := parseReply(str)
  92. if err == nil {
  93. t.Fatalf("Should throw an error.r:%v\n", r)
  94. }
  95. }