Skip to content

Commit

Permalink
libraster: fix Rast_legal_bandref() (OSGeo#1796)
Browse files Browse the repository at this point in the history
relax Rast_legal_bandref(), adjust tests for band names
  • Loading branch information
metzm authored Aug 17, 2021
1 parent 319b6bb commit abe194d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 30 deletions.
23 changes: 9 additions & 14 deletions lib/raster/raster_metadata.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,7 @@ void Rast_write_bandref(const char *name, const char *str)
*/
int Rast_legal_bandref(const char *bandref)
{
char **tokens;
int ntok;
const char *s;

if (strlen(bandref) >= GNAME_MAX) {
G_warning(_("Band reference is too long"));
Expand All @@ -146,20 +145,16 @@ int Rast_legal_bandref(const char *bandref)
if (G_legal_filename(bandref) != 1)
return -1;

tokens = G_tokenize(bandref, "_");
ntok = G_number_of_tokens(tokens);
if (ntok < 2) {
G_warning(_("Band reference must be in form <shortcut>_<bandname>"));
G_free_tokens(tokens);
return -1;
}

if (strlen(tokens[1]) < 1) {
G_free_tokens(tokens);
return -1;
s = bandref;
while (*s) {
if (!((*s >= 'A' && *s <= 'Z') || (*s >= 'a' && *s <= 'z') ||
(*s >= '0' && *s <= '9') || *s == '_' || *s == '-')) {
G_warning(_("Character '%c' not allowed in band reference."), *s);
return -1;
}
s++;
}

G_free_tokens(tokens);
return 1;
}

Expand Down
24 changes: 8 additions & 16 deletions lib/raster/testsuite/test_raster_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,28 +32,12 @@ def test_empty_name(self):
def test_illegal_name(self):
ret = Rast_legal_bandref(".a")
self.assertEqual(ret, -1)
ret = Rast_legal_bandref("1")
self.assertEqual(ret, -1)
ret = Rast_legal_bandref("1a")
self.assertEqual(ret, -1)
ret = Rast_legal_bandref("a/b")
self.assertEqual(ret, -1)
ret = Rast_legal_bandref("a@b")
self.assertEqual(ret, -1)
ret = Rast_legal_bandref("a#b")
self.assertEqual(ret, -1)
ret = Rast_legal_bandref("GRASS")
self.assertEqual(ret, -1)
ret = Rast_legal_bandref("USER")
self.assertEqual(ret, -1)

def test_no_second_token(self):
ret = Rast_legal_bandref("GRASS_")
self.assertEqual(ret, -1)
ret = Rast_legal_bandref("USER_")
self.assertEqual(ret, -1)
ret = Rast_legal_bandref("S2_")
self.assertEqual(ret, -1)

def test_too_long(self):
ret = Rast_legal_bandref(
Expand All @@ -66,6 +50,14 @@ def test_too_long(self):
self.assertEqual(ret, -1)

def test_good_name(self):
ret = Rast_legal_bandref("1")
self.assertEqual(ret, 1)
ret = Rast_legal_bandref("1a")
self.assertEqual(ret, 1)
ret = Rast_legal_bandref("clouds")
self.assertEqual(ret, 1)
ret = Rast_legal_bandref("rededge1")
self.assertEqual(ret, 1)
ret = Rast_legal_bandref("S2_1")
self.assertEqual(ret, 1)
ret = Rast_legal_bandref("GRASS_aspect_deg")
Expand Down

0 comments on commit abe194d

Please sign in to comment.