converter_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. // Copyright 2022-2023 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package protobuf
  15. import (
  16. "fmt"
  17. "os"
  18. "path/filepath"
  19. "reflect"
  20. "testing"
  21. "github.com/stretchr/testify/assert"
  22. "github.com/lf-edge/ekuiper/internal/conf"
  23. "github.com/lf-edge/ekuiper/internal/schema"
  24. "github.com/lf-edge/ekuiper/internal/testx"
  25. )
  26. func TestEncode(t *testing.T) {
  27. c, err := NewConverter("../../schema/test/test1.proto", "", "Person")
  28. if err != nil {
  29. t.Fatal(err)
  30. }
  31. tests := []struct {
  32. m map[string]interface{}
  33. r []byte
  34. e string
  35. }{
  36. {
  37. m: map[string]interface{}{
  38. "name": "test",
  39. "id": 1,
  40. "age": 1,
  41. },
  42. r: []byte{0x0a, 0x04, 0x74, 0x65, 0x73, 0x74, 0x10, 0x01},
  43. }, {
  44. m: map[string]interface{}{
  45. "name": "test",
  46. "id": 1,
  47. "email": "Dddd",
  48. },
  49. r: []byte{0x0a, 0x04, 0x74, 0x65, 0x73, 0x74, 0x10, 0x01, 0x1a, 0x04, 0x44, 0x64, 0x64, 0x64},
  50. }, {
  51. m: map[string]interface{}{
  52. "name": "test",
  53. "id": 1,
  54. "code": []any{
  55. map[string]any{"doubles": []any{1.1, 2.2, 3.3}},
  56. map[string]any{"doubles": []any{3.3, 1.1}},
  57. },
  58. },
  59. r: []byte{0x0a, 0x04, 0x74, 0x65, 0x73, 0x74, 0x10, 0x01, 0x22, 0x1b, 0x09, 0x9a, 0x99, 0x99, 0x99, 0x99, 0x99, 0xf1, 0x3f, 0x09, 0x9a, 0x99, 0x99, 0x99, 0x99, 0x99, 0x01, 0x40, 0x09, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x0a, 0x40, 0x22, 0x12, 0x09, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x0a, 0x40, 0x09, 0x9a, 0x99, 0x99, 0x99, 0x99, 0x99, 0xf1, 0x3f},
  60. },
  61. }
  62. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  63. for i, tt := range tests {
  64. a, err := c.Encode(tt.m)
  65. if !reflect.DeepEqual(tt.e, testx.Errstring(err)) {
  66. t.Errorf("%d.error mismatch:\n exp=%s\n got=%s\n\n", i, tt.e, err)
  67. } else if tt.e == "" && !reflect.DeepEqual(tt.r, a) {
  68. t.Errorf("%d. \n\nresult mismatch:\n\nexp=%x\n\ngot=%x\n\n", i, tt.r, a)
  69. }
  70. }
  71. }
  72. func TestEmbedType(t *testing.T) {
  73. c, err := NewConverter("../../schema/test/test3.proto", "", "DrivingData")
  74. if err != nil {
  75. t.Fatal(err)
  76. }
  77. tests := []struct {
  78. m map[string]interface{}
  79. r []byte
  80. e string
  81. }{
  82. {
  83. m: map[string]interface{}{
  84. "drvg_mod": int64(1),
  85. "brk_pedal_sts": map[string]interface{}{
  86. "valid": int64(0),
  87. },
  88. "average_speed": 90.56,
  89. },
  90. r: []byte{0x08, 0x01, 0x11, 0xa4, 0x70, 0x3d, 0x0a, 0xd7, 0xa3, 0x56, 0x40, 0x1a, 0x02, 0x08, 0x00},
  91. },
  92. }
  93. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  94. for i, tt := range tests {
  95. a, err := c.Encode(tt.m)
  96. if !reflect.DeepEqual(tt.e, testx.Errstring(err)) {
  97. t.Errorf("%d.error mismatch:\n exp=%s\n got=%s\n\n", i, tt.e, err)
  98. } else if tt.e == "" && !reflect.DeepEqual(tt.r, a) {
  99. t.Errorf("%d. \n\nresult mismatch:\n\nexp=%x\n\ngot=%x\n\n", i, tt.r, a)
  100. }
  101. m, err := c.Decode(a)
  102. if !reflect.DeepEqual(tt.e, testx.Errstring(err)) {
  103. t.Errorf("%d.error mismatch:\n exp=%s\n got=%s\n\n", i, tt.e, err)
  104. } else if tt.e == "" && !reflect.DeepEqual(tt.m, m) {
  105. t.Errorf("%d. \n\nresult mismatch:\n\nexp=%v\n\ngot=%v\n\n", i, tt.m, m)
  106. }
  107. }
  108. }
  109. func TestDecode(t *testing.T) {
  110. c, err := NewConverter("../../schema/test/test1.proto", "", "Person")
  111. if err != nil {
  112. t.Fatal(err)
  113. }
  114. tests := []struct {
  115. m map[string]interface{}
  116. r []byte
  117. }{
  118. {
  119. m: map[string]interface{}{
  120. "name": "test",
  121. "id": int64(1),
  122. "email": "Dddd",
  123. "code": []interface{}{},
  124. },
  125. r: []byte{0x0a, 0x04, 0x74, 0x65, 0x73, 0x74, 0x10, 0x01, 0x1a, 0x04, 0x44, 0x64, 0x64, 0x64},
  126. },
  127. {
  128. m: map[string]interface{}{
  129. "name": "test",
  130. "id": int64(1),
  131. "email": "",
  132. "code": []map[string]any{
  133. {"doubles": []float64{1.1, 2.2, 3.3}},
  134. {"doubles": []float64{3.3, 1.1}},
  135. },
  136. },
  137. r: []byte{0x0a, 0x04, 0x74, 0x65, 0x73, 0x74, 0x10, 0x01, 0x22, 0x1b, 0x09, 0x9a, 0x99, 0x99, 0x99, 0x99, 0x99, 0xf1, 0x3f, 0x09, 0x9a, 0x99, 0x99, 0x99, 0x99, 0x99, 0x01, 0x40, 0x09, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x0a, 0x40, 0x22, 0x12, 0x09, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x0a, 0x40, 0x09, 0x9a, 0x99, 0x99, 0x99, 0x99, 0x99, 0xf1, 0x3f},
  138. },
  139. }
  140. for i, tt := range tests {
  141. t.Run(fmt.Sprintf("test %d", i), func(t *testing.T) {
  142. a, err := c.Decode(tt.r)
  143. assert.NoError(t, err)
  144. assert.Equal(t, tt.m, a)
  145. })
  146. }
  147. }
  148. func TestStatic(t *testing.T) {
  149. dataDir, err := conf.GetDataLoc()
  150. if err != nil {
  151. t.Fatal(err)
  152. }
  153. etcDir := filepath.Join(dataDir, "schemas", "custom")
  154. err = os.MkdirAll(etcDir, os.ModePerm)
  155. if err != nil {
  156. t.Fatal(err)
  157. }
  158. defer func() {
  159. err = os.RemoveAll(etcDir)
  160. if err != nil {
  161. t.Fatal(err)
  162. }
  163. }()
  164. // build the so file into data/test prior to running the test
  165. // Copy the helloworld.so
  166. bytesRead, err := os.ReadFile(filepath.Join(dataDir, "helloworld.so"))
  167. if err != nil {
  168. t.Fatal(err)
  169. }
  170. err = os.WriteFile(filepath.Join(etcDir, "helloworld.so"), bytesRead, 0o755)
  171. if err != nil {
  172. t.Fatal(err)
  173. }
  174. schema.InitRegistry()
  175. c, err := NewConverter("../../schema/test/test1.proto", "../../../data/test/schemas/custom/helloworld.so", "HelloReply")
  176. if err != nil {
  177. t.Fatal(err)
  178. }
  179. tests := []struct {
  180. m map[string]interface{}
  181. r []byte
  182. e string
  183. }{
  184. {
  185. m: map[string]interface{}{
  186. "message": "test",
  187. },
  188. r: []byte{0x0a, 0x04, 0x74, 0x65, 0x73, 0x74},
  189. }, {
  190. m: map[string]interface{}{
  191. "message": "another test 2",
  192. },
  193. r: []byte{0x0a, 0x0e, 0x61, 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x32},
  194. },
  195. }
  196. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  197. for i, tt := range tests {
  198. a, err := c.Encode(tt.m)
  199. if !reflect.DeepEqual(tt.e, testx.Errstring(err)) {
  200. t.Errorf("%d.error mismatch:\n exp=%s\n got=%s\n\n", i, tt.e, err)
  201. } else if tt.e == "" && !reflect.DeepEqual(tt.r, a) {
  202. t.Errorf("%d. \n\nresult mismatch:\n\nexp=%x\n\ngot=%x\n\n", i, tt.r, a)
  203. }
  204. m, err := c.Decode(tt.r)
  205. if !reflect.DeepEqual(tt.e, testx.Errstring(err)) {
  206. t.Errorf("%d.error mismatch:\n exp=%s\n got=%s\n\n", i, tt.e, err)
  207. } else if tt.e == "" && !reflect.DeepEqual(tt.m, m) {
  208. t.Errorf("%d. \n\nresult mismatch:\n\nexp=%v\n\ngot=%v\n\n", i, tt.m, m)
  209. }
  210. }
  211. }
  212. func TestDecodeProto3(t *testing.T) {
  213. c, err := NewConverter("../../schema/test/test4.proto", "", "Classroom")
  214. if err != nil {
  215. t.Fatal(err)
  216. }
  217. tests := []struct {
  218. m map[string]interface{}
  219. r []byte
  220. }{
  221. {
  222. m: map[string]interface{}{
  223. "name": "test",
  224. "number": int64(1),
  225. "stu": []interface{}{},
  226. },
  227. r: []byte{0x0a, 0x04, 0x74, 0x65, 0x73, 0x74, 0x10, 0x01},
  228. },
  229. {
  230. m: map[string]interface{}{
  231. "name": "test",
  232. "number": int64(1),
  233. "stu": []map[string]interface{}{
  234. {
  235. "age": int64(12),
  236. "name": "test",
  237. "info": nil,
  238. },
  239. },
  240. },
  241. r: []byte{0x0a, 0x04, 0x74, 0x65, 0x73, 0x74, 0x10, 0x01, 0x1a, 0x08, 0x08, 0x0c, 0x12, 0x04, 0x74, 0x65, 0x73, 0x74},
  242. },
  243. }
  244. for i, tt := range tests {
  245. t.Run(fmt.Sprintf("test %d", i), func(t *testing.T) {
  246. a, err := c.Decode(tt.r)
  247. assert.NoError(t, err)
  248. assert.Equal(t, tt.m, a)
  249. })
  250. }
  251. }
  252. func TestEncodeDecodeForAllTypes(t *testing.T) {
  253. c, err := NewConverter("../../schema/test/alltypes.proto", "", "AllTypesTest")
  254. if err != nil {
  255. t.Fatal(err)
  256. }
  257. tests := []struct {
  258. name string
  259. m map[string]interface{}
  260. b []byte
  261. r map[string]interface{}
  262. }{
  263. {
  264. name: "all valid",
  265. m: map[string]interface{}{
  266. "adouble": 20.44,
  267. "afloat": 20.44,
  268. "anint32": -67,
  269. "anint64": -67,
  270. "auint32": 67,
  271. "auint64": 67,
  272. "abool": true,
  273. "abytes": []byte{0x01, 0x02, 0x03},
  274. "double_list": []float64{1.2, 2.3, 3.4},
  275. "float_list": []float64{1.2, 2.3, 3.4},
  276. "int32_list": []int64{1, 2, 3},
  277. "int64_list": []int64{1, 2, 3},
  278. "uint32_list": []int64{1, 2, 3},
  279. "uint64_list": []int64{1, 2, 3},
  280. "bool_list": []bool{true, false, true},
  281. "bytes_list": [][]byte{{0x01, 0x02, 0x03}, {0x04, 0x05, 0x06}},
  282. },
  283. b: []byte{0x9, 0x71, 0x3d, 0xa, 0xd7, 0xa3, 0x70, 0x34, 0x40, 0x15, 0x1f, 0x85, 0xa3, 0x41, 0x18, 0xbd, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1, 0x20, 0xbd, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1, 0x28, 0x43, 0x30, 0x43, 0x38, 0x1, 0x42, 0x3, 0x1, 0x2, 0x3, 0x4a, 0x18, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0xf3, 0x3f, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x2, 0x40, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0xb, 0x40, 0x52, 0xc, 0x9a, 0x99, 0x99, 0x3f, 0x33, 0x33, 0x13, 0x40, 0x9a, 0x99, 0x59, 0x40, 0x5a, 0x3, 0x2, 0x4, 0x6, 0x62, 0x18, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x3, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x6a, 0xc, 0x1, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x3, 0x0, 0x0, 0x0, 0x72, 0x18, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x3, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x7a, 0x3, 0x1, 0x0, 0x1, 0x82, 0x1, 0x3, 0x1, 0x2, 0x3, 0x82, 0x1, 0x3, 0x4, 0x5, 0x6},
  284. r: map[string]interface{}{
  285. "adouble": 20.44,
  286. "afloat": 20.440000534057617,
  287. "anint32": int64(-67),
  288. "anint64": int64(-67),
  289. "auint32": int64(67),
  290. "auint64": int64(67),
  291. "abool": true,
  292. "abytes": []byte{0x01, 0x02, 0x03},
  293. "double_list": []float64{1.2, 2.3, 3.4},
  294. "float_list": []float64{1.2000000476837158, 2.299999952316284, 3.4000000953674316},
  295. "int32_list": []int64{1, 2, 3},
  296. "int64_list": []int64{1, 2, 3},
  297. "uint32_list": []int64{1, 2, 3},
  298. "uint64_list": []int64{1, 2, 3},
  299. "bool_list": []bool{true, false, true},
  300. "bytes_list": [][]byte{{0x01, 0x02, 0x03}, {0x04, 0x05, 0x06}},
  301. },
  302. },
  303. }
  304. for _, tt := range tests {
  305. t.Run(tt.name, func(t *testing.T) {
  306. a, err := c.Encode(tt.m)
  307. assert.NoError(t, err)
  308. assert.Equal(t, tt.b, a)
  309. m, err := c.Decode(a)
  310. assert.NoError(t, err)
  311. assert.Equal(t, tt.r, m)
  312. })
  313. }
  314. }