Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
jamiesun committed Jun 29, 2023
2 parents 72728bc + 7eda004 commit c79bfd6
Show file tree
Hide file tree
Showing 7 changed files with 219 additions and 9 deletions.
10 changes: 5 additions & 5 deletions assets/buildinfo.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
BuildVersion=latest v8.0.1 2023-06-29 16:23:51
BuildVersion=latest v8.0.1 2023-06-29 16:57:00
ReleaseVersion=v8.0.1
BuildTime=2023-06-29 16:23:51
BuildTime=2023-06-29 16:57:00
BuildName=toughradius
CommitID=42fd7f94f2e4cec5769388a5ce59a42fec38ecf3
CommitDate=Thu, 29 Jun 2023 16:23:46 +0800
CommitID=3e42860ebef788c8ce597e7b5e676b6ec8003013
CommitDate=Thu, 29 Jun 2023 16:56:53 +0800
[email protected]
CommitSubject=fix docker-compose config
CommitSubject=add testcase
45 changes: 45 additions & 0 deletions common/cwmp/DownloadResponse_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package cwmp

import (
"strings"
"testing"

"github.com/talkincode/toughradius/v8/common/xmlx"
)

func TestDownloadResponse(t *testing.T) {
downloadResponse := &DownloadResponse{
ID: "test-id",
Name: "test-name",
Status: 200,
StartTime: "2023-07-01T00:00:00",
CompleteTime: "2023-07-01T00:01:00",
}

xml := downloadResponse.CreateXML()
if strings.Contains(string(xml), "test-id") == false {
t.Errorf("XML encoding failed: %s", xml)
}

doc := xmlx.New()
if err := doc.LoadBytes(xml, nil); err != nil {
t.Errorf("XML parse failed: %v", err)
}

downloadResponse.Parse(doc)
if downloadResponse.ID != "test-id" {
t.Errorf("XML parse failed: ID")
}
if downloadResponse.Name != "test-name" {
t.Errorf("XML parse failed: Name")
}
if downloadResponse.Status != 200 {
t.Errorf("XML parse failed: Status")
}
if downloadResponse.StartTime != "2023-07-01T00:00:00" {
t.Errorf("XML parse failed: StartTime")
}
if downloadResponse.CompleteTime != "2023-07-01T00:01:00" {
t.Errorf("XML parse failed: CompleteTime")
}
}
48 changes: 48 additions & 0 deletions common/cwmp/Download_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package cwmp

import (
"strings"
"testing"

"github.com/talkincode/toughradius/v8/common/xmlx"
)

func TestDownload(t *testing.T) {
download := &Download{
ID: "test-id",
Name: "test-name",
NoMore: 1,
CommandKey: "test-command",
FileType: "test-filetype",
URL: "http://test.com",
Username: "test-username",
Password: "test-password",
FileSize: 1024,
TargetFileName: "test-filename",
DelaySeconds: 60,
SuccessURL: "http://success.com",
FailureURL: "http://failure.com",
}

xml := download.CreateXML()
if strings.Contains(string(xml), "test-id") == false {
t.Errorf("XML encoding failed: %s", xml)
}

doc := xmlx.New()
if err := doc.LoadBytes(xml, nil); err != nil {
t.Errorf("XML parse failed: %v", err)
}

download.Parse(doc)
if download.ID != "test-id" {
t.Errorf("XML parse failed: ID")
}
if download.Name != "test-name" {
t.Errorf("XML parse failed: Name")
}
if download.NoMore != 1 {
t.Errorf("XML parse failed: NoMore")
}
// ... 对于其他字段做类似的检查
}
33 changes: 33 additions & 0 deletions common/cwmp/FactoryResetResponse_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package cwmp

import (
"strings"
"testing"

"github.com/talkincode/toughradius/v8/common/xmlx"
)

func TestFactoryResetResponse(t *testing.T) {
factoryResetResponse := &FactoryResetResponse{
ID: "test-id",
Name: "test-name",
}

xml := factoryResetResponse.CreateXML()
if strings.Contains(string(xml), "test-id") == false {
t.Errorf("XML encoding failed: %s", xml)
}

doc := xmlx.New()
if err := doc.LoadBytes(xml, nil); err != nil {
t.Errorf("XML parse failed: %v", err)
}

factoryResetResponse.Parse(doc)
if factoryResetResponse.ID != "test-id" {
t.Errorf("XML parse failed: ID")
}
if factoryResetResponse.Name != "test-name" {
t.Errorf("XML parse failed: Name")
}
}
37 changes: 37 additions & 0 deletions common/cwmp/FactoryReset_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package cwmp

import (
"strings"
"testing"

"github.com/talkincode/toughradius/v8/common/xmlx"
)

func TestFactoryReset(t *testing.T) {
factoryReset := &FactoryReset{
ID: "test-id",
Name: "test-name",
NoMore: 1,
}

xml := factoryReset.CreateXML()
if strings.Contains(string(xml), "test-id") == false {
t.Errorf("XML encoding failed: %s", xml)
}

doc := xmlx.New()
if err := doc.LoadBytes(xml, nil); err != nil {
t.Errorf("XML parse failed: %v", err)
}

factoryReset.Parse(doc)
if factoryReset.ID != "test-id" {
t.Errorf("XML parse failed: ID")
}
if factoryReset.Name != "test-name" {
t.Errorf("XML parse failed: Name")
}
if factoryReset.NoMore != 1 {
t.Errorf("XML parse failed: NoMore")
}
}
8 changes: 4 additions & 4 deletions common/cwmp/auth_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ func ConnectionRequestAuth(username string, password string, uri string) (bool,
}

/*
Parse Authorization header from the http.Request. Returns a map of
auth parameters or nil if the header is not a valid parsable Digest
auth header.
Parse Authorization header from the http.Request. Returns a map of
auth parameters or nil if the header is not a valid parsable Digest
auth header.
*/
func DigestAuthParams(r *http.Response) map[string]string {
s := strings.SplitN(r.Header.Get("Www-Authenticate"), " ", 2)
Expand Down Expand Up @@ -105,7 +105,7 @@ func RandomKey() string {
}

/*
H function for MD5 algorithm (returns a lower-case hex MD5 digest)
H function for MD5 algorithm (returns a lower-case hex MD5 digest)
*/
func H(data string) string {
digest := md5.New()
Expand Down
47 changes: 47 additions & 0 deletions common/cwmp/auth_client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package cwmp

import (
"net/http"
"net/http/httptest"
"strings"
"testing"
)

func TestConnectionRequestAuth(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.String() == "/auth" {
if r.Header.Get("Authorization") == "" {
w.Header().Set("WWW-Authenticate", `Digest realm="myRealm", nonce="randomNonce", opaque="randomOpaque", qop="auth"`)
http.Error(w, "Unauthorized.", http.StatusUnauthorized)
return
}

// 验证Authorization头
auth := r.Header.Get("Authorization")
if strings.Contains(auth, `username="wronguser"`) {
http.Error(w, "Unauthorized.", http.StatusUnauthorized)
return
}

w.WriteHeader(http.StatusOK)
return
}
}))
defer ts.Close()

authorized, err := ConnectionRequestAuth("username", "password", ts.URL+"/auth")
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if !authorized {
t.Errorf("Expected authorized, but was not")
}

authorized, err = ConnectionRequestAuth("wronguser", "wrongpass", ts.URL+"/auth")
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if authorized {
t.Errorf("Expected unauthorized, but was authorized")
}
}

0 comments on commit c79bfd6

Please sign in to comment.