github.com/Laplace-Game-Development/Laplace-Entangled-Environment@v0.0.3/internal/route/parse_test.go (about) 1 package route 2 3 import ( 4 "encoding/asn1" 5 "encoding/base64" 6 "encoding/json" 7 "testing" 8 9 "github.com/Laplace-Game-Development/Laplace-Entangled-Environment/internal/policy" 10 "github.com/Laplace-Game-Development/Laplace-Entangled-Environment/internal/util" 11 ) 12 13 const foo = "derp" 14 const bar = "herp" 15 const baz = 1 16 17 type FoobarInterface struct { 18 Foo string 19 Bar string 20 Baz int 21 } 22 23 func TestParseASN1(t *testing.T) { 24 fooey := FoobarInterface{foo, bar, baz} 25 marshalled, err := asn1.Marshal(fooey) 26 if err != nil { 27 t.Errorf("Error marshalling dummy interface! Err: %v\n", err) 28 } 29 30 t.Logf("Marshalled ASN1: %s\n", marshalled) 31 32 fooeyAssert := FoobarInterface{} 33 err = parseASN1(&fooeyAssert, &marshalled) 34 if err != nil { 35 t.Errorf("Error parsing/unmarshalling dummy bytes! Err: %v\n", err) 36 } 37 38 if fooey.Foo != fooeyAssert.Foo || 39 fooey.Bar != fooeyAssert.Bar || 40 fooey.Baz != fooeyAssert.Baz { 41 t.Errorf("Expected Value %v Was not the actual value %v\n", fooey, fooeyAssert) 42 } 43 } 44 45 func TestParseJson(t *testing.T) { 46 fooey := FoobarInterface{foo, bar, baz} 47 marshalled, err := json.Marshal(fooey) 48 if err != nil { 49 t.Errorf("Error marshalling dummy interface! Err: %v\n", err) 50 } 51 52 t.Logf("Marshalled JSON: %s\n", marshalled) 53 54 fooeyAssert := FoobarInterface{} 55 err = parseJson(&fooeyAssert, &marshalled) 56 if err != nil { 57 t.Errorf("Error parsing/unmarshalling dummy bytes! Err: %v\n", err) 58 } 59 60 if fooey.Foo != fooeyAssert.Foo || 61 fooey.Bar != fooeyAssert.Bar || 62 fooey.Baz != fooeyAssert.Baz { 63 t.Errorf("Expected Value %v Was not the actual value %v\n", fooey, fooeyAssert) 64 } 65 } 66 67 func TestBase64Decode(t *testing.T) { 68 fooey := "DerpityDerp{}12345" 69 encoded := make([]byte, base64.StdEncoding.EncodedLen(len(fooey))) 70 base64.StdEncoding.Encode(encoded, []byte(fooey)) 71 t.Logf("Encoded base64 %s\n", encoded) 72 73 fooeyAssert, err := util.Base64Decode(&encoded) 74 if err != nil { 75 t.Errorf("Error parsing/unmarshalling dummy bytes! Err: %v\n", err) 76 } 77 78 fooeyAssertCasted := string(fooeyAssert) 79 if fooey != fooeyAssertCasted { 80 t.Errorf("Expected %s does not match Actual %s\n", fooey, fooeyAssertCasted) 81 } 82 } 83 84 func TestSwitchCommand(t *testing.T) { 85 cmd, err := ParseCommand(0, 0) 86 if err != nil { 87 t.Errorf("Error Parsing Command For 0 Bytes! Err: %v\n", err) 88 cmd = policy.CmdEmpty 89 } else if cmd != policy.CmdEmpty { 90 t.Fatalf("Empty Command Was Not Expected Empty Command!\n") 91 } 92 93 // Request Empty 94 fooey := FoobarInterface{} 95 req, err := policy.RequestWithUserForTesting("0", false, cmd, fooey) 96 if err != nil { 97 t.Errorf("Error creating empty request! Err: %v\n", err) 98 } 99 100 respExpected := policy.SuccessfulResponse() 101 dataExpected, err := respExpected.Digest(respExpected.Data) 102 if err != nil { 103 t.Errorf("Error Getting Expected Data! Err: %v\n", err) 104 } 105 106 resp, err := switchOnCommand(req.Header, req.BodyFactories, false) 107 if err != nil { 108 t.Errorf("Error using empty request! Err: %v\n", err) 109 } else if string(resp) != string(dataExpected) { 110 t.Errorf("Response to Empty Command was %s instead of %s\n", resp, dataExpected) 111 } 112 113 // Request Insecure Login 114 req, err = policy.RequestWithUserForTesting("0", false, policy.CmdLogin, fooey) 115 if err != nil { 116 t.Errorf("Error creating empty request! Err: %v\n", err) 117 } 118 119 dataExpected = util.NewErrorJson("Unsecure Connection!") 120 if err != nil { 121 t.Errorf("Error Getting Expected Data! Err: %v\n", err) 122 } 123 124 resp, err = switchOnCommand(req.Header, req.BodyFactories, false) 125 if err != nil { 126 t.Errorf("Error using empty request! Err: %v\n", err) 127 } else if string(resp) != string(dataExpected) { 128 t.Errorf("Response to Empty Command was %s instead of %s\n", resp, dataExpected) 129 } 130 } 131 132 func TestParseAttachmentBody(t *testing.T) { 133 attachment := policy.RequestAttachment{UserID: "0", Sig: ""} 134 marshalledSub0, err := json.Marshal(attachment) 135 if err != nil { 136 t.Errorf("Error marshalling dummy interface! Err: %v\n", err) 137 } 138 139 fooey := FoobarInterface{foo, bar, baz} 140 marshalledSub1, err := json.Marshal(fooey) 141 if err != nil { 142 t.Errorf("Error marshalling dummy interface! Err: %v\n", err) 143 } 144 145 marshalled := make([]byte, len(marshalledSub0)+len(marshalledSub1)) 146 err = util.Concat(&marshalled, &marshalledSub0, 0) 147 if err != nil { 148 t.Errorf("Error Concatenating Marshalled JSONs! Err: %v\n", err) 149 } 150 151 err = util.Concat(&marshalled, &marshalledSub1, len(marshalledSub0)) 152 if err != nil { 153 t.Errorf("Error Concatenating Marshalled JSONs! Err: %v\n", err) 154 } 155 156 fooeyAssert := FoobarInterface{} 157 attachmentAssert, bodyStart, err := parseRequestAttachment(true, &marshalled) 158 if err != nil { 159 t.Errorf("Error Parsing TCP Request Attachment! Err: %v\n", err) 160 } else if attachmentAssert.UserID != attachment.UserID || 161 attachmentAssert.Sig != attachment.Sig { 162 t.Errorf("Expected %v does not match actual %v\n", attachment, attachmentAssert) 163 } 164 165 marshalledSlice := marshalled[bodyStart:] 166 prefix := TCPRequestPrefix{IsBase64Enc: false, IsJSON: true} 167 err = parseBody(&fooeyAssert, prefix, &marshalledSlice) 168 if err != nil { 169 t.Errorf("Error Parsing Body! Err: %v\n", err) 170 } else if fooey.Foo != fooeyAssert.Foo || 171 fooey.Bar != fooeyAssert.Bar || 172 fooey.Baz != fooeyAssert.Baz { 173 t.Errorf("Expected %v does not match actual %v\n", attachment, attachmentAssert) 174 } 175 }