Advertisement
Guest User

Untitled

a guest
Jul 9th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 5.37 KB | None | 0 0
  1. package plugin
  2.  
  3. import (
  4.     "testing"
  5.  
  6.     "github.com/davecgh/go-spew/spew"
  7.     . "github.com/stretchr/testify/assert"
  8. )
  9.  
  10. func TestStandardMethodEncodeMethodCall(t *testing.T) {
  11.     scenarios := []struct {
  12.         value MethodCall
  13.         data  []byte
  14.     }{
  15.         {
  16.             value: MethodCall{
  17.                 Method:    "12345678",
  18.                 Arguments: "foobar",
  19.             },
  20.             data: []byte{
  21.                 0x07, 0x08, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, // string type, string length, "123456"
  22.                 0x37, 0x38, 0x07, 0x06, 0x66, 0x6f, 0x6f, 0x62, // "78", string type, string length, "foob"
  23.                 0x61, 0x72, // "ar"
  24.             },
  25.         },
  26.         {
  27.             value: MethodCall{
  28.                 Method:    "",
  29.                 Arguments: float64(3.1415),
  30.             },
  31.             data: []byte{
  32.                 0x07, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, // string type, string length, "", float type, 5 alignment bytes
  33.                 0x6f, 0x12, 0x83, 0xc0, 0xca, 0x21, 0x09, 0x40, // encoded float value (3.1415)
  34.             },
  35.         },
  36.         {
  37.             value: MethodCall{
  38.                 Method:    "1",
  39.                 Arguments: float64(3.1415),
  40.             },
  41.             data: []byte{
  42.                 0x07, 0x01, 0x31, 0x06, 0x00, 0x00, 0x00, 0x00, // string type, string length, "1", float type, 4 alignment bytes
  43.                 0x6f, 0x12, 0x83, 0xc0, 0xca, 0x21, 0x09, 0x40, // encoded float value (3.1415)
  44.             },
  45.         },
  46.         {
  47.             value: MethodCall{
  48.                 Method:    "12",
  49.                 Arguments: float64(3.1415),
  50.             },
  51.             data: []byte{
  52.                 0x07, 0x02, 0x31, 0x32, 0x06, 0x00, 0x00, 0x00, // string type, string length, "12", float type, 3 alignment bytes
  53.                 0x6f, 0x12, 0x83, 0xc0, 0xca, 0x21, 0x09, 0x40, // encoded float value (3.1415)
  54.             },
  55.         },
  56.         {
  57.             value: MethodCall{
  58.                 Method:    "123",
  59.                 Arguments: float64(3.1415),
  60.             },
  61.             data: []byte{
  62.                 0x07, 0x03, 0x31, 0x32, 0x33, 0x06, 0x00, 0x00, // string type, string length, "123", float type, 2 alignment bytes
  63.                 0x6f, 0x12, 0x83, 0xc0, 0xca, 0x21, 0x09, 0x40, // encoded float value (3.1415)
  64.             },
  65.         },
  66.         {
  67.             value: MethodCall{
  68.                 Method:    "1234",
  69.                 Arguments: float64(3.1415),
  70.             },
  71.             data: []byte{
  72.                 0x07, 0x04, 0x31, 0x32, 0x33, 0x34, 0x06, 0x00, // string type, string length, "1234", float type, 1 alignment byte
  73.                 0x6f, 0x12, 0x83, 0xc0, 0xca, 0x21, 0x09, 0x40, // encoded float value (3.1415)
  74.             },
  75.         },
  76.         {
  77.             value: MethodCall{
  78.                 Method:    "12345",
  79.                 Arguments: float64(3.1415),
  80.             },
  81.             data: []byte{
  82.                 0x07, 0x05, 0x31, 0x32, 0x33, 0x34, 0x35, 0x06, // string type, string length, "12345", float type
  83.                 0x6f, 0x12, 0x83, 0xc0, 0xca, 0x21, 0x09, 0x40, // encoded float value (3.1415)
  84.             },
  85.         },
  86.         {
  87.             value: MethodCall{
  88.                 Method:    "123456",
  89.                 Arguments: float64(3.1415),
  90.             },
  91.             data: []byte{
  92.                 0x07, 0x06, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, // string type, string length, "123456"
  93.                 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // float type, alignment bytes
  94.                 0x6f, 0x12, 0x83, 0xc0, 0xca, 0x21, 0x09, 0x40, // encoded float value (3.1415)
  95.             },
  96.         },
  97.     }
  98.     codec := StandardMethodCodec{}
  99.  
  100.     for _, s := range scenarios {
  101.         t.Logf("encoding: %v\n", s.value)
  102.  
  103.         encodedData, err := codec.EncodeMethodCall(s.value)
  104.         if err != nil {
  105.             t.Fatal(err)
  106.         }
  107.         t.Logf(spew.Sdump(encodedData))
  108.         Equal(t, s.data, encodedData)
  109.  
  110.         decodedValue, err := codec.DecodeMethodCall(encodedData)
  111.         if err != nil {
  112.             t.Fatal(err)
  113.         }
  114.  
  115.         Equal(t, s.value, decodedValue)
  116.     }
  117. }
  118.  
  119. // TestStandardMethodDecodeRealWorldMethodCall tests decoding a method call
  120. // found in the real world, that posed some problems for an early
  121. // implementation.
  122. func TestStandardMethodDecodeRealWorldMethodCall(t *testing.T) {
  123.     scenarios := []struct {
  124.         value MethodCall
  125.         data  []byte
  126.     }{
  127.         {
  128.             value: MethodCall{
  129.                 Method: "play",
  130.                 Arguments: map[interface{}]interface{}{
  131.                     "playerId":       "fcc45fbf-d44f-4468-8d9a-f9cf64e3443f",
  132.                     "mode":           "PlayerMode.MEDIA_PLAYER",
  133.                     "url":            "https://luan.xyz/files/audio/ambient_c_motion.mp3",
  134.                     "isLocal":        false,
  135.                     "volume":         float64(1),
  136.                     "position":       nil,
  137.                     "respectSilence": false,
  138.                 },
  139.             },
  140.             data: []byte{0x07, 0x04, 0x70, 0x6c, 0x61, 0x79, 0x0d, 0x07, 0x07, 0x03, 0x75, 0x72, 0x6c, 0x07, 0x31, 0x68, 0x74, 0x74, 0x70, 0x73, 0x3a, 0x2f, 0x2f, 0x6c, 0x75, 0x61, 0x6e, 0x2e, 0x78, 0x79, 0x7a, 0x2f, 0x66, 0x69, 0x6c, 0x65, 0x73, 0x2f, 0x61, 0x75, 0x64, 0x69, 0x6f, 0x2f, 0x61, 0x6d, 0x62, 0x69, 0x65, 0x6e, 0x74, 0x5f, 0x63, 0x5f, 0x6d, 0x6f, 0x74, 0x69, 0x6f, 0x6e, 0x2e, 0x6d, 0x70, 0x33, 0x07, 0x07, 0x69, 0x73, 0x4c, 0x6f, 0x63, 0x61, 0x6c, 0x02, 0x07, 0x06, 0x76, 0x6f, 0x6c, 0x75, 0x6d, 0x65, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x3f, 0x07, 0x08, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x00, 0x07, 0x0e, 0x72, 0x65, 0x73, 0x70, 0x65, 0x63, 0x74, 0x53, 0x69, 0x6c, 0x65, 0x6e, 0x63, 0x65, 0x02, 0x07, 0x08, 0x70, 0x6c, 0x61, 0x79, 0x65, 0x72, 0x49, 0x64, 0x07, 0x24, 0x66, 0x63, 0x63, 0x34, 0x35, 0x66, 0x62, 0x66, 0x2d, 0x64, 0x34, 0x34, 0x66, 0x2d, 0x34, 0x34, 0x36, 0x38, 0x2d, 0x38, 0x64, 0x39, 0x61, 0x2d, 0x66, 0x39, 0x63, 0x66, 0x36, 0x34, 0x65, 0x33, 0x34, 0x34, 0x33, 0x66, 0x07, 0x04, 0x6d, 0x6f, 0x64, 0x65, 0x07, 0x17, 0x50, 0x6c, 0x61, 0x79, 0x65, 0x72, 0x4d, 0x6f, 0x64, 0x65, 0x2e, 0x4d, 0x45, 0x44, 0x49, 0x41, 0x5f, 0x50, 0x4c, 0x41, 0x59, 0x45, 0x52},
  141.         },
  142.     }
  143.  
  144.     codec := StandardMethodCodec{}
  145.  
  146.     for _, s := range scenarios {
  147.         result, err := codec.DecodeMethodCall(s.data)
  148.         if err != nil {
  149.             t.Fatal(err)
  150.         }
  151.         Equal(t, s.value, result)
  152.     }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement