Skip to content
Snippets Groups Projects
path_attributes_test.go 47.4 KiB
Newer Older
  • Learn to ignore specific revisions
  • 				2,    // Msg Type
    				0, 0, // Withdrawn Routes Length
    
    				0, 14, // Total Path Attribute Length
    				// OriginatorID
    				128,        // Attr. Flags (Opt.)
    				9,          // Attr. Type Code
    				4,          // Attr Length
    				9, 8, 7, 6, // 9.8.7.6
    
    				// ClusterList
    				128, // Attr Flags (Opt.)
    				10,  // Attr. Type Code
    				4,
    				1, 2, 3, 4,
    
    				17, 100, 110, 128, // NLRI
    			},
    		},
    
    	}
    
    	for _, test := range tests {
    
    Oliver Herms's avatar
    Oliver Herms committed
    		opt := &types.Options{
    			AddPathRX: false,
    		}
    
    		res, err := test.msg.SerializeUpdate(opt)
    
    		if err != nil {
    			if test.wantFail {
    				continue
    			}
    
    			t.Errorf("Unexpected failure for test %q: %v", test.name, err)
    			continue
    		}
    
    		if test.wantFail {
    			t.Errorf("Unexpected success for test %q", test.name)
    			continue
    		}
    
    		assert.Equalf(t, test.expected, res, "%s", test.name)
    	}
    }
    
    func TestSerializeAddPath(t *testing.T) {
    	tests := []struct {
    		name     string
    
    Oliver Herms's avatar
    Oliver Herms committed
    		msg      *BGPUpdate
    
    		expected []byte
    		wantFail bool
    	}{
    		{
    			name: "Withdraw only",
    
    Oliver Herms's avatar
    Oliver Herms committed
    			msg: &BGPUpdate{
    				WithdrawnRoutes: &NLRI{
    
    					PathIdentifier: 257,
    					IP:             strAddr("100.110.120.0"),
    					Pfxlen:         24,
    				},
    			},
    			expected: []byte{
    				0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    				0, 31, // Length
    				2,    // Msg Type
    				0, 8, // Withdrawn Routes Length
    				0, 0, 1, 1, // Path Identifier
    				24, 100, 110, 120, // NLRI
    				0, 0, // Total Path Attribute Length
    			},
    		},
    		{
    			name: "NLRI only",
    
    Oliver Herms's avatar
    Oliver Herms committed
    			msg: &BGPUpdate{
    				NLRI: &NLRI{
    
    					PathIdentifier: 257,
    					IP:             strAddr("100.110.128.0"),
    					Pfxlen:         17,
    				},
    			},
    			expected: []byte{
    				0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    				0, 31, // Length
    				2,    // Msg Type
    				0, 0, // Withdrawn Routes Length
    				0, 0, // Total Path Attribute Length
    				0, 0, 1, 1, // Path Identifier
    				17, 100, 110, 128, // NLRI
    			},
    		},
    		{
    			name: "Path Attributes only",
    
    Oliver Herms's avatar
    Oliver Herms committed
    			msg: &BGPUpdate{
    
    				PathAttributes: &PathAttribute{
    					Optional:   true,
    					Transitive: true,
    					TypeCode:   OriginAttr,
    					Value:      uint8(0), // IGP
    				},
    			},
    			expected: []byte{
    				0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    				0, 27, // Length
    				2,    // Msg Type
    				0, 0, // Withdrawn Routes Length
    				0, 4, // Total Path Attribute Length
    				64, // Attr. Flags
    				1,  // Attr. Type Code
    				1,  // Length
    				0,  // Value
    			},
    		},
    		{
    			name: "Full test",
    
    Oliver Herms's avatar
    Oliver Herms committed
    			msg: &BGPUpdate{
    				WithdrawnRoutes: &NLRI{
    
    					IP:     strAddr("10.0.0.0"),
    					Pfxlen: 8,
    
    Oliver Herms's avatar
    Oliver Herms committed
    					Next: &NLRI{
    
    						IP:     strAddr("192.168.0.0"),
    						Pfxlen: 16,
    					},
    				},
    				PathAttributes: &PathAttribute{
    					TypeCode: OriginAttr,
    					Value:    uint8(0),
    					Next: &PathAttribute{
    						TypeCode: ASPathAttr,
    
    Oliver Herms's avatar
    Oliver Herms committed
    						Value: types.ASPath{
    
    							{
    								Type: 2,
    								ASNs: []uint32{100, 155, 200},
    							},
    							{
    								Type: 1,
    								ASNs: []uint32{10, 20},
    							},
    						},
    						Next: &PathAttribute{
    							TypeCode: NextHopAttr,
    
    							Value:    bnet.IPv4FromOctets(10, 20, 30, 40),
    
    							Next: &PathAttribute{
    								TypeCode: MEDAttr,
    								Value:    uint32(100),
    								Next: &PathAttribute{
    									TypeCode: LocalPrefAttr,
    									Value:    uint32(500),
    									Next: &PathAttribute{
    										TypeCode: AtomicAggrAttr,
    										Next: &PathAttribute{
    											TypeCode: AggregatorAttr,
    
    											Value: types.Aggregator{
    												ASN:     200,
    												Address: strAddr("10.20.30.40"),
    											},
    
    Oliver Herms's avatar
    Oliver Herms committed
    				NLRI: &NLRI{
    
    					IP:     strAddr("8.8.8.0"),
    					Pfxlen: 24,
    
    Oliver Herms's avatar
    Oliver Herms committed
    					Next: &NLRI{
    
    						IP:     strAddr("185.65.240.0"),
    						Pfxlen: 22,
    					},
    				},
    			},
    			expected: []byte{
    				0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    
    				0, 106, // Length
    
    				2, // Msg Type
    
    				// Withdraws
    				0, 13, // Withdrawn Routes Length
    				0, 0, 0, 0, // Path Identifier
    				8, 10, // Withdraw 10/8
    				0, 0, 0, 0, // Path Identifier
    				16, 192, 168, // Withdraw 192.168/16
    
    
    				0, 54, // Total Path Attribute Length
    
    
    				// ORIGIN
    				64, // Attr. Flags
    				1,  // Attr. Type Code
    				1,  // Length
    				0,  // Value
    				// ASPath
    				64,                     // Attr. Flags
    				2,                      // Attr. Type Code
    				14,                     // Attr. Length
    				2,                      // Path Segment Type = AS_SEQUENCE
    				3,                      // Path Segment Length
    				0, 100, 0, 155, 0, 200, // ASNs
    				1,            // Path Segment Type = AS_SET
    				2,            // Path Segment Type = AS_SET
    				0, 10, 0, 20, // ASNs
    				// Next Hop
    				64,             // Attr. Flags
    				3,              // Attr. Type Code
    				4,              // Length
    				10, 20, 30, 40, // Next Hop Address
    				// MED
    				128,          // Attr. Flags
    				4,            // Attr Type Code
    				4,            // Length
    				0, 0, 0, 100, // MED = 100
    				// LocalPref
    				64,           // Attr. Flags
    				5,            // Attr. Type Code
    				4,            // Length
    				0, 0, 1, 244, // Localpref
    				// Atomic Aggregate
    				64, // Attr. Flags
    				6,  // Attr. Type Code
    				0,  // Length
    				// Aggregator
    				192,    // Attr. Flags
    				7,      // Attr. Type Code
    
    				6,      // Length
    
    				0, 200, // Aggregator ASN = 200
    
    				10, 20, 30, 40, // Aggregator Address
    
    
    				// NLRI
    				0, 0, 0, 0, // Path Identifier
    				24, 8, 8, 8, // 8.8.8.0/24
    				0, 0, 0, 0, // Path Identifier
    				22, 185, 65, 240, // 185.65.240.0/22
    			},
    		},
    	}
    
    	for _, test := range tests {
    
    Oliver Herms's avatar
    Oliver Herms committed
    		opt := &types.Options{
    			AddPathRX: true,
    		}
    
    		res, err := test.msg.SerializeUpdate(opt)
    
    		if err != nil {
    			if test.wantFail {
    				continue
    			}
    
    			t.Errorf("Unexpected failure for test %q: %v", test.name, err)
    			continue
    		}
    
    		if test.wantFail {
    			t.Errorf("Unexpected success for test %q", test.name)
    			continue
    		}
    
    
    		assert.Equalf(t, test.expected, res, "%s", test.name)
    
    Oliver Herms's avatar
    Oliver Herms committed
    
    func TestFourBytesToUint32(t *testing.T) {
    	tests := []struct {
    		name     string
    		input    [4]byte
    		expected uint32
    	}{
    		{
    			name:     "Test #1",
    			input:    [4]byte{0, 0, 0, 200},
    			expected: 200,
    		},
    		{
    			name:     "Test #2",
    			input:    [4]byte{1, 0, 0, 200},
    			expected: 16777416,
    		},
    	}
    
    	for _, test := range tests {
    		res := fourBytesToUint32(test.input)
    		if res != test.expected {
    			t.Errorf("Unexpected result for test %q: Got: %d Want: %d", test.name, res, test.expected)
    		}
    	}
    }