forked from AdguardTeam/AdGuardHome
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pull request: dhcpd: do not override ra-slaac settings
Merge in DNS/adguard-home from 2653-ra-slaac to master Updates AdguardTeam#2653. Squashed commit of the following: commit f261413 Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Thu Feb 11 20:37:13 2021 +0300 all: doc changes, rm debug commit 4a8c6e4 Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Thu Feb 11 20:11:46 2021 +0300 dhcpd: do not override ra-slaac settings
- Loading branch information
Showing
10 changed files
with
222 additions
and
66 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,58 @@ | ||
package dhcpd | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
) | ||
|
||
// nullBool is a nullable boolean. Use these in JSON requests and responses | ||
// instead of pointers to bool. | ||
// | ||
// TODO(a.garipov): Inspect uses of *bool, move this type into some new package | ||
// if we need it somewhere else. | ||
type nullBool uint8 | ||
|
||
// nullBool values | ||
const ( | ||
nbNull nullBool = iota | ||
nbTrue | ||
nbFalse | ||
) | ||
|
||
// String implements the fmt.Stringer interface for nullBool. | ||
func (nb nullBool) String() (s string) { | ||
switch nb { | ||
case nbNull: | ||
return "null" | ||
case nbTrue: | ||
return "true" | ||
case nbFalse: | ||
return "false" | ||
} | ||
|
||
return fmt.Sprintf("!invalid nullBool %d", uint8(nb)) | ||
} | ||
|
||
// boolToNullBool converts a bool into a nullBool. | ||
func boolToNullBool(cond bool) (nb nullBool) { | ||
if cond { | ||
return nbTrue | ||
} | ||
|
||
return nbFalse | ||
} | ||
|
||
// UnmarshalJSON implements the json.Unmarshaler interface for *nullBool. | ||
func (nb *nullBool) UnmarshalJSON(b []byte) (err error) { | ||
if len(b) == 0 || bytes.Equal(b, []byte("null")) { | ||
*nb = nbNull | ||
} else if bytes.Equal(b, []byte("true")) { | ||
*nb = nbTrue | ||
} else if bytes.Equal(b, []byte("false")) { | ||
*nb = nbFalse | ||
} else { | ||
return fmt.Errorf("invalid nullBool value %q", b) | ||
} | ||
|
||
return 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,69 @@ | ||
package dhcpd | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestNullBool_UnmarshalText(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
data []byte | ||
wantErrMsg string | ||
want nullBool | ||
}{{ | ||
name: "empty", | ||
data: []byte{}, | ||
wantErrMsg: "", | ||
want: nbNull, | ||
}, { | ||
name: "null", | ||
data: []byte("null"), | ||
wantErrMsg: "", | ||
want: nbNull, | ||
}, { | ||
name: "true", | ||
data: []byte("true"), | ||
wantErrMsg: "", | ||
want: nbTrue, | ||
}, { | ||
name: "false", | ||
data: []byte("false"), | ||
wantErrMsg: "", | ||
want: nbFalse, | ||
}, { | ||
name: "invalid", | ||
data: []byte("flase"), | ||
wantErrMsg: `invalid nullBool value "flase"`, | ||
want: nbNull, | ||
}} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
var got nullBool | ||
err := got.UnmarshalJSON(tc.data) | ||
if tc.wantErrMsg == "" { | ||
assert.Nil(t, err) | ||
} else { | ||
require.NotNil(t, err) | ||
assert.Equal(t, tc.wantErrMsg, err.Error()) | ||
} | ||
|
||
assert.Equal(t, tc.want, got) | ||
}) | ||
} | ||
|
||
t.Run("json", func(t *testing.T) { | ||
want := nbTrue | ||
var got struct { | ||
A nullBool | ||
} | ||
|
||
err := json.Unmarshal([]byte(`{"A":true}`), &got) | ||
require.Nil(t, err) | ||
assert.Equal(t, want, got.A) | ||
}) | ||
} |
Oops, something went wrong.