base64wrap_test.go

 1package clib
 2
 3import (
 4	"strings"
 5	"testing"
 6)
 7
 8func TestWrapBase64Empty(t *testing.T) {
 9	result := WrapBase64("")
10	if result != "" {
11		t.Errorf("expected empty string, got %q", result)
12	}
13}
14
15func TestWrapBase64ShortString(t *testing.T) {
16	input := "SGVsbG8gV29ybGQ="
17	result := WrapBase64(input)
18	if result != input {
19		t.Errorf("expected %q, got %q", input, result)
20	}
21}
22
23func TestWrapBase64Exactly76(t *testing.T) {
24	input := strings.Repeat("A", 76)
25	result := WrapBase64(input)
26	if result != input {
27		t.Errorf("expected no wrapping for exactly 76 chars, got %q", result)
28	}
29}
30
31func TestWrapBase64MultipleLines(t *testing.T) {
32	input := strings.Repeat("A", 200)
33	result := WrapBase64(input)
34
35	lines := strings.Split(result, "\r\n")
36	if len(lines) != 3 {
37		t.Fatalf("expected 3 lines, got %d", len(lines))
38	}
39	if len(lines[0]) != 76 {
40		t.Errorf("first line length: expected 76, got %d", len(lines[0]))
41	}
42	if len(lines[1]) != 76 {
43		t.Errorf("second line length: expected 76, got %d", len(lines[1]))
44	}
45	if len(lines[2]) != 200-152 {
46		t.Errorf("third line length: expected %d, got %d", 200-152, len(lines[2]))
47	}
48}
49
50func TestWrapBase64MatchesGoImpl(t *testing.T) {
51	// Reference Go implementation (the original from sender.go)
52	goWrapBase64 := func(data string) string {
53		const lineLength = 76
54		var result strings.Builder
55		for i := 0; i < len(data); i += lineLength {
56			end := i + lineLength
57			if end > len(data) {
58				end = len(data)
59			}
60			result.WriteString(data[i:end])
61			if end < len(data) {
62				result.WriteString("\r\n")
63			}
64		}
65		return result.String()
66	}
67
68	tests := []string{
69		"",
70		"A",
71		strings.Repeat("B", 75),
72		strings.Repeat("C", 76),
73		strings.Repeat("D", 77),
74		strings.Repeat("E", 152),
75		strings.Repeat("F", 153),
76		strings.Repeat("G", 1000),
77		strings.Repeat("H", 10000),
78	}
79
80	for _, input := range tests {
81		expected := goWrapBase64(input)
82		got := WrapBase64(input)
83		if got != expected {
84			t.Errorf("mismatch for len=%d:\nexpected: %q\ngot:      %q", len(input), expected, got)
85		}
86	}
87}