github.com/mdlayher/yamlnetlink@v0.0.0-20221112165554-61a6fe600cd8/spec_test.go (about) 1 package yamlnetlink_test 2 3 import ( 4 "strings" 5 "testing" 6 7 "github.com/google/go-cmp/cmp" 8 "github.com/mdlayher/yamlnetlink" 9 ) 10 11 func TestParse(t *testing.T) { 12 s, err := yamlnetlink.Parse(strings.NewReader(nlctrlYAML)) 13 if err != nil { 14 t.Fatalf("failed to parse nlctrl YAML: %v", err) 15 } 16 17 if diff := cmp.Diff(nlctrl(), s); diff != "" { 18 t.Fatalf("unexpected Spec (-want +got):\n%s", diff) 19 } 20 } 21 22 // nlctrl returns a well-formed YAML netlink Spec for the generic netlink nlctrl 23 // family, for use in tests. 24 func nlctrl() *yamlnetlink.Spec { 25 return &yamlnetlink.Spec{ 26 Name: "nlctrl", 27 Protocol: "genetlink-legacy", 28 Doc: "Generic netlink control protocol. Interface to query information about generic netlink families registered in the kernel - their names, ids, accepted messages and attributes.", 29 UAPIHeader: "linux/genetlink.h", 30 AttributeSets: []yamlnetlink.AttributeSet{ 31 { 32 Name: "main", 33 NamePrefix: "ctrl-attr-", 34 Attributes: []yamlnetlink.Attribute{ 35 { 36 Name: "family-id", 37 Type: "u16", 38 Doc: "Numerical identifier of the family.", 39 }, 40 { 41 Name: "family-name", 42 Type: "nul-string", 43 Len: "GENL_NAMSIZ - 1", 44 Doc: "String identifier of the family. Guaranteed to be unique.", 45 }, 46 { 47 Name: "version", 48 Type: "u32", 49 }, 50 { 51 Name: "hdrsize", 52 Type: "u32", 53 }, 54 { 55 Name: "maxattr", 56 Type: "u32", 57 }, 58 { 59 Name: "ops", 60 Type: "array-nest", 61 NestedAttributes: "operation", 62 }, 63 { 64 Name: "mcast-groups", 65 Type: "array-nest", 66 NestedAttributes: "mcast-group", 67 }, 68 { 69 Name: "op", 70 Type: "u32", 71 }, 72 { 73 Name: "op-policy", 74 Type: "nest-type-value", 75 TypeValue: []string{"cmd"}, 76 NestedAttributes: "policy", 77 }, 78 { 79 Name: "policy", 80 Type: "nest-type-value", 81 TypeValue: []string{"current-policy-idx", "attr-idx"}, 82 NestedAttributes: "nl-policy", 83 }, 84 }, 85 }, 86 { 87 Name: "operation", 88 NamePrefix: "ctrl-attr-op-", 89 Attributes: []yamlnetlink.Attribute{ 90 { 91 Name: "id", 92 Type: "u32", 93 }, 94 { 95 Name: "flags", 96 Type: "u32", 97 }, 98 }, 99 }, 100 { 101 Name: "mcast-group", 102 NamePrefix: "ctrl-attr-mcast-grp-", 103 Attributes: []yamlnetlink.Attribute{ 104 { 105 Name: "id", 106 Type: "u32", 107 }, 108 { 109 Name: "name", 110 Type: "nul-string", Len: "GENL_NAMSIZ - 1", 111 }, 112 }, 113 }, 114 { 115 Name: "policy", 116 NamePrefix: "ctrl-attr-policy-", 117 Attributes: []yamlnetlink.Attribute{ 118 { 119 Name: "do", 120 Type: "u32", 121 }, 122 { 123 Name: "dump", 124 Type: "u32", 125 }, 126 }, 127 }, 128 { 129 Name: "nl-policy", 130 NamePrefix: "nl-policy-type-attr-", 131 Attributes: []yamlnetlink.Attribute{ 132 { 133 Name: "type", 134 Type: "u32", 135 }, 136 { 137 Name: "min-value-u", 138 Type: "u64", 139 }, 140 { 141 Name: "max-value-u", 142 Type: "u64", 143 }, 144 { 145 Name: "min-value-s", 146 Type: "s64", 147 }, 148 { 149 Name: "max-value-s", 150 Type: "s64", 151 }, 152 { 153 Name: "mask", 154 Type: "u64", 155 }, 156 { 157 Name: "min-length", 158 Type: "u32", 159 }, 160 { 161 Name: "max-length", 162 Type: "u32", 163 }, 164 { 165 Name: "policy-idx", 166 Type: "u32", 167 }, 168 { 169 Name: "policy-maxtype", 170 Type: "u32", 171 }, 172 { 173 Name: "bitfield32-mask", 174 Type: "u32", 175 }, 176 }, 177 }, 178 }, 179 Operations: yamlnetlink.Operations{ 180 NamePrefix: "ctrl-cmd-", 181 List: []yamlnetlink.Operation{ 182 { 183 Name: "getfamily", 184 Doc: "Get information about genetlink family.", 185 AttributeSet: "main", 186 DontValidate: []string{"strict", "dump"}, 187 188 Do: yamlnetlink.OperationAttributes{ 189 Request: yamlnetlink.OperationAttributesList{ 190 Attributes: []string{"family-id", "family-name"}, 191 }, 192 Reply: yamlnetlink.OperationAttributesList{ 193 Attributes: []string{ 194 "family-id", "family-name", "version", "hdrsize", "maxattr", "ops", "mcast-groups", 195 }, 196 }, 197 }, 198 Dump: yamlnetlink.OperationAttributes{ 199 Reply: yamlnetlink.OperationAttributesList{ 200 Attributes: []string{ 201 "family-id", "family-name", "version", "hdrsize", "maxattr", "ops", "mcast-groups", 202 }, 203 }, 204 }, 205 }, 206 { 207 Name: "newfamily", 208 Doc: "Notification for new families being registered.", 209 Notify: "getfamily", 210 }, 211 { 212 Name: "delfamily", 213 Doc: "Notification for families being unregistered.", 214 Notify: "getfamily", 215 }, 216 { 217 Name: "newmcast-grp", 218 Doc: "Notification for new multicast groups.", 219 Notify: "getfamily", 220 }, 221 { 222 Name: "delmcast-grp", 223 Doc: "Notification for deleted multicast groups.", 224 Notify: "getfamily", 225 }, 226 { 227 Name: "getpolicy", 228 Doc: "Get attribute policy for a genetlink family.", 229 AttributeSet: "main", 230 Dump: yamlnetlink.OperationAttributes{ 231 Request: yamlnetlink.OperationAttributesList{ 232 Attributes: []string{"family-id", "family-name", "op"}, 233 }, 234 Reply: yamlnetlink.OperationAttributesList{ 235 Attributes: []string{"family-id", "op-policy", "policy"}, 236 }, 237 }, 238 }, 239 }, 240 }, 241 } 242 } 243 244 // Copied from: 245 // https://github.com/kuba-moo/ynl/blob/main/Documentation/netlink/specs/genetlink.yaml 246 const nlctrlYAML = ` 247 name: nlctrl 248 249 protocol: genetlink-legacy 250 251 doc: | 252 Generic netlink control protocol. Interface to query information about 253 generic netlink families registered in the kernel - their names, ids, 254 accepted messages and attributes. 255 256 uapi-header: linux/genetlink.h 257 258 attribute-sets: 259 - 260 name: main 261 name-prefix: ctrl-attr- 262 attributes: 263 - 264 name: family-id 265 type: u16 266 doc: | 267 Numerical identifier of the family. 268 - 269 name: family-name 270 type: nul-string 271 len: GENL_NAMSIZ - 1 272 doc: | 273 String identifier of the family. Guaranteed to be unique. 274 - 275 name: version 276 type: u32 277 - 278 name: hdrsize 279 type: u32 280 - 281 name: maxattr 282 type: u32 283 - 284 name: ops 285 type: array-nest 286 nested-attributes: operation 287 - 288 name: mcast-groups 289 type: array-nest 290 nested-attributes: mcast-group 291 - 292 name: op 293 type: u32 294 - 295 name: op-policy 296 type: nest-type-value 297 type-value: [ cmd ] 298 nested-attributes: policy 299 - 300 name: policy 301 type: nest-type-value 302 type-value: [ current-policy-idx, attr-idx ] 303 nested-attributes: nl-policy 304 - 305 name: operation 306 name-prefix: ctrl-attr-op- 307 attributes: 308 - 309 name: id 310 type: u32 311 - 312 name: flags 313 type: u32 314 - 315 name: mcast-group 316 name-prefix: ctrl-attr-mcast-grp- 317 attributes: 318 - 319 name: id 320 type: u32 321 - 322 name: name 323 type: nul-string 324 len: GENL_NAMSIZ - 1 325 - 326 name: policy 327 name-prefix: ctrl-attr-policy- 328 attributes: 329 - 330 name: do 331 type: u32 332 - 333 name: dump 334 type: u32 335 - 336 name: nl-policy 337 name-prefix: nl-policy-type-attr- 338 attributes: 339 - 340 name: type 341 type: u32 342 - 343 name: min-value-u 344 type: u64 345 - 346 name: max-value-u 347 type: u64 348 - 349 name: min-value-s 350 type: s64 351 - 352 name: max-value-s 353 type: s64 354 - 355 name: mask 356 type: u64 357 - 358 name: min-length 359 type: u32 360 - 361 name: max-length 362 type: u32 363 - 364 name: policy-idx 365 type: u32 366 - 367 name: policy-maxtype 368 type: u32 369 - 370 name: bitfield32-mask 371 type: u32 372 373 operations: 374 name-prefix: ctrl-cmd- 375 list: 376 - 377 name: getfamily 378 doc: Get information about genetlink family. 379 attribute-set: main 380 dont-validate: [ strict, dump ] 381 382 do: 383 request: 384 attributes: 385 - family-id 386 - family-name 387 reply: &getfamily-do-reply 388 attributes: 389 - family-id 390 - family-name 391 - version 392 - hdrsize 393 - maxattr 394 - ops 395 - mcast-groups 396 dump: 397 reply: *getfamily-do-reply 398 - 399 name: newfamily 400 doc: Notification for new families being registered. 401 notify: getfamily 402 - 403 name: delfamily 404 doc: Notification for families being unregistered. 405 notify: getfamily 406 - 407 name: newmcast-grp 408 doc: Notification for new multicast groups. 409 notify: getfamily 410 - 411 name: delmcast-grp 412 doc: Notification for deleted multicast groups. 413 notify: getfamily 414 - 415 name: getpolicy 416 doc: Get attribute policy for a genetlink family. 417 attribute-set: main 418 419 dump: 420 request: 421 attributes: 422 - family-id 423 - family-name 424 - op 425 reply: 426 attributes: 427 - family-id 428 - op-policy 429 - policy 430 `