github.com/safing/portbase@v0.19.5/formats/dsd/dsd_test.go (about) 1 //nolint:maligned,gocyclo,gocognit 2 package dsd 3 4 import ( 5 "math/big" 6 "reflect" 7 "testing" 8 ) 9 10 // SimpleTestStruct is used for testing. 11 type SimpleTestStruct struct { 12 S string 13 B byte 14 } 15 16 type ComplexTestStruct struct { 17 I int 18 I8 int8 19 I16 int16 20 I32 int32 21 I64 int64 22 UI uint 23 UI8 uint8 24 UI16 uint16 25 UI32 uint32 26 UI64 uint64 27 BI *big.Int 28 S string 29 Sp *string 30 Sa []string 31 Sap *[]string 32 B byte 33 Bp *byte 34 Ba []byte 35 Bap *[]byte 36 M map[string]string 37 Mp *map[string]string 38 } 39 40 type GenCodeTestStruct struct { 41 I8 int8 42 I16 int16 43 I32 int32 44 I64 int64 45 UI8 uint8 46 UI16 uint16 47 UI32 uint32 48 UI64 uint64 49 S string 50 Sp *string 51 Sa []string 52 Sap *[]string 53 B byte 54 Bp *byte 55 Ba []byte 56 Bap *[]byte 57 } 58 59 var ( 60 simpleSubject = &SimpleTestStruct{ 61 "a", 62 0x01, 63 } 64 65 bString = "b" 66 bBytes byte = 0x02 67 68 complexSubject = &ComplexTestStruct{ 69 -1, 70 -2, 71 -3, 72 -4, 73 -5, 74 1, 75 2, 76 3, 77 4, 78 5, 79 big.NewInt(6), 80 "a", 81 &bString, 82 []string{"c", "d", "e"}, 83 &[]string{"f", "g", "h"}, 84 0x01, 85 &bBytes, 86 []byte{0x03, 0x04, 0x05}, 87 &[]byte{0x05, 0x06, 0x07}, 88 map[string]string{ 89 "a": "b", 90 "c": "d", 91 "e": "f", 92 }, 93 &map[string]string{ 94 "g": "h", 95 "i": "j", 96 "k": "l", 97 }, 98 } 99 100 genCodeSubject = &GenCodeTestStruct{ 101 -2, 102 -3, 103 -4, 104 -5, 105 2, 106 3, 107 4, 108 5, 109 "a", 110 &bString, 111 []string{"c", "d", "e"}, 112 &[]string{"f", "g", "h"}, 113 0x01, 114 &bBytes, 115 []byte{0x03, 0x04, 0x05}, 116 &[]byte{0x05, 0x06, 0x07}, 117 } 118 ) 119 120 func TestConversion(t *testing.T) { //nolint:maintidx 121 t.Parallel() 122 123 compressionFormats := []uint8{AUTO, GZIP} 124 formats := []uint8{JSON, CBOR, MsgPack} 125 126 for _, compression := range compressionFormats { 127 for _, format := range formats { 128 129 // simple 130 var b []byte 131 var err error 132 if compression != AUTO { 133 b, err = DumpAndCompress(simpleSubject, format, compression) 134 } else { 135 b, err = Dump(simpleSubject, format) 136 } 137 if err != nil { 138 t.Fatalf("Dump error (simple struct): %s", err) 139 } 140 141 si := &SimpleTestStruct{} 142 _, err = Load(b, si) 143 if err != nil { 144 t.Fatalf("Load error (simple struct): %s", err) 145 } 146 147 if !reflect.DeepEqual(simpleSubject, si) { 148 t.Errorf("Load (simple struct): subject does not match loaded object") 149 t.Errorf("Encoded: %v", string(b)) 150 t.Errorf("Compared: %v == %v", simpleSubject, si) 151 } 152 153 // complex 154 if compression != AUTO { 155 b, err = DumpAndCompress(complexSubject, format, compression) 156 } else { 157 b, err = Dump(complexSubject, format) 158 } 159 if err != nil { 160 t.Fatalf("Dump error (complex struct): %s", err) 161 } 162 163 co := &ComplexTestStruct{} 164 _, err = Load(b, co) 165 if err != nil { 166 t.Fatalf("Load error (complex struct): %s", err) 167 } 168 169 if complexSubject.I != co.I { 170 t.Errorf("Load (complex struct): struct.I is not equal (%v != %v)", complexSubject.I, co.I) 171 } 172 if complexSubject.I8 != co.I8 { 173 t.Errorf("Load (complex struct): struct.I8 is not equal (%v != %v)", complexSubject.I8, co.I8) 174 } 175 if complexSubject.I16 != co.I16 { 176 t.Errorf("Load (complex struct): struct.I16 is not equal (%v != %v)", complexSubject.I16, co.I16) 177 } 178 if complexSubject.I32 != co.I32 { 179 t.Errorf("Load (complex struct): struct.I32 is not equal (%v != %v)", complexSubject.I32, co.I32) 180 } 181 if complexSubject.I64 != co.I64 { 182 t.Errorf("Load (complex struct): struct.I64 is not equal (%v != %v)", complexSubject.I64, co.I64) 183 } 184 if complexSubject.UI != co.UI { 185 t.Errorf("Load (complex struct): struct.UI is not equal (%v != %v)", complexSubject.UI, co.UI) 186 } 187 if complexSubject.UI8 != co.UI8 { 188 t.Errorf("Load (complex struct): struct.UI8 is not equal (%v != %v)", complexSubject.UI8, co.UI8) 189 } 190 if complexSubject.UI16 != co.UI16 { 191 t.Errorf("Load (complex struct): struct.UI16 is not equal (%v != %v)", complexSubject.UI16, co.UI16) 192 } 193 if complexSubject.UI32 != co.UI32 { 194 t.Errorf("Load (complex struct): struct.UI32 is not equal (%v != %v)", complexSubject.UI32, co.UI32) 195 } 196 if complexSubject.UI64 != co.UI64 { 197 t.Errorf("Load (complex struct): struct.UI64 is not equal (%v != %v)", complexSubject.UI64, co.UI64) 198 } 199 if complexSubject.BI.Cmp(co.BI) != 0 { 200 t.Errorf("Load (complex struct): struct.BI is not equal (%v != %v)", complexSubject.BI, co.BI) 201 } 202 if complexSubject.S != co.S { 203 t.Errorf("Load (complex struct): struct.S is not equal (%v != %v)", complexSubject.S, co.S) 204 } 205 if !reflect.DeepEqual(complexSubject.Sp, co.Sp) { 206 t.Errorf("Load (complex struct): struct.Sp is not equal (%v != %v)", complexSubject.Sp, co.Sp) 207 } 208 if !reflect.DeepEqual(complexSubject.Sa, co.Sa) { 209 t.Errorf("Load (complex struct): struct.Sa is not equal (%v != %v)", complexSubject.Sa, co.Sa) 210 } 211 if !reflect.DeepEqual(complexSubject.Sap, co.Sap) { 212 t.Errorf("Load (complex struct): struct.Sap is not equal (%v != %v)", complexSubject.Sap, co.Sap) 213 } 214 if complexSubject.B != co.B { 215 t.Errorf("Load (complex struct): struct.B is not equal (%v != %v)", complexSubject.B, co.B) 216 } 217 if !reflect.DeepEqual(complexSubject.Bp, co.Bp) { 218 t.Errorf("Load (complex struct): struct.Bp is not equal (%v != %v)", complexSubject.Bp, co.Bp) 219 } 220 if !reflect.DeepEqual(complexSubject.Ba, co.Ba) { 221 t.Errorf("Load (complex struct): struct.Ba is not equal (%v != %v)", complexSubject.Ba, co.Ba) 222 } 223 if !reflect.DeepEqual(complexSubject.Bap, co.Bap) { 224 t.Errorf("Load (complex struct): struct.Bap is not equal (%v != %v)", complexSubject.Bap, co.Bap) 225 } 226 if !reflect.DeepEqual(complexSubject.M, co.M) { 227 t.Errorf("Load (complex struct): struct.M is not equal (%v != %v)", complexSubject.M, co.M) 228 } 229 if !reflect.DeepEqual(complexSubject.Mp, co.Mp) { 230 t.Errorf("Load (complex struct): struct.Mp is not equal (%v != %v)", complexSubject.Mp, co.Mp) 231 } 232 233 } 234 235 // test all formats 236 simplifiedFormatTesting := []uint8{JSON, CBOR, MsgPack, GenCode} 237 238 for _, format := range simplifiedFormatTesting { 239 240 // simple 241 var b []byte 242 var err error 243 if compression != AUTO { 244 b, err = DumpAndCompress(simpleSubject, format, compression) 245 } else { 246 b, err = Dump(simpleSubject, format) 247 } 248 if err != nil { 249 t.Fatalf("Dump error (simple struct): %s", err) 250 } 251 252 si := &SimpleTestStruct{} 253 _, err = Load(b, si) 254 if err != nil { 255 t.Fatalf("Load error (simple struct): %s", err) 256 } 257 258 if !reflect.DeepEqual(simpleSubject, si) { 259 t.Errorf("Load (simple struct): subject does not match loaded object") 260 t.Errorf("Encoded: %v", string(b)) 261 t.Errorf("Compared: %v == %v", simpleSubject, si) 262 } 263 264 // complex 265 b, err = DumpAndCompress(genCodeSubject, format, compression) 266 if err != nil { 267 t.Fatalf("Dump error (complex struct): %s", err) 268 } 269 270 co := &GenCodeTestStruct{} 271 _, err = Load(b, co) 272 if err != nil { 273 t.Fatalf("Load error (complex struct): %s", err) 274 } 275 276 if genCodeSubject.I8 != co.I8 { 277 t.Errorf("Load (complex struct): struct.I8 is not equal (%v != %v)", genCodeSubject.I8, co.I8) 278 } 279 if genCodeSubject.I16 != co.I16 { 280 t.Errorf("Load (complex struct): struct.I16 is not equal (%v != %v)", genCodeSubject.I16, co.I16) 281 } 282 if genCodeSubject.I32 != co.I32 { 283 t.Errorf("Load (complex struct): struct.I32 is not equal (%v != %v)", genCodeSubject.I32, co.I32) 284 } 285 if genCodeSubject.I64 != co.I64 { 286 t.Errorf("Load (complex struct): struct.I64 is not equal (%v != %v)", genCodeSubject.I64, co.I64) 287 } 288 if genCodeSubject.UI8 != co.UI8 { 289 t.Errorf("Load (complex struct): struct.UI8 is not equal (%v != %v)", genCodeSubject.UI8, co.UI8) 290 } 291 if genCodeSubject.UI16 != co.UI16 { 292 t.Errorf("Load (complex struct): struct.UI16 is not equal (%v != %v)", genCodeSubject.UI16, co.UI16) 293 } 294 if genCodeSubject.UI32 != co.UI32 { 295 t.Errorf("Load (complex struct): struct.UI32 is not equal (%v != %v)", genCodeSubject.UI32, co.UI32) 296 } 297 if genCodeSubject.UI64 != co.UI64 { 298 t.Errorf("Load (complex struct): struct.UI64 is not equal (%v != %v)", genCodeSubject.UI64, co.UI64) 299 } 300 if genCodeSubject.S != co.S { 301 t.Errorf("Load (complex struct): struct.S is not equal (%v != %v)", genCodeSubject.S, co.S) 302 } 303 if !reflect.DeepEqual(genCodeSubject.Sp, co.Sp) { 304 t.Errorf("Load (complex struct): struct.Sp is not equal (%v != %v)", genCodeSubject.Sp, co.Sp) 305 } 306 if !reflect.DeepEqual(genCodeSubject.Sa, co.Sa) { 307 t.Errorf("Load (complex struct): struct.Sa is not equal (%v != %v)", genCodeSubject.Sa, co.Sa) 308 } 309 if !reflect.DeepEqual(genCodeSubject.Sap, co.Sap) { 310 t.Errorf("Load (complex struct): struct.Sap is not equal (%v != %v)", genCodeSubject.Sap, co.Sap) 311 } 312 if genCodeSubject.B != co.B { 313 t.Errorf("Load (complex struct): struct.B is not equal (%v != %v)", genCodeSubject.B, co.B) 314 } 315 if !reflect.DeepEqual(genCodeSubject.Bp, co.Bp) { 316 t.Errorf("Load (complex struct): struct.Bp is not equal (%v != %v)", genCodeSubject.Bp, co.Bp) 317 } 318 if !reflect.DeepEqual(genCodeSubject.Ba, co.Ba) { 319 t.Errorf("Load (complex struct): struct.Ba is not equal (%v != %v)", genCodeSubject.Ba, co.Ba) 320 } 321 if !reflect.DeepEqual(genCodeSubject.Bap, co.Bap) { 322 t.Errorf("Load (complex struct): struct.Bap is not equal (%v != %v)", genCodeSubject.Bap, co.Bap) 323 } 324 } 325 326 } 327 }