-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
number571
committed
Nov 3, 2024
1 parent
6c3623f
commit 9b14f21
Showing
3 changed files
with
128 additions
and
10 deletions.
There are no files selected for viewing
118 changes: 118 additions & 0 deletions
118
internal/applications/messenger/internal/handler/data_type_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package handler | ||
|
||
import ( | ||
"encoding/base64" | ||
"testing" | ||
|
||
hlm_settings "github.com/number571/hidden-lake/internal/applications/messenger/pkg/settings" | ||
) | ||
|
||
const ( | ||
tcFile = "file.txt" | ||
tcFileEscaped = "file.txt<b>some</b>.txt" | ||
tcText = "<a href='xxx'>hello</a>" | ||
tcTextEscaped = "<a href='xxx'>hello</a>" | ||
) | ||
|
||
func TestDataTypePanic(t *testing.T) { | ||
t.Parallel() | ||
|
||
testUnwrapTextPanic(t) | ||
testUnwrapFilePanic(t) | ||
} | ||
|
||
func testUnwrapTextPanic(t *testing.T) { | ||
defer func() { | ||
if r := recover(); r == nil { | ||
t.Error("nothing panics") | ||
return | ||
} | ||
}() | ||
|
||
_ = unwrapText([]byte{}, false) | ||
} | ||
|
||
func testUnwrapFilePanic(t *testing.T) { | ||
defer func() { | ||
if r := recover(); r == nil { | ||
t.Error("nothing panics") | ||
return | ||
} | ||
}() | ||
|
||
_, _ = unwrapFile([]byte{}, false) | ||
} | ||
|
||
func TestDataType(t *testing.T) { | ||
t.Parallel() | ||
|
||
if isText([]byte{}) { | ||
t.Error("isText([]byte{}) = ok") | ||
return | ||
} | ||
if isFile([]byte{}) { | ||
t.Error("isFile([]byte{}) = ok") | ||
return | ||
} | ||
|
||
wt := wrapText(tcText) | ||
if !isText(wt) { | ||
t.Error("wrapText: !isText(wt)") | ||
return | ||
} | ||
if isFile(wt) { | ||
t.Error("wrapText: isFile(wt)") | ||
return | ||
} | ||
if wt[0] != hlm_settings.CIsText { | ||
t.Error("wrapText: wt[0] != hlm_settings.CIsText") | ||
return | ||
} | ||
if unwrapText(wt, false) != tcText { | ||
t.Error("wrapText: unwrapText(wt, false) != tcText") | ||
return | ||
} | ||
if unwrapText(wt, true) != tcTextEscaped { | ||
t.Error("wrapText: unwrapText(wt, true) != tcTextEscaped") | ||
return | ||
} | ||
|
||
wf := wrapFile(tcFile, []byte(tcText)) | ||
if !isFile(wf) { | ||
t.Error("wrapFile: !isFile(wf)") | ||
return | ||
} | ||
if isText(wf) { | ||
t.Error("wrapFile: isText(wf)") | ||
return | ||
} | ||
if wf[0] != hlm_settings.CIsFile || wf[len(tcFile)+1] != hlm_settings.CIsFile { | ||
t.Error("wrapFile: wf[0] != hlm_settings.CIsFile || wf[len(tcFile)+1] != hlm_settings.CIsFile") | ||
return | ||
} | ||
if f, b := unwrapFile(wf, false); f != tcFile || b != base64.StdEncoding.EncodeToString([]byte(tcText)) { | ||
t.Error("wrapText: f, b := unwrapFile(wf, false); f != tcFile || b != base64.StdEncoding.EncodeToString([]byte(tcText))") | ||
return | ||
} | ||
|
||
wfx := wrapFile(tcFile+"<b>some</b>"+".txt", []byte(tcText)) | ||
if f, b := unwrapFile(wfx, true); f != tcFileEscaped || b != base64.StdEncoding.EncodeToString([]byte(tcText)) { | ||
t.Error("wrapText: f, b := unwrapFile(wf, true); f != tcFileEscaped || b != base64.StdEncoding.EncodeToString([]byte(tcText))") | ||
return | ||
} | ||
|
||
if f, b := unwrapFile([]byte{1}, false); f != "" || b != "" { | ||
t.Error(`wrapFile: f, b := unwrapFile([]byte{1}, false); f != "" || b != ""`) | ||
return | ||
} | ||
wf2 := wrapFile(tcFile+"\x01"+".txt", []byte(tcText)) | ||
if f, b := unwrapFile(wf2, false); f != "" || b != "" { | ||
t.Error(`wrapFile: f, b := unwrapFile(wf2, false); f != "" || b != ""`) | ||
return | ||
} | ||
wf3 := wrapFile(tcFile, []byte{}) | ||
if f, b := unwrapFile(wf3, false); f != "" || b != "" { | ||
t.Error(`wrapFile: f, b := unwrapFile(wf3, false); f != "" || b != ""`) | ||
return | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.