-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix version header parsing for tasmota
Fix #35
Showing
5 changed files
with
83 additions
and
7 deletions.
There are no files selected for viewing
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
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
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
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,32 @@ | ||
package server | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
const unknownVersionKey = "ver" | ||
|
||
func parseVersionHeader(versionHdr []string) (map[string]string, error) { | ||
vmap := make(map[string]string) | ||
|
||
for _, ver := range versionHdr { | ||
// version header doesn't have key:value pairs | ||
if !strings.Contains(ver, ":") { | ||
vmap[unknownVersionKey] = ver | ||
continue | ||
} | ||
|
||
for _, kv := range strings.Split(ver, " ") { | ||
n := strings.SplitN(kv, ":", 2) | ||
|
||
if len(n) != 2 { | ||
return nil, fmt.Errorf("failed to parse version: %s", kv) | ||
} | ||
|
||
vmap[n[0]] = n[1] | ||
} | ||
} | ||
|
||
return vmap, nil | ||
} |
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,38 @@ | ||
package server | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestParseVersionHeader(t *testing.T) { | ||
|
||
testCases := []struct { | ||
name string | ||
hdrs []string | ||
expected map[string]string | ||
expectedErrMsg string | ||
}{ | ||
{"nil", nil, map[string]string{}, ""}, | ||
{"kv", []string{"fw:1.2.3 hv:1.0"}, map[string]string{"fw": "1.2.3", "hv": "1.0"}, ""}, | ||
{"tasmota", []string{"13.3.0.3(tasmota-4M)"}, map[string]string{"ver": "13.3.0.3(tasmota-4M)"}, ""}, // see #35 | ||
{"multiple-kv", []string{"fw:1.2.3", "hv:1.0"}, map[string]string{"fw": "1.2.3", "hv": "1.0"}, ""}, // not actually sent by esp, just check corner case | ||
{"malicious-space", []string{"foo:1.2.3 bar"}, nil, "failed to parse version:"}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
vmap, err := parseVersionHeader(tc.hdrs) | ||
if tc.expectedErrMsg == "" { | ||
assert.NoError(err) | ||
assert.Equal(tc.expected, vmap) | ||
} else { | ||
assert.ErrorContains(err, tc.expectedErrMsg) | ||
} | ||
|
||
}) | ||
} | ||
} |