|
| 1 | +package core |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/chainreactors/IoM-go/consts" |
| 9 | + implantpb "github.com/chainreactors/IoM-go/proto/implant/implantpb" |
| 10 | +) |
| 11 | + |
| 12 | +func TestDownloadChunkBroadcastDoesNotCarryFullFileContent(t *testing.T) { |
| 13 | + origUpdateCur := taskDBUpdateCur |
| 14 | + origUpdateFinish := taskDBUpdateFinish |
| 15 | + taskDBUpdateCur = func(string, int) error { return nil } |
| 16 | + taskDBUpdateFinish = func(string) error { return nil } |
| 17 | + t.Cleanup(func() { |
| 18 | + taskDBUpdateCur = origUpdateCur |
| 19 | + taskDBUpdateFinish = origUpdateFinish |
| 20 | + }) |
| 21 | + |
| 22 | + origBroker := EventBroker |
| 23 | + broker := newTestBroker() |
| 24 | + EventBroker = broker |
| 25 | + broker.Start() |
| 26 | + t.Cleanup(func() { |
| 27 | + broker.Stop() |
| 28 | + EventBroker = origBroker |
| 29 | + }) |
| 30 | + |
| 31 | + deadline := time.After(2 * time.Second) |
| 32 | + for !broker.alive.Load() { |
| 33 | + select { |
| 34 | + case <-deadline: |
| 35 | + t.Fatal("broker did not become alive") |
| 36 | + default: |
| 37 | + time.Sleep(5 * time.Millisecond) |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + sub, err := broker.Subscribe() |
| 42 | + if err != nil { |
| 43 | + t.Fatalf("Subscribe error = %v", err) |
| 44 | + } |
| 45 | + t.Cleanup(func() { broker.Unsubscribe(sub) }) |
| 46 | + |
| 47 | + const ( |
| 48 | + chunkSize = 1 << 20 // 1 MiB per chunk (matches the packet_length order) |
| 49 | + numChunks = 8 // an 8 MiB "download" |
| 50 | + ) |
| 51 | + fileSize := chunkSize * numChunks |
| 52 | + checksum := "download-checksum" |
| 53 | + |
| 54 | + sess := newTestSession("download-amp") |
| 55 | + task := sess.NewTask("download", numChunks) |
| 56 | + |
| 57 | + assertEvent := func(expectedOp string, expectedCur int32) { |
| 58 | + t.Helper() |
| 59 | + select { |
| 60 | + case event := <-sub: |
| 61 | + if event.Op != expectedOp { |
| 62 | + t.Fatalf("event op = %q, want %q", event.Op, expectedOp) |
| 63 | + } |
| 64 | + download := event.Spite.GetDownloadResponse() |
| 65 | + if download == nil { |
| 66 | + t.Fatal("event download response is nil") |
| 67 | + } |
| 68 | + if got := len(download.GetContent()); got != 0 { |
| 69 | + t.Fatalf("event download content length = %d, want 0", got) |
| 70 | + } |
| 71 | + if download.GetCur() != expectedCur { |
| 72 | + t.Fatalf("event download cur = %d, want %d", download.GetCur(), expectedCur) |
| 73 | + } |
| 74 | + if download.GetSize() != uint64(fileSize) { |
| 75 | + t.Fatalf("event download size = %d, want %d", download.GetSize(), fileSize) |
| 76 | + } |
| 77 | + if download.GetChecksum() != checksum { |
| 78 | + t.Fatalf("event download checksum = %q, want %q", download.GetChecksum(), checksum) |
| 79 | + } |
| 80 | + case <-time.After(2 * time.Second): |
| 81 | + t.Fatalf("did not receive %s event for chunk %d", expectedOp, expectedCur) |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + var lastResponse *implantpb.Spite |
| 86 | + for i := 1; i <= numChunks; i++ { |
| 87 | + chunk := make([]byte, chunkSize) |
| 88 | + resp := &implantpb.Spite{ |
| 89 | + TaskId: task.Id, |
| 90 | + Body: &implantpb.Spite_DownloadResponse{ |
| 91 | + DownloadResponse: &implantpb.DownloadResponse{ |
| 92 | + Checksum: checksum, |
| 93 | + Cur: int32(i), |
| 94 | + Size: uint64(fileSize), |
| 95 | + Content: chunk, |
| 96 | + }, |
| 97 | + }, |
| 98 | + } |
| 99 | + task.Done(resp, fmt.Sprintf("chunk %d/%d", i, numChunks)) |
| 100 | + assertEvent(consts.CtrlTaskCallback, int32(i)) |
| 101 | + |
| 102 | + if got := len(resp.GetDownloadResponse().GetContent()); got != chunkSize { |
| 103 | + t.Fatalf("source download content length = %d, want %d", got, chunkSize) |
| 104 | + } |
| 105 | + lastResponse = resp |
| 106 | + } |
| 107 | + |
| 108 | + task.Finish(lastResponse, "download completed") |
| 109 | + assertEvent(consts.CtrlTaskFinish, numChunks) |
| 110 | + if got := len(lastResponse.GetDownloadResponse().GetContent()); got != chunkSize { |
| 111 | + t.Fatalf("source download content length after finish = %d, want %d", got, chunkSize) |
| 112 | + } |
| 113 | +} |
0 commit comments