Skip to content

auth: add unit tests for xdp connection reset and fix uninitialized prev_key#1750

Open
Aryanburnwal05 wants to merge 1 commit into
kmesh-net:mainfrom
Aryanburnwal05:test-xdp-conn-reset
Open

auth: add unit tests for xdp connection reset and fix uninitialized prev_key#1750
Aryanburnwal05 wants to merge 1 commit into
kmesh-net:mainfrom
Aryanburnwal05:test-xdp-conn-reset

Conversation

@Aryanburnwal05

Copy link
Copy Markdown

This PR adds unit tests for xdpNotifyConnRst to verify the connection reset logic for XDP authorization. It also fixes a compilation error in the deserialization module.

Changes :-

  1. Added pkg/auth/xdp_auth_handler_test.go to cover xdpNotifyConnRst :-
  • Verified nil map safety.
  • Verified that trailing bytes for IPv4 keys are zeroed out (important for correct eBPF map lookups).
  • Verified IPv6 key insertion.
  1. Initialized prev_key to NULL in map_restore (deserialization_to_bpf_map.c) to fix a compilation failure (sometimes-uninitialized warning treated as error under -Werror).
  2. Refactored test setup to use rlimit memory lock adjustments instead of loading the full Kmesh BPF programs, which helps tests run locally on setups like WSL2.

Verification :-
Ran tests locally on WSL2 :-

go test -v ./pkg/auth -run Test_xdpNotifyConnRst

Copilot AI review requested due to automatic review settings June 14, 2026 09:35
@kmesh-bot

Copy link
Copy Markdown
Collaborator

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign nlgwcy for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@gemini-code-assist

Copy link
Copy Markdown

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request improves the reliability and maintainability of the XDP authorization module by introducing targeted unit tests and fixing a build-time initialization issue. The changes ensure robust handling of connection reset logic and facilitate easier local development and testing workflows.

Highlights

  • Unit Testing: Added comprehensive unit tests for xdpNotifyConnRst to validate connection reset logic, including nil map handling and correct key formatting for IPv4 and IPv6.
  • Bug Fix: Resolved a compilation error in deserialization_to_bpf_map.c by initializing prev_key to NULL, preventing uninitialized variable warnings.
  • Test Infrastructure: Refactored test setup to utilize rlimit memory lock adjustments, enabling local test execution on environments like WSL2.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.


The map was waiting for a key, / A pointer set to null, you see. / With tests now in the testing suite, / The code is stable and complete.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@kmesh-bot

Copy link
Copy Markdown
Collaborator

Welcome @Aryanburnwal05! It looks like this is your first PR to kmesh-net/kmesh 🎉

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

This PR improves robustness around BPF map restoration and adds unit tests to validate XDP auth notification behavior when writing to an eBPF map.

Changes:

  • Add Go unit tests for xdpNotifyConnRst covering nil-map handling and IPv4/IPv6 key mutation behavior.
  • Initialize prev_key to NULL in map_restore to avoid undefined behavior on first bpf_map_get_next_key call.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
pkg/auth/xdp_auth_handler_test.go Adds tests verifying xdpNotifyConnRst inserts expected values and mutates IPv4 keys correctly.
bpf/deserialization_to_bpf_map/deserialization_to_bpf_map.c Fixes potential UB by initializing prev_key before use in map iteration.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pkg/auth/xdp_auth_handler_test.go Outdated
Comment on lines +14 to +15
err := rlimit.RemoveMemlock()
assert.NoError(t, err)
Comment on lines +17 to +23
mapOfAuth, err := ebpf.NewMap(&ebpf.MapSpec{
Name: "test_map_of_auth_result",
Type: ebpf.Hash,
KeySize: uint32(TUPLE_LEN),
ValueSize: uint32(unsafe.Sizeof(uint32(0))),
MaxEntries: 4096,
})
key := make([]byte, TUPLE_LEN)
err := xdpNotifyConnRst(nil, constants.MSG_TYPE_IPV4, key)
assert.Error(t, err)
assert.Equal(t, "map_of_auth_result is nil", err.Error())
Comment on lines +1429 to 1432
void *prev_key = NULL;
unsigned int key = 0;

while (!bpf_map_get_next_key(map_fd, prev_key, &key)) {

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request initializes prev_key to NULL in map_restore to prevent undefined behavior, and introduces a new unit test file xdp_auth_handler_test.go to test xdpNotifyConnRst. The review feedback recommends using t.Fatalf instead of assert.NoError during critical test setup steps (such as removing the memlock limit and creating the eBPF map) to prevent potential nil pointer dereference panics if those setup operations fail.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +14 to +25
err := rlimit.RemoveMemlock()
assert.NoError(t, err)

mapOfAuth, err := ebpf.NewMap(&ebpf.MapSpec{
Name: "test_map_of_auth_result",
Type: ebpf.Hash,
KeySize: uint32(TUPLE_LEN),
ValueSize: uint32(unsafe.Sizeof(uint32(0))),
MaxEntries: 4096,
})
assert.NoError(t, err)
defer mapOfAuth.Close()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Using assert.NoError for critical test setup steps (such as removing the memlock limit and creating the eBPF map) allows the test to continue executing even if these operations fail. If ebpf.NewMap fails, mapOfAuth will be nil, and subsequent calls like mapOfAuth.Lookup in the subtests will cause a nil pointer dereference panic. Use t.Fatalf to stop test execution immediately if the setup fails.

	if err := rlimit.RemoveMemlock(); err != nil {
		t.Fatalf("Failed to remove memlock: %v", err)
	}

	mapOfAuth, err := ebpf.NewMap(&ebpf.MapSpec{
		Name:       "test_map_of_auth_result",
		Type:       ebpf.Hash,
		KeySize:    uint32(TUPLE_LEN),
		ValueSize:  uint32(unsafe.Sizeof(uint32(0))),
		MaxEntries: 4096,
	})
	if err != nil {
		t.Fatalf("Failed to create ebpf map: %v", err)
	}
	defer mapOfAuth.Close()

@kmesh-bot kmesh-bot added size/L and removed size/M labels Jun 18, 2026
Copilot AI review requested due to automatic review settings June 18, 2026 06:54
@kmesh-bot kmesh-bot added size/M and removed size/L labels Jun 18, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

Comment on lines +14 to +25
err := rlimit.RemoveMemlock()
assert.NoError(t, err)

mapOfAuth, err := ebpf.NewMap(&ebpf.MapSpec{
Name: "test_map_of_auth_result",
Type: ebpf.Hash,
KeySize: uint32(TUPLE_LEN),
ValueSize: uint32(unsafe.Sizeof(uint32(0))),
MaxEntries: 4096,
})
assert.NoError(t, err)
defer mapOfAuth.Close()
Comment on lines +29 to +31
err := xdpNotifyConnRst(nil, constants.MSG_TYPE_IPV4, key)
assert.Error(t, err)
assert.Equal(t, "map_of_auth_result is nil", err.Error())
…rev_key

Signed-off-by: Aryan Burnwal <burnwalaryan922@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants