gopkg.in/ubuntu-core/snappy.v0@v0.0.0-20210902073436-25a8614f10a6/osutil/disks/labels_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2020 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package disks_test
    21  
    22  import (
    23  	"testing"
    24  
    25  	. "gopkg.in/check.v1"
    26  
    27  	"github.com/snapcore/snapd/osutil/disks"
    28  )
    29  
    30  func Test(t *testing.T) { TestingT(t) }
    31  
    32  type diskLabelSuite struct{}
    33  
    34  var _ = Suite(&diskLabelSuite{})
    35  
    36  func (ts *diskLabelSuite) TestEncodeHexBlkIDFormat(c *C) {
    37  	// Test output obtained with the following program:
    38  	//
    39  	// #include <string.h>
    40  	// #include <stdio.h>
    41  	// #include <blkid/blkid.h>
    42  	// int main(int argc, char *argv[]) {
    43  	//   char out[2048] = {0};
    44  	//   if (blkid_encode_string(argv[1], out, sizeof(out)) != 0) {
    45  	//     fprintf(stderr, "failed to encode string\n");
    46  	//     return 1;
    47  	//   }
    48  	//   fprintf(stdout, out);
    49  	//   return 0;
    50  	// }
    51  
    52  	tt := []struct {
    53  		in  string
    54  		out string
    55  	}{
    56  		// no changes
    57  		{"foo", "foo"},
    58  		{"plain", "plain"},
    59  		{"plain-ol-data", "plain-ol-data"},
    60  		{"foo:#.@bar", `foo:#.@bar`},
    61  		{"foo..bar", `foo..bar`},
    62  		{"3005", "3005"},
    63  		{"#1-the_BEST@colons:+easter.eggs=something", "#1-the_BEST@colons:+easter.eggs=something"},
    64  		{"", ""},
    65  		{"befs_test", "befs_test"},
    66  		{"P01_S16A", "P01_S16A"},
    67  
    68  		// these are single length utf-8 runes, so they are not encoded
    69  		{"héllo", "héllo"},
    70  		{"he🐧lo", "he🐧lo"},
    71  		{"Новый_том", "Новый_том"},
    72  
    73  		// these are "unsafe" chars, so they get encoded
    74  		{"ubuntu data", `ubuntu\x20data`},
    75  		{"ubuntu\ttab", `ubuntu\x9tab`},
    76  		{"ubuntu\nnewline", `ubuntu\xanewline`},
    77  		{"foo bar", `foo\x20bar`},
    78  		{"foo/bar", `foo\x2fbar`},
    79  		{"foo/../bar", `foo\x2f..\x2fbar`},
    80  		{"foo\\bar", `foo\x5cbar`},
    81  		{"pinkié pie", `pinkié\x20pie`},
    82  		{"(EFI Boot)", `\x28EFI\x20Boot\x29`},
    83  		{"[System Boot]", `\x5bSystem\x20Boot\x5d`},
    84  	}
    85  	for _, t := range tt {
    86  		c.Logf("tc: %v %q", t.in, t.out)
    87  		c.Assert(disks.BlkIDEncodeLabel(t.in), Equals, t.out)
    88  	}
    89  }