| 1 |
package request |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"encoding/json" |
| 6 |
"io" |
| 7 |
"reflect" |
| 8 |
"testing" |
| 9 |
) |
| 10 |
|
| 11 |
func Test_ParseRequestLength_ConsidersFirstFourBytes(t *testing.T) { |
| 12 |
// Arrange |
| 13 |
expected := uint32(201334791) // 0x0c002007 |
| 14 |
|
| 15 |
// The first 4 bytes represent the value of `expected` in Little Endian format, |
| 16 |
// the rest should be completely ignored during the parsing. |
| 17 |
input := bytes.NewReader([]byte{7, 32, 0, 12, 13, 13, 13}) |
| 18 |
|
| 19 |
// Act |
| 20 |
actual, err := parseRequestLength(input) |
| 21 |
|
| 22 |
// Assert |
| 23 |
if err != nil { |
| 24 |
t.Fatalf("Error parsing request length: %v", err) |
| 25 |
} |
| 26 |
|
| 27 |
if expected != actual { |
| 28 |
t.Fatalf("The actual length '%v' does not match the expected value of '%v'", actual, expected) |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
func Test_ParseRequestLength_ConnectionAborted(t *testing.T) { |
| 33 |
// Arrange |
| 34 |
expectedErr := io.ErrUnexpectedEOF |
| 35 |
input := bytes.NewReader([]byte{7}) |
| 36 |
|
| 37 |
// Act |
| 38 |
_, err := parseRequestLength(input) |
| 39 |
|
| 40 |
// Assert |
| 41 |
if expectedErr != err { |
| 42 |
t.Fatalf("The expected error is '%v', but got '%v'", expectedErr, err) |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
func Test_ParseRequest_CanParse(t *testing.T) { |
| 47 |
// Arrange |
| 48 |
expected := &request{ |
| 49 |
Action: "list", |
| 50 |
Settings: settings{ |
| 51 |
Stores: map[string]store{ |
| 52 |
"id1": store{ |
| 53 |
ID: "id1", |
| 54 |
Name: "default", |
| 55 |
Path: "~/.password-store", |
| 56 |
}, |
| 57 |
}, |
| 58 |
}, |
| 59 |
} |
| 60 |
|
| 61 |
jsonBytes, err := json.Marshal(expected) |
| 62 |
if err != nil { |
| 63 |
t.Fatal("Unable to marshal the expected object to initialize the test") |
| 64 |
} |
| 65 |
|
| 66 |
inputLength := uint32(len(jsonBytes)) |
| 67 |
input := bytes.NewReader(jsonBytes) |
| 68 |
|
| 69 |
// Act |
| 70 |
actual, err := parseRequest(inputLength, input) |
| 71 |
|
| 72 |
// Assert |
| 73 |
if err != nil { |
| 74 |
t.Fatalf("Error parsing request: %v", err) |
| 75 |
} |
| 76 |
|
| 77 |
if !reflect.DeepEqual(expected, actual) { |
| 78 |
t.Fatalf("The request was parsed incorrectly.\nExpected: %+v\nActual: %+v", expected, actual) |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
func Test_ParseRequest_WrongLength(t *testing.T) { |
| 83 |
// Arrange |
| 84 |
expectedErr := io.ErrUnexpectedEOF |
| 85 |
|
| 86 |
jsonBytes, err := json.Marshal(&request{Action: "list"}) |
| 87 |
if err != nil { |
| 88 |
t.Fatal("Unable to marshal the expected object to initialize the test") |
| 89 |
} |
| 90 |
|
| 91 |
wrongInputLength := uint32(len(jsonBytes)) - 1 |
| 92 |
input := bytes.NewReader(jsonBytes) |
| 93 |
|
| 94 |
// Act |
| 95 |
_, err = parseRequest(wrongInputLength, input) |
| 96 |
|
| 97 |
// Assert |
| 98 |
if expectedErr != err { |
| 99 |
t.Fatalf("The expected error is '%v', but got '%v'", expectedErr, err) |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
func Test_ParseRequest_InvalidJson(t *testing.T) { |
| 104 |
// Arrange |
| 105 |
jsonBytes := []byte("not_a_json") |
| 106 |
inputLength := uint32(len(jsonBytes)) |
| 107 |
input := bytes.NewReader(jsonBytes) |
| 108 |
|
| 109 |
// Act |
| 110 |
_, err := parseRequest(inputLength, input) |
| 111 |
|
| 112 |
// Assert |
| 113 |
if err == nil { |
| 114 |
t.Fatalf("Expected a parsing error, but didn't get it") |
| 115 |
} |
| 116 |
} |