╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve_ssh.zig ────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
656 const line = try allocator.alloc(u8, key_type.len + 1 + encoder.calcSize(blob.len)); 657 @memcpy(line[0..key_type.len], key_type); 658 line[key_type.len] = ' '; 659 _ = encoder.encode(line[key_type.len + 1 ..], blob); 660 return line; 661 } 662 663 fn writeError(sess: *ssh.SessionCtx, comptime msg: []const u8) !void { 664 const body = "ERR haxy ssh: " ++ msg; 665 var packet: [body.len + 4]u8 = undefined; 666 const line = try std.fmt.bufPrint(&packet, "{x:0>4}{s}", .{ packet.len, body }); 667 try sess.writeBytes(line); 668 try sess.exit(1); 669 } 670 671 test "parseGitCommand rejects malformed input" { 672 const allocator = std.testing.allocator; 673 try std.testing.expectError(error.InvalidCommand, parseGitCommand(allocator, "git-upload-pack")); 674 try std.testing.expectError(error.UnsupportedService, parseGitCommand(allocator, "ls -la")); 675 try std.testing.expectError(error.UnsupportedService, parseGitCommand(allocator, "git-fake-pack 'repo'")); 676 } 677 678 test "parseGitCommand happy paths" { 679 const allocator = std.testing.allocator; 680 681 { 682 const parsed = try parseGitCommand(allocator, "git-upload-pack 'some-repo'"); 683 defer parsed.deinit(allocator); 684 try std.testing.expectEqual(GitService.upload_pack, parsed.service); 685 try std.testing.expectEqualStrings("some-repo", parsed.dir); 686 } 687 { 688 const parsed = try parseGitCommand(allocator, "git-receive-pack 'user/proj'"); 689 defer parsed.deinit(allocator); 690 try std.testing.expectEqual(GitService.receive_pack, parsed.service); 691 try std.testing.expectEqualStrings("user/proj", parsed.dir); 692 } 693 { 694 const parsed = try parseGitCommand(allocator, "git-upload-pack repo"); 695 defer parsed.deinit(allocator); 696 try std.testing.expectEqual(GitService.upload_pack, parsed.service); 697 try std.testing.expectEqualStrings("repo", parsed.dir); 698 } 699 } 700