| 1 |
//go:build !e2e_testing |
| 2 |
// +build !e2e_testing |
| 3 |
|
| 4 |
package overlay |
| 5 |
|
| 6 |
import ( |
| 7 |
"testing" |
| 8 |
) |
| 9 |
|
| 10 |
func Test_parseNetworkCategory(t *testing.T) { |
| 11 |
cases := []struct { |
| 12 |
in string |
| 13 |
wantCat networkCategory |
| 14 |
wantApply bool |
| 15 |
wantErr bool |
| 16 |
}{ |
| 17 |
{"", 0, false, false}, |
| 18 |
{"unset", 0, false, false}, |
| 19 |
{" UNSET ", 0, false, false}, |
| 20 |
{"private", networkCategoryPrivate, true, false}, |
| 21 |
{"Private", networkCategoryPrivate, true, false}, |
| 22 |
{" PRIVATE ", networkCategoryPrivate, true, false}, |
| 23 |
{"public", networkCategoryPublic, true, false}, |
| 24 |
{"PUBLIC", networkCategoryPublic, true, false}, |
| 25 |
{"domain", networkCategoryDomainAuthenticated, true, false}, |
| 26 |
{"DomainAuthenticated", networkCategoryDomainAuthenticated, true, false}, |
| 27 |
{"garbage", 0, false, true}, |
| 28 |
{"privates", 0, false, true}, |
| 29 |
} |
| 30 |
for _, tc := range cases { |
| 31 |
cat, apply, err := parseNetworkCategory(tc.in) |
| 32 |
if (err != nil) != tc.wantErr { |
| 33 |
t.Errorf("parseNetworkCategory(%q) err=%v, wantErr=%v", tc.in, err, tc.wantErr) |
| 34 |
continue |
| 35 |
} |
| 36 |
if cat != tc.wantCat || apply != tc.wantApply { |
| 37 |
t.Errorf("parseNetworkCategory(%q) = (%v, %v), want (%v, %v)", tc.in, cat, apply, tc.wantCat, tc.wantApply) |
| 38 |
} |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
// Test_NLM_round_trip exercises every COM call path used by setNetworkCategory |
| 43 |
// without mutating the host's network state. It validates the CLSID/IID |
| 44 |
// constants and every vtable index by enumerating connections, fetching the |
| 45 |
// adapter id and parent network, reading the current category, and writing it |
| 46 |
// back unchanged. |
| 47 |
// |
| 48 |
// Requires Windows but does not require admin or the wintun driver. Skips if |
| 49 |
// no network connections are available (unlikely outside of an isolated |
| 50 |
// container). |
| 51 |
func Test_NLM_round_trip(t *testing.T) { |
| 52 |
deinit, err := coInit() |
| 53 |
if err != nil { |
| 54 |
t.Fatalf("coInit: %v", err) |
| 55 |
} |
| 56 |
defer deinit() |
| 57 |
|
| 58 |
nlm, err := createNetworkListManager() |
| 59 |
if err != nil { |
| 60 |
t.Fatalf("createNetworkListManager: %v", err) |
| 61 |
} |
| 62 |
defer nlm.Release() |
| 63 |
|
| 64 |
enum, err := nlm.GetNetworkConnections() |
| 65 |
if err != nil { |
| 66 |
t.Fatalf("GetNetworkConnections: %v", err) |
| 67 |
} |
| 68 |
defer enum.Release() |
| 69 |
|
| 70 |
saw := 0 |
| 71 |
for { |
| 72 |
conn, err := enum.Next() |
| 73 |
if err != nil { |
| 74 |
t.Fatalf("EnumNetworkConnections.Next: %v", err) |
| 75 |
} |
| 76 |
if conn == nil { |
| 77 |
break |
| 78 |
} |
| 79 |
saw++ |
| 80 |
|
| 81 |
if _, err := conn.GetAdapterId(); err != nil { |
| 82 |
conn.Release() |
| 83 |
t.Fatalf("INetworkConnection.GetAdapterId: %v", err) |
| 84 |
} |
| 85 |
|
| 86 |
net, err := conn.GetNetwork() |
| 87 |
conn.Release() |
| 88 |
if err != nil { |
| 89 |
t.Fatalf("INetworkConnection.GetNetwork: %v", err) |
| 90 |
} |
| 91 |
|
| 92 |
cat, err := net.GetCategory() |
| 93 |
if err != nil { |
| 94 |
net.Release() |
| 95 |
t.Fatalf("INetwork.GetCategory: %v", err) |
| 96 |
} |
| 97 |
// Set to the current value so the host's NLM state is unchanged but |
| 98 |
// SetCategory's vtable slot is still validated end-to-end. |
| 99 |
if err := net.SetCategory(cat); err != nil { |
| 100 |
net.Release() |
| 101 |
t.Fatalf("INetwork.SetCategory(%v): %v", cat, err) |
| 102 |
} |
| 103 |
net.Release() |
| 104 |
} |
| 105 |
|
| 106 |
if saw == 0 { |
| 107 |
t.Skip("no NLM network connections available; skipping round-trip") |
| 108 |
} |
| 109 |
} |